CVE-2021-3918: Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution') (CWE-1321)
value: json-schema is vulnerable to Improperly Controlled Modification of Object Prototype Attributes ('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: 2021-11-13T09:15Z
Repository: https://github.com/kriszyp/json-schema
Language: JavaScript
Files changed: .gitignore, lib/validate.js, test/tests.js
Commit date: 2021-10-09 15:53:41 -0600
Commit message: Don't allow __proto__ property to be used for schema default/coerce, fixes #84

Diff: diff --git a/.gitignore b/.gitignore
index 3c3629e..4dfb120 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
 node_modules
+yarn.lock
+.vscode
\ No newline at end of file
diff --git a/lib/validate.js b/lib/validate.js
index 824a4b8..99c9c9c 100644
--- a/lib/validate.js
+++ b/lib/validate.js
@@ -205,11 +205,11 @@ var validate = exports._validate = function(/*Any*/instance,/*Object*/schema,/*O
 			if(typeof instance != 'object' || instance instanceof Array){
 				errors.push({property:path,message:"an object is required"});
 			}
 			
 			for(var i in objTypeDef){ 
-				if(objTypeDef.hasOwnProperty(i)){
+				if(objTypeDef.hasOwnProperty(i) && i != '__proto__'){
 					var value = instance[i];
 					// skip _not_ specified properties
 					if (value === undefined && options.existingOnly) continue;
 					var propDef = objTypeDef[i];
 					// set default
diff --git a/test/tests.js b/test/tests.js
index 784085f..0830ca8 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -90,6 +90,32 @@ var suite = vows.describe('JSON Schema').addBatch({
     'Links/Core': assertValidates('links', 'schema'),
 
     'Json-Ref self-validates': assertSelfValidates('json-ref'),
     'Json-Ref/Hyper': assertValidates('json-ref', 'hyper-schema'),
     'Json-Ref/Core': assertValidates('json-ref', 'schema')*/
+    prototypePollution: function() {
+        console.log('testing')
+        const instance = JSON.parse(`
+        {
+        "$schema":{
+            "type": "object",
+            "properties":{
+            "__proto__": {
+                "type": "object",
+                
+                "properties":{
+                "polluted": {
+                    "type": "string",
+                    "default": "polluted"
+                }
+                }
+            }
+            },
+            "__proto__": {}
+        }
+        }`);
+
+        const a = {};
+        validate(instance);
+        assert.equal(a.polluted, undefined);
+    }
 }).export(module);

Vulnerable code: 				if(objTypeDef.hasOwnProperty(i)){
Fixed code: yarn.lock
.vscode
				if(objTypeDef.hasOwnProperty(i) && i != '__proto__'){
    prototypePollution: function() {
        console.log('testing')
        const instance = JSON.parse(`
        {
        "$schema":{
            "type": "object",
            "properties":{
            "__proto__": {
                "type": "object",
                
                "properties":{
                "polluted": {
                    "type": "string",
                    "default": "polluted"
                }
                }
            }
            },
            "__proto__": {}
        }
        }`);

        const a = {};
        validate(instance);
        assert.equal(a.polluted, undefined);
    }