Full answer
Almost every JavaScript object inherits from Object.prototype. If code writes to Object.prototype, every object that does not define that property itself will see the value.
How attackers get there: a recursive merge, clone or "set by path" function walks the keys of user JSON. The JSON {"proto": {"isAdmin": true}} is parsed by JSON.parse as an object with an own property named proto. A naive merge then does target['proto']['isAdmin'] = true, and target['proto'] is the real Object.prototype. After that, ({}).isAdmin is true everywhere in the process. Query strings with a[proto][x]=1 and paths such as constructor.prototype lead to the same problem.
Impact: authorization bypass (if (user.isAdmin)), denial of service, and in some template or child_process option patterns, remote code execution.
Defences:
- Validate input with a schema that rejects unknown keys.
- In merge code, skip proto, constructor and prototype, or only copy own properties with Object.hasOwn.
- Use Object.create(null) for dictionaries, or Map, which have no inherited keys.
- Use maintained libraries and update them. Most past vulnerabilities were in merge, clone and query-string packages.
- Freeze the prototype at start-up (Object.freeze(Object.prototype)) or run with node --disable-proto=delete. Test that your dependencies still work.
- Check privileges with an own property or a Set of IDs, not with a plain truthy lookup on an object that can inherit.
const input = JSON.parse('{"theme": "dark", "__proto__": {"isAdmin": true}}');
function unsafeMerge(target, source) {
for (const key of Object.keys(source)) {
if (typeof source[key] === 'object' && source[key] !== null) {
if (typeof target[key] !== 'object' || target[key] === null) target[key] = {};
unsafeMerge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
return target;
}
function safeMerge(target, source) {
for (const key of Object.keys(source)) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue;
if (typeof source[key] === 'object' && source[key] !== null) {
if (!Object.hasOwn(target, key) || typeof target[key] !== 'object') target[key] = {};
safeMerge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
return target;
}
const user = {};
console.log('before: a new object has isAdmin:', ({}).isAdmin);
safeMerge({}, input);
console.log('after safeMerge: a new object has isAdmin:', ({}).isAdmin);
unsafeMerge({}, input); // (error on purpose) the vulnerable version
console.log('after unsafeMerge: a new object has isAdmin:', ({}).isAdmin);
console.log('an unrelated user object is now admin:', user.isAdmin === true);
// A dictionary without a prototype cannot be polluted this way.
const safeDictionary = Object.create(null);
console.log('null-prototype object has isAdmin:', safeDictionary.isAdmin);The unsafe merge is the deliberate bug (error on purpose). After it runs, every object in the process inherits isAdmin. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): before: a new object has isAdmin: undefined after safeMerge: a new object has isAdmin: undefined after unsafeMerge: a new object has isAdmin: true an unrelated user object is now admin: true null-prototype object has isAdmin: undefined
Key points
- JSON.parse creates an own '__proto__' key that merge code may follow into Object.prototype.
- Everything inherits the polluted property.
- Skip dangerous keys, validate input, use Map or null-prototype objects.
- --disable-proto=delete and freezing add defence in depth.
Common mistakes
- Using a hand-written deep merge on user input.
- Checking permissions with if (obj.isAdmin) on an object built from user data.
- Believing JSON.parse itself pollutes the prototype. The merge does.
- Ignoring the problem in dependencies such as old lodash or query parsers.
Follow-ups
- How does Object.hasOwn differ from the in operator?
- What is the difference between __proto__ and prototype?
- How can prototype pollution lead to remote code execution?
Tips
- Walk through the attack in three lines: JSON, merge, inheritance.
- Offer both prevention (validation) and mitigation (freeze, flags).
- Say that outdated dependencies are the usual source.