Skip to main content
MediumCodeCWE-1321

How to fix: Object.assign source from user input

Object.assign(target, userInput) with a user-supplied source (the second argument) copies every enumerable key from userInput onto target, including __proto__ or unexpected fields the caller never intended to accept.

Why it matters

Never pass unsanitized user input as the Object.assign source (second argument); use an allowlist or spread with key filtering instead

Object.assign(target, userInput) with a user-supplied source can pollute Object.prototype or let the caller overwrite fields on target that were never meant to be user-settable.

How to fix it

  1. 1Never pass unsanitized user input as the Object.assign source (second argument); use an allowlist or spread with key filtering instead

Code examples

Block prototype pollution in Object.assign

TypeScript
function safeAssign<T extends object>(target: T, source: Record<string, unknown>): T {
  const banned = new Set(["__proto__", "constructor", "prototype"]);
  for (const [k, v] of Object.entries(source)) {
    if (!banned.has(k)) (target as Record<string, unknown>)[k] = v;
  }
  return target;
}
// Never: Object.assign(config, req.body) without validation

References

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

See every code check

Questions

How severe is Object.assign source from user input?
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-object-assign-from-user, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does Object.assign source from user input 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 source from user input

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 source from user input | VulnRadar