What is Prototype Pollution?
Prototype Pollution is a JavaScript vulnerability where an attacker modifies the prototype of a base object (Object.prototype), affecting all objects in the application. Because JavaScript uses prototypal inheritance, properties added to Object.prototype become accessible on every object.
How Prototype Pollution Works
The Prototype Chain
In JavaScript, every object inherits from Object.prototype:
const obj = {};
obj.toString(); // Inherited from Object.prototype
When you access a property, JavaScript walks the prototype chain:
- Check own properties
- Check
__proto__(constructor's prototype) - Check
Object.prototype - Return
undefined
The Attack
Attackers inject properties through vulnerable merge/clone operations:
// Vulnerable deep merge function
function merge(target, source) {
for (let key in source) {
if (typeof source[key] === 'object') {
target[key] = merge(target[key] || {}, source[key]);
} else {
target[key] = source[key];
}
}
return target;
}
// Attacker payload
const malicious = JSON.parse('{"__proto__": {"isAdmin": true}}');
merge({}, malicious);
// Now ALL objects have isAdmin = true
const user = {};
console.log(user.isAdmin); // true!
Attack Vectors
Client-Side Prototype Pollution
Via URL Parameters:
https://example.com/page?__proto__[isAdmin]=true
https://example.com/page?constructor[prototype][isAdmin]=true
Via JSON Input:
{
"__proto__": {
"innerHTML": "<img src=x onerror=alert(1)>"
}
}
Server-Side Prototype Pollution (Node.js)
More dangerous, because it can lead to Remote Code Execution:
// If Object.prototype gets polluted with:
Object.prototype.shell = "/proc/self/exe"
Object.prototype.env = {"NODE_OPTIONS": "--require /proc/self/environ"}
// child_process.spawn without explicit env will inherit polluted properties
Gadget Chains
Prototype pollution alone often isn't exploitable without a "gadget": existing code that reads the polluted property and does something dangerous:
// Gadget in a template engine
function render(template, data) {
const options = Object.assign({}, defaults, data);
if (options.allowCode) { // Reads from prototype!
eval(template); // RCE!
}
}
Detection Methods
Static Analysis
Look for vulnerable patterns:- Recursive merge/deep copy functions
Object.assignwith user-controlled input- Lodash
_.merge,_.defaultsDeep(older versions) - jQuery
$.extend(true, ...)with untrusted data
Dynamic Testing
// Test if prototype is pollutable
fetch('/api/settings', {
method: 'POST',
body: JSON.stringify({"__proto__": {"polluted": true}})
});
// Check if pollution occurred
fetch('/api/any-endpoint').then(r => r.json()).then(data => {
console.log(data.polluted); // true = vulnerable
});
Prevention
1. Use Object.create(null) for Dictionaries
const safeMap = Object.create(null); // No prototype chain
safeMap.__proto__ = "harmless"; // Just a regular property
2. Sanitize Keys
function safeMerge(target, source) {
for (let key in source) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue;
// ... merge logic
}
}
3. Freeze the Prototype
Object.freeze(Object.prototype);
4. Use Map Instead of Objects
const data = new Map(); // Not affected by prototype pollution
Testing with SuperClue
SuperClue scans for prototype pollution through parameter injection testing and known vulnerable library version detection.