Skip to main content
MediumCodeCWE-1321

How to fix: Object.assign with __proto__ key

Object.assign(target, JSON.parse(userInput)) or spread {...userInput} into a target object copies a __proto__ key literally; combined with a subsequent access, this can pollute Object.prototype in engines that honor the assignment.

Why it matters

Object.prototype pollution enables XSS, type confusion, and bypass of authorization checks.

The `code-proto-pollution-object-assign-proto` check verifies that the server does not expose the `proto-pollution-object-assign-proto` weakness in the code category. The detection runs against the response headers, body, or auxiliary probes (DNS, TLS, async fetch) as appropriate.

How to fix it

  1. 1Use Object.create(null) as the target for untrusted key sets.
  2. 2Filter keys against an allowlist before Object.assign.
  3. 3Adopt Object.hasOwn or hasOwnProperty checks on every read.

Code examples

Strip prototype keys before Object.assign

TypeScript
function safeAssign<T>(target: T, ...sources: Record<string, unknown>[]): T {
  const blocked = new Set(["__proto__", "constructor", "prototype"]);
  for (const source of sources) {
    for (const [k, v] of Object.entries(source)) {
      if (!blocked.has(k)) (target as Record<string, unknown>)[k] = v;
    }
  }
  return target;
}

References

Detected automatically by VulnRadar. The check id is code-proto-pollution-object-assign-proto, stable across every scan so you can filter or diff on it.

See every code check

Questions

How severe is Object.assign with __proto__ key?
VulnRadar rates this medium severity in the code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id code-proto-pollution-object-assign-proto, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does Object.assign with __proto__ key map to?
It maps to CWE-1321.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for object.assign with __proto__ key

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

How to fix: Object.assign with __proto__ key | VulnRadar