Skip to content

Commit

Permalink
fix: enforce string-type pattern in replace, fixes #243
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Dec 7, 2020
1 parent d8f9091 commit c8afa39
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/builtin/filters/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ export function upcase (str: string) {
}

export function remove (v: string, arg: string) {
return stringify(v).split(arg).join('')
return stringify(v).split(String(arg)).join('')
}

export function removeFirst (v: string, l: string) {
return stringify(v).replace(l, '')
return stringify(v).replace(String(l), '')
}

export function rstrip (str: string) {
return stringify(str).replace(/\s+$/, '')
}

export function split (v: string, arg: string) {
return stringify(v).split(arg)
return stringify(v).split(String(arg))
}

export function strip (v: string) {
Expand All @@ -58,11 +58,11 @@ export function capitalize (str: string) {
}

export function replace (v: string, pattern: string, replacement: string) {
return stringify(v).split(pattern).join(replacement)
return stringify(v).split(String(pattern)).join(replacement)
}

export function replaceFirst (v: string, arg1: string, arg2: string) {
return stringify(v).replace(arg1, arg2)
return stringify(v).replace(String(arg1), arg2)
}

export function truncate (v: string, l = 50, o = '...') {
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,17 @@ describe('Issues', function () {
const html = engine.parseAndRenderSync('{{ ["complex key"] }}', { 'complex key': 'foo' })
expect(html).to.equal('foo')
})
it('#243 Potential for ReDoS through string replace function', async () => {
const engine = new Liquid()
const INPUT = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!'
const BROKEN_REGEX = /([a-z]+)+$/

// string filters vulnerable to regexp parameter: split, replace, replace_first, remove_first
const parameters = { input: INPUT, regex: BROKEN_REGEX }
const template = `{{ input | replace:regex,'' }}`
const html = engine.parseAndRenderSync(template, parameters)

// should stringify the regexp rather than execute it
expect(html).to.equal(INPUT)
})
})

0 comments on commit c8afa39

Please sign in to comment.