Track 08 · Lesson 6 of 20Attacks and defenses

Track 08 · Attacks and defenses

Cross-site scripting (XSS)

Lesson 6 of 20· 8 min

Cross-site scripting happens when a site shows user input as HTML, so an attacker’s code runs in other people’s browsers, with their logins. It’s the attack most other browser defenses exist to contain.

Post a comment

Choose how the site displays it

Mallory’s comment
The site displays comments using

The page every visitor sees

Ana

Really clear explanation, thanks!

Mallory’s server log

No requests yet.

The code

// Vulnerable: the comment is parsed as HTML
commentEl.innerHTML = comment.text

// The same hole in React
<div dangerouslySetInnerHTML={{ __html: comment.text }} />

Three flavours#

  • Stored: saved in a database and served to everyone, like the comment above.
  • Reflected: bounced straight back from a URL, like a search page that prints ?q= into the HTML.
  • DOM-based: created by your own JavaScript, reading the URL or a message and writing it into the page.

Where the holes are#

Frameworks escape text by default: in React, {value} is always text. The holes are the escape hatches: dangerouslySetInnerHTML, innerHTML, v-html, markdown renderers fed with user content, and URLs that come from users. If you truly need user HTML, sanitize it with DOMPurify.

Once script runs, it is the user#

An injected script runs on your origin. It can read anything JavaScript can read, and call your API exactly like your own code does, including asking your refresh endpoint for fresh tokens.

XSS becomes token theft

Step through, then switch to the fixed version

XSS becomes token theft

Injected script

Your page

browser

Token refresh endpoint

Attacker host

Step 1 of 5 · Injected script → Your page

Inline <script> runs

script-src allows 'unsafe-inline'.

XSS (cross-site scripting):
Getting attacker-written JavaScript to run on our origin. That script acts as the user: it can read localStorage, call our APIs with their token and send data anywhere CSP allows.
Content Security Policy (CSP):
A response header listing where the page may load scripts, images and connections from. It is the main containment for XSS. Directives: script-src (code), connect-src (fetch/WebSocket targets), img-src (images), frame-src (iframes).
Nonces, 'unsafe-inline' and 'unsafe-eval':
'unsafe-inline' lets any inline run, which is exactly what XSS injects. 'unsafe-eval' allows eval(). A nonce is a random value generated per response: only scripts carrying that nonce run, so injected scripts fail.
Session, bearer token, access and refresh token:
After login the server hands the client a credential. A bearer token works for whoever holds it, sent as Authorization: Bearer <token>. Access tokens are short-lived; a refresh token (or a refresh endpoint) quietly gets new ones.

This is why the last track kept long-lived secrets in HttpOnly cookies and out of localStorage: XSS can still act while the tab is open, but it can’t walk away with the keys.

In a code review

LowApp

Relaying upstream error bodies as-is

Fix: Relay only JSON content types; replace anything else with a generic error.

0 of 2 checked

Check yourself

Why didn’t the script-tag comment run when inserted with innerHTML?