CVE-2020-7770: Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution') (CWE-1321)
value: This affects the package json8 before 1.0.3. The function adds in the target object the property specified in the path, however it does not properly check the key being set, leading to a prototype pollution.
CWE description: The product receives input from an upstream component that specifies attributes that are to be initialized or updated in an object, but it does not properly control modifications of attributes of the object prototype.
CVSS v3 base score: 9.8
Published: 2020-11-12T11:15Z
Repository: https://github.com/sonnyp/JSON8
Language: JavaScript
Files changed: packages/merge-patch/README.md, packages/merge-patch/lib/apply.js, packages/merge-patch/test/apply.js
Commit date: 2020-09-13 10:53:21 +0200
Commit message: json8-merge-patch: Prevent prototype pollution 2 (#116)


Diff: diff --git a/packages/merge-patch/README.md b/packages/merge-patch/README.md
index 622a873..717bc49 100644
--- a/packages/merge-patch/README.md
+++ b/packages/merge-patch/README.md
@@ -67,10 +67,22 @@ person = mergePatch.apply(person, patch)
 */
 ```
 
 [↑](#json8-merge-patch)
 
+### object creation
+
+When needed, `apply` creates objects with `null` prototype, you can choose the prototype to use with `{proto: Object}` as a third argument.
+
+[↑](#json8-merge-patch)
+
+### prototype pollution
+
+`apply` will throw with an error if [prototype pollution](https://github.com/HoLyVieR/prototype-pollution-nsec18) is attempted. You can allow for prototype pollution by passing `{pollute: true}` as a third argument.
+
+[↑](#json8-merge-patch)
+
 ### patch
 
 Alias for [apply](#apply) method.
 
 [↑](#json8-merge-patch)
diff --git a/packages/merge-patch/lib/apply.js b/packages/merge-patch/lib/apply.js
index 5415d2b..9fc9453 100644
--- a/packages/merge-patch/lib/apply.js
+++ b/packages/merge-patch/lib/apply.js
@@ -3,25 +3,33 @@
 const OBJECT = "object";
 
 /**
  * Apply a JSON merge patch onto a document
  * https://tools.ietf.org/html/rfc7396
- * @param  {Object} doc    - JSON object document
- * @param  {Object} patch  - JSON object patch
- * @return {Object}        - JSON object document
+ * @param  {Object}  doc                       - JSON object document
+ * @param  {Object}  patch                     - JSON object patch
+ * @param  {Object}  [options]                 - options
+ * @param  {Boolean} [options.pollute=false]   - Allow prototype pollution - throw otherwise
+ * @param  {Object}  [options.proto=null]      - Prototype to use for object creation
+ * @return {Object}                            - JSON object document
  */
-module.exports = function apply(doc, patch) {
+module.exports = function apply(doc, patch, options) {
   if (typeof patch !== OBJECT || patch === null || Array.isArray(patch)) {
     return patch;
   }
 
+  options = options || Object.create(null);
+
   if (typeof doc !== OBJECT || doc === null || Array.isArray(doc)) {
-    doc = Object.create(null);
+    doc = Object.create(options.proto || null);
   }
 
   const keys = Object.keys(patch);
   for (const key of keys) {
+    if (options.pollute !== true && key === "__proto__") {
+      throw new Error("Prototype pollution attempt");
+    }
     const v = patch[key];
     if (v === null) {
       delete doc[key];
       continue;
     }
diff --git a/packages/merge-patch/test/apply.js b/packages/merge-patch/test/apply.js
index 4a8b799..b9de955 100644
--- a/packages/merge-patch/test/apply.js
+++ b/packages/merge-patch/test/apply.js
@@ -49,25 +49,23 @@ describe("apply", () => {
     const patch = Object.create({ foo: "bar" });
     doc = apply(doc, patch);
     assert.deepEqual(doc, {});
   });
 
-  // https://github.com/lodash/lodash/pull/4337
+  // https://github.com/sonnyp/JSON8/issues/113
+  // https://github.com/HoLyVieR/prototype-pollution-nsec18
   it("prevents prototype pollution", () => {
     let doc = {};
-    const patch = { __proto__: { foobar: true } };
-    doc = apply(doc, patch);
+    const patch = JSON.parse('{ "__proto__": { "isAdmin": true }}');
 
-    assert.deepEqual(doc, {});
-  });
+    assert.throws(
+      () => {
+        doc = apply(doc, patch);
+      },
+      Error,
+      "Prototype pollution attempt"
+    );
 
-  // https://github.com/lodash/lodash/pull/4336
-  it("prevents constructor pollution", () => {
-    let doc = {};
-
-    const patch = { constructor: { foo: "bar" } };
-    doc = apply(doc, patch);
-    assert.equal("foo" in Object, false);
-    assert.equal(Object.foo, undefined);
-    assert.deepEqual(doc, patch);
+    assert.equal(doc.isAdmin, undefined);
+    assert.equal("isAdmin" in doc, false);
   });
 });

Vulnerable code:  * @param  {Object} doc    - JSON object document
 * @param  {Object} patch  - JSON object patch
 * @return {Object}        - JSON object document
module.exports = function apply(doc, patch) {
    doc = Object.create(null);
  // https://github.com/lodash/lodash/pull/4337
    const patch = { __proto__: { foobar: true } };
    doc = apply(doc, patch);
    assert.deepEqual(doc, {});
  });
  // https://github.com/lodash/lodash/pull/4336
  it("prevents constructor pollution", () => {
    let doc = {};

    const patch = { constructor: { foo: "bar" } };
    doc = apply(doc, patch);
    assert.equal("foo" in Object, false);
    assert.equal(Object.foo, undefined);
    assert.deepEqual(doc, patch);
Fixed code: ### object creation

When needed, `apply` creates objects with `null` prototype, you can choose the prototype to use with `{proto: Object}` as a third argument.

[↑](#json8-merge-patch)

### prototype pollution

`apply` will throw with an error if [prototype pollution](https://github.com/HoLyVieR/prototype-pollution-nsec18) is attempted. You can allow for prototype pollution by passing `{pollute: true}` as a third argument.

[↑](#json8-merge-patch)

 * @param  {Object}  doc                       - JSON object document
 * @param  {Object}  patch                     - JSON object patch
 * @param  {Object}  [options]                 - options
 * @param  {Boolean} [options.pollute=false]   - Allow prototype pollution - throw otherwise
 * @param  {Object}  [options.proto=null]      - Prototype to use for object creation
 * @return {Object}                            - JSON object document
module.exports = function apply(doc, patch, options) {
  options = options || Object.create(null);

    doc = Object.create(options.proto || null);
    if (options.pollute !== true && key === "__proto__") {
      throw new Error("Prototype pollution attempt");
    }
  // https://github.com/sonnyp/JSON8/issues/113
  // https://github.com/HoLyVieR/prototype-pollution-nsec18
    const patch = JSON.parse('{ "__proto__": { "isAdmin": true }}');
    assert.throws(
      () => {
        doc = apply(doc, patch);
      },
      Error,
      "Prototype pollution attempt"
    );
    assert.equal(doc.isAdmin, undefined);
    assert.equal("isAdmin" in doc, false);