Add extra safety checks for proto pollution (#20396)

Co-authored-by: Gal Zahavi <38544478+galz10@users.noreply.github.com>
This commit is contained in:
Jacob Richman
2026-03-03 16:21:09 -08:00
committed by GitHub
parent 28af4e127f
commit af424aefa9
2 changed files with 18 additions and 4 deletions

View File

@@ -152,13 +152,27 @@ describe('customDeepMerge', () => {
}); });
it('should not pollute the prototype', () => { it('should not pollute the prototype', () => {
const maliciousSource = JSON.parse('{"__proto__": {"polluted": "true"}}'); const maliciousSource = JSON.parse('{"__proto__": {"polluted1": "true"}}');
const getMergeStrategy = () => undefined; const getMergeStrategy = () => undefined;
const result = customDeepMerge(getMergeStrategy, {}, maliciousSource); let result = customDeepMerge(getMergeStrategy, {}, maliciousSource);
expect(result).toEqual({}); expect(result).toEqual({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(({} as any).polluted).toBeUndefined(); expect(({} as any).polluted1).toBeUndefined();
const maliciousSource2 = JSON.parse(
'{"constructor": {"prototype": {"polluted2": "true"}}}',
);
result = customDeepMerge(getMergeStrategy, {}, maliciousSource2);
expect(result).toEqual({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(({} as any).polluted2).toBeUndefined();
const maliciousSource3 = JSON.parse('{"prototype": {"polluted3": "true"}}');
result = customDeepMerge(getMergeStrategy, {}, maliciousSource3);
expect(result).toEqual({});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(({} as any).polluted3).toBeUndefined();
}); });
it('should use additionalProperties merge strategy for dynamic properties', () => { it('should use additionalProperties merge strategy for dynamic properties', () => {

View File

@@ -30,7 +30,7 @@ function mergeRecursively(
for (const key of Object.keys(source)) { for (const key of Object.keys(source)) {
// JSON.parse can create objects with __proto__ as an own property. // JSON.parse can create objects with __proto__ as an own property.
// We must skip it to prevent prototype pollution. // We must skip it to prevent prototype pollution.
if (key === '__proto__') { if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
continue; continue;
} }
const srcValue = source[key]; const srcValue = source[key];