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
- When you read
user.toString, JavaScript looks inuserfirst. If it isn't there, the search goes on to its prototype,Object.prototype, and finds the property there. __proto__is an old shortcut to that prototype. Writingobj.__proto__ = xdoes not create an ordinary key. It replaces the prototype ofobj.- As a result, if someone adds a property to
Object.prototype, every ordinary object "has" that property too.
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
Object.keys(source)takes only the keys thatsourceitself owns, without keys inherited from the prototype.- If
blocked.includes(key)is true,continuejumps to the next key. - Other keys are copied as usual, then
targetis returned. A new empty object stays clean:({}).pollutedisundefined.
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
- In an object from
Object.create(null), writing the key__proto__only adds an ordinary key. - A
Mapstores keys throughsetandget, not as properties. So"__proto__"is only text. - An extra layer: validate the request body with a schema (rules for the data's shape, with Zod, for example) that refuses unknown keys. Also update old merge libraries: lodash had prototype pollution again and again, last fixed in 4.18.1 (April 2026). Don't memorise version numbers, run
npm audit.
⚠️ Where is key filtering usually missed?
- Filtering after copying. Deleting
__proto__aftertarget[key] = ...is too late. Check first, then copy. - Blocking only
__proto__.constructorandprototypecan also open a path to the prototype. Block all three. for...inwithoutObject.keys.for...inalso walks through keys inherited from the prototype.- Thinking a shallow merge is safe. A shallow merge copies only the first level. If a shallow merge copies
__proto__,Object.prototypedoes not change. But the prototype of the target object itself is replaced, sotarget.pollutedis suddenlytrue. Skip the__proto__key anyway.
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.