mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-29 15:15:23 -07:00
fix(security): charge sample filter full clone allocation to memoryLimit (CWE-770)
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -254,7 +254,7 @@ export function sample<T> (this: FilterImpl, v: T[] | string, count = 1): T | st
|
|||||||
v = toValue(v)
|
v = toValue(v)
|
||||||
if (isNil(v)) return []
|
if (isNil(v)) return []
|
||||||
if (!isArray(v)) v = stringify(v)
|
if (!isArray(v)) v = stringify(v)
|
||||||
this.context.memoryLimit.use(count)
|
this.context.memoryLimit.use(v.length)
|
||||||
const shuffled = [...v].sort(() => Math.random() - 0.5)
|
const shuffled = [...v].sort(() => Math.random() - 0.5)
|
||||||
if (count === 1) return shuffled[0]
|
if (count === 1) return shuffled[0]
|
||||||
return shuffled.slice(0, count)
|
return shuffled.slice(0, count)
|
||||||
|
|||||||
@@ -84,6 +84,11 @@ describe('DoS related', function () {
|
|||||||
const liquid = new Liquid({ memoryLimit: 100 })
|
const liquid = new Liquid({ memoryLimit: 100 })
|
||||||
await expect(liquid.parseAndRender('{{ array | pop | size }}', { array })).rejects.toThrow('memory alloc limit exceeded')
|
await expect(liquid.parseAndRender('{{ array | pop | size }}', { array })).rejects.toThrow('memory alloc limit exceeded')
|
||||||
})
|
})
|
||||||
|
it('should charge sample allocation to memoryLimit', async () => {
|
||||||
|
const array = Array(1e3).fill(0)
|
||||||
|
const liquid = new Liquid({ memoryLimit: 100 })
|
||||||
|
await expect(liquid.parseAndRender('{{ array | sample: 1 | size }}', { array })).rejects.toThrow('memory alloc limit exceeded')
|
||||||
|
})
|
||||||
it('should charge strip_html input length to memoryLimit', () => {
|
it('should charge strip_html input length to memoryLimit', () => {
|
||||||
const liquid = new Liquid({ memoryLimit: 100 })
|
const liquid = new Liquid({ memoryLimit: 100 })
|
||||||
expect(() => liquid.parseAndRenderSync('{{ s | strip_html }}', { s: 'a'.repeat(200) }))
|
expect(() => liquid.parseAndRenderSync('{{ s | strip_html }}', { s: 'a'.repeat(200) }))
|
||||||
|
|||||||
Reference in New Issue
Block a user