JSON.parse Owns __proto__, Assignment Triggers The Setter
JSON.parse defines __proto__ as an ordinary own property, but assigning that key hits the setter and hijacks the target object, so merges must reject it.
Code
// JSON.parse defines '__proto__' as an ordinary own property: the object's
// prototype stays Object.prototype and the payload reads back verbatim.
const parsed = JSON.parse('{"__proto__": {"admin": true}}');
console.log('own property:', Object.hasOwn(parsed, '__proto__'));
console.log('prototype untouched:', Object.getPrototypeOf(parsed) === Object.prototype);
console.log('payload readable:', parsed.__proto__.admin === true);
// Object.assign copies with [[Set]], so '__proto__' hits the setter and
// hijacks the TARGET's prototype instead of creating a property.
const target = {};
Object.assign(target, parsed);
console.log('target prototype hijacked:', Object.getPrototypeOf(target).admin === true);
console.log('Object.prototype still clean:', ({}).admin === undefined);
// Hand-rolled merges have the same hazard; reject the dangerous keys.
function safeMerge(target, patch) {
for (const key of Object.keys(patch)) {
if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue;
target[key] = patch[key];
}
return target;
}
const guarded = {};
safeMerge(guarded, parsed);
console.log('guarded merge keeps prototype:', Object.getPrototypeOf(guarded) === Object.prototype);Output
Captured from a sandboxed run of this snippet in the build pipeline.
own property: true
prototype untouched: true
payload readable: true
target prototype hijacked: true
Object.prototype still clean: true
guarded merge keeps prototype: trueHow this file behaves — JSON.parse Owns proto, Assignment Triggers The Setter
JSON.parse Owns proto, Assignment Triggers The Setter comes down to the function safeMerge.
Repetition is confined to for (const key of Object.keys(patch)) — the rest of the file is straight-line.
Points of care
- Nothing to vet in a supply-chain review — the file pulls in no packages.
- The guard
key === '__proto__' || key === 'constructor' || key === 'prototype'stops that pass of the loop early by way ofcontinuewhen its condition holds.
Using it
The entry-point signature is function safeMerge(target, patch)
The demo output is labeled (own property:, prototype untouched:, payload readable:); each label names the call that printed it.
Key statements
if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue;
const parsed = JSON.parse('{"__proto__": {"admin": true}}');
Object.assign(target, parsed);
Interactive Tools for This Topic
Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.CORS Header Policy DebuggerSimulate browser CORS preflight and credential policies against server response headers to pinpoint blocked origins, methods, or headers and fix them.CSP Header BuilderBuild a Content-Security-Policy header from per-directive sources, skipping empty ones and rejecting a blanket wildcard.
FAQ
- Are there third-party dependencies?
- No. The dependency list is empty; the code stays inside the standard library. The file defines `safeMerge` at load time.
Report a problem with this exampleOpens a pre-filled issue on GitHub.