← All tracks
Advanced / Zero-DayPrototype Pollution10 labsAdvanced7h practice

Prototype Pollution: Poisoning the Shared Well

Inheritance is shared state.

“The settings merge looked harmless. It changed every object's inheritance.”

CWE-1321OWASP Top 10 A03:2021OWASP ASVS 5.5.1
The mental model
JavaScript objects inherit from prototypes. If an attacker can write to `__proto__` or `constructor.prototype`, every object that inherits from it may see the new property.

A recursive merge walks untrusted JSON and assigns keys. When the key is `__proto__`, the assignment reaches the prototype instead of a local property. A later check for `isAdmin` reads the inherited value.

We think in own properties. JavaScript thinks in prototypes.

Threat model

What is actually at stake?

Before touching a payload, map the assets, the actors and the boundary that is supposed to protect them. A finding only becomes a vulnerability when you can connect the failed boundary to a real asset.

Assets

  • Authorization flags read from object properties
  • Application configuration and defaults
  • Template or query options
  • Server-side rendering context

Actors

  • User who controls a JSON body or query-string parser
  • Attacker with a nested settings object
  • Malicious dependency or integration

Trust boundaries

  • Parsed JSON to object merge
  • Object property to inherited default
  • Library option to privileged behavior
  • Client object to server-side logic

Abuse cases

  • Send __proto__ with isAdmin=true
  • Use constructor.prototype in a nested merge
  • Pollute options used by a template or HTTP client
  • Reach a gadget that turns pollution into code execution
PrerequisitesJavaScript objects and prototypesRecursive merge and deep-copy behaviorHow property lookup walks the prototype chain
Request anatomy

Follow the decision, not the payload

Every family has a moment where trusted data becomes an authorization, parsing or state decision. Learn to point at that moment in the request path.

  1. 01

    Parse

    The application accepts JSON, query-string syntax or a config object with nested keys.

  2. 02

    Merge

    A recursive merge copies keys without blocking __proto__, constructor or prototype.

  3. 03

    Pollute

    The merge writes to Object.prototype or a shared object, affecting every object in the process.

  4. 04

    Inherit

    A later lookup such as options.isAdmin or config.escape reads the polluted value.

  5. 05

    Exploit

    The inherited property changes authorization, rendering or a library gadget, sometimes leading to code execution.

Case study

lodash defaultsDeep prototype pollution, CVE-2019-10744

CVE-2019-10744, public advisory
Context

A widely used JavaScript utility allowed a crafted object to modify Object.prototype through a recursive merge of untrusted input.

Discovery

Security researchers demonstrated that a nested __proto__ key reached the prototype during a merge. Applications that merged request data into options were affected.

Impact

The primitive could change application behavior and, through gadgets, lead to denial of service or code execution depending on the dependent libraries.

Root cause

The merge traversed dangerous keys and wrote them onto shared prototype objects without an own-property check.

Fix

Upgrade the library, block __proto__, constructor and prototype keys, use null-prototype objects or Maps for untrusted data, and validate with a strict schema.

Read it as a chain: discovery → failed decision → impact → fix. Then ask which link would have broken earliest.

Attacker lens

A repeatable testing sequence

  1. Find JSON merge, settings, clone and query-string parsers.
  2. Send `__proto__` and `constructor.prototype` keys.
  3. Try nested objects and arrays.
  4. Look for a privileged property that is read from an object.

Where smart people get fooled

  • JSON is data, but recursive merge interprets keys.
  • Client-side pollution can still affect server-side merges.
  • A schema that allows arbitrary keys is not a schema.
Detection engineering

What would the logs show?

A control is stronger when someone can see it fail. These signals are starting points for a detection rule, not a complete SIEM use case.

  • JSON or form bodies containing __proto__, constructor or prototype
  • Unexpected defaults appearing across unrelated objects
  • Authorization or rendering behavior changing after a settings update
  • A process restart clearing the anomaly
  • Dependency alerts for prototype-pollution CVEs
Starter query
index=web sourcetype=app_logs | eval proto=if(match(request_body,"(?i)(__proto__|constructor|prototype)"),1,0) | where proto=1 | stats count values(path) by clientip, user_id | where count > 0
The defender's move

Fix the decision, then prove it stays fixed

  • Use null-prototype objects and freeze prototypes.
  • Allowlist keys and reject `__proto__`, `constructor` and `prototype`.
  • Avoid recursive merge on untrusted input.
Vulnerable patterntypescript
function merge(target, source) {
  for (const key in source) {
    if (typeof source[key] === "object") merge(target[key], source[key]);
    else target[key] = source[key];
  }
}
Safer patterntypescript
const BLOCKED = new Set(["__proto__", "constructor", "prototype"]);
function safeMerge(target, source) {
  for (const key of Object.keys(source)) {
    if (BLOCKED.has(key)) continue;
    const value = source[key];
    if (value && typeof value === "object" && !Array.isArray(value)) {
      target[key] = safeMerge(Object.create(null), value);
    } else target[key] = value;
  }
  return target;
}

Block dangerous keys, iterate own enumerable keys, and use null-prototype objects for untrusted maps. Prefer a strict schema that rejects unknown nested keys over a hand-written merge.

Regression tests a reviewer would ask for

Proto key

GivenAn endpoint merges a JSON settings object

WhenThe body contains __proto__ with isAdmin=true

ThenThe key is rejected or ignored and no shared object changes

Constructor path

GivenA recursive merge processes nested objects

WhenThe body uses constructor.prototype

ThenThe merge blocks the path and Object.prototype is unchanged

Cross-request

GivenThe process handles multiple requests

WhenA polluted request is followed by a normal authorization check

ThenThe normal request is unaffected

Dependency gadget

GivenA library consumes merged options

WhenA dangerous option is inherited

ThenThe library receives only own, allowlisted properties

False positives

What this finding is not

Strong testers kill their own findings before someone else does. These are the shortcuts that turn a real bug into a weak report — or a false alarm into a wasted escalation.

  • The response does not show isAdmin: pollution may affect a later request or a different code path in the same process.
  • The merge rejects __proto__: test constructor.prototype and nested variants.
  • A JSON parser blocks the key: query-string parsers, YAML and config loaders may still accept it.
  • One request seems harmless: prototype pollution can persist across requests in a long-lived process.
Working vocabulary

Terms you should be able to use precisely

Prototype
The object from which JavaScript objects inherit properties and methods.
Prototype pollution
Writing attacker-controlled properties onto a shared prototype.
Gadget
Existing code that turns a polluted property into a security-relevant effect.
Own property
A property defined directly on an object rather than inherited.
Null-prototype object
An object created with Object.create(null), which has no prototype chain to pollute.
Recursive merge
A function that copies nested objects into a target, often the source of the flaw.
Interview drill

Answer these without reciting a payload

Why can prototype pollution persist across requests?

Object.prototype is shared by the process. A long-lived Node.js server may keep the polluted value until restart, affecting unrelated requests.

Is blocking __proto__ enough?

No. constructor.prototype and nested paths can reach the same target. Use an allowlist or strict schema and safe data structures.

How does prototype pollution become RCE?

It changes options consumed by a library gadget, such as a template engine or child_process wrapper, which then executes attacker-influenced behavior.

Knowledge check

Can you separate the mechanism from the payload?

Four questions, graded on the server. Answers and explanations are released only after you submit, and signed-in learners keep a best score.

4 questions
01What does {"__proto__":{"isAdmin":true}} attempt to do?
02Which coding pattern is safest for untrusted settings?
03Why can a single request be enough to cause damage?
04What is a gadget in this context?
الخلاصة الموسّعة
لماذا هذا مهم؟

تلوث prototype ليس مجرد مفتاح غريب في JSON؛ هو كتابة في كائن مشترك يراه كل الكود لاحقًا.

قد لا يظهر التأثير في نفس الطلب، بل في طلب آخر يقرأ خاصية وراثية مثل isAdmin أو خيار مكتبة. امنع __proto__ وconstructor.prototype، واستخدم Schema صارمًا أو كائنات بلا prototype. ولا تنسَ تحديث المكتبات، لأن سلسلة الاستغلال غالبًا تمر عبر gadget موجود فيها.

Which object will inherit the key you just wrote?