# 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 ([[raw/bblabs.es/dom-xss-via-postmessage-to-eval-lab-based-on-a-real-report-15814|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) ([[raw/medium.com/how-a-simple-postmessage-validation-flaw-led-to-dom-xss-15816|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 ([[raw/xenops.ae/the-last-writer-wins-chess-com-account-takeover-via-postmessage-xss-15815|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 ([[raw/xenops.ae/the-last-writer-wins-chess-com-account-takeover-via-postmessage-xss-15815|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.
## Related
- [[Cross-Site Scripting]] · [[Reflected XSS]] · [[Mutation XSS]] · [[XSS Filter Bypasses]] · [[Content Security Policy]] · [[Account Takeover]]
See also: [[Prototype Pollution]]
## Sources
- [[raw/portswigger.net/what-is-dom-based-xss-cross-site-scripting-tutorial-examples-web-secur-5032|What is DOM-based XSS? — PortSwigger]]
- [[raw/portswigger.net/dom-based-vulnerabilities-web-security-academy-5051|DOM-based vulnerabilities — Web Security Academy]]
- [[raw/medium.com/understanding-dom-based-xss-sources-and-sinks-5027|Understanding DOM-Based XSS: Sources and Sinks]]
- [[raw/portswigger.net/introducing-dom-invader-dom-xss-just-got-a-whole-lot-easier-to-find-7419|Introducing DOM Invader]]
- [[raw/portswigger.net/finding-dom-polyglot-xss-in-paypal-the-easy-way-4755|Finding DOM Polyglot XSS in PayPal]]
- [[raw/herodevs.com/cve-2025-1647-bootstrap-3-xss-vulnerability-via-dom-clobbering-2949|CVE-2025-1647: Bootstrap 3 XSS via DOM Clobbering]]
- [[raw/medium.com/site-dom-xss-using-cookie-injection-the-ai-hackers-are-coming-2952|Site-DOM XSS using Cookie Injection]]
- [[raw/pepdep.com/let-the-page-do-it-reflected-dom-xss-via-double-parsing-13191|Reflected DOM XSS via double parsing]]
- [[raw/medium.com/7500-worth-dom-xss-in-facebook-mobile-site-johns-simon-medium-380|$7500 DOM XSS in Facebook Mobile]]
- [[raw/blog.mozilla.org/finding-and-fixing-dom-based-xss-with-static-analysis-attack-defense-5064|Finding and Fixing DOM-based XSS with Static Analysis]]
- [[raw/bblabs.es/dom-xss-via-postmessage-to-eval-lab-based-on-a-real-report-15814|DOM XSS via postMessage to eval: lab based on a real report]]
- [[raw/medium.com/how-a-simple-postmessage-validation-flaw-led-to-dom-xss-15816|How a simple postMessage validation flaw led to DOM XSS]]
- [[raw/xenops.ae/the-last-writer-wins-chess-com-account-takeover-via-postmessage-xss-15815|The Last Writer Wins: Chess.com account takeover via postMessage XSS]]