Prototype Pollution Fundamentals
What you'll learn: How attackers exploit JavaScript's prototype chain to inject malicious properties that affect every object in your application.
What Is JavaScript's Prototype Chain?
In JavaScript, nearly every object inherits properties from Object.prototype — the root ancestor. When you access a property like user.name, JavaScript looks first on the user object itself, then walks up the prototype chain to find it. This inheritance mechanism powers JavaScript's object model.
Think of it like a family recipe book. If your personal cookbook doesn't have a recipe, you check your parent's cookbook, then your grandparent's. If someone modifies that shared grandparent cookbook, everyone in the family suddenly sees the change.
The Attack: Polluting Object.prototype
Prototype pollution occurs when an attacker finds a way to inject properties into Object.prototype. Since all objects inherit from it, this pollution spreads to every object in your application.
The classic attack vector involves unsafe merging of user-controlled input:
// Vulnerable code pattern (conceptual)
function merge(target, source) {
for (let key in source) {
target[key] = source[key]; // No validation!
}
}
An attacker sends input like:
{
"__proto__": {
"isAdmin": true
}
}
This modifies Object.prototype.isAdmin, so now every object in your application has isAdmin: true — even objects created afterward. This can bypass authorization checks, trigger unexpected code paths, or enable XSS if the polluted property is later rendered unsafely.
Real-World Impact
Prototype pollution enables:
- Authorization bypass (injecting admin flags)
- Denial of service (breaking critical properties)
- Remote code execution (in Node.js environments)
- Client-side logic manipulation
Key Takeaway: Prototype pollution exploits JavaScript's inheritance model by injecting properties into Object.prototype, silently affecting all objects in the application — always validate keys when merging user input into objects.