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 pattern | Typical input | Why it pollutes |
|---|---|---|
| Recursive deep merge | Parsed JSON request body | Follows __proto__ as a nested object |
| Path-based setters | Keys like a.b.c or a[b][c] | Builds the path through constructor.prototype |
| Query string parsers | ?__proto__[role]=admin | Turns bracket syntax into nested objects |
| Object cloning helpers | Configuration or user profile data | Copies special keys without filtering |
| Template or schema defaults | User-supplied option objects | Merges user keys over shared defaults |
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 class | Example behavior | Resulting impact |
|---|---|---|
| Authorization checks | Code tests user.isAdmin or user.role | Privilege escalation |
| Child process options | shell or env read from an options object | Remote code execution on Node.js |
| Template engines | Compiler options read from defaults | Server-side code execution |
| DOM sinks in browsers | Library reads innerHTML or src from config | DOM-based cross-site scripting |
| Type-sensitive logic | Polluted toString or length | Exceptions and denial of service |
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 compared with related flaws
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.
| Vulnerability | What the attacker controls | Typical result |
|---|---|---|
| Prototype pollution | Properties inherited by all objects | Changed logic across the whole runtime |
| Insecure deserialization | Object types and their reconstruction | Code execution during parsing |
| Mass assignment | Own properties of one model object | Overwritten fields on a single record |
| XSS | Script content rendered in a page | Code running in the victim’s browser |
| Command injection | Arguments passed to a system shell | Direct operating system commands |
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.
| Control | How it helps | Limitation |
|---|---|---|
| Block dangerous keys | Rejects __proto__, constructor, prototype | Must be applied in every merge path |
| Null-prototype objects | Object.create(null) has no chain to pollute | Breaks code expecting standard methods |
| Use `Map` for key-value data | Keys never touch the prototype | Requires refactoring existing code |
| Freeze the prototype | Object.freeze(Object.prototype) blocks writes | Can break older libraries |
| Schema validation | Allows only expected keys and types | Ineffective if additional keys are permitted |
| Node.js runtime flag | --disable-proto=delete removes __proto__ accessor | Does not stop constructor.prototype paths |
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.