A trick that makes your browser send a request you never meant to send, and how to stop it.
CSRF (Cross-Site Request Forgery) is a trick that makes a logged-in user’s browser send a request they never meant to send, to a site that trusts them.
Think about how a cookie works. Once you log in to bank.example, the browser stores a session cookie. After that, every request to bank.example carries that cookie. The browser does this on its own. It does not ask where the request came from.
That is the weakness. If you visit evil.example while still logged in to the bank, the evil page can quietly send a request to the bank (a form that submits itself, or an image or link). The browser attaches your bank cookie, and the bank thinks you asked for it.
| Attack | What happens | The key difference |
|---|---|---|
| CSRF | The attacker makes your browser send a request | The attacker cannot read the answer. It abuses the browser’s automatic cookies. |
| XSS | The attacker runs script inside the trusted page | The attacker can read pages and tokens. XSS can also beat CSRF defences. |
| CORS misconfiguration | A site is allowed to read another site’s responses | CORS is a read-permission rule. It does not, on its own, stop a forged write. |
GET request change data.| Defence | How it works | Good and bad |
|---|---|---|
| SameSite cookies | The cookie is not sent on cross-site requests (Strict) or only on top-level GET navigations (Lax) | Easy and strong. Lax is the default in many modern browsers. Does not separate sibling subdomains, and old browsers differ. |
| Synchronizer token | The server puts a random token in each form and in the session, and checks they match | Classic and strong. Needs server-side state per session. |
| Double-submit cookie | A random value is sent both in a cookie and in a header or form field. They must match | Needs no server state. Weaker if an attacker can set cookies on your domain. Signed values help. |
| Origin / Referer check | Reject a state-changing request whose Origin is not your own site | Cheap extra layer. Some requests have no Origin, so do not rely on it alone. |
| Custom header | Require a header such as X-Requested-With on API calls | A cross-site form cannot add it, and a cross-site script call triggers a CORS preflight. |
| Re-authentication | Ask for the password or a code for risky actions | Strong for sensitive steps. Costs user effort. |
Set-Cookie: session=abc; Secure; HttpOnly; SameSite=Lax Set-Cookie: session=abc; Secure; HttpOnly; SameSite=Strict Set-Cookie: session=abc; Secure; HttpOnly; SameSite=None (only with Secure; sent on cross-site requests)
A simplified model of the classic attack: the forged request comes from a form or link on another site. Browsers differ in details, and a site on the same registrable domain counts as “same-site”, so SameSite does not separate sibling subdomains. Use several defences together.
| Term | Meaning in easy words |
|---|---|
| CSRF | Cross-Site Request Forgery: tricking a browser into sending a request the user did not intend |
| Session cookie | A cookie that proves a user is logged in. The browser attaches it automatically |
| SameSite | A cookie setting that limits sending the cookie on cross-site requests (Strict, Lax, None) |
| CSRF token | A random secret that must come back with each state-changing request |
| Synchronizer token | A token stored on the server per session and compared on each request |
| Double-submit cookie | The same random value is sent in a cookie and in a header or field. They must match |
| Origin header | A header that tells the server which site started the request |
| Same-origin policy | Browser rule that stops a page from reading data from another origin |
| Preflight | An OPTIONS request the browser sends first for some cross-site requests |
| Safe method | A method that must not change data (GET, HEAD, OPTIONS) |
| Login CSRF | Forcing a user to log in as the attacker’s account. The OAuth state parameter helps stop it |
| Choice | We gain | We lose |
|---|---|---|
| SameSite=Strict | Strongest cookie protection | Users arriving from a link may look logged out until they click again |
| SameSite=Lax | Good protection with normal links working | GET requests are still sent cross-site, so GET must stay safe |
| Synchronizer token | Strong, simple to reason about | Per-session server state, and care with caching and multiple tabs |
| Double-submit cookie | No server state | Weaker against attackers who can write cookies (use signed values) |
| Authorization header | Immune to classic CSRF | The app must store the token in JavaScript, which raises XSS exposure |