fix: enforce string-type pattern in replace, fixes #243

This commit is contained in:
harttle
2020-12-08 00:08:31 +08:00
parent d8f9091a12
commit c8afa39a01
2 changed files with 18 additions and 5 deletions
+5 -5
View File
@@ -29,11 +29,11 @@ export function upcase (str: string) {
} }
export function remove (v: string, arg: 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) { export function removeFirst (v: string, l: string) {
return stringify(v).replace(l, '') return stringify(v).replace(String(l), '')
} }
export function rstrip (str: string) { export function rstrip (str: string) {
@@ -41,7 +41,7 @@ export function rstrip (str: string) {
} }
export function split (v: string, arg: string) { export function split (v: string, arg: string) {
return stringify(v).split(arg) return stringify(v).split(String(arg))
} }
export function strip (v: string) { export function strip (v: string) {
@@ -58,11 +58,11 @@ export function capitalize (str: string) {
} }
export function replace (v: string, pattern: string, replacement: 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) { 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 = '...') { export function truncate (v: string, l = 50, o = '...') {
+13
View File
@@ -23,4 +23,17 @@ describe('Issues', function () {
const html = engine.parseAndRenderSync('{{ ["complex key"] }}', { 'complex key': 'foo' }) const html = engine.parseAndRenderSync('{{ ["complex key"] }}', { 'complex key': 'foo' })
expect(html).to.equal('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)
})
}) })