Prototype pollution: when a key rewrites the object model

Prototype pollution is a JavaScript flaw in which attacker-controlled property names such as __proto__ make code modify a prototype that many objects share. Every object that inherits from it then carries a property nobody set on purpose. It appears in browsers and in Node.js, and the usual cause is a function that merges or copies objects without checking which keys it accepts.

Section Web attacks Updated Weakness CWE-1321

Quick facts

Weakness
CWE-1321 Improperly Controlled Modification of Object Prototype Attributes
Keys involved
__proto__, constructor, prototype
Where it occurs
Browser JavaScript and server-side Node.js
Typical cause
Recursive merge, clone or “set by path” code that trusts every key in the input
Safer containers
OWASP cheat sheet: Map, Set, Object.create(null)
Main fix
Refuse prototype-related keys in merge code, validate input against a schema, and keep libraries updated

Definitions follow the CWE entry and the OWASP Prototype Pollution Prevention Cheat Sheet; the language mechanics are described by MDN. The Node.js flag in the prevention list is documented in the Node.js command-line reference.

The prototype chain in two minutes

A JavaScript object does not hold every property it answers to. When code reads a property, the engine looks at the object itself, then at its prototype, then at that prototype’s prototype, until it finds the name or reaches null. MDN describes the chain this way in its page on inheritance and the prototype chain. A plain object such as {} sits one step below Object.prototype, and that single object is shared by nearly everything in the program.

Two details matter here. First, __proto__ is an accessor on Object.prototype; MDN notes that assigning to it runs a setter that changes the object’s prototype (see Object.prototype.__proto__). Second, every object also reaches its constructor function through a constructor property, and that function has a prototype property. So a path such as constructor.prototype leads to the same shared object without ever writing __proto__.

How pollution happens

The flaw needs two things: a key the attacker chooses, and code that writes through that key without asking where it leads. The key arrives as ordinary data, for example in a JSON body or in a query string that a parser turns into nested objects. The code is typically a deep merge, a deep clone, or a helper that sets a value at a path like a.b.c.

When such code walks into __proto__ as if it were a normal child object and then writes into it, the write lands on the shared prototype instead of on the object the developer was updating. After that, any object that does not define the same property itself will appear to have it. CWE-1321 describes this as a product that does not control modification of prototype attributes when it initializes or updates an object from external input.

The damage depends on what the application later reads from objects. A configuration lookup, an options bag or an authorization flag that is checked with a plain property read can pick up an inherited value. That is why the same bug is a nuisance in one codebase and serious in another, and why this page does not list working inputs or chains; the point is the shape of the code that allows it.

FROM A KEY TO A SHARED OBJECT 1 INPUT HAS THE KEY 2 MERGE WRITES THROUGH IT 3 SHARED PROTOTYPE CHANGES FIX: KEY REFUSED OR NO PROTOTYPE // the bug is in the write, not in the data
// how one key reaches every object

Client side

In the browser, the polluted prototype lives in one page’s JavaScript environment. The sources are usually values the page parses itself: the query string, the URL fragment, or a message from another window. Pollution alone does nothing visible; it matters when other code on the page, often a third-party library, later reads a property that is now inherited and uses it somewhere dangerous, such as building markup.

That is why client-side pollution is usually reported together with cross-site scripting, and why the analysis resembles DOM-based XSS: a source the attacker controls, a path through the page’s code, and a sink that trusts what it reads. The fix for the sink is the same as ever, which is to encode output, but the cleaner fix is to stop the pollution upstream.

Server side in Node.js

On the server the polluted object is shared by every request that the process handles, until it restarts. A request from one user can therefore change behaviour for others. The OWASP cheat sheet lists unauthorized data access, privilege escalation and remote code execution among the possible outcomes; each one needs the application to read the polluted property in a sensitive place. Where a property is passed to a template engine, a shell or a process option, the consequences resemble remote code execution.

Server-side pollution also has a neighbour in data stores. Query objects built from request bodies are another place where structure supplied by the client changes meaning; see NoSQL injection. And when a request binds client fields straight onto a model, the related risk is mass assignment. Duplicate or nested parameters, which parsers handle in different ways, are covered under HTTP parameter pollution.

Node.js ships one hardening switch. The --disable-proto option takes the value delete, which removes the __proto__ property, or throw, which makes access raise an error (see the Node.js docs). The OWASP cheat sheet treats it as defence in depth and notes that pollution through constructor.prototype is still possible. It does not replace fixing the merge code.

Where a WAF helps and where it does not

A WAF sees requests. It can notice a query parameter or a JSON member named __proto__ or constructor, or a bracketed path that contains them, and record the match. That is a useful signal: nothing legitimate normally sends such names, so a match tells you that someone is probing, and which endpoint accepts nested input.

It does not see your merge function. It cannot tell whether the endpoint copies keys recursively, and it cannot see keys that only appear after your code assembles an object from several inputs, or after a library decodes a format the filter did not parse. A request that the filter passes may still pollute; a request it flags may be harmless. The reliable control is in the application: key checks, schemas and safe containers.

In monitor mode a match is recorded and the request is still served, so the activity log shows which routes receive these keys before any enforcement. Switch to block only for routes where you have confirmed that legitimate clients never send them. Off inspects nothing. The steps for reading the log before enforcing are in WAF monitor mode. A rule can also buy time while a library update is prepared, which is the idea of virtual patching; it is a stopgap, not the repair.

How to prevent prototype pollution

  1. Reject prototype keys in merge and set-by-path code. CWE-1321 lists blocking attributes that resolve to the prototype, such as __proto__ and prototype, with a high effectiveness rating. Check constructor too, because it leads to the same place.
  2. Validate input against a schema. Accept only the fields you expect, at the depth you expect. CWE rates this mitigation as limited on its own, so pair it with the first point.
  3. Use Map or Set for keyed data. The OWASP cheat sheet recommends them over object literals when keys come from outside.
  4. Create lookup objects with Object.create(null). They inherit nothing from Object.prototype, and MDN notes that assigning to __proto__ on such an object creates an ordinary property instead of changing the prototype.
  5. Consider freezing built-in prototypes. CWE and OWASP both mention Object.freeze on Object.prototype. Freezing is shallow and can break libraries that extend built-ins, so test first; do not rely on Object.seal.
  6. Update dependencies. Many reports concern merge, clone and parsing libraries. A routine review of supply chain failures covers this, and the fix usually arrives as a version bump.
  7. Add --disable-proto on Node.js as a second layer, knowing what it leaves open.

Pollution is one case of a broader class: untrusted input changes how code interprets data. The wider category is described under injection, and the parent page, web attacks, lists the others.

Questions

What is prototype pollution?

Prototype pollution is a flaw in which attacker-controlled keys such as __proto__ or constructor cause code to change a shared JavaScript prototype, so objects that never held the attacker's data inherit it.

Is prototype pollution only a browser problem?

No. It occurs in browser code and in server-side Node.js code. In the browser it usually feeds client-side flaws such as cross-site scripting; on a server it can change how the application behaves for every request handled by that process.

How do you prevent prototype pollution?

Do not merge untrusted objects with recursive copy code that accepts any key. Block the keys __proto__, constructor and prototype, validate input against a schema, use Map or Object.create(null) for keyed data, and keep libraries updated.

Can a WAF stop prototype pollution?

A WAF can flag requests that carry the keys __proto__ or constructor in a query string or JSON body, which is worth watching in monitor mode. It cannot see how your code merges objects, so the fix has to be in the application.

What does Object.create(null) do?

Object.create(null) makes an object whose prototype is null. It inherits nothing from Object.prototype, so a key named __proto__ on it is an ordinary property and cannot reach the shared prototype.