Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.79k stars 215 forks source link

Security Fix for Prototype Pollution - huntr.dev #262

Closed huntr-helper closed 3 years ago

huntr-helper commented 3 years ago

https://huntr.dev/users/alromh87 has fixed the Prototype Pollution vulnerability πŸ”¨. alromh87 has been awarded $25 for fixing the vulnerability through the huntr bug bounty program πŸ’΅. Think you could fix a vulnerability like this?

Get involved at https://huntr.dev/

Q | A Version Affected | ALL Bug Fix | YES Original Pull Request | https://github.com/418sec/JSON-Patch/pull/2 Vulnerability README | https://github.com/418sec/huntr/blob/master/bounties/npm/fast-json-patch/1/README.md

User Comments:

πŸ“Š Metadata *

fast-json-patch is vulnerable to Prototype Pollution. This package allowing for modification of prototype behavior, which may result in Information Disclosure/DoS/RCE.

Bounty URL: https://www.huntr.dev/bounties/1-npm-fast-json-patch

βš™οΈ Description *

Prototype Pollution refers to the ability to inject properties into existing JavaScript language construct prototypes, such as objects. JavaScript allows all Object attributes to be altered, including their magical attributes such as proto, constructor and prototype. An attacker manipulates these attributes to overwrite, or pollute, a JavaScript application object prototype of the base object by injecting other values. Properties on the Object.prototype are then inherited by all the JavaScript objects through the prototype chain.

πŸ’» Technical Description *

Fixed by avoiding setting missing magical attributes.

πŸ› Proof of Concept (PoC) *

  1. Create the following PoC file:
// poc.js
let fastjsonpatch = require("fast-json-patch");
function A() {}
let a = new A();
let b = new A();
console.log("Before : " + a.polluted);
console.log("Before : " + b.polluted);
const patch = [{op: "replace", path: "/constructor/prototype/polluted", value: "Yes! Its Polluted"}];
fastjsonpatch.applyPatch(a, patch);
console.log("After : " + a.polluted);
console.log("After : " + b.polluted);
  1. Execute the following commands in another terminal:
npm i fast-json-patch # Install affected module
node poc.js #  Run the PoC
  1. Check the Output:
    Before : undefined
    Before : undefined
    After : Yes! Its Polluted
    After : Yes! Its Polluted

    Captura de pantalla de 2020-10-22 13-56-22

πŸ”₯ Proof of Fix (PoF) *

After fix and exception will be thrown

Captura de pantalla de 2020-10-22 13-55-23

πŸ‘ User Acceptance Testing (UAT)

After fix functionality is unafected

JamieSlome commented 3 years ago

@Starcounter-Jack @warpech - let me know if you have any thoughts or questions!

Cheers! 🍰

nicdumz commented 3 years ago

@alromh87 @Starcounter-Jack @JamieSlome it looks like this PR would need a bit of love in order to be merged :-)

alromh87 commented 3 years ago

@nicdumz would the needed love would be merge latest changes or should it include something else?

alromh87 commented 3 years ago

updated to latest master @ https://github.com/alromh87/JSON-Patch/commit/f76022ef758cd239c952aeb707ac0cbdab135aea @JamieSlome should I PR to 418sec repo?

JamieSlome commented 3 years ago

@alromh87 - merged!

nicdumz commented 3 years ago

Great, thanks! @Starcounter-Jack this PR is now conflict-free.

For the record, I became interested in the topic this weekend as there was a CTF task combining this fast-json-patch unpatched problem and an EJS vuln to get RCE. One good writeup, here, even mentions this unmerged PR ;-)