Prototype pollution is a bug class where untrusted input adds or changes properties on a shared prototype — almost always Object.prototype. Every ordinary object then inherits the value, so code far away that reads an absent property (options.isAdmin, config.template) suddenly sees the attacker's value. The impact depends on those "gadgets": privilege checks, HTML rendering (XSS), or options passed to a process or template engine.
The usual sink is a recursive merge, clone, or path setter (set(obj, 'a.b.c', v)) that walks keys from input and assigns with target[key] = value. Two key paths reach the prototype:
__proto__.JSON.parse('{"__proto__": {...}}')creates an ordinary own property named"__proto__"— JSON parsing doesn't use the literal's special__proto__syntax.Object.keyslists it, so a merge copies it. But readingtarget['__proto__']on a normal object hits the inheritedObject.prototype.__proto__getter and returnsObject.prototype, and the merge then writes into it.constructor.prototype.target.constructorisObject(inherited), andObject.prototypeis the prototype. No accessor is involved, so removing__proto__doesn't stop this path.
Prevention, strongest first:
- Validate input against a schema or key allow-list, so unknown keys never reach a merge.
- Never descend into or assign the keys
__proto__,constructor,prototype, and recurse only into the target's own properties (Object.hasOwn). - Use dictionaries without a prototype: a
Map, orObject.create(null)/{ __proto__: null }at every level you create. - Define, don't assign.
Object.defineProperty, spread andObject.fromEntriescreate own properties and never trigger the__proto__setter. - Hardening:
Object.freeze(Object.prototype)makes writes fail, and Node.js 24's--disable-proto=deleteor=throwremoves or locks the accessor. Both have costs and neither covers every path.