CSRF: requests your users never meant to send
Cross-site request forgery (CSRF, also XSRF) tricks a signed-in user's browser into sending a request to your site that the user did not intend: changing an email address, moving money, deleting data. The browser attaches the user's cookies automatically, so the site sees a valid, authenticated request. The defence is to require proof that the request came from your own pages.
Quick facts
- Also called
- XSRF, session riding, one-click attack
- Weakness
- CWE-352: cross-site request forgery
- OWASP category
- A01:2025 Broken Access Control
- Main fix
- Anti-forgery tokens and SameSite cookies
- Does a WAF stop it?
- Little. The forged request is valid and carries the user's own session
What does CSRF mean? In practice, CSRF means cross-site request forgery, and the meaning sits in the three words. Cross-site: the request starts on a page the attacker controls, not on yours. Request: it is an ordinary HTTP request with no exploit code in it. Forgery: your site cannot tell it from one the user sent on purpose.
So what is a CSRF attack? It is one forged request that changes something, sent through the browser of a user who is signed in: a new email address, a transfer, a deleted record. It is not a break-in: the attacker does not need the password and does not see the response.
How a CSRF attack works
- The victim is signed in to your site. Their browser holds a session cookie for it.
- The victim visits another page. A malicious site, a forum post or an email the attacker controls.
- That page makes the browser send a request to your site. Through a form that submits itself, an image address or a script.
- The browser attaches the session cookie. Your site sees an authenticated request and carries it out.
The attacker never sees the response; CSRF is about making the victim act, not about reading. It works only on actions that change something and can be triggered by a request the attacker can predict in full. A WebSocket connection behaves differently, because the page can read what comes back; see cross-site WebSocket hijacking. The OAuth state parameter is a CSRF defence of its own: OAuth security. A related session attack is session fixation.
A CSRF example, step by step
Imagine a site where a signed-in user can change the email address on their account by submitting a form with one field. The form has no token, and the session cookie has no SameSite attribute.
An attacker builds a page with an identical hidden form that sets the email to an address they control, and makes it submit itself when the page loads. A user who is signed in and opens that page sends the change without seeing anything. The attacker then asks for a password reset, which goes to the new address, and takes over the account. A token in the real form, unknown to the attacker, would have made the forged submission fail.
CSRF vs XSS: what the difference is
Cross-site scripting puts attacker script inside your site, where it can read pages and act as the user. CSRF works from outside: it can make the user's browser send a request, but it cannot read anything back.
The two interact. A site with an XSS flaw loses most CSRF protection, because script on the site can read the token the same way the page does. Fixing XSS is part of keeping CSRF defences meaningful.
CSRF in single-page apps and JSON APIs
An API that reads its session from a cookie is open to CSRF in the same way as a form, if it accepts requests a cross-site page can send. A form can post application/x-www-form-urlencoded or plain text with the user's cookies, so an endpoint that also accepts those types, or ignores the type altogether, can be forged.
Two habits close most of the gap: accept only application/json on state-changing endpoints, which a cross-site page cannot send without a preflight, and require a custom header or a token that only your own front end sets. Neither depends on CORS being permissive or strict, as long as it never reflects any origin with credentials.
How to prevent CSRF
What is CSRF protection? CSRF protection, or cross site request forgery protection written out in full, is any control that makes a forged request fail while the user's own request still goes through. The five measures below are the ways to build it; the tokens come first because they are the core defence.
- Use anti-forgery tokens. A secret value tied to the session (a CSRF token), sent in every form and checked on the server; the OWASP CSRF Prevention Cheat Sheet describes the patterns.
- Set SameSite on session cookies. With
SameSite=LaxorStrict, browsers stop sending the cookie on most cross-site requests; see Set-Cookie on MDN. - Never change state with GET. Actions that change data should require a method a link or image cannot trigger.
- Check Origin or Referer on sensitive actions. As an extra layer, not the only one.
- Ask again for the most sensitive actions. Re-entering a password or a second factor defeats a forged request.
When the check rejects a real user
"CSRF verification failed. Request aborted." The application writes this 403 page, not the browser, so Firefox shows it the same way any other browser does. Django, for example, shows it when the cookie is missing ("CSRF cookie not set"), the token is missing ("CSRF token missing") or the Origin check fails. The check refused a request it could not tie to one of your own pages, so make the real request carry proof: render the token into the form and make sure the browser sends the cookie the check reads. Do not switch the check off to hide the message.
"CSRF state token does not match." In an OAuth sign-in the client sends a state value and expects it back on the redirect. RFC 6749 says the client MUST implement CSRF protection for its redirection URI and SHOULD use the state parameter for a value that binds the request to the user agent's authenticated state. A mismatch means the returned value is not the one this session sent, so check where state is stored between the two requests. The idea is the same as a CSRF token in a form.
Login CSRF
CSRF is usually described against signed-in users, but the sign-in form can be forged too. The attacker's page signs the victim into the attacker's own account. Whatever the victim does next, searches, saved cards, uploaded documents, lands in an account the attacker can read later.
The defence is the same as elsewhere: an anti-forgery token on the sign-in form, tied to a session created before sign-in, and a new session issued once sign-in succeeds.
What a WAF can do about CSRF
Very little on its own. A forged request has the right method, the right parameters and the user's real cookies; nothing in its content looks like an attack. A filter can check headers such as Origin on chosen paths, but the decision about which requests need proof of intent belongs to the application.
That is why CSRF sits in broken access control: the server accepted an action without checking that the user meant it. Filters do better where the request carries syntax, as with command injection, or where it is malformed, as in HTTP request smuggling; SSRF is also a forged request, but one the server makes rather than the browser.
Questions
What is CSRF?
CSRF, cross-site request forgery, is an attack that makes a signed-in user's browser send a request to a site the user did not intend, using the user's own cookies to make it look legitimate.
How do you prevent CSRF attacks?
Use anti-forgery tokens checked on the server, set SameSite on session cookies, never change state with GET requests, and ask users to confirm the most sensitive actions.
What is the difference between CSRF and XSS?
XSS runs attacker script inside your site and can read and act. CSRF makes the victim's browser send a request from another site; it can act but cannot read the response.
Is CSRF still a risk with SameSite cookies?
Less of one, but yes. SameSite does not cover requests from same-site subdomains or older browsers, so anti-forgery tokens remain the core defence.
Why does Firefox show "CSRF verification failed. Request aborted."?
Firefox only displays it: the site's CSRF check wrote the message. The browser matters when it does not send the cookie the check reads, so try the same form in another browser, confirm cookies are accepted for the site, and confirm the form carries the token.
Why does the CSRF state token not match?
The state value that came back on the OAuth redirect differs from the one the client sent, or the client cannot find it. Check where the value is kept between the two requests and that the callback reads it from the same session.