Skip to content

Prototype Property Pollution

Quick summary

You merged untrusted data that contained a __proto__ key — now every plain object inherits the injected property, breaking comparisons and logic.

Why this happens

  • Recursive merge/assign that treats __proto__ or constructor as a normal key

  • Using Object.prototype methods on dictionaries built from user input

  • Deep clone that copies __proto__ from the source into the target

Minimal example

✗ Broken code
function merge(target, src) {
  for (const k in src) target[k] = src[k];
}
merge({}, JSON.parse('{"__proto__":{"isAdmin":true}}'));
console.log({}.isAdmin); // true — polluted!
✓ Fixed code
function merge(target, src) {
  for (const k in src) {
    if (k === '__proto__' || k === 'constructor') continue;
    target[k] = src[k];
  }
}
merge({}, JSON.parse('{"__proto__":{"isAdmin":true}}'));
console.log({}.isAdmin); // undefined

The merge copies __proto__ onto the target, injecting isAdmin into Object.prototype so every object inherits it. Skipping __proto__ and constructor keys prevents the pollution.

How to diagnose

  • Check whether {}.hasOwnProperty('pollutedKey') is unexpectedly true on fresh objects

  • Inspect Object.prototype for unexpected keys added at runtime

  • Trace the merge/assign call that processed the untrusted input

How to fix

  • Block __proto__, constructor, and prototype keys before merging

  • Use Object.create(null) for dictionaries that hold untrusted keys

  • Prefer structuredClone or a merge that uses Object.defineProperty with enumerable check

How to prevent

  • Never recursively merge untrusted input without a key blocklist

  • Use Map or Object.create(null) for key-value storage of external data

Related Resources

Related Glossary

Related Lessons

  • Objects

    Learn about prototypes and objects.

← Back to language