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.

JavascriptSecurity Boundaries

Code

snippet
// 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: true

How 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 of continue when 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

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.