Application security CSRF 4 min read
What is CSRF?
You’re signed in to your bank in one tab. In another, you open a forum post. Can that forum page make your bank move money, without ever knowing your password?
-
Your browser brings the cookie along
The forum post contains a hidden form that points at your bank’s transfer page, and it submits itself the moment the page loads.
Your browser sends it. Browsers attach a site’s cookies to every request to that site, whoever started it. The bank sees your valid session and has no way to tell you didn’t mean it.
Your browser, already signed in to the bank, loads a forum post with a hidden form. The form submits a transfer to the bank, and the browser attaches the bank cookie automatically. The bank sees a valid session and treats the request as yours.
You never clicked anything on the bank’s site. In short CSRF doesn’t steal your session. It borrows the one your browser already has.
-
What actually stops it
SameSite=LaxorStricton the session cookie tells the browser to leave it off requests other sites start. The forged transfer arrives with no session at all.Two more checks help on the server. A CSRF token is a secret in each of the bank’s own forms that other sites can’t read, so a forged form can’t include it. And the
Originheader says which site sent the request, so the bank can refuse state changes from anyone else.The same forged form submits a transfer, but the session cookie is marked SameSite, so the browser leaves it off. The bank sees no session and refuses the request.
Same forged form, with SameSite set on the cookie. In short Make sure a request that changes something really came from your own pages.
-
What doesn’t help
HTTPS doesn’t help: the forged request travels over HTTPS too, cookie and all. A strong password doesn’t either, because you were already signed in and the password never comes into it.
Switching transfers from GET to POST isn’t enough on its own. A hidden form can POST just as easily.
In short The browser is genuinely signed in, so anything that only proves that won’t stop CSRF.
Where CSRF doesn’t reach
CSRF relies on the browser attaching credentials by itself. An API where the app’s own JavaScript adds Authorization: Bearer … isn’t open to classic CSRF, because other sites can’t make the browser add that header.
And CSRF defences don’t stop XSS. A script running inside your own pages can read the CSRF token and send requests from your origin, so every check passes.
Keep this
Cookies ride along on any request. Make sure state changes really came from your own pages.
SameSite cookies, CSRF tokens and Origin checks stop it. HTTPS and strong passwords don’t, because the browser is genuinely signed in.- Cause
- The browser attaches cookies automatically.
- Effect
- Another site can trigger actions as you.
- Defences
- SameSite cookies, CSRF tokens, Origin checks.
- Not affected
- APIs that use tokens in an Authorization header.
Questions worth carrying
Do modern browsers already stop CSRF?
Many now treat cookies without a SameSite setting as Lax, which blocks a lot. Set SameSite explicitly and keep tokens or Origin checks; don’t rely on defaults.
How is CSRF different from XSS?
XSS runs the attacker’s script inside your site. CSRF never touches your site’s pages; it just makes the browser send a request your site trusts.