Cross-site request forgery: when the browser’s cookie is the only proof
How another site can make a signed-in browser send a state-changing request, how SameSite cookies changed the picture, where CSRF still appears, and how to verify tokens and Origin checks in a review.
Diagram
A forged state change
Step 1 of 4
Signed in
The user has a session cookie for bank.example.test.
# Signed in # The user has a session cookie for bank.example.test.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
SameSite helps, not always
- What fails
- Browsers default many cookies to SameSite=Lax, which blocks cross-site POSTs but still allows top-level GET navigations.
- Who is affected
- Users of apps that change state on GET or that set SameSite=None.
- What you would notice
- A GET /account/delete route, or session cookies explicitly SameSite=None.
- What you change
- Never change state on GET, and keep SameSite=Lax or Strict on session cookies.
Tokens prove intent
- What fails
- A per-session token in the form proves the request came from your page.
- Who is affected
- Users of any route that skipped the middleware.
- What you would notice
- A state-changing endpoint that succeeds without the csrf field.
- What you change
- Apply CSRF middleware globally and list exemptions explicitly.
JSON APIs are not exempt
- What fails
- If an API accepts form-encoded or text/plain bodies with cookie auth, a cross-site form can reach it.
- Who is affected
- API users authenticated by cookie.
- What you would notice
- An endpoint accepting both JSON and form bodies.
- What you change
- Require application/json and check Origin, or use header-based tokens.
Review walkthrough
List every route that changes state. For each, check that it rejects GET, that the CSRF middleware covers it or it checks Origin, and that session cookies carry SameSite. Replay a captured request in your proxy with the token removed and with the Origin changed; a correctly protected route returns 403 both times.
POST /settings/email (token removed) -> 403 ok POST /settings/email (Origin: https://other.example) -> 403 ok POST /settings/notify (token removed) -> 200 finding: middleware exemption
Where it still shows up
Login CSRF signs the victim into the attacker’s account. Subdomains under the same site count as same-site for SameSite purposes, so a compromised sibling subdomain can bypass it. Legacy browsers and embedded webviews may not apply SameSite defaults. The CSRF on JSON APIs and login CSRF pages cover two of those cases.
Writing the finding
Name the route, the state it changes, the cookie flags, and which check is missing. A proof-of-concept form pointed at a test account is enough.
Check yourself
Recognition and the fix. Nothing here is a procedure.
A route deletes an account on GET /account/delete. Why is SameSite=Lax not enough?
Go deeper
Specific techniques, exercises, and glossary cards for this topic.
- 01CSRF on JSON APIsA JSON API is not immune to cross-site request forgery if the browser will attach the session cookie and the server changes state without a token. Includes the diagram and a defensive fix.
- 02Login CSRFA login is a state change. Includes the diagram and a defensive fix.
Exercises
- 01Evidence deskRead a query review and a server-side fetch that were already collected. Name the failure and the fix.
- 02CSRF token flowFollow a CSRF token from form to server check, then pick SameSite and server checks until four forged requests fail and your own three still work.