Variant · Low-Medium

CWE-1321: Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')

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...

CWE-1321 · Variant Level ·4 CVEs ·5 Mitigations

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.

Potential Impact

Confidentiality, Integrity, Availability

Read Application Data, Modify Application Data

Availability

DoS: Crash, Exit, or Restart

Demonstrative Examples

This function sets object attributes based on a dot-separated path.
Bad
function setValueByPath (object, path, value) {
					
					    const pathArray = path.split(".");
					    const attributeToSet = pathArray.pop();
					    let objectToModify = object;
					    for (const attr of pathArray) {
					    if (typeof objectToModify[attr] !== 'object') {
					    objectToModify[attr] = {};
					        }
					        objectToModify = objectToModify[attr];
					    }
					    objectToModify[attributeToSet] = value;
					    return object;
					}
This function does not check if the attribute resolves to the object prototype. These codes can be used to add "isAdmin: true" to the object prototype.
Bad
setValueByPath({}, "__proto__.isAdmin", true)
					  setValueByPath({}, "constructor.prototype.isAdmin", true)
By using a denylist of dangerous attributes, this weakness can be eliminated.
Good
function setValueByPath (object, path, value) {
					
					    const pathArray = path.split(".");
					    const attributeToSet = pathArray.pop();
					    let objectToModify = object;
					    for (const attr of pathArray) {
					
					        // Ignore attributes which resolve to object prototype
					        if (attr === "__proto__" || attr === "constructor" || attr === "prototype") {
					
					            continue;
					        }
					
					        if (typeof objectToModify[attr] !== "object") {
					
					            objectToModify[attr] = {};
					        }
					
					        objectToModify = objectToModify[attr];
					    }
					
					    objectToModify[attributeToSet] = value;
					    return object;
					}

Mitigations & Prevention

Implementation High

By freezing the object prototype first (for example, Object.freeze(Object.prototype)), modification of the prototype becomes impossible.

Architecture and Design High

By blocking modifications of attributes that resolve to object prototype, such as proto or prototype, this weakness can be mitigated.

Implementation Limited

When handling untrusted objects, validating using a schema can be used.

Implementation High

By using an object without prototypes (via Object.create(null) ), adding object prototype attributes by accessing the prototype via the special attributes becomes impossible, mitigating this weakness.

Implementation Moderate

Map can be used instead of objects in most cases. If Map methods are used instead of object attributes, it is not possible to access the object prototype or modify it.

Detection Methods

  • Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea

Real-World CVE Examples

CVE IDDescription
CVE-2018-3721Prototype pollution by merging objects.
CVE-2019-10744Prototype pollution by setting default values to object attributes recursively.
CVE-2019-11358Prototype pollution by merging objects recursively.
CVE-2020-8203Prototype pollution by setting object attributes based on dot-separated path.

Frequently Asked Questions

What is CWE-1321?

CWE-1321 (Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Variant-level weakness. 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...

How can CWE-1321 be exploited?

Attackers can exploit CWE-1321 (Improperly Controlled Modification of Object Prototype Attributes ('Prototype Pollution')) to read application data, modify application data. This weakness is typically introduced during the Architecture and Design, Implementation phase of software development.

How do I prevent CWE-1321?

Key mitigations include: By freezing the object prototype first (for example, Object.freeze(Object.prototype)), modification of the prototype becomes impossible.

What is the severity of CWE-1321?

CWE-1321 is classified as a Variant-level weakness (Low-Medium abstraction). It has been observed in 4 real-world CVEs.