Skip to main content
HighCodeCWE-1321

How to fix: Lodash _.merge / _.defaultsDeep prototype pollution

_.merge(target, userInput) and _.defaultsDeep(target, userInput) recursively copy properties from user input. Before Lodash 4.17.12 the copy wrote through __proto__, allowing attackers to set arbitrary properties on Object.prototype.

Why it matters

Object.prototype pollution enables XSS, authentication bypass, and DoS via type-confusion gadgets.

The `code-proto-pollution-lodash-merge` check verifies that the server does not expose the `proto-pollution-lodash-merge` 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. 1Upgrade Lodash to 4.17.21 or later.
  2. 2Switch to structuredClone for trusted internal data and validate keys for the rest.
  3. 3Block keys __proto__, constructor, prototype in any merge helper.

Code examples

Use lodash.mergeWith to block prototype keys

TypeScript
import mergeWith from "lodash/mergeWith";

const BLOCKED = new Set(["__proto__", "constructor", "prototype"]);

function safeMerge<T>(a: T, b: Record<string, unknown>): T {
  return mergeWith(a, b, (objVal: unknown, srcVal: unknown, key: string) => {
    if (BLOCKED.has(key)) return objVal; // keep existing, ignore source
  });
}

References

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

See every code check

Questions

How severe is Lodash _.merge / _.defaultsDeep prototype pollution?
VulnRadar rates this high 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-lodash-merge, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does Lodash _.merge / _.defaultsDeep prototype pollution 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 lodash _.merge / _.defaultsdeep prototype pollution

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

Lodash _.merge / _.defaultsDeep prototype pollution | VulnRadar