Prototype Pollution — Security

All ordinary objects borrow properties from one shared prototype, Object.prototype. A deep merge (a merge that also goes into the objects inside) that copies…

Your app's settings page accepts JSON from the user, then merges it into a settings object. One day, a regular user can suddenly open the admin menu. The user data in the database has not changed at all. What changed is something that every object on the server shares.

The cause is prototype pollution, a bug specific to JavaScript. A merge function copies keys from user data without filtering them. A key named __proto__ makes that function write to the shared blueprint of all objects instead of the object it was meant to fill. Imagine that shared blueprint as one rulebook in an office, and each object as an employee using it. If someone slips the line "everyone may enter the server room" into that book, the rule applies to every employee at once, including the ones who start tomorrow.

Prototype: the shared rulebook

Every ordinary object in JavaScript has a prototype: another object it "borrows" properties from when it doesn't have them itself. All ordinary objects have the same prototype: Object.prototype.

const user = {};
console.log(user.toString);                          // function: borrowed from Object.prototype
console.log(Object.getPrototypeOf(user) === Object.prototype); // true
console.log(user.isAdmin);                           // undefined: nowhere at all

A merge that does not filter keys

A deep merge is a function that copies the content of one object into another, including the objects inside it. The version below copies every key without checking:

// VULNERABLE: every key is copied, "__proto__" included
function deepMerge(target, source) {
  for (const key in source) {
    if (typeof source[key] === "object") {
      target[key] = target[key] || {};
      deepMerge(target[key], source[key]);
    } else {
      target[key] = source[key];
    }
  }
  return target;
}

const settings = {};
deepMerge(settings, JSON.parse('{"__proto__": {"isAdmin": true}}'));
console.log(({}).isAdmin); // true

Follow the steps with the user's input from above:

Step What happens
key = "__proto__" target[key] reads the prototype of settings, which is Object.prototype
deepMerge(Object.prototype, {isAdmin: true}) the merge walks into the shared rulebook
key = "isAdmin" Object.prototype.isAdmin = true

The final result: ({}).isAdmin is true. Even a newly created empty object "has" isAdmin: true. So code like if (user.isAdmin) can let anyone through.

The keys constructor and prototype can open the same path, because for an ordinary object, obj.constructor.prototype also leads to Object.prototype. So all three keys must be filtered.

Fix 1: skip the dangerous keys

Check every key before it is copied. If the key is on the blocked list, skip it with continue:

function safeMerge(target, source) {
  const blocked = ["__proto__", "constructor", "prototype"];

  for (const key of Object.keys(source)) {
    if (blocked.includes(key)) {
      continue; // skipped, never copied
    }
    target[key] = source[key];
  }

  return target;
}

console.log(safeMerge({ a: 1 }, { b: 2 }));                      // { a: 1, b: 2 }
safeMerge({}, JSON.parse('{"__proto__": {"polluted": true}}'));
console.log(({}).polluted);                                       // undefined

Fix 2: a container with no prototype

Another way: keep user data in a container that does not look up keys through the shared prototype. There are two choices: an object that has no prototype, or a Map. That way, there is no shared book to pollute.

const store = Object.create(null); // an object with no prototype
store.theme = "dark";
console.log(store.toString); // undefined: inherits nothing

const prefs = new Map();     // Map: any key is just an ordinary key
prefs.set("__proto__", { isAdmin: true });
console.log(({}).isAdmin);   // undefined

⚠️ Where is key filtering usually missed?

TL;DR: All ordinary objects borrow properties from one shared prototype, Object.prototype. A deep merge (a merge that also goes into the objects inside) that copies every key lets {"__proto__": {"isAdmin": true}} write to it, so ({}).isAdmin is true for all objects. Prevent it by checking every key before it is copied: for (const key of Object.keys(source)), then if (blocked.includes(key)) continue; for __proto__, constructor and prototype. For user data, use Object.create(null) (an object with no prototype) or a Map, which stores keys through set instead of as properties.

⚡ One shared rulebook

Write:

const user = {};
console.log(Object.getPrototypeOf(user) === Object.prototype); // true

Meaning: All ordinary objects borrow properties from the same prototype, Object.prototype. This prototype is like one shared rulebook: if someone adds a property there, every object "has" that property too.

⚡ A merge that doesn't filter keys

Write:

deepMerge(settings, JSON.parse('{"__proto__": {"isAdmin": true}}'));
console.log(({}).isAdmin);
Guess first: deepMerge copies every key, __proto__ (a shortcut to the prototype) included. What is ({}).isAdmin now?

true. The key __proto__ leads the merge into Object.prototype, so even a new empty object "has" isAdmin: true.

Meaning: Code like if (user.isAdmin) can let anyone through.

⚡ Check first, then copy

Write:

for (const key of Object.keys(source)) {
  if (blocked.includes(key)) {
    continue; // skipped, never copied
  }
  target[key] = source[key];
}

Meaning: blocked holds the three keys that can open a path to the prototype: __proto__, constructor and prototype. Deleting a key after it has been copied is too late.

⚡ A container with no prototype

Write:

const store = Object.create(null); // an object with no prototype

Meaning: For user data, use Object.create(null) or a Map. There is no shared book to pollute.

What you will learn