guides · 2026-11-14
Prototype Pollution in JavaScript: How One JSON Body Poisons Every Object
Published 2026-11-14 · Reviewed 2026-11-14 · By the Ingenious AI team
The short answer
If user-controlled JSON is deep-merged into your objects, a key named __proto__ does not set a property — it rewrites the prototype every object inherits from, so checks like user.isAdmin start returning attacker-chosen values process-wide. Block __proto__, constructor and prototype keys at every merge and parse boundary, prefer Map or Object.create(null) for user-keyed data, and keep lodash patched.
Check your own app free: paste your code or enter your site URL on the Vibe-Code Audit homepage — the free scan checks for exposed secrets, injection flaws, XSS patterns, missing headers and exposed .env/.git paths, with a copy-paste fix for every finding. No credit card.
Why this bug class only exists in JavaScript
Every normal object in JavaScript inherits from Object.prototype. Assign a property through a polluted path — obj['__proto__']['isAdmin'] = true — and you have not edited one object; you have edited the ancestor, so {} .isAdmin is now true everywhere, for every user, until the process restarts. The vulnerability enters through deep-merge utilities, query-string parsers that build nested objects, and config loaders — all places where AI-generated code happily merges request JSON with helpers like _.merge or a hand-rolled recursive extend copied from a snippet. Vibe-Code Audit detects both the _.set-with-user-path shape and parsed-query merges, plus a direct __proto__ reference pattern, because the sink is the merge, not any single endpoint.
What pollution buys an attacker
Consequences depend on what your code reads from absent properties: options objects merged with defaults (a polluted 'secure' or 'role' default applies to everyone), template settings, and — in documented real-world cases — authorization middleware that treats options.isAdmin or a polluted 'authenticated' flag as truthy. Pollution also pairs with other bugs: a polluted property can turn a merely odd code path into an exploitable one, which is why it is rated as a serious finding despite needing a logic partner to cash out. The stealth property matters too: nothing crashes, no error is logged — the process simply starts making wrong decisions.
What is Prototype pollution?
a JavaScript-specific flaw where attacker data modifies Object.prototype, the shared ancestor of nearly every object. Polluted properties then appear as defaults on objects across the entire process, corrupting logic that relies on absent properties being undefined.
The fix: starve the merge
Three layers, all cheap. First, reject or strip the three dangerous keys — __proto__, constructor, prototype — in any recursive merge, query parser configuration and JSON ingestion of untrusted objects; do it in one shared helper so every endpoint inherits it. Second, stop using plain objects as dictionaries for attacker-keyed data: Map, or Object.create(null), has no prototype to poison. Third, keep merge libraries patched — lodash's history here is a string of CVEs — and prefer structuredClone-style explicit field copying over generic deep merges for request data. Object.freeze(Object.prototype) in server bootstrap is a legitimate belt-and-braces move for processes that never legitimately extend it.
The 60-second test
Against staging, send the classic probe to any endpoint that merges or stores JSON: {"__proto__": {"polluted": "yes"}} (and the constructor.prototype variant). Then, in a server console or a follow-up request that echoes a fresh object, check whether {}.polluted exists. If it does, you are polluted — restart the process (pollution persists in memory) after fixing, because cleaning the code does not clean the already-running process's prototype.
Pollution through the query string, concretely
Consider a settings endpoint that parses ?filter[role]=user with a nesting query parser, then merges the result into a shared options object. The attacker does not need a JSON body at all: ?options[__proto__][defaultRole]=admin walks the same path — parser builds the nested object, merge walks it, and the prototype gains a property your authorization defaults now inherit. This is why the audit cannot stop at 'we never merge req.body': parsers, config loaders, cookie parsers and deep-clone helpers all merge shapes, and any of them can be the sink. Inventory the libraries that turn strings into nested objects in your stack, read their prototype-pollution advisories (this class has produced CVEs in mainstream parsers more than once), and pin versions that offer prototype-pollution protections or safe modes. The probe in the checklist should be run through the query string as well as the body — two delivery routes, one sink.
Worked example: FormForge, a form-builder SaaS
FormForge (fictional), with 860 paying workspaces, merged form-schema JSON using a recursive extend helper an AI assistant had written. A scan flagged prototype pollution via deep merge on two endpoints. The probe {"__proto__": {"isAdmin": true}} against staging made a subsequent fresh session evaluate an admin check as true — the middleware read an options object that inherited the polluted default. The team replaced the helper with an explicit schema-based builder, blocked the three dangerous keys at the JSON boundary, converted user-keyed stores to Map, and added the probe as a regression test. A restart cleared the in-memory pollution; the re-scan came back clean.
Five actions to take today
- Grep for deep-merge patterns — _.merge, defaultsDeep, recursive extend helpers, object-assign loops — and list which ones touch request data.
- Block __proto__, constructor and prototype keys in one shared sanitiser used by every merge and parser.
- Use Map or Object.create(null) for dictionaries keyed by user-supplied names.
- Probe staging with the __proto__ and constructor.prototype payloads; verify a fresh object stays clean, and restart any polluted process.
- Pin and patch lodash and query parsers, and add the pollution probe to your regression suite.
Sources
- OWASP Top 10, the OWASP Foundation's standard awareness list of the most critical web application security risks: owasp.org, 2021
- CWE-89: Improper Neutralization of Special Elements used in an SQL Command, MITRE's weakness entry for SQL injection and its mitigations: cwe.mitre.org, 2026
- OWASP Cheat Sheet Series — Secrets Management, OWASP's best-practice guide to storing, rotating and auditing secrets: cheatsheetseries.owasp.org, 2026
Frequently asked questions
Does JSON.parse itself cause pollution?
No — parsing creates a normal object that merely has an own property named __proto__ in some engines' representations. Pollution happens when code merges or assigns that key recursively into other objects. The sink is the merge, which is where the fix belongs.
We don't use lodash. Are we safe?
Not necessarily: hand-rolled recursive merges are the most common sink in AI-generated code, and several query parsers and config loaders have had the bug. Test the behaviour, not the dependency list.
How do I clean a polluted production process?
Restart it after deploying the fix. Pollution lives in the running process's memory; there is no reliable in-place unpollution, and continuing to serve from a poisoned prototype compounds the damage.
Can pollution happen through query strings?
Yes — parsers that nest keys like ?a[__proto__][x]=1 into objects feed the same merges. Configure your parser's depth and key rules, and run the same sanitiser on parsed query objects.
Related guides
- Path Traversal: How a Download Endpoint Serves Files It Never Meant to Share
- Command Injection: shell=True, os.system and child_process.exec in Generated Code
- Mass Assignment: How Passing req.body to Your Model Lets Users Promote Themselves
- Open Redirects: How Your Real Domain Becomes Someone Else's Phishing Link
- Prompt-Injection Sinks: The Exact Code Shapes That Let Text Become Instructions
- When LLM Output Is Rendered as HTML: The Stored XSS Nobody Escaped
- 7 Security Mistakes AI Coders Keep Making
- Supply-Chain Attacks in the AI-Coding Era
- Secret Hygiene: Stop API Key Leaks
- What Code Scanners Can't Find
- Cursor-Generated Code Review Checklist
- Bolt.new Pre-Deploy Security Checklist
Browse the full library on the guides index, run the checks in the launch audit, or compare plans on pricing.
Informational explainer, not a security guarantee. Scan your code free before every deploy.