1 What is Prototype Pollution?
Prototype Pollution is a JavaScript-specific vulnerability where an attacker can add or modify properties on Object.prototype — the root prototype that all plain JavaScript objects inherit from. Once polluted, every object in the application inherits the injected properties.
How JavaScript prototypes work:
const obj = {};
console.log(obj.isAdmin); // undefined — property doesn't exist
// If an attacker pollutes Object.prototype:
Object.prototype.isAdmin = true;
// Now EVERY plain object inherits it:
const user = {};
console.log(user.isAdmin); // true — without it ever being set on user!
Vulnerable deep merge function (the classic source):
function merge(target, source) {
for (let key in source) {
if (typeof source[key] === 'object') {
merge(target[key], source[key]); // recurse
} else {
target[key] = source[key]; // assign
}
}
return target;
}
// Attacker provides this JSON as source:
const malicious = JSON.parse('{"__proto__": {"isAdmin": true}}');
merge({}, malicious);
// Now every object in the app thinks it is an admin:
console.log({}.isAdmin); // true
When key is __proto__ and the code does target[key] = source[key], it sets a property directly on Object.prototype, polluting every subsequent object.