Skip to the page

Cookies, CSRF and cross-window attacks · mid

CSRF defences: tokens, SameSite, Fetch Metadata and Origin checks

Defend every state-changing endpoint with a check the attacker cannot pass: a CSRF token tied to the session (synchronizer or signed double-submit), or a Fetch Metadata / Origin check that rejects cross-site requests, with SameSite cookies as an extra layer, and no state changes on GET.

The notes

What you must know

  • Synchronizer token (stateful apps): the server stores a random token in the session and puts it in forms or a meta tag; every POST/PUT/PATCH/DELETE must send it (hidden field or X-CSRF-Token header) and the server compares.
  • Signed double-submit cookie (stateless apps): token = HMAC(secret, sessionId + random). Send it in a cookie AND in a header/field; the server recomputes the HMAC. The naive version (just compare cookie == header, no signature) is discouraged because attackers who can set cookies (a subdomain) can forge it.
  • Fetch Metadata: browsers send Sec-Fetch-Site (same-origin, same-site, cross-site, none). Rejecting state-changing requests with cross-site is a simple, strong defence for modern browsers; keep a fallback (Origin check or token).
  • Origin/Referer check: compare the Origin header to your own origin on unsafe methods. A missing Origin or Origin: null is not proof of same-origin.
  • SameSite=Lax/Strict is defence in depth, not the only control: Lax still allows cross-site top-level GET, Chrome allows cross-site top-level POST for 2 minutes after a no-SameSite cookie is set, and same-site subdomains are not blocked.
  • Custom header for JSON APIs (X-Requested-With or Content-Type: application/json enforced by the server) forces a CORS preflight that an attacker's site cannot pass; keep the CORS allow-list tight.
  • XSS defeats every CSRF defence (the script runs on your origin and can read the token). Next.js Server Actions are POST-only and compare Origin with Host, but still need authentication and authorisation.

The red pen

Common traps: mistake → fix

  • Mistake: "SameSite cookies mean we don't need CSRF protection"

    Fix: Lax allows top-level GET, Firefox/Safari don't default to Lax, and same-site subdomains can still attack.

  • Mistake: "Check the token only if it is present"

    Fix: attackers simply omit it; a missing token must fail.

  • Mistake: "A token not tied to the session is fine"

    Fix: an attacker can use their own valid token in the victim's request.

  • Mistake: "Validate the token only on POST"

    Fix: PUT, PATCH, DELETE (and any GET with side effects) need it too.

Easy to hard

Questions interviewers ask, easy to hard

  1. How do you protect a cookie-authenticated app against CSRF?
  2. Synchronizer token vs double-submit cookie: how do they work and which is safer?
  3. What is Fetch Metadata and how would you use Sec-Fetch-Site?
  4. Is SameSite=Lax enough? Give three cases where it is not.
  5. How do Next.js Server Actions handle CSRF, and what must you still do yourself?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview