Prototype pollution

On this page 7

Prototype pollution is a JavaScript vulnerability in which an attacker injects properties into a shared object prototype, usually Object.prototype, so that every object in the running program inherits attacker-controlled values. In application security and cybersecurity more broadly, it is classified under CWE-1321 and treated as an enabling flaw: the pollution itself rarely causes harm, but it changes how otherwise safe code behaves.

The consequences range from denial of service and authorization bypass to cross-site scripting in browsers and remote code execution on Node.js servers. Severity depends less on the polluting code than on the gadgets elsewhere in the application that read the injected property.

How prototype pollution works in JavaScript

Prototype pollution works because JavaScript resolves missing properties through the prototype chain. When code reads options.isAdmin and the object has no such own property, the engine walks up to Object.prototype; if an attacker has already set isAdmin there, the lookup returns the attacker’s value instead of undefined.

The injection usually happens in code that recursively merges, clones or assigns nested data from untrusted input. A JSON body such as {"__proto__": {"isAdmin": true}} passed to a naive deep-merge function causes the function to treat __proto__ as an ordinary key, descend into the real prototype and write the property there. The special keys __proto__, constructor and prototype are the three classic entry paths.

The following table lists the code patterns that most often act as pollution sources.

Code patternTypical inputWhy it pollutes
Recursive deep mergeParsed JSON request bodyFollows __proto__ as a nested object
Path-based settersKeys like a.b.c or a[b][c]Builds the path through constructor.prototype
Query string parsers?__proto__[role]=adminTurns bracket syntax into nested objects
Object cloning helpersConfiguration or user profile dataCopies special keys without filtering
Template or schema defaultsUser-supplied option objectsMerges user keys over shared defaults
Common sources of prototype pollution

Gadgets and the impact of a polluted prototype

A gadget is any code path that reads a property which is normally undefined and uses it in a security-relevant way. Pollution plus a gadget forms an exploit chain; pollution without a reachable gadget is often only a crash risk. This is why scanners report many findings that are hard to exploit, while a single gadget in a popular library can turn a minor bug into a critical one.

The table below maps typical gadget classes to the impact an attacker can achieve.

Gadget classExample behaviorResulting impact
Authorization checksCode tests user.isAdmin or user.rolePrivilege escalation
Child process optionsshell or env read from an options objectRemote code execution on Node.js
Template enginesCompiler options read from defaultsServer-side code execution
DOM sinks in browsersLibrary reads innerHTML or src from configDOM-based cross-site scripting
Type-sensitive logicPolluted toString or lengthExceptions and denial of service
Gadget classes and resulting impact

Server-side pollution is usually more serious than client-side pollution, because the polluted prototype persists for the lifetime of the process and affects every subsequent request from every user until the server restarts.

Prototype pollution belongs to the broad family of injection and unsafe data handling flaws, but it differs from its neighbors in where the attacker’s data lands. The comparison below helps when triaging a finding.

VulnerabilityWhat the attacker controlsTypical result
Prototype pollutionProperties inherited by all objectsChanged logic across the whole runtime
Insecure deserializationObject types and their reconstructionCode execution during parsing
Mass assignmentOwn properties of one model objectOverwritten fields on a single record
XSSScript content rendered in a pageCode running in the victim’s browser
Command injectionArguments passed to a system shellDirect operating system commands
Comparison with neighboring vulnerability classes

Mass assignment is the closest relative: both abuse unfiltered object keys. The difference is scope, because mass assignment changes one object, whereas prototype pollution changes the behavior of every object that relies on inheritance.

Detecting and preventing prototype pollution

Effective prevention combines safe object construction, key filtering and runtime hardening. No single control covers every library, so mature teams layer several of them and verify dependencies with software composition analysis.

ControlHow it helpsLimitation
Block dangerous keysRejects __proto__, constructor, prototypeMust be applied in every merge path
Null-prototype objectsObject.create(null) has no chain to polluteBreaks code expecting standard methods
Use `Map` for key-value dataKeys never touch the prototypeRequires refactoring existing code
Freeze the prototypeObject.freeze(Object.prototype) blocks writesCan break older libraries
Schema validationAllows only expected keys and typesIneffective if additional keys are permitted
Node.js runtime flag--disable-proto=delete removes __proto__ accessorDoes not stop constructor.prototype paths
Defensive controls and their limits

For detection, static analysis flags recursive merges over user input, dynamic testing sends payloads such as __proto__[polluted]=1 and checks whether a fresh object inherits the value, and dependency scanning catches known vulnerable versions of merge and parsing libraries.

Common mistakes when fixing the flaw

  • Filtering only __proto__ while leaving the `constructor.prototype` path open.
  • Checking keys with a case-sensitive or encoded comparison that URL-encoded payloads bypass.
  • Patching the vulnerable library but ignoring transitive copies bundled in other packages.
  • Assuming that TypeScript types prevent the issue, although types disappear at runtime.
  • Treating a finding as harmless without searching for reachable gadgets in the application.

In everyday language

Imagine an office where every new form is printed from one master template. If someone secretly writes “approved” on the master copy, every form printed afterward already says approved, even though nobody filled it in. Prototype pollution is tampering with that master template.

In JavaScript, objects quietly borrow missing details from a shared parent. An attacker who sneaks a value into that parent changes how the whole program behaves. The fix is to stop untrusted data from ever reaching the shared parent.

Seen in the real world

A development team runs a Node.js customer portal that accepts profile updates as JSON and combines them with stored settings through a home-grown deep-merge helper. During a routine penetration test, the tester sends a profile update containing {"__proto__": {"isAdmin": true}}. The server answers normally, but on the next request a regular test account can open the administration dashboard, because the access check reads an isAdmin property that the account never defined.

The team restarts the service to clear the polluted state, replaces the helper with a merge that rejects the three dangerous keys, adds schema validation that refuses unknown fields and enables the --disable-proto=delete flag. A retest the following week shows that the payload no longer affects fresh objects, and the finding is closed within one sprint.