FSD Frontend System Design

Part II — The Shield · Lesson 3.15

Cross-Site Request Forgery (CSRF)

In one lineAnother site makes the user's browser send an authenticated request — cookies attach automatically.

Where you've seen itA hidden auto-submitting form on an unrelated page, POSTing to your bank while your session cookie is still valid.

  • Time16 min
  • DiagramAttack flow + SameSite and token defences
  • Chapter3 · Security

Diagram

diagram
diagram
diagram

How it works

  1. Cookies are attached by the browser based on destination, not on who initiated the request.
  2. So any site can cause an authenticated write unless something proves the request came from your UI.
  3. SameSite=Lax (now the browser default) stops cross-site POSTs while keeping normal top-level navigation working.
  4. CSRF tokens remain necessary where SameSite=None is required (embedded flows, some SSO) — the double-submit cookie pattern is the common implementation.
  5. Authorization: Bearer headers are not auto-attached, so token-in-header APIs are structurally CSRF-immune — but then you're exposed to XSS token theft instead (see 3.5).

Trade-offs

DefenceStrengthCost
SameSite=LaxBlocks most CSRFBreaks legitimate cross-site POST flows
SameSite=StrictStrongestUser arrives from a link already "logged out"
CSRF tokenWorks with NoneServer state or signed double-submit
Origin checkSimple, effectiveSome proxies strip the header

Interview angle

"Are you safe from CSRF if your API uses JWT in an Authorization header?"

  • Structurally yes, because the browser never attaches that header automatically.
  • But storing the JWT where JS can read it hands it to any XSS — you traded one risk for another.
  • The balanced answer: HttpOnly + Secure + SameSite=Lax cookies, plus a CSRF token on state-changing routes.

Recap

  • CSRF abuses automatic cookie attachment; it needs the write, not the read.
  • SameSite=Lax closes most of it; tokens cover the rest.
  • Header-based auth avoids CSRF but moves the risk to XSS — pick deliberately.