Skip to content

feat: add secure JSON parsing with prototype poisoning handling #603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

bjohansebas
Copy link
Member

Users of this middleware are given the option to control prototype poisoning
closes: #347

Copy link

socket-security bot commented Apr 5, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedsecure-json-parse@​3.0.210010010088100

View full report

@bjohansebas bjohansebas marked this pull request as ready for review April 5, 2025 01:22
@bjohansebas
Copy link
Member Author

secure-json-parse also has protection against constructor poisoning. Should I include that as well? Are there any use cases for it?

@@ -55,6 +56,7 @@ function json (options) {

var reviver = options?.reviver
var strict = options?.strict !== false
const protoPoisoning = options?.onProtoPoisoning || 'ignore'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future major version, we can make this throw an error or ignore it, but in the meantime, to maintain compatibility, this option is handled.

@@ -17,6 +17,7 @@
"on-finished": "^2.4.1",
"qs": "^6.14.0",
"raw-body": "^3.0.0",
"secure-json-parse": "^3.0.2",
Copy link
Member Author

@bjohansebas bjohansebas Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a version 4, but they theoretically support from node 20 onwards, so to avoid any potential issues, we’re sticking with version 3, which supports Node 18+

@@ -74,7 +76,7 @@ function json (options) {

try {
debug('parse json')
return JSON.parse(body, reviver)
return secureJson.parse(body, reviver, { protoAction: protoPoisoning })
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option would be to always protect against this issue by passing 'error' or 'remove', but I'm not sure. Other packages chose to make it a configurable option, and I think it would make some sense to do the same.

@UlisesGascon
Copy link
Member

UlisesGascon commented Apr 25, 2025

Do we want to cover this type of scenarios (constructor.prototype) or just __proto__? Anyway... I will like to include constructor.prototype in the tests so we can document the current situation. Otherwise might we misleading as most developers are aware of __proto__ but not constructor behaviors.

const obj = {};
const otherObj = {}

console.log("obj.polluted", obj.polluted) // undefined
console.log("otherObj.polluted", otherObj.polluted) // undefined

obj.constructor.prototype.polluted = true;

console.log("obj.polluted", obj.polluted) // true
console.log("otherObj.polluted", otherObj.polluted) // true

@bjohansebas
Copy link
Member Author

Yeah, although I don't think it hurts to add an option for constructor poisoning, so I'm going to add tests for the constructor prototype and add the option

@bjohansebas bjohansebas requested review from Phillip9587 and a team April 27, 2025 01:00
@bjohansebas bjohansebas requested a review from Copilot April 27, 2025 01:09
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces secure JSON parsing that addresses prototype poisoning by leveraging the secure-json-parse library and adds middleware options to handle both proto and constructor.prototype poisoning.

  • Added tests for various poisoning handling modes (ignore, error, remove) in the JSON parser.
  • Updated the JSON parser in lib/types/json.js to make use of secure-json-parse with poisoning options.
  • Enhanced documentation in README.md to detail the new onProto and onConstructor options.

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
test/json.js Added tests for prototype and constructor poisoning handling scenarios
lib/types/json.js Updated JSON parser to use secure-json-parse with poisoning options
README.md Extended documentation to explain new poisoning handling options
Files not reviewed (1)
  • package.json: Language not supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement a __proto__ check option
3 participants