Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,9 @@ yarn.lock

**/._*
**/*.pem

# CLAUDE.md
CLAUDE.md

# Claude Code directory
.claude
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ function filter (obj, { protoAction = 'error', constructorAction = 'error', safe

if (constructorAction !== 'ignore' &&
Object.prototype.hasOwnProperty.call(node, 'constructor') &&
node.constructor !== null &&
typeof node.constructor === 'object' &&
Object.prototype.hasOwnProperty.call(node.constructor, 'prototype')) { // Avoid calling node.hasOwnProperty directly
if (safe === true) {
return null
Expand Down
21 changes: 21 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,27 @@ test('parse', t => {
t.end()
})

t.test('handles constructor null safely', t => {
// Test that constructor: null doesn't trigger prototype pollution checks
t.deepEqual(
j.parse('{"constructor": null}', { constructorAction: 'remove' }),
{ constructor: null }
)

// Test that constructor: null doesn't throw error when using error action
t.deepEqual(
j.parse('{"constructor": null}', { constructorAction: 'error' }),
{ constructor: null }
)

// Test that constructor: null is preserved when using ignore action
t.deepEqual(
j.parse('{"constructor": null}', { constructorAction: 'ignore' }),
{ constructor: null }
)
t.end()
})

t.end()
})

Expand Down