|
| 1 | +import { describe, expect, it } from '@jest/globals'; |
| 2 | +import { set } from 'immutable'; |
| 3 | + |
| 4 | +describe('set', () => { |
| 5 | + it('for immutable structure', () => { |
| 6 | + const originalArray = ['dog', 'frog', 'cat']; |
| 7 | + expect(set(originalArray, 1, 'cow')).toEqual(['dog', 'cow', 'cat']); |
| 8 | + expect(set(originalArray, 4, 'cow')).toEqual([ |
| 9 | + 'dog', |
| 10 | + 'frog', |
| 11 | + 'cat', |
| 12 | + undefined, |
| 13 | + 'cow', |
| 14 | + ]); |
| 15 | + expect(originalArray).toEqual(['dog', 'frog', 'cat']); |
| 16 | + |
| 17 | + const originalObject = { x: 123, y: 456 }; |
| 18 | + expect(set(originalObject, 'x', 789)).toEqual({ x: 789, y: 456 }); |
| 19 | + expect(set(originalObject, 'z', 789)).toEqual({ x: 123, y: 456, z: 789 }); |
| 20 | + expect(originalObject).toEqual({ x: 123, y: 456 }); |
| 21 | + }); |
| 22 | + |
| 23 | + it('for Array', () => { |
| 24 | + const originalArray = ['dog', 'frog', 'cat']; |
| 25 | + expect(set(originalArray, 1, 'cow')).toEqual(['dog', 'cow', 'cat']); |
| 26 | + expect(set(originalArray, 4, 'cow')).toEqual([ |
| 27 | + 'dog', |
| 28 | + 'frog', |
| 29 | + 'cat', |
| 30 | + undefined, |
| 31 | + 'cow', |
| 32 | + ]); |
| 33 | + expect(originalArray).toEqual(['dog', 'frog', 'cat']); |
| 34 | + }); |
| 35 | + |
| 36 | + it('for plain objects', () => { |
| 37 | + const originalObject = { x: 123, y: 456 }; |
| 38 | + expect(set(originalObject, 'x', 789)).toEqual({ x: 789, y: 456 }); |
| 39 | + expect(set(originalObject, 'z', 789)).toEqual({ x: 123, y: 456, z: 789 }); |
| 40 | + expect(originalObject).toEqual({ x: 123, y: 456 }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('is not sensible to prototype pollution via set on plain object', () => { |
| 44 | + type User = { user: string; admin?: boolean }; |
| 45 | + |
| 46 | + const obj: User = { user: 'Alice' }; |
| 47 | + // Setting __proto__ key should not change the returned object's prototype chain |
| 48 | + // @ts-expect-error -- intentionally setting __proto__ to test prototype pollution |
| 49 | + const result = set(obj, '__proto__', { admin: true }); |
| 50 | + |
| 51 | + // The returned copy should NOT have 'admin' accessible via prototype |
| 52 | + // @ts-expect-error -- testing prototype pollution |
| 53 | + expect(result.admin).toBeUndefined(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('is not sensible to prototype pollution via set with JSON.parse source', () => { |
| 57 | + type User = { user: string; admin?: boolean }; |
| 58 | + |
| 59 | + // JSON.parse creates __proto__ as an own property |
| 60 | + const malicious = JSON.parse( |
| 61 | + '{"user":"Eve","__proto__":{"admin":true}}' |
| 62 | + ) as User; |
| 63 | + // set on an object that already carries __proto__ from JSON.parse |
| 64 | + const result = set(malicious, 'user', 'Alice'); |
| 65 | + |
| 66 | + // The returned copy should NOT have 'admin' accessible via prototype pollution |
| 67 | + expect(result.admin).toBeUndefined(); |
| 68 | + }); |
| 69 | +}); |
0 commit comments