mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-19 06:20:38 -07:00
test: assert null-prototype registries vs all Object.prototype keys
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -77,15 +77,16 @@ describe('Issues', function () {
|
|||||||
)
|
)
|
||||||
expect(html).toBe('BAR')
|
expect(html).toBe('BAR')
|
||||||
})
|
})
|
||||||
it('filter/tag lookup must not inherit Object.prototype (node + UMD)', async () => {
|
it('filter/tag maps are null-prototype; built-ins work (node + UMD)', async () => {
|
||||||
const tpl = '{% assign r = 1 | valueOf %}{{ r.context }}{{ r.liquid }}{{ r.token }}|{{ r }}'
|
const tpl = `{{ 'a' | append: 'b' }}`
|
||||||
const nodeEngine = new Liquid()
|
const nodeEngine = new Liquid()
|
||||||
expect(await nodeEngine.parseAndRender(tpl)).toBe('|1')
|
|
||||||
expect(() => nodeEngine.parse('{% constructor %}')).toThrow('tag "constructor" not found')
|
|
||||||
|
|
||||||
const umdEngine = new LiquidUMD()
|
const umdEngine = new LiquidUMD()
|
||||||
expect(await umdEngine.parseAndRender(tpl)).toBe('|1')
|
expect(Object.getPrototypeOf(nodeEngine.filters)).toBeNull()
|
||||||
expect(() => umdEngine.parse('{% constructor %}')).toThrow('tag "constructor" not found')
|
expect(Object.getPrototypeOf(nodeEngine.tags)).toBeNull()
|
||||||
|
expect(Object.getPrototypeOf(umdEngine.filters)).toBeNull()
|
||||||
|
expect(Object.getPrototypeOf(umdEngine.tags)).toBeNull()
|
||||||
|
expect(await nodeEngine.parseAndRender(tpl)).toBe('ab')
|
||||||
|
expect(await umdEngine.parseAndRender(tpl)).toBe('ab')
|
||||||
})
|
})
|
||||||
it('lenientIf not working as expected in umd #313', async () => {
|
it('lenientIf not working as expected in umd #313', async () => {
|
||||||
const engine = new LiquidUMD({
|
const engine = new LiquidUMD({
|
||||||
|
|||||||
@@ -61,29 +61,28 @@ describe('liquid#registerFilter()', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('filter name must not inherit from Object.prototype', () => {
|
describe('filter registry storage', () => {
|
||||||
it('should treat valueOf as unregistered (no FilterImpl leak)', async () => {
|
it('should use a null-prototype map for filters', () => {
|
||||||
const out = await liquid.parseAndRender(
|
expect(Object.getPrototypeOf(liquid.filters)).toBeNull()
|
||||||
'{% assign r = 1 | valueOf %}{{ r.liquid.options.fs.sep }}|{{ r }}'
|
|
||||||
)
|
|
||||||
expect(out).toBe('|1')
|
|
||||||
})
|
})
|
||||||
it('should not expose context, liquid, or token via valueOf', async () => {
|
it('should still resolve built-in filters', async () => {
|
||||||
const out = await liquid.parseAndRender(
|
expect(await liquid.parseAndRender(`{{ 'a' | append: 'b' }}`)).toBe('ab')
|
||||||
'{% assign r = 1 | valueOf %}{{ r.context }}/{{ r.liquid }}/{{ r.token }}'
|
|
||||||
)
|
|
||||||
expect(out).toBe('//')
|
|
||||||
})
|
})
|
||||||
it.each(['toString', 'constructor', 'hasOwnProperty', 'isPrototypeOf', '__proto__', '__defineGetter__'])(
|
it('should not resolve names that exist only on Object.prototype', async () => {
|
||||||
'should treat %s as unregistered filter',
|
const registered = new Set(Object.keys(liquid.filters))
|
||||||
async (name) => {
|
for (const name of Object.getOwnPropertyNames(Object.prototype)) {
|
||||||
const out = await liquid.parseAndRender(`{{ "x" | ${name} }}`)
|
if (registered.has(name)) continue
|
||||||
expect(out).toBe('x')
|
const out = await liquid.parseAndRender(`{{ x | ${name} }}`, { x: 42 })
|
||||||
|
expect(out).toBe('42')
|
||||||
}
|
}
|
||||||
)
|
})
|
||||||
it('should throw under strictFilters for valueOf', async () => {
|
it('should reject unknown filter names under strictFilters, including Object.prototype keys', async () => {
|
||||||
const strict = new Liquid({ strictFilters: true })
|
const strict = new Liquid({ strictFilters: true })
|
||||||
await expect(strict.parseAndRender('{{ 1 | valueOf }}')).rejects.toThrow('undefined filter: valueOf')
|
const registered = new Set(Object.keys(strict.filters))
|
||||||
|
for (const name of Object.getOwnPropertyNames(Object.prototype)) {
|
||||||
|
if (registered.has(name)) continue
|
||||||
|
await expect(strict.parseAndRender(`{{ 1 | ${name} }}`)).rejects.toThrow('undefined filter')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -39,13 +39,17 @@ describe('liquid#registerTag()', function () {
|
|||||||
return expect(html).toBe('ABC')
|
return expect(html).toBe('ABC')
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('tag name must not inherit from Object.prototype', () => {
|
describe('tag registry storage', () => {
|
||||||
it.each(['constructor', 'toString', 'valueOf', 'hasOwnProperty', '__proto__'])(
|
it('should use a null-prototype map for tags', () => {
|
||||||
'should report %s as unknown tag',
|
expect(Object.getPrototypeOf(new Liquid().tags)).toBeNull()
|
||||||
(name) => {
|
})
|
||||||
const l = new Liquid()
|
it('should not resolve names that exist only on Object.prototype', () => {
|
||||||
|
const l = new Liquid()
|
||||||
|
const registered = new Set(Object.keys(l.tags))
|
||||||
|
for (const name of Object.getOwnPropertyNames(Object.prototype)) {
|
||||||
|
if (registered.has(name)) continue
|
||||||
expect(() => l.parse(`{% ${name} %}`)).toThrow(`tag "${name}" not found`)
|
expect(() => l.parse(`{% ${name} %}`)).toThrow(`tag "${name}" not found`)
|
||||||
}
|
}
|
||||||
)
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user