|
1 | | -import { describe, expect, it } from 'vitest'; |
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
2 | 2 |
|
3 | 3 | import { fastDeepMergeAndKeep, fastDeepMergeAndReplace } from '../utils/fastDeepMerge'; |
4 | 4 |
|
| 5 | +// Helper to clean up any accidental prototype pollution during tests |
| 6 | +afterEach(() => { |
| 7 | + // @ts-expect-error - cleaning up potential pollution |
| 8 | + delete Object.prototype.polluted; |
| 9 | + // @ts-expect-error - cleaning up potential pollution |
| 10 | + delete Object.prototype.isAdmin; |
| 11 | +}); |
| 12 | + |
5 | 13 | describe('fastDeepMergeReplace', () => { |
6 | 14 | it('merges simple objects', () => { |
7 | 15 | const source = { a: '1', b: '2', c: '3' }; |
@@ -61,3 +69,99 @@ describe('fastDeepMergeKeep', () => { |
61 | 69 | expect(target).toEqual({ a: '10', b: '2', c: '3', obj: { a: '10', b: '20' } }); |
62 | 70 | }); |
63 | 71 | }); |
| 72 | + |
| 73 | +describe('prototype pollution prevention', () => { |
| 74 | + describe('fastDeepMergeAndReplace', () => { |
| 75 | + it('should not pollute Object.prototype via __proto__', () => { |
| 76 | + const payload = JSON.parse('{"__proto__": {"polluted": "true"}}'); |
| 77 | + const target = {}; |
| 78 | + |
| 79 | + fastDeepMergeAndReplace(payload, target); |
| 80 | + |
| 81 | + // @ts-expect-error - checking for pollution |
| 82 | + expect(Object.prototype.polluted).toBeUndefined(); |
| 83 | + // @ts-expect-error - checking for pollution |
| 84 | + expect({}.polluted).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should not pollute via constructor.prototype', () => { |
| 88 | + const payload = { constructor: { prototype: { isAdmin: true } } }; |
| 89 | + const target = {}; |
| 90 | + |
| 91 | + fastDeepMergeAndReplace(payload, target); |
| 92 | + |
| 93 | + // @ts-expect-error - checking for pollution |
| 94 | + expect(Object.prototype.isAdmin).toBeUndefined(); |
| 95 | + // @ts-expect-error - checking for pollution |
| 96 | + expect({}.isAdmin).toBeUndefined(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should not pollute via nested __proto__', () => { |
| 100 | + const payload = JSON.parse('{"nested": {"__proto__": {"polluted": "nested"}}}'); |
| 101 | + const target = { nested: {} }; |
| 102 | + |
| 103 | + fastDeepMergeAndReplace(payload, target); |
| 104 | + |
| 105 | + // @ts-expect-error - checking for pollution |
| 106 | + expect(Object.prototype.polluted).toBeUndefined(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should still merge safe keys normally', () => { |
| 110 | + const payload = JSON.parse('{"__proto__": {"polluted": "true"}, "safe": "value"}'); |
| 111 | + const target = {}; |
| 112 | + |
| 113 | + fastDeepMergeAndReplace(payload, target); |
| 114 | + |
| 115 | + expect(target).toEqual({ safe: 'value' }); |
| 116 | + // @ts-expect-error - checking for pollution |
| 117 | + expect(Object.prototype.polluted).toBeUndefined(); |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + describe('fastDeepMergeAndKeep', () => { |
| 122 | + it('should not pollute Object.prototype via __proto__', () => { |
| 123 | + const payload = JSON.parse('{"__proto__": {"polluted": "true"}}'); |
| 124 | + const target = {}; |
| 125 | + |
| 126 | + fastDeepMergeAndKeep(payload, target); |
| 127 | + |
| 128 | + // @ts-expect-error - checking for pollution |
| 129 | + expect(Object.prototype.polluted).toBeUndefined(); |
| 130 | + // @ts-expect-error - checking for pollution |
| 131 | + expect({}.polluted).toBeUndefined(); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should not pollute via constructor.prototype', () => { |
| 135 | + const payload = { constructor: { prototype: { isAdmin: true } } }; |
| 136 | + const target = {}; |
| 137 | + |
| 138 | + fastDeepMergeAndKeep(payload, target); |
| 139 | + |
| 140 | + // @ts-expect-error - checking for pollution |
| 141 | + expect(Object.prototype.isAdmin).toBeUndefined(); |
| 142 | + // @ts-expect-error - checking for pollution |
| 143 | + expect({}.isAdmin).toBeUndefined(); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should not pollute via nested __proto__', () => { |
| 147 | + const payload = JSON.parse('{"nested": {"__proto__": {"polluted": "nested"}}}'); |
| 148 | + const target = { nested: {} }; |
| 149 | + |
| 150 | + fastDeepMergeAndKeep(payload, target); |
| 151 | + |
| 152 | + // @ts-expect-error - checking for pollution |
| 153 | + expect(Object.prototype.polluted).toBeUndefined(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('should still merge safe keys normally', () => { |
| 157 | + const payload = JSON.parse('{"__proto__": {"polluted": "true"}, "safe": "value"}'); |
| 158 | + const target = {}; |
| 159 | + |
| 160 | + fastDeepMergeAndKeep(payload, target); |
| 161 | + |
| 162 | + expect(target).toEqual({ safe: 'value' }); |
| 163 | + // @ts-expect-error - checking for pollution |
| 164 | + expect(Object.prototype.polluted).toBeUndefined(); |
| 165 | + }); |
| 166 | + }); |
| 167 | +}); |
0 commit comments