Skip to the page

XSS and injection in the browser · senior

Prototype pollution

Prototype pollution happens when code copies attacker-controlled keys like proto into objects (deep merge, set-by-path, query-string parsing), adding properties to Object.prototype that every object then inherits, which can lead to DOM XSS or logic bypasses.

The notes

What you must know

  • Sources: URL query or hash (?proto[x]=y), JSON bodies (JSON.parse keeps 'proto' as a normal own key), postMessage data.
  • Vulnerable code: recursive merge/extend, set(obj, 'a.b.c', value), query-string parsers that build nested objects.
  • Gadgets: code that reads an optional property and uses it unsafely, for example config.scriptUrl || default used as a script src, or options.isAdmin checks.
  • Fixes: skip the keys proto, constructor and prototype; use Object.create(null) or Map for dictionaries; validate input with a schema; Object.freeze(Object.prototype) in hardened apps; keep libraries (lodash, qs, jQuery) updated.
  • Server-side (Node) impact can be remote code execution; client-side impact is usually DOM XSS.

The red pen

Common traps: mistake → fix

  • Mistake: "JSON.parse is safe, so merging parsed JSON is safe"

    Fix: JSON.parse creates an own 'proto' key, and a naive merge then writes to Object.prototype.

  • Mistake: "Only old jQuery has this bug"

    Fix: any hand-written deep merge or set-by-path utility can have it.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is prototype pollution?
  2. Show code that is vulnerable and fix it.
  3. How can prototype pollution in the browser lead to XSS?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview