The Web FrontendDeeper

Kayavak / The Web Frontend / FE·28 Frontend Security / Deeper

Deeper · The Web Frontend

Frontend Security

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

When pages began showing content from their users (comments, profiles, search terms), they began running those users' scripts by accident. Microsoft engineers named the problem cross-site scripting around 2000. Its most famous demonstration was the Samy worm (2005): a script in one MySpace profile added its author as a friend of everyone who viewed it and copied itself into their profiles, reaching over a million accounts in about a day.

Defences grew in layers. Frameworks escaped output by default. Mozilla proposed Content Security Policy, first shipped in Firefox 4 (2011), so a page can declare which scripts may run. SameSite cookies closed most cross-site request forgery. Then the attack surface moved to the code teams import: the 2018 event-stream incident, where a new maintainer of a popular npm package added code to steal cryptocurrency wallets, and the 2024 compromise of the polyfill.io domain, which served malicious script to over a hundred thousand sites, made the supply chain part of frontend security.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

The browser runs whatever script ends up in your page with all your users' privileges. Security is controlling which data can become code (injection), which sites can act as your users (cross-site), and whose code you ship (supply chain), while the server still enforces everything that matters.

Question Answer
What is it? Defending users from attacks that run in or through the browser
Problem it solves Untrusted data, other sites and third-party code all meet in one page with the user's session
Parts XSS (sources and sinks), CSP and Trusted Types, CSRF and SameSite, clickjacking, dependencies and SRI, token handling
How it works Treat all input as text unless sanitised; restrict what scripts may run; restrict what other sites can make the browser do
Assumptions The browser enforces its rules; the server re-checks every request
When they change Client-side checks alone are bypassed by calling the API directly
Where it fails One injected script defeats every in-page protection, including storing tokens "securely" in JavaScript
Related ideas Origins (ch12); cookies and CORS (ch19); application security (Security)

What if?

What if you kept the access token in localStorage?

Reason it through, then open

Any script running in your page (an XSS payload, a compromised dependency) could read it and send it anywhere, and use it from another machine until it expires. An HttpOnly cookie can't be read by script, so a successful XSS can still act as the user while the page is open, but can't steal the credential for later. That difference in blast radius is why the backend-for-frontend pattern keeps tokens on a server.

What if CSP allowed scripts from your CDN's whole domain?

Reason it through, then open

Any script hosted on that CDN, including old libraries with known gadgets (such as old Angular versions that evaluate expressions in templates), could be loaded by an attacker who finds an injection point. Allowlists of hosts were bypassed so often that "strict" CSP uses nonces or hashes instead: only scripts carrying this response's secret nonce run.

Where the boundary is

Aspect XSS CSRF
Attacker's code runs In your page, as your origin On their own site
What it can do Anything your page can, including reading data Send requests with your cookies, but not read responses
Main defences Output escaping, sanitising, CSP, Trusted Types SameSite cookies, anti-CSRF tokens, checking Origin
Which is worse XSS defeats CSRF protections

How sure are we?

Claim How it's known Where from
The Samy worm spread to over a million MySpace profiles in 2005 Historical Contemporary reports and its author's account
CSP first shipped in Firefox 4 (2011) Historical Mozilla release notes
Host-allowlist CSPs are usually bypassable Measured Weichselbaum et al., CSP Is Dead, Long Live CSP!, 2016: about 95% of policies studied offered no XSS protection
HttpOnly cookies stop token theft by XSS Model They stop reading the cookie; the attacker can still act during the session

Still open

Trusted Types and the Sanitizer API aim to make DOM XSS impossible by construction, but browser support is uneven. Supply-chain defences (signed packages, provenance attestations, reproducible builds) are being adopted, slowly.

What would change your mind? If XSS stopped appearing in real breach reports and bug bounties, strict CSP would be belt and braces. It remains one of the most reported web vulnerability classes.

Recall

Why is XSS worse than CSRF?

XSS runs code as your origin and can read data and defeat CSRF defences; CSRF can only send requests.

Why do strict CSPs use nonces instead of host allowlists?

Allowlisted hosts often serve scripts attackers can abuse; nonces allow only scripts from this response.

What can an XSS attacker do even if tokens are in HttpOnly cookies?

Act as the user while the page is open, though not steal the token.

What did the event-stream and polyfill.io incidents show?

Code you import can be turned malicious; the supply chain is part of your attack surface.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.