Dev.Lib
arrow_backBack to Blogs
LEVEL_066.4June 27, 20261 min read

Prototype pollution

Web Security

Merging untrusted JSON onto `{}` can set `__proto__` / `constructor.prototype` — affects all objects.

Summary

Merging untrusted JSON onto {} can set __proto__ / constructor.prototype — affects all objects. Use Object.create(null) maps, structured clone, schema validation (Zod), libraries patched against pollution. Supply chain in lodash-era merge utilities — validate deep merge sources.

Code example

function safeMerge(target, source) {
  for (const key of Object.keys(source)) {
    if (key === '__proto__' || key === 'constructor') continue;
    target[key] = source[key];
  }
  return target;
}

const data = safeMerge(Object.create(null), untrustedJson);

Key points

  • Merging untrusted JSON onto {} can set __proto__ / constructor.prototype — affects all objects.
  • Use Object.create(null) maps, structured clone, schema validation (Zod), libraries patched against pollution.
  • Supply chain in lodash-era merge utilities — validate deep merge sources.

Further reading

Related concepts