CSRF Attacks and SameSite Cookies Explained
On this page
What is CSRF? A forged request from your own browser
You're logged in to your bank in one tab. In another, you open a page with a harmless-looking article. Hidden in it is a form that posts to the bank. Your browser attaches your bank cookie, as it always has, and the bank sees a valid, authenticated request to transfer money. That's cross-site request forgery (CSRF).
<!-- On the attacker's page; the victim only has to visit it while logged in -->
<form action="https://bank.example.com/transfer" method="POST" id="f">
<input type="hidden" name="to" value="attacker-account">
<input type="hidden" name="amount" value="1000">
</form>
<script>document.getElementById('f').submit();</script>The attacker never sees the cookie or the response. They don't need to: the damage is the request itself.
What makes a site vulnerable to CSRF
- Authentication is automatic: cookies (or HTTP basic auth, or client certificates) the browser sends by itself.
- The action can be triggered with a request the attacker can construct: a form post, an image tag for a GET, or a simple
fetch. - Nothing in the request proves it came from your own pages.
APIs authenticated only with an Authorization: Bearer header that your JavaScript adds aren't vulnerable to classic CSRF: another site can't make the browser add that header.
SameSite cookies: Strict vs Lax vs None
The SameSite cookie attribute tells the browser when to send a cookie on requests initiated by other sites:
| Value | Cookie sent on cross-site requests? | Effect |
|---|---|---|
| Strict | Never | Strongest; but following a link from email to your site arrives logged out |
| Lax | Only on top-level navigations with safe methods (clicking a link, a GET) | Blocks cross-site form POSTs, fetches and iframes; the usual choice |
| None | Always (requires Secure) | For deliberate cross-site use, such as embedded widgets; no CSRF protection |
Chrome and Edge treat cookies without a SameSite attribute as Lax; set it yourself anyway, since other browsers differ. With SameSite=Lax, the hidden form above arrives without the bank cookie, so the request is anonymous and fails.
Why SameSite alone isn't enough
- GET requests that change state are still sent on top-level navigation under
Lax. Never change anything with GET. - "Same site" isn't "same origin":
evil.example.comis the same site asapp.example.com. A compromised or user-controlled subdomain can still forge requests. - Older browsers and embedded webviews may not enforce it.
A complete CSRF defense: tokens and Origin checks
SameSite=Lax(orStrict) on session cookies, plusSecureandHttpOnly(Day 4).- CSRF tokens for cookie-authenticated, state-changing requests: a random value tied to the session, included in forms or a custom header, and checked by the server. The attacker's page can't read it. Most frameworks provide this (Django, Rails, Spring Security, ASP.NET antiforgery,
csrf-csrffor Express). - Check
Origin(orSec-Fetch-Site) on state-changing requests and reject other origins. Modern browsers send these headers on cross-site requests. In Express:
app.use((req, res, next) => {
if (['GET', 'HEAD', 'OPTIONS'].includes(req.method)) return next();
const site = req.get('Sec-Fetch-Site');
if (site && site !== 'same-origin' && site !== 'none')
return res.status(403).json({ title: 'Cross-site request blocked' });
next();
});POST /transfer Sec-Fetch-Site: same-origin 200
POST /transfer Sec-Fetch-Site: cross-site 403
POST /transfer Sec-Fetch-Site: same-site 403
POST /transfer (header absent) 200none means the user typed the URL or used a bookmark. Requests without the header (older browsers, server-to-server calls) are let through here and must be covered by tokens or SameSite; reject same-site too unless you trust every subdomain.
- No state changes on GET, and re-authentication for very sensitive actions (changing email, wiring money).
Check yourself
The same-origin policy stops the attacker's page from reading responses from your site. The attack works only by causing a state-changing request, like a transfer or email change, that the browser sends with the victim's cookies.
No. Lax still sends cookies on top-level GET navigations, so an attacker's link or redirect to the delete URL works. State-changing actions must never use GET.
The browser doesn't attach those tokens automatically; only your own JavaScript adds them, and other sites can't read or add them. CSRF relies on credentials the browser sends by itself, like cookies.
The server issues a random token tied to the session, the page includes it in forms or headers, and the server rejects state-changing requests without a matching token. Attacker pages can't read it.
Browsers send the origin of cross-site requests, so the server can reject state-changing requests from other origins as an extra layer.