5. CSRF & synchronizer token

8 cards

Review this deck
All cards in this deck

CSRF

The attacker tricks the victim's browser into sending a request with valid cookies to the target site without the victim intending to.

Why SameSite alone does not stop CSRF

Old browsers do not support it, Lax still allows top-level GET, and a compromised subdomain is still same-site.

Synchronizer token pattern

The server generates a token stored in the session and hands it to the client; the client sends it back in the `X-XSRF-TOKEN` header; the server compares it with the token in the session.

XsrfTokenMiddleware

Middleware applied to every `/api/s/*`: reads `sid` → fetches xsrfToken from Redis → compares with the `X-XSRF-TOKEN` header; wrong or missing → 401.

Why a header instead of the body for the XSRF token

A custom header cannot be sent from a cross-site HTML form, so the attacker would need to run same-origin JS (i.e. already have XSS).

Attacker has `uid`

Can do nothing because the BFF does not use `uid` for authentication; they only learn that a sub exists.

Attacker has `sid` but not `xsrf`

Calls to `/api/s/*` get 401 for missing XSRF with every method (the middleware also applies to GET); the only intentional exception is `GET /auth/web/logout` (top-level navigation, it can only log the user out).

Attacker has both `sid` + `xsrf`

Full session takeover; this shows every cookie must be Secure and `sid` must be HttpOnly so it cannot leak via XSS.

5. CSRF & synchronizer token – Flashcards – Login Flow | Học Dev