Update safeParseJson to check for dangerous object keys recursively
Improve detection of prototype pollution patterns in JSON parsing
This commit is contained in:
+13
-2
@@ -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', () => {
|
||||
|
||||
Reference in New Issue
Block a user