CVE-2021-25927 (published 2021-04-26T11:15Z)

Prototype pollution vulnerability in 'safe-flat' versions 2.0.0 through 2.0.1 allows an attacker to cause a denial of service and may lead to remote code execution.

Severity: HIGH
CVSS v2 base score: 7.5 (AV:N/AC:L/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 9.8, CRITICAL (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-1321
cwe_name: Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')
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.
extended_description: 
				By adding or modifying attributes of an object prototype, it is possible to create attributes that exist on every object, or replace critical attributes with malicious ones. This can be problematic if the product depends on existence or non-existence of certain attributes, or uses pre-defined attributes of object prototype (such as hasOwnProperty, toString or valueOf).
				This weakness is usually exploited by using a special attribute of objects called proto,  constructor or prototype. Such attributes give access to the object prototype. This weakness is often found in code that assigns object attributes based on user input, or merges or clones objects recursively.
			
			

Fix information:
repo_url: https://github.com/jessie-codes/safe-flat
commit_details: repo_url: https://github.com/jessie-codes/safe-flat
author: Jessie Barnett
author_date: 2021-01-23 19:14:38-05:00
msg: fix(unflat): fix potential for prototype pollution
repository: repo_url: https://github.com/jessie-codes/safe-flat
repo_name: jessie-codes/safe-flat
description: Safely flatten a nested JavaScript object.
repo_language: JavaScript
owner: jessie-codes
file_changes: filename: index.js
change_type: ModificationType.MODIFY
diff: @@ -45,6 +45,7 @@ const flatten = (obj, delimiter) => {
 const unflatten = (obj, delimiter) => {
   const result = {}
   const seperator = delimiter || defaultDelimiter
+  const proto = ['__proto__', 'constructor', 'prototype']
 
   if (typeof obj !== 'object' || isDate(obj)) return obj
 
@@ -52,6 +53,7 @@ const unflatten = (obj, delimiter) => {
     Object.keys(original).forEach((key) => {
       const newKeys = key.split(seperator)
       newKeys.reduce((o, k, i) => {
+        if (proto.includes(newKeys[i])) return o
         return o[k] || (o[k] = isNaN(Number(newKeys[i + 1])) ? (newKeys.length - 1 === i ? original[key] : {}) : [])
       }, result)
     })

programming_language: JavaScript
filename: unflatten.spec.js
change_type: ModificationType.MODIFY
diff: @@ -150,3 +150,15 @@ test('it should handle date objects', (t) => {
 
   t.deepEqual(unflatten(original), expected)
 })
+
+test('it should not pollute the prototype', (t) => {
+  const original = {
+    '__proto__.polluted': 'Attempt to pollute the prototype',
+    'a.prototype.polluted': 'Attempt to pollute the prototype',
+    'a.b': 'This attribute is safe',
+    'c.constructor.polluted': 'Attempt to pollute the prototype',
+    'constructor.polluted': 'Attempt to pollute the prototype'
+  }
+  unflatten(original)
+  t.assert({}.polluted == null)
+})

programming_language: TypeScript