CSRF Explained: How It Works and How to Stop It
Cross-site request forgery makes a browser send an authenticated request. Learn the attack, the token defence, and SameSite cookies.
By Velloreth
The attack
A CSRF attack makes the victim’s browser send an authenticated request.
The setup:
- The victim logs in to
bank.example.com, and the browser stores a session cookie. - The victim visits
evil.example, which contains a hidden form. - The form submits to
bank.example.com/transfer. - The browser sends the session cookie automatically, because the request is to
bank.example.com. - The bank sees an authenticated request and performs the transfer.
The attacker never sees the response. The attack does not need to read anything; it only needs the request to be sent.
This is why CSRF is different from cross-site scripting. XSS runs code in the target origin; CSRF makes the browser send a request from another origin.
Why it works
The browser sends the cookie automatically, based on the destination, not on which site made the request.
The Origin and Referer headers are sent, but a server that does not check them cannot tell the difference.
The attack needs no JavaScript. A plain HTML form, an image tag, or a link is enough.
<!-- A form that submits automatically -->
<form action="https://bank.example.com/transfer" method="POST" id="f">
<input name="to" value="attacker">
<input name="amount" value="1000">
</form>
<script>document.getElementById('f').submit();</script>
Even without the script, a user who clicks a button submits the form.
The GET problem
A GET request can be triggered by an image tag. The attacker embeds a URL such as /transfer?to=attacker&amount=1000 as the src of an image on their own page. The browser loads the image, which sends the request to the bank with the cookie attached, and the attacker never needs to read the response.
The browser loads the image, which sends the request with the cookie.
The rule that follows: a GET must never change state. A state change is a POST, PUT, PATCH, or DELETE.
This is a REST principle and a security one. A crawler, a prefetch, or a browser extension can trigger a GET, so a GET that changes state is a bug even without an attacker.
Defence 1: SameSite cookies
Set-Cookie: session=abc; HttpOnly; Secure; SameSite=Lax
SameSite=Lax sends the cookie on a top-level navigation but not on a cross-site POST or an embedded request.
This blocks the classic attack, because the forged request is a cross-site POST and the cookie is not sent.
SameSite=Strict is stronger and breaks a legitimate inbound link, because the user arrives without the cookie and appears logged out.
Lax is the right default for a session cookie, and it is now the browser default when the attribute is omitted.
The limitation: Lax still sends the cookie on a top-level GET navigation, so a GET that changes state remains vulnerable. That is another reason to keep a GET safe.
Defence 2: A synchroniser token
The server includes a random token in the form, and checks it on submission.
<form method="POST" action="/transfer">
<input type="hidden" name="csrf_token" value="a8f3...">
...
</form>
The server compares the submitted token with the one in the session.
Why it works: an attacker on another origin cannot read the token, because the same-origin policy prevents reading the response. The attacker can send a request, but cannot include the correct token.
The token must be unpredictable, tied to the session, and checked on every state-changing request.
A double-submit cookie is a variant: the token is also set as a cookie, and the server checks that the form value matches the cookie. It works without server-side state, and it is slightly weaker.
Defence 3: Check the Origin
Origin: https://app.example.com
The browser sends Origin on a cross-origin request, and it cannot be forged by a script.
The server rejects a request whose Origin is not in an allow-list.
This is a good secondary check, and it is simple. It fails for a request with no Origin header, which is why it is a complement to a token rather than a replacement.
Defence 4: A custom header
A request with a custom header triggers a CORS preflight.
fetch('/api/transfer', {
method: 'POST',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
body: JSON.stringify(data),
});
A cross-origin form cannot set a custom header, so a request that requires one cannot be forged by a form.
This works because of the preflight, which the attacker’s origin cannot pass without the server’s permission.
It is a reasonable defence for a JSON API, and it is weaker than a token because it depends on the CORS configuration.
What does not work
Checking the Referer alone. It can be absent, and a privacy tool may strip it.
A secret URL. It leaks through a log, a referrer, or a browser history.
A CAPTCHA on every request. It is a usability cost, and it does not address the root cause.
Relying on SameSite alone for a state-changing GET. The cookie is still sent on a top-level navigation.
Practical guidance
- Never change state on a
GET. - Set
SameSite=Laxon the session cookie. - Use a synchroniser token for a state-changing request.
- Check the
Originheader as a secondary defence. - Require a custom header for a JSON API.
- Tie the token to the session.
- Check the token on every state-changing request.
- Do not rely on the
Refereralone. - Remember an API token is not vulnerable in the classic form.
For the related guides, see cookies-explained and cors-explained. For the broader context, content-security-policy-guide and api-authentication-methods.
Quotable summary
CSRF makes a victim’s browser send an authenticated request to a site where they are logged in, and the attack works because the browser sends the session cookie automatically regardless of which site made the request. A synchroniser token is the classic defence, because an attacker on another origin cannot read the token due to the same-origin policy, and SameSite=Lax blocks the classic cross-site POST as the modern baseline. A GET request must never change state, because a GET can be triggered by an image tag, a link, or a prefetch, so a state change belongs in a POST, PUT, PATCH, or DELETE. An API token is not vulnerable in the classic form, because the browser does not attach an Authorization header automatically, which makes a cookie-based session the vulnerable case.