Home / patterns

Prototype pollution from deep-merging user JSON into objects

A developer at a standing desk reviewing JavaScript on a laptop beside a printed object-tree diagram with one branch circled in red, no brand logos
Quick answer: AI coding tools love a quick deepMerge(), setByPath() or recursive “apply settings” helper that copies a user’s JSON into an options or config object. If keys like __proto__, constructor or prototype aren’t blocked, an attacker can add properties to every object in the Node.js process—an isAdmin flag that suddenly reads true, a crashed server, or worse when a library trusts those properties. MITRE tracks this as CWE-1321.

Last updated November 11, 2026

Steps

  1. Search the project for hand-written merge, clone, extend, defaultsDeep, set-by-path and “apply patch” helpers, plus any loop that does target[key] = source[key] over request JSON, query strings or form data.
  2. In each, skip or reject the keys __proto__, constructor and prototype at every level—or better, rebuild the object from an allowlist of expected fields.
  3. Validate request bodies with a strict schema (Zod, Joi, Ajv, Pydantic-style) that rejects unknown keys, so arbitrary nested keys never reach merge code.
  4. Use Map or Set for user-keyed lookups instead of plain objects, and create plain dictionaries with Object.create(null) so they have no prototype to pollute.
  5. Avoid authorization checks that read a property that might come from the prototype (if (user.isAdmin)); use Object.hasOwn(user, 'isAdmin') or explicit fields from your database.
  6. Update merge and query-string libraries: prototype pollution has been reported in popular helpers, and fixes ship in newer versions. Run npm audit and check advisories.
  7. As defense in depth on Node.js servers, consider starting with --disable-proto=delete, and test that your dependencies still work.
  8. Retest: send {"__proto__":{"polluted":true}} and a constructor.prototype variant to every JSON endpoint, then confirm ({}).polluted is still undefined on the server.

Why it matters

MITRE CWE-1321 notes the weakness is often found in code that assigns object attributes from user input or merges and clones objects recursively, and lists real CVEs in object-merging and set-by-path helpers. OWASP’s Prototype Pollution Prevention Cheat Sheet recommends Map/Set, prototype-less objects via Object.create(null), carefully tested freezing of built-in prototypes, and Node’s --disable-proto flag as a defense-in-depth measure. PortSwigger’s Web Security Academy covers both server-side and client-side variants.

Sources

MITRE CWE-1321: Improperly Controlled Modification of Object Prototype Attributes — https://cwe.mitre.org/data/definitions/1321.html

OWASP Cheat Sheet: Prototype Pollution Prevention — https://cheatsheetseries.owasp.org/cheatsheets/Prototype_Pollution_Prevention_Cheat_Sheet.html

PortSwigger Web Security Academy: Prototype pollution — https://portswigger.net/web-security/prototype-pollution

MDN Web Docs: JavaScript prototype pollution — https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/Prototype_pollution

Node.js documentation: Command-line options (--disable-proto) — https://nodejs.org/api/cli.html

Checked at the source on November 7, 2026.

Related patterns

Found a problem you'd rather not fix yourself?

Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.

Get it fixed by StorenTech

Vibe Security Weekly

One email a week with new failure patterns and how to fix them.

By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.