Full answer
Every plain object inherits from Object.prototype. If an attacker can add a property there, every object in the program appears to have it. The attack needs a code path that writes to an attacker-chosen path of keys. The usual culprits are deep-merge, deep-clone, set(obj, 'a.b.c', value), and query-string parsers.
How it works: JSON.parse('{"__proto__": {"isAdmin": true}}') produces an object with an own property that is literally named __proto__. A naive merge function loops over the keys of the source. For the key __proto__, target['__proto__'] does not create a property, but reads the accessor that returns Object.prototype. The merge then recurses into it and copies isAdmin onto Object.prototype. From then on, ({}).isAdmin is true, and a check such as if (user.isAdmin) passes for users who never had the flag. The same result can come from the path constructor.prototype. The impact ranges from changed behavior, to denial of service, to remote code execution when a polluted value reaches a template engine or a child process option.
Defenses, strongest first:
- Do not use unsafe recursive merge on untrusted input. Use a maintained library at a patched version.
- Skip the keys
__proto__,constructor, andprototypewhile merging or setting paths. - Read and write only own properties, with
Object.hasOwn. - Use
Object.create(null)or aMapfor lookup tables built from user keys, so there is no prototype to pollute. - Validate input against a schema before using it.
- Harden the runtime:
Object.freeze(Object.prototype)early in startup, or Node.js--disable-proto=delete, which removes the__proto__accessor. Test that your libraries still work.
Review code that handles JSON.parse output, query strings, and configuration from outside as untrusted.
function unsafeMerge(target, source) {
for (const key in source) {
if (typeof source[key] === 'object' && source[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) {
const existing = Object.hasOwn(target, key) ? target[key] : undefined;
target[key] = safeMerge(typeof existing === 'object' && existing !== null ? existing : {}, source[key]);
} else {
target[key] = source[key];
}
}
return target;
}
const payload = '{"__proto__": {"isAdmin": true}}';
const innocent = {};
console.log('before:', innocent.isAdmin);
unsafeMerge({}, JSON.parse(payload));
console.log('after unsafe merge, a brand new object has isAdmin:', {}.isAdmin);
const user = { name: 'Asha' };
console.log('authorization check passes for:', user.name, '->', user.isAdmin === true);
delete Object.prototype.isAdmin; // clean up so the rest of the demo starts fresh
console.log('after cleanup:', {}.isAdmin);
unsafeMerge({}, JSON.parse('{"constructor": {"prototype": {"polluted": "yes"}}}'));
console.log('via constructor.prototype:', {}.polluted);
delete Object.prototype.polluted;
safeMerge({}, JSON.parse(payload));
console.log('after safe merge:', {}.isAdmin);
console.log(safeMerge({ a: { b: 1 } }, JSON.parse('{"a": {"c": 2}, "d": 3}')));
const lookup = Object.create(null);
lookup['__proto__'] = 'just a normal key here';
console.log(Object.keys(lookup), Object.getPrototypeOf(lookup), typeof lookup.toString);
const map = new Map(Object.entries(JSON.parse(payload)));
console.log([...map.keys()], map.get('__proto__'));
console.log(Object.hasOwn({}, 'isAdmin'), Object.hasOwn(JSON.parse(payload), '__proto__'));Run with Node.js v24.22.1 as js-90.js; two runs gave identical output. Output: before: undefined after unsafe merge, a brand new object has isAdmin: true authorization check passes for: Asha -> true after cleanup: undefined via constructor.prototype: yes after safe merge: undefined { a: { b: 1, c: 2 }, d: 3 } [ '__proto__' ] null undefined [ '__proto__' ] { isAdmin: true } false true
Key points
- Pollution writes properties onto Object.prototype, which every object inherits.
- Recursive merge, clone, and set-by-path with attacker keys are the common sinks.
- JSON.parse can create an own key named __proto__ that a naive merge then follows.
- Block __proto__, constructor, and prototype; use hasOwn, null-prototype objects, or Maps.
- Freeze the prototype or disable __proto__ as extra hardening.
Common mistakes
- Writing a recursive merge or set-by-path function and trusting its input.
- Checking only for the key __proto__ and missing constructor.prototype.
- Using the in operator or plain property reads for authorization flags, which see inherited values.
- Assuming JSON.parse protects against it; it only creates the dangerous own key.
Follow-ups
- Why does Object.keys not show a polluted property while for...in would?
- How can pollution of Object.prototype lead to remote code execution in a Node.js server?
- What does Object.freeze(Object.prototype) break, and how would you test for it?
Tips
- Explain the mechanism with the __proto__ key in JSON; it is the core of the question.
- Give three defenses: key filtering, null-prototype or Map, and runtime hardening.
- Say that authorization flags should be checked with hasOwn or read from a trusted structure.