Prototype pollution
Prototype pollution mutates a runtime JavaScript prototype so injected properties cause XSS, privilege escalation, or RCE.
Material
Every JavaScript object has a prototype that supplies properties the object does not own itself. Some libraries combine recursive assignment functions such as Object.assign or deep merge with unchecked key names taken from user JSON, including the key __proto__. The attacker can then inject a property into the global prototype and change how the whole application behaves.
How the prototype chain works and how pollution happens
Plain assignment like obj[key] = value follows the ordinary set semantics that reject __proto__ as a target, while defineProperty, Object.assign, and deep merge implementations can pass __proto__ through as a real key. When a recursive merge runs across two levels, an attacker key such as isAdmin propagates into every nested object, so a forged property spreads to objects the attacker never touched.
// Fungsi merge recursive yang memeriksa __proto__
function safeMerge(target, source) {
for (const key of Object.keys(source)) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue;
const val = source[key];
if (val && typeof val === 'object' && !Array.isArray(val)) {
target[key] = safeMerge(target[key] || {}, val);
} else {
target[key] = val;
}
}
return target;
}
// Dampak: properti palsu terbawa ke objek yang tidak disentuh attacker
const payload = JSON.parse('{"__proto__":{"isAdmin":true,"role":"admin"}}');
const settings = {};
safeMerge(settings, payload);
console.log(settings.isAdmin); // undefined, tidak tercemar
console.log(({}).isAdmin); // true bila merge tidak dicurigai
console.log(JSON.stringify({})); // {} — prototype tak terlihat di JSON
Server-side detection cannot always rely on reflecting the property, because JSON.stringify only shows own properties. A more reliable method checks the derived effect, such as whether a fresh object inherits a property it should never inherit, or whether a configuration option silently changed its value.
Attack forms
- Client-side via browser APIs: DOM gadgets such as jQuery read a polluted attribute and produce XSS.
- DOM XSS: a template or renderer reads the attacker-controlled prototype property and writes it into the DOM.
- Flawed sanitisation: the filter blocks __proto__ but lets constructor.prototype through.
- Third-party libraries: a vulnerable library is called with user JSON in the browser.
- Server-side privilege escalation: a forged property such as isAdmin makes the process pass a role check.
- Remote code execution: in Node.js, a property overriding a child_process option causes a command to run.
- Data exfiltration: the carried-over fake property makes sensitive data leak into a response or a log.
Prevention
- Block the keys __proto__, constructor, and prototype when merging objects from external input.
- Use Object.create(null) for lookup maps so they have no prototype at all.
- Update libraries that ship recursive merge functions to versions where the issue is fixed.
- Freeze the prototype with Object.freeze(Object.prototype) on the server to surface pollution early.
- Validate JSON against a schema and use a key allowlist for every endpoint accepting dynamic objects.
- On the client, avoid DOM APIs prone to injection and never merge raw JSON into the DOM.
// Deteksi: cek apakah prototype global sudah tercemar
function isPrototypePolluted() {
return {}.polluted !== undefined || {}.isAdmin !== undefined;
}
console.log('prototype tercemar:', isPrototypePolluted());
Labs in this topic
Labs run on a domain separate from this site, so a payload cannot reach your account session.
Labs for this category land in Phase 2. The material is readable now.
Challenges in this category
Challenges from the PortSwigger Web Security Academy. The original labs run on PortSwigger domains, so the link opens there.