CSRF
Your browser sends cookies, even from evil.com
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.
<!-- 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);
});// 1. Per-session token in every form, checked on every state change
<input type="hidden" name="csrf_token" value="<%= session.csrfToken %>">
app.post('/transfer', requireSession, (req, res) => {
if (!checkCsrf(req, req.session)) return res.sendStatus(403);
transfer(req.session.user, req.body.to, req.body.amount);
});
// 2. And/or: the browser withholds the cookie on cross-site POSTs
Set-Cookie: session=...; Secure; HttpOnly; SameSite=Lax
// Lax still sends the cookie on cross-site top-level GETs:
// never change state on GET. 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.
- A security engineer's weekSecurity
- Authentication and authorisation for a multi-tenant SaaS and its public APISecurity
- Secure code review and AppSecSecurity
- The map of security roles: AppSec, cloud, SecOps, GRC, red teamSecurity
- Threat modellingSecurity
- Triaging researcher reports: running a disclosure programme day to daySecurity