DOM XSS: sinks in the browser
A page can be safe on the wire and still treat a URL, a message, or a stored string as HTML once script already on the page assigns it.
Diagram
From a browser source to a sink
Step 1 of 4
Source
Script reads location, a query parameter, postMessage, or a value from storage. That text is not a program.
name = params.get("name") || ""
# The value came from the URL.
# It is text.A query value is read in script.
It is assigned to innerHTML.
The browser parses it as HTML.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
4 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Sources in the browser
- What fails
- The dangerous part is not that script read a string. It is that the string later becomes markup or a script URL.
- Who is affected
- The person who opened the page, and anyone their session can act for.
- What you would notice
- A parameter, a hash, or a message used to build a fragment.
- What you change
- Treat those strings as data until they are encoded for one specific context.
Sinks that parse
- What fails
- innerHTML, outerHTML, document.write, and insertAdjacentHTML parse HTML. textContent and setAttribute for plain text do not.
- Who is affected
- The same person, because the parser runs in their origin.
- What you would notice
- A code search for innerHTML and document.write fed by a non-constant.
- What you change
- Replace the sink. Do not try to clean the string and then parse it.
URLs and messages
- What fails
- A javascript URL or an unexpected origin on postMessage is a sink even when the page never builds an HTML string.
- Who is affected
- The person who clicked, or the page that trusted a message from any window.
- What you would notice
- Links built by concatenating a query value, or a message listener that does not check event.origin.
- What you change
- Parse URLs and allow only https on hosts you listed. Check the origin before you trust a message.
The policy behind the page
- What fails
- A content security policy does not fix a bad sink, and it limits what a leftover sink can load.
- Who is affected
- People who would have run injected script if a sink remains.
- What you would notice
- A response with no script-src, or a policy that allows unsafe-inline.
- What you change
- Ship a policy that names your script sources and does not allow inline script.
The server can be right and the page still wrong
Reflected and stored cross-site scripting are about bytes the server wrote into HTML. DOM XSS is about script you already shipped. That script reads a source — the query string, the hash, storage, a message from another window — and writes it into a sink that parses HTML or navigates to a script URL.
A scanner that only looks at the HTTP response will call the page clean. The review that matters is the front-end assignment. If the string is not a constant and the API parses markup, you have the bug, whether or not you have a sample that demonstrates it.
Which assignments parse
innerHTML, outerHTML, document.write, and insertAdjacentHTML parse. So do some framework helpers that mean “trust this HTML.” textContent, createTextNode, and text bindings in the major frameworks do not. Attributes that become URLs need a parser, not concatenation: allow https, allow hosts you named, refuse everything else.
postMessage is a source only after you check event.origin against a list. A listener that acts on any message is trusting every window that can get a reference to yours. The data in the message stays data even after the origin checks out.
Whose session runs it
The person who opened the page is the one affected. Script in the origin can act as them: change settings, call APIs that accept the session, rewrite what they see. HttpOnly on the session cookie stops script from reading the cookie. It does not stop script from making requests the browser will authenticate. That is why the sink has to go, not only the cookie flag.
You would notice this in a code search and in a lab page where a parameter appears as structure instead of as characters. The note names the file, the sink, and whose session would run it. It does not include a script to paste.
What you change
Replace the sink. Pass the string as text. Where you truly need formatted text, sanitize with a library you maintain and a list of tags you can explain, then still encode for any context that is not that sanitizer’s output. Do not hand-roll a filter that deletes a few words and then calls innerHTML.
Send a content security policy that does not allow inline script and does not allow data URLs as script. The policy is the seatbelt. The sink change is the fix. Add a test that a string with angle brackets renders as those characters.
Check yourself
Recognition and the fix. Nothing here is a procedure.
The server encodes a name correctly. Client script then reads a query parameter and assigns it to innerHTML. What failed?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.