Update safeParseJson to check for dangerous object keys recursively

Improve detection of prototype pollution patterns in JSON parsing
This commit is contained in:
2026-05-06 22:38:31 +02:00
parent 579e60f5d5
commit ea792b0034
11 changed files with 1105 additions and 1129 deletions
+13 -2
View File
@@ -138,11 +138,22 @@ describe('Validation Functions', () => {
expect(() => safeParseJson('{invalid json}')).toThrow('Invalid JSON');
});
it('should throw on code injection patterns', () => {
it('should throw on dangerous object keys', () => {
expect(() => safeParseJson('{"constructor": "function(){}"}')).toThrow('dangerous');
expect(() => safeParseJson('{"prototype": "something"}')).toThrow('dangerous');
expect(() => safeParseJson('{"__proto__": "something"}')).toThrow('dangerous');
expect(() => safeParseJson('{"function": "alert"}')).toThrow('dangerous');
});
it('should allow legitimate content containing function-related words', () => {
// Should not throw - these are legitimate values, not dangerous keys
expect(() =>
safeParseJson('{"message": "The function constructor is used to create objects"}')
).not.toThrow();
expect(() =>
safeParseJson('{"content": "JavaScript prototype inheritance is powerful"}')
).not.toThrow();
expect(() => safeParseJson('{"code": "function example() { return true; }"}')).not.toThrow();
expect(() => safeParseJson('{"function": "alert"}')).not.toThrow(); // function as key is now allowed
});
it('should throw on deeply nested JSON', () => {