Prototype Pollution Explained: The JavaScript Bug That Rewrites Your App’s Logic


JavaScript has a quirk most developers never think about: every object inherits from a shared blueprint called a prototype. Change that blueprint, and you change every object in the application at once. Prototype pollution is the bug that lets an attacker do exactly that — with a single crafted input. It has hit real libraries, real companies, and real bug bounty payouts, and it’s one of the most satisfying bugs to hunt once you understand it.

How JavaScript Prototypes Work

In JavaScript, objects don’t carry all their properties themselves — they inherit them from their prototype. When you read user.isAdmin and user doesn’t define it, JavaScript walks up the prototype chain and checks Object.prototype. Normally that’s harmless. But if an attacker can write to Object.prototype, every object in the app suddenly has that property.

The Vulnerable Pattern

The bug appears wherever user input is merged into an object without filtering special keys. The classic pattern is a deep-merge or config-update function:

// DANGEROUS: merges user input into an object recursively
function merge(target, source) {
    for (let key in source) {
        if (typeof source[key] === 'object') {
            merge(target[key], source[key]);
        } else {
            target[key] = source[key];
        }
    }
}

Send this JSON to an app using a function like that:

{"__proto__": {"isAdmin": true}}

The merge walks into __proto__ — JavaScript’s magic key that points at the prototype — and writes isAdmin: true onto Object.prototype. From that moment, every object in the app reads isAdmin as true. An authorization check like if (user.isAdmin) now passes for everyone. The same trick works with constructor.prototype as an alternative path.

What Attackers Do With It

Prototype pollution rarely gives a shell directly — it gives leverage:

  • Privilege escalation — polluting isAdmin, role or verified flips authorization checks for all users.
  • Bypassing validation — polluting a property the app checks for safety (like a blocked-file list) can neutralize defenses.
  • Denial of service — polluting a property the app expects to be a certain type can crash the server for everyone.
  • Remote code execution — combined with a second bug (like a template engine that reads polluted properties), pollution becomes the first link in an RCE chain.

How to Hunt It (Legally)

  1. Find the merge. In a bug bounty target, look for endpoints that accept JSON objects — profile updates, settings pages, config imports. Test whether the app merges your input into an existing object.
  2. Send a harmless probe. Try {“__proto__”: {“polluted”: “yes123”}} and then check whether a different object in the app now has a polluted property. Never use isAdmin or destructive keys in testing — a harmless marker proves the bug without changing app behavior.
  3. Confirm the impact. Show what the pollution changes: does a feature behave differently? Document it with screenshots and the exact request.
  4. Stay in scope. Only test programs you are authorized to test — a public bug bounty program or your own lab. Probing random production apps is illegal.

How Developers Fix It

  • Freeze the prototype. Object.freeze(Object.prototype) stops all writes to it.
  • Block the magic keys. Reject __proto__, constructor and prototype in any merge or deep-copy function.
  • Use null-prototype objects. Object.create(null) makes objects with no prototype at all — nothing to pollute.
  • Use safe libraries. Modern versions of lodash, jQuery and other merge utilities have patched this; keep dependencies updated.

Prototype pollution is a reminder that in JavaScript, the language’s own flexibility is the attack surface. The bug isn’t in the code the developer wrote — it’s in the code they didn’t write, the merge function that trusted every key it was handed.

Only test systems you own or have written permission to test.

Comments

Popular Posts