CSRF

Your browser sends cookies, even from evil.com

L215 MINSECURITY
PREDICT

The bank checks the session cookie on every POST. evil.example cannot read bank.example's cookies. Can a page on evil.example still make a transfer from your account?

Your browser keeps a cookie jar per site. When any page sends a request to bank.example, the browser attaches bank.example’s cookies, even if the page is evil.example. The bank sees a valid session and runs the transfer. The cookie proves which browser sent it, not that the user meant to.

Two fixes. A CSRF token: the bank puts a per-session secret in its own forms and rejects any POST without it. The attacker cannot read bank.example’s pages, so it cannot copy the token. A SameSite=Lax cookie: the browser stops attaching the cookie to cross-site POSTs. SameSite is keyed on the site (the registrable domain), not the origin, so it trusts every subdomain of bank.example. Keep the token (or an Origin check) as the main defense and SameSite as a second layer.

▶ SIMULATION
VICTIM BROWSER
active tab: —
cookie for bank.example: (none)
balance $5000
BANK.EXAMPLE
defense: none
session token: —
transfers executed: 0 · not intended: 0
ATTACKER
received $0
knows the CSRF token: no
last—
✓ every executed transfer was one the user meant to send
SCHEDULE
01[BANK]bank: cookie SameSite=None, no CSRF token
02[VICTIM]victim logs in to bank.example; the bank sets a session cookie
03[VICTIM]victim opens bank.example/transfer; the bank renders the form
04[VICTIM]victim clicks SEND: POST /transfer to=friend amount=100 (from bank.example)
05[VICTIM]victim opens evil.example in another tab
06[EVIL]evil.example auto-submits a hidden form: POST bank.example/transfer to=attacker amount=1000
07[BANK]report
Start of no defense scenario. 7 steps.
<!-- evil.example -->
<body onload="document.forms[0].submit()">
  <form action="https://bank.example/transfer" method="POST">
    <input type="hidden" name="to" value="attacker">
    <input type="hidden" name="amount" value="1000">
  </form>
</body>

// bank.example: a valid cookie is treated as consent
app.post('/transfer', requireSession, (req, res) => {
  transfer(req.session.user, req.body.to, req.body.amount);
});

Watch the request log and the invariant strip. The forged POST arrives in all three attack scenarios. With no defense it moves $1000. With the token it gets 403 (token missing). With SameSite=Lax the cookie never arrives, so it gets 401.

BREAK IT NOT YET

The bank has no CSRF defense, and on this day nothing bad happens. Inject the attacker's page load at a point where it moves money the victim never sent.

GOAL: a transfer the victim did not intend is executed

FIX IT NOT YET

The bank calls checkCsrf(req, session) on every POST and returns 403 when it is false. Allow a request only if it carries the per-session token that the bank put in its own form. The grader replays legitimate and forged requests.

Mental model:

A cookie is ambient authority: the browser attaches it to every request for that site, so it proves which browser sent a request and nothing about who meant it. A state change needs proof of intent: a secret only your own page could have read (the token), or a browser that refuses to attach the cookie to requests other sites start (SameSite, a second layer, since it trusts your own subdomains).

IN PRODUCTION

Every form or JSON endpoint that changes state and authenticates by cookie: transfers, password and email changes, admin actions. Framework middleware (Django, Rails, Spring Security, csurf) adds the token; Chrome treats a cookie with no SameSite attribute as Lax, with a short exception for top-level POSTs right after the cookie is set, and not every browser does this, so set SameSite explicitly. APIs that authenticate with an Authorization header are not exposed, because the browser does not attach that header for you.

IN INTERVIEWS

“What is CSRF and how do you prevent it?” Say the cookie is attached automatically, so a valid session is not proof of intent. Name the synchronizer token and SameSite, say why the attacker cannot get the token (same-origin policy), and add that GET must never change state, because Lax still sends cookies on top-level GET navigations.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: OAUTH 2.0 →