Improperly Controlled Modification of Dynamically-Determined Object Attributes

The product receives input from an upstream component that specifies multiple attributes, properties, or fields that are to be initialized or updated in an object, but it does not properly control which attributes can be modified.


Description

If the object contains attributes that were only intended for internal use, then their unexpected modification could lead to a vulnerability.

This weakness is sometimes known by the language-specific mechanisms that make it possible, such as mass assignment, autobinding, or object injection.

Demonstrations

The following examples help to illustrate the nature of this weakness and describe methods or techniques which can be used to mitigate the risk.

Note that the examples here are by no means exhaustive and any given weakness may have many subtle varieties, each of which may require different detection methods or runtime controls.

Example One

This function sets object attributes based on a dot-separated path.

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.

setValueByPath({}, "__proto__.isAdmin", true)
setValueByPath({}, "constructor.prototype.isAdmin", true)

By using a denylist of dangerous attributes, this weakness can be eliminated.

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;
  }

See Also

Comprehensive Categorization: Resource Control

Weaknesses in this category are related to resource control.

OWASP Top Ten 2021 Category A08:2021 - Software and Data Integrity Failures

Weaknesses in this category are related to the A08 category "Software and Data Integrity Failures" in the OWASP Top Ten 2021.

Resource Management Errors

Weaknesses in this category are related to improper management of system resources.

Comprehensive CWE Dictionary

This view (slice) covers all the elements in CWE.

CISQ Data Protection Measures

This view outlines the SMM representation of the Automated Source Code Data Protection Measurement specifications, as identified by the Consortium for Information & So...

Entries with Maintenance Notes

CWE entries in this view have maintenance notes. Maintenance notes are an indicator that an entry might change significantly in future versions. This view was created...


Common Weakness Enumeration content on this website is copyright of The MITRE Corporation unless otherwise specified. Use of the Common Weakness Enumeration and the associated references on this website are subject to the Terms of Use as specified by The MITRE Corporation.