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/setIntervalwith a string,location = javascript:…. - Framework: React
dangerouslySetInnerHTML, Vuev-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
postMessageand prototype-pollution/DOM-clobbering gadgets. Makes DOM XSS “feel like reflected.” - Fragment delivery:
https://target/#payloadkeeps the payload off the wire; e.g.document.write(location.hash.slice(1))→#<img src=x onerror=alert(1)>. evalon unescaped JSON (reflected-DOM): PortSwigger lab breakout"-alert(1)}//cancels the quote-escaping and comments out the rest.javascript:URL sinks:location.assign/.href/.replacewith 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.nameas sources: write a value into a client-read source then navigate the victim; see the double-parsing and window.name-smuggling cases below. postMessagehandlers as cross-origin sources. Any window that can obtain a reference (opener,window.openpopup,frames[n]) can post to a listener. Review eachmessagehandler for: (1) isevent.originchecked 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 fromevent.datainsideArray#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")acceptsredacted.com.attacker.tld; the acceptedREDIRECTmessage wrotee.data.value.urltowindow.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 aninnerHTMLrender branch (Chess.com).
- No origin check +
- 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_MODALaccepted from any origin;isMessageable:false+ reason code 2 routedusernameintoinnerHTML; the sanitizer lifted&-diagramtype:comments out, ran DOMPurify 3.2.5, then pasted them back, so a pre-planted placeholder token inside adata-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.originroot cause; both the origin check and plain-DOMPurify path sit behind feature flags (XENOPS). See XSS Filter Bypasses. - postMessage →
eval(YesWeHack, $600) and postMessageincludes()origin bypass →location.href(HackerOne, $250) — see Techniques above. - SPA pitfalls: JWTs in
localStorageturn any DOM XSS into silent, persistent session theft (React/Vue).
Defenses
- Prefer safe sinks:
textContent,setAttributefor non-URL attrs,element.style.xover string HTML; avoideval/string timers. - Sanitize before HTML sinks with DOMPurify (mind Mutation XSS); validate URL schemes before
location/hrefassignment. - 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-unsanitizedflagsinnerHTML/document.writeassignments at build time. - Keep session tokens in
HttpOnlycookies, not JS-readable storage (the eval lab’s cookie washttpOnly:falsefor a legacy analytics SDK). postMessagelisteners: compareevent.originfor exact equality against an allowlist of full origins (neverincludes/startsWith/endsWithon the hostname), validate the message schema and types, copy only the fields the sender may set, and never route message data toeval,innerHTMLorlocation. 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
- What is DOM-based XSS? — PortSwigger · original
- DOM-based vulnerabilities — Web Security Academy · original
- Understanding DOM-Based XSS: Sources and Sinks · original
- Introducing DOM Invader · original
- Finding DOM Polyglot XSS in PayPal · original
- CVE-2025-1647: Bootstrap 3 XSS via DOM Clobbering · original
- Site-DOM XSS using Cookie Injection · original
- Reflected DOM XSS via double parsing · original
- $7500 DOM XSS in Facebook Mobile · original
- Finding and Fixing DOM-based XSS with Static Analysis · original
- DOM XSS via postMessage to eval: lab based on a real report · original
- How a simple postMessage validation flaw led to DOM XSS · original
- The Last Writer Wins: Chess.com account takeover via postMessage XSS · original