Cross-site scripting: reflected, stored, and DOM-based, and the defenses that stack
How untrusted text becomes script in another user’s browser, how to tell reflected, stored, and DOM-based XSS apart in a review, and why output encoding, a strict CSP, and HttpOnly cookies work together.
Diagram
Text becoming markup
Step 1 of 4
Input
A display name, comment, or URL parameter arrives from a user.
# Input # A display name, comment, or URL parameter arrives from a # user.
Sometimes missed in one template
None, or allows unsafe-inline
Readable by script
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
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Three places it happens
- What fails
- Reflected XSS echoes request data immediately, stored XSS saves it for later viewers, and DOM XSS happens entirely in client JavaScript.
- Who is affected
- Whoever views the page, including administrators reviewing user content.
- What you would notice
- User text rendered with innerHTML, v-html, dangerouslySetInnerHTML, or a template’s raw filter.
- What you change
- Use text APIs and autoescaping, and sanitize rich text with a maintained library.
Context decides encoding
- What fails
- HTML body, attribute, JavaScript string, and URL contexts each need different encoding.
- Who is affected
- Users of the page where the wrong encoder was used.
- What you would notice
- A value HTML-encoded but placed inside a script block or an href.
- What you change
- Avoid placing user data in script or URL contexts; when needed, use the matching encoder and validate URL schemes.
CSP is the safety net
- What fails
- A strict Content-Security-Policy blocks inline and unknown scripts even if a bug exists.
- Who is affected
- Every user, when one template is missed.
- What you would notice
- No CSP header, or one with unsafe-inline.
- What you change
- Deploy CSP in report-only mode, fix violations, then enforce with nonces.
Confirming it safely in an authorized test
Use a harmless marker that proves markup is parsed without running anything meaningful, such as a unique bold tag, and check whether it renders as formatting or as literal text. Proxies like Burp Suite and ZAP help find every place a value is reflected. Do not store test values where real users will see them; use a test account and remove them afterward.
Input: hb-test-<b>7731</b> Rendered as text: hb-test-<b>7731</b> -> encoded, fine Rendered bold: hb-test-7731 -> markup parsed, report the field
Rolling out a strict CSP
Start with Content-Security-Policy-Report-Only and a report endpoint, read the violations for a week, move inline scripts to files or add nonces, then switch to enforcing. Keep object-src none and base-uri none.
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'nonce-{per-response}'; report-to csp
Report-To: {"group":"csp","max_age":10886400,"endpoints":[{"url":"https://example.org/csp-report"}]}Going further
The DOM XSS sinks page lists the client-side sinks in detail, and the stored XSS in rich text page covers sanitizing HTML from editors.
Check yourself
Recognition and the fix. Nothing here is a procedure.
A comment renders a test bold tag as bold text for other users. Which kind of XSS risk is this?
Go deeper
Specific techniques, exercises, and glossary cards for this topic.
- 01DOM XSS: sinks in the browserA 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. Includes the diagram and a defensive fix.
- 02Stored XSS in rich textA comment, a profile, or a document that allows formatting is stored and later parsed as HTML for someone else. Includes the diagram and a defensive fix.
- 03Content Security Policy: writing, testing, and enforcing a strict policyHow CSP tells the browser which scripts, styles, frames, and connections a page may use, how to build a nonce-based strict policy, how to roll it out with report-only mode, and how to read violation reports. Includes the diagram and a defensive fix.
Exercises
- 01Evidence deskRead a query review and a server-side fetch that were already collected. Name the failure and the fix.
- 02Output encoding sandboxOne display name lands in page text, an attribute, an inline script, and a link. Pick the encoder each place needs and test it with probe values.
- 03CORS and CSP evaluatorSend cross-origin requests to four CORS configurations and run scripts against three Content Security Policies, then see what the browser allows.
- 04Terminal mission: Security headers that vanish on one pathnginx’s add_header is inherited only when a block has none of its own. Compare response headers for two paths with curl -I and find the location that drops HSTS and CSP.