|
| 1 | +import { assert, describe, it } from 'poku'; |
| 2 | +import { escape } from '../src/index.ts'; |
| 3 | + |
| 4 | +describe("Can't bypass via object injection using escape directly", () => { |
| 5 | + const value = { password: 1 }; |
| 6 | + const expected = "'[object Object]'"; |
| 7 | + |
| 8 | + it('should stringify object when stringifyObjects is true', () => { |
| 9 | + assert.strictEqual(escape(value, true), expected); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should stringify object when stringifyObjects is false', () => { |
| 13 | + assert.strictEqual(escape(value, false), expected); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should stringify object when stringifyObjects is 0', () => { |
| 17 | + // @ts-expect-error: testing 0 as a falsy runtime value |
| 18 | + assert.strictEqual(escape(value, 0), expected); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should stringify object when stringifyObjects is empty string', () => { |
| 22 | + // @ts-expect-error: testing empty string as a falsy runtime value |
| 23 | + assert.strictEqual(escape(value, ''), expected); |
| 24 | + }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe('Object expansion when stringifyObjects is nullish', () => { |
| 28 | + const value = { password: 1 }; |
| 29 | + const expanded = '`password` = 1'; |
| 30 | + |
| 31 | + it('should expand object when stringifyObjects is undefined', () => { |
| 32 | + assert.strictEqual(escape(value, undefined), expanded); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should expand object when stringifyObjects is null', () => { |
| 36 | + // @ts-expect-error: testing null as a falsy runtime value |
| 37 | + assert.strictEqual(escape(value, null), expanded); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should expand object when stringifyObjects is omitted', () => { |
| 41 | + assert.strictEqual(escape(value), expanded); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe('Safe object to key-value expansion for SET clauses', () => { |
| 46 | + it('should expand single key-value pair', () => { |
| 47 | + assert.strictEqual(escape({ name: 'foo' }), "`name` = 'foo'"); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should expand multiple key-value pairs', () => { |
| 51 | + assert.strictEqual( |
| 52 | + escape({ name: 'foo', email: 'bar@test.com' }), |
| 53 | + "`name` = 'foo', `email` = 'bar@test.com'" |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should expand mixed value types', () => { |
| 58 | + assert.strictEqual( |
| 59 | + escape({ name: 'foo', active: true, age: 30 }), |
| 60 | + "`name` = 'foo', `active` = true, `age` = 30" |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should skip function values', () => { |
| 65 | + assert.strictEqual(escape({ name: 'foo', fn: () => {} }), "`name` = 'foo'"); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should return empty string for empty object', () => { |
| 69 | + assert.strictEqual(escape({}), ''); |
| 70 | + }); |
| 71 | +}); |
0 commit comments