What is CSRF — and why your browser attaches your session for free

Here's what makes CSRF confusing: nobody stole your password. Nobody guessed a token. The session is genuine — yours.
It's just that the request to your site was sent by another site, and the cookie was attached by your own browser. Voluntarily. That's how it's built.
What it looks like in real life
You're logged into your own app — the admin panel is open in a neighbouring tab. Then you open some other page that contains this image:
<img src="https://your-site.com/api/delete-account" width="1" height="1">
The browser sees <img> and goes to fetch the image. It goes to your domain. And since the domain is yours, it attaches your session cookie, exactly as it would for any request there.
The server receives a request with a valid session. And does what it's asked.
OWASP describes the essence like this: if a user is authenticated to the site, an unprotected site "cannot distinguish between legitimate authorized requests and forged authenticated requests".
Which gives you rule number one right away: GET must not change anything. Deleting, paying, changing an email — POST/PUT/DELETE only. That's not API aesthetics, that's defence.
Why the cookie shows up on its own
A cookie is bound to a domain, not to a tab and not to whoever initiated the request. That's exactly what makes it convenient (the site remembers you) and vulnerable at the same time.
And here's an important distinction. If you keep the session token not in a cookie but in page memory, and put it into an Authorization header by hand, another site can't set that header. Classic CSRF simply doesn't work. That's one of the rare wins of the JWT-in-a-header approach — one you pay for with other complications.
SameSite: what it covers and what it doesn't
Modern browsers added the SameSite cookie attribute, and it genuinely helps.
Strict— the cookie only goes out with requests from your own site. Maximally strict: someone arriving from a link in a messenger will find themselves logged out.Lax— the cookie goes out from your site, and from outside only if it's a top-level navigation and a safe method. POST, PUT and DELETE from outside get no cookie.None— always sent, but requires theSecureflag.
Most browsers today apply Lax by default if you specify nothing. Sounds like the problem solved itself. But there are two things worth knowing.
First — a two-minute window. MDN spells it out: when Lax is applied as a default, a more permissive version is used, and cookies are also included in POST requests — as long as they were set no more than two minutes before. So right after login the door is slightly open. Set SameSite=Lax explicitly and the exception goes away.
Second — subdomains. SameSite looks at the registrable domain, not the full origin. A vulnerable blog.your-site.com counts as "same site" for app.your-site.com.
Hence OWASP's careful conclusion: SameSite is "useful as a defense-in-depth control but it does not replace a proper CSRF defense in most deployments".
A token another site can't guess
The classic fix is the synchronizer token. It works like this:
- The server generates a random string and remembers it for the session.
- It hands it back in a hidden form field, or in a header for fetch.
- On any state-changing request it compares what arrived with what it stored.
Another site can make the browser send a request. But it can't read your page and pull the token out — the same-origin policy stops it. No token, no request.
OWASP's requirements for the token are simple: unique per session, secret, unpredictable — and delivered not through a cookie.
If you'd rather not keep state, there's the double submit cookie approach: the same value goes into both a cookie and a form field, and the server compares them. OWASP explicitly warns against the naive version — it's vulnerable if an attacker can inject a cookie from a subdomain; use the HMAC-signed variant.
The good news: nearly every framework already has this. Django, Rails, Laravel, Next.js Server Actions — on by default or one line away. Trouble starts where an AI agent wrote you a "simple manual endpoint" that bypasses the framework. Look through the pre-launch security checklist before you open the doors to real people.
Do I need a CSRF token if my app has no login?
No. With no logged-in users there's nothing to forge — there's no session for the browser to attach on someone's behalf. The moment you add login, the question becomes real.
Is SameSite=Strict enough instead of a token?
Sometimes — but the conditions pile up: no shared subdomains, not a single state-changing GET endpoint, and origin checking on top. For an ordinary app it's easier to switch on your framework's protection and keep SameSite as the second layer.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





