Skip to content

Lesson 2 of 14 · 15m

Reflected, Stored, and DOM-Based XSS

How the payload arrives defines the bug class: reflected rides the request, stored waits in the database for admins, and DOM XSS never crosses your perimeter at all.

Three ways the payload arrives

XSS is classified by delivery, not by sink. The sink is the same innerHTML either way. What differs is where the malicious string travels before it reaches the parser — and that determines who sees it, what defenses can touch it, and who the victims are.

Reflected: the payload rides the request

The classic shape: GET /search?q=<img src=x onerror=...> and the response echoes You searched for: <img src=x onerror=...> unencoded. The payload lives in the request, bounces off the response, executes once.

Two things follow. First, nobody is hurt who doesn't click the link — delivery is phishing, so reflected XSS scales poorly. Second, the payload passes through everything on the perimeter: the WAF, the framework's router, your logs. Reflected XSS is the class that template engines mostly killed — EJS <%=, Jinja {{, React JSX interpolation all escape by default. It survives in error pages, sort/filter parameters, and anywhere a dev bypassed the templating layer to print raw request data.

Stored: the payload waits in the database

The attacker saves the string — a comment, a profile bio, a filename, a support ticket — and the app serves it to everyone who views it. One request, every viewer executes, including admins reading the ticket in an internal panel.

Stored is the feared class because it self-distributes. On October 4, 2005, Samy Kamkar put a stored XSS in his MySpace profile that appended "samy is my hero" to viewers' profiles and copied itself into them. MySpace stripped the word javascript; Samy split it with a newline inside java\nscript inside a CSS expression-style URL and it still ran. Over a million users infected in about twenty hours. MySpace took the site down to clean it. In June 2014 the TweetDeck client did the same thing to itself: a tweet containing a script-in-a-class-attribute worm retweeted itself over 80,000 times before Twitter pulled the service offline.

That is the asymmetric part: the attacker's cost is one write; the victim count is everyone.

DOM-based: the payload never touches your server

location.hash, location.search, postMessage data, localStorage — these are read by client-side code and piped into sinks. The server never sees the string.

The location.hash case deserves a slow look. Per RFC 3986, the fragment is resolved client-side and is never sent in the HTTP request. A payload in https://yourapp.example/#<img src=x onerror=...> is invisible to your access logs, your WAF, your IDS, and every server-side filter. Your server could not encode it if it wanted to — it never received it. Output encoding happens in the response; DOM XSS is a bug in code that runs after the response ends.

The canonical bug pattern: older jQuery code did $(location.hash) to scroll to an anchor, and jQuery treated a hash beginning with < as HTML to parse — #<img src=x onerror=alert(1)> executed. That bug class got fixed in jQuery, and the pattern persisted in a thousand homegrown routers and analytics snippets.

The practical consequence: you cannot hunt DOM XSS by fuzzing endpoints. You hunt it by tracing source → sink through client code — which is why the sink list from lesson 1 and the source list (location.*, postMessage, storage) are the audit.

The fix is contextual

"Escape the output" is incomplete advice because there are four contexts with four encodings: HTML body (< → &lt;), HTML attribute (quote the attribute and encode), JavaScript string (hex-encode), and URL component (encodeURIComponent, plus a scheme allowlist for href). Escaping for the wrong context is a bypass with extra steps: an HTML-escaped javascript: URL is still a javascript: URL.

Audit by tracing wires, then fix at the sink: textContent for text, DOMPurify for markup, scheme allowlists for URLs — and for the DOM class, remember the perimeter never saw the payload. Your client code is the perimeter.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.