One JSON key that changes every object in your app: prototype pollution

Prototype pollution happens when your code merges user JSON into a plain object, so a key like proto reaches the shared prototype. The OWASP fix: Map and Set, objects with no prototype, and frozen prototypes.

Published 3 min read#App security#Web development

Every ordinary object in JavaScript inherits from one shared object, Object.prototype. So if someone can write a property on that shared object, every object in your app suddenly sees it, even ones that have nothing to do with the request. This article shows how an innocent merge line opens that door, and how to close it.

What is prototype pollution?

Prototype pollution is a vulnerability that lets an attacker change your app's objects and their properties through the prototype they inherit from. The result can be access to data they have no right to, higher privileges, or code execution.

Those three outcomes are listed in the OWASP Prototype Pollution Prevention Cheat Sheet, which is the reference for every step below.

How does an attacker reach the prototype?

An attacker reaches the prototype when your code merges JSON from the user into a plain object, key by key, without checking the key names.

According to the MDN page on prototype pollution, the dangerous shape is obj[key1][key2] = value: if key1 is "__proto__", the code adds the property to Object.prototype, which is the prototype of all ordinary objects. JSON.parse alone does nothing harmful; the merge after it is what writes.

Here is a merge you will find in many projects:

function merge(target, source) {
  for (const key in source) {
    if (typeof source[key] === 'object' && source[key] !== null) {
      target[key] = target[key] || {};
      merge(target[key], source[key]);
    } else {
      target[key] = source[key];
    }
  }
  return target;
}

const settings = merge({}, JSON.parse(req.body));

If the request is {"__proto__": {"isAdmin": true}}, the merge walks into __proto__ and writes isAdmin on Object.prototype. The same page gives the isAdmin example: if it becomes true on Object.prototype, and your code does not set an explicit false for non-admins, every user is treated as an admin.

How do you prevent it in your code?

The main fix is to stop using plain objects for data whose keys come from outside. The OWASP cheat sheet gives these steps in order:

  1. Store allowed values in a Set, and key-value settings in a Map, instead of a plain object.

  2. If you need a plain object, create it with no prototype: Object.create(null). As a last resort, write the literal as {__proto__: null}.

  3. Freeze the built-in prototypes with Object.freeze(), so nobody can add a property to them.

const settings = new Map(Object.entries(JSON.parse(req.body)));

const lookup = Object.create(null);

Object.freeze(Object.prototype);

What are the limits of Object.freeze and Object.seal?

Object.freeze is shallow, and it can break libraries that modify built-in prototypes, so test it before you ship. Object.seal is not enough, because it only blocks adding and removing properties, and existing values can still change.

In Node.js there is a --disable-proto=delete option that removes __proto__ entirely. The cheat sheet treats it as an extra layer, because pollution through constructor.prototype is still possible.

Where do you start now?

The lesson for us as developers: data whose keys come from the user belongs in a Map, not in an object that inherits from a shared one.

  1. Search your code for every merge or deep copy of JSON from the request, and for the libraries that do it.

  2. Replace objects whose keys come from outside with a Map or Object.create(null).

  3. Try Object.freeze(Object.prototype) in a test environment, and see what breaks.

Deep copying itself, without a hand-written merge, is covered in the structuredClone article. Checking access on every object is in the IDOR article. And if you want someone to test your app the way an attacker would, that is what Muhassan does.

In your code, where do you merge JSON from the request into a plain object?

FAQ

Is Object.seal enough instead of Object.freeze?

No. The OWASP cheat sheet says Object.seal only blocks adding and removing properties, and existing writable values can still change.

If I freeze Object.prototype, will something break?

It can. The cheat sheet warns that freezing can break libraries that modify built-in prototypes, so test it on your app before you ship it.

Does the Node.js option that deletes proto solve the whole problem?

No. The cheat sheet treats it as an extra layer only, because pollution through constructor.prototype is still possible.

Share

More articles