DOM-Based XSS

XSS where the injection happens entirely client-side: attacker-controlled data flows from a JavaScript source to a dangerous sink without the payload ever appearing in the server’s HTTP response (OWASP Type-0).

How it works

The vulnerability is in the page’s JavaScript, not the server template. A source reads attacker-controllable data; a sink renders or evaluates it. The server may never see the payload at all — notably when it rides in the URL fragment (#…), which browsers don’t send upstream, defeating server-side WAFs and logging. This is the dominant XSS class in SPAs, where routing, templating and state live in the client.

Sources: location (.href, .search, .hash, .pathname), document.URL, document.referrer, document.cookie, window.name (persists across cross-origin navigation), postMessage data, localStorage/sessionStorage.

Sinks:

  • HTML: innerHTML, outerHTML, insertAdjacentHTML, document.write/writeln, jQuery .html().
  • Code eval: eval, Function, setTimeout/setInterval with a string, location = javascript:….
  • Framework: React dangerouslySetInnerHTML, Vue v-html, Angular [innerHTML]/bypassSecurityTrust*.

Techniques / payloads

  • Trace taint source→sink with DOM Invader (Burp’s browser): it injects canaries, flags controllable sinks, and auto-tests postMessage and prototype-pollution/DOM-clobbering gadgets. Makes DOM XSS “feel like reflected.”
  • Fragment delivery: https://target/#payload keeps the payload off the wire; e.g. document.write(location.hash.slice(1)) → #<img src=x onerror=alert(1)>.
  • eval on unescaped JSON (reflected-DOM): PortSwigger lab breakout "-alert(1)}// cancels the quote-escaping and comments out the rest.
  • javascript: URL sinks: location.assign/.href/.replace with no scheme check executes synchronously in the page origin — a prime Account Takeover primitive.
  • DOM clobbering: inject named HTML elements (<a id=x>, <form><input id=__depth>) to overwrite JS variable lookups and defeat sanitizer internals or config checks (Bootstrap 3 Tooltip CVE-2025-1647).
  • Cookie / postMessage / window.name as sources: write a value into a client-read source then navigate the victim; see the double-parsing and window.name-smuggling cases below.
  • postMessage handlers as cross-origin sources. Any window that can obtain a reference (opener, window.open popup, frames[n]) can post to a listener. Review each message handler for: (1) is event.origin checked at all, (2) is the check an exact origin comparison, (3) which message fields reach which sink. Failure patterns from the notes:
    • No origin check + eval: a same-origin “recommendations” iframe evaluated a filter formula from event.data inside Array#filter (CWE-79 + CWE-94); a popup of the parent SPA gave the attacker a frame handle to post into (bblabs lab, $600 YesWeHack).
    • Substring origin check: new URL(e.origin).hostname.includes("redacted.com") accepts redacted.com.attacker.tld; the accepted REDIRECT message wrote e.data.value.url to window.location.href (javascript: sink) (foorw1nner, $250 HackerOne).
    • No origin check + state merge: Object.assign(store().user, r.user) copied every attacker field into component state, including flags that selected an innerHTML render branch (Chess.com).
  • Double parsing: input survives a server sanitizer, then the page’s own JS re-reads the raw URL and assigns it to innerHTML, reviving a handler the first pass removed (Intigriti BRP challenge).

Notable cases

  • $7,500 DOM XSS, Facebook mobile (Johns Simon) via an insecure client source feeding a sink.
  • One-click ATO via DOM XSS: an exposed test runner exposed a sink reachable from a crafted URL.
  • PayPal DOM polyglot XSS — a polyglot found with DOM Invader, chained with script-gadget CSP bypass (old Bootstrap, custom youtube.js).
  • Site-DOM XSS via cookie injection — attacker-set cookie read as a source and written to a sink.
  • Chess.com message-modal ATO (2026) — SHOW_MESSAGE_MODAL accepted from any origin; isMessageable:false + reason code 2 routed username into innerHTML; the sanitizer lifted &-diagramtype: comments out, ran DOMPurify 3.2.5, then pasted them back, so a pre-planted placeholder token inside a data- attribute received raw markup whose " broke out of the attribute. Executed in the logged-in origin; OAuth-created accounts could set a password without the old one. Closed as a duplicate of the missing-event.origin root cause; both the origin check and plain-DOMPurify path sit behind feature flags (XENOPS). See XSS Filter Bypasses.
  • postMessage → eval (YesWeHack, $600) and postMessage includes() origin bypass → location.href (HackerOne, $250) — see Techniques above.
  • SPA pitfalls: JWTs in localStorage turn any DOM XSS into silent, persistent session theft (React/Vue).

Defenses

  • Prefer safe sinks: textContent, setAttribute for non-URL attrs, element.style.x over string HTML; avoid eval/string timers.
  • Sanitize before HTML sinks with DOMPurify (mind Mutation XSS); validate URL schemes before location/href assignment.
  • Trusted Types (require-trusted-types-for 'script') makes dangerous sinks throw unless given a typed, sanitized value — the strongest structural fix.
  • Static analysis: eslint-plugin-no-unsanitized flags innerHTML/document.write assignments at build time.
  • Keep session tokens in HttpOnly cookies, not JS-readable storage (the eval lab’s cookie was httpOnly:false for a legacy analytics SDK).
  • postMessage listeners: compare event.origin for exact equality against an allowlist of full origins (never includes/startsWith/endsWith on the hostname), validate the message schema and types, copy only the fields the sender may set, and never route message data to eval, innerHTML or location. Fixes gated behind feature flags leave the vulnerable path one flag away.

See also: Prototype Pollution

Sources