Cross-site request forgery, or CSRF, is an attack that tricks a logged-in user browser into sending an unwanted request to a trusted site, such as a money transfer or password change. The browser automatically attaches the session cookie, so the site cannot tell the forged request from a real one. The main defense is a secret CSRF token on every state-changing request, plus SameSite cookies and origin checks.
Key takeaways
- CSRF forges requests from your logged-in browser, so the site cannot tell them from real ones.
- It targets state-changing actions such as transfers and password changes, not data theft.
- CSRF tokens are the main defense, with SameSite cookies, origin checks, and re-authentication as backup.
You are logged in to your bank, and you click a link in an email. Nothing looks wrong, but behind the scenes your browser just sent a transfer request to the bank, using your own session. That is cross-site request forgery, and it works because the bank trusts your browser.
What is CSRF?
Cross-site request forgery, or CSRF, is an attack that tricks your browser into performing an unwanted action on a site where you are already logged in. The attacker does not need your password. They only need you to visit a malicious page while your session is active.
OWASP defines it plainly: a CSRF attack forces a logged-on victim browser to send a forged HTTP request, including the victim session cookie and any other automatically included authentication information, to a vulnerable web application. The site cannot tell the forged request apart from a real one.
CSRF is also called XSRF, session riding, and sea surf. The names differ, but the trick is the same: your browser is the confused deputy, carrying out the attacker instructions with your authority.
It targets actions, not data. The attacker usually cannot see the response to the forged request, so CSRF is used to change things: transfer money, change an email address or password, make a purchase, or grant permissions.
How does a CSRF attack work?
A CSRF attack works because browsers automatically attach cookies to requests. When you are logged in to a site, your browser sends your session cookie with every request to that site, even if the request started on a different site.
Here is a classic example. You log in to your bank, which sets a session cookie in your browser. Later, while still logged in, you open a malicious page. That page contains a hidden form or image tag that points at the bank transfer address, with the attacker account as the destination.
Your browser loads the hidden request automatically and attaches your bank session cookie. The bank sees a valid session and a well-formed request, so it processes the transfer. You never clicked anything on the bank site, and you may not notice until the money is gone.
The attacker needs to know the exact request format: the URL, the parameters, and the method. That is usually easy, because many web actions use predictable addresses, and the attacker can simply create their own account on the target site and watch what their own browser sends.
GET requests are the easiest to forge, because a simple image tag or link can trigger one. That is why state-changing actions must never use GET. A link should only fetch data, never move money or change settings.
What does a CSRF attack look like in practice?
A typical CSRF payload is small and boring, which is why it is dangerous. It hides in an ordinary-looking page, email, or forum post, and it needs only one visit from a logged-in victim.
Imagine a bank transfer that uses a simple form with two fields: the destination account and the amount. An attacker page could contain a hidden form with those fields already filled in, plus a script that submits it the moment the page loads. The victim sees a normal page, or even a blank one, while the transfer runs.
Another version uses an image tag. A forum post or email contains an image whose source is the transfer address. The browser tries to load the image, sends the request with the session cookie, and the bank processes it as a transfer. No form and no click are needed at all.
Login CSRF is a special twist. Instead of acting as you, the attacker logs you in to the attacker own account on a real site. Later, when you save payment details or search history, they land in the attacker account, where the attacker can read them.
Real CSRF flaws have hit banks, email services, routers, and social networks over the years. The pattern is always the same: a state-changing request that the server accepts on the strength of a cookie alone, with no proof that you meant to send it.
What is the difference between CSRF and XSS?
CSRF and XSS are often confused because both involve a malicious site and a victim browser, but they attack from opposite directions. CSRF forges a request from your browser to a trusted site. XSS injects malicious code into a trusted site, which then runs in your browser.
With XSS, the attacker code runs on the trusted site itself, so it can read your cookies, your page content, and your keystrokes. With CSRF, the attacker never sees the response. They can only make your browser send a request they already know how to build.
That difference matters for defense. CSRF tokens stop CSRF, because the attacker cannot guess the secret token. But XSS defeats all CSRF defenses: if an attacker can run code on your site, they can read the CSRF token from the page and include it in their forged request.
OWASP puts it bluntly in its CSRF prevention cheat sheet: cross-site scripting can defeat all CSRF mitigation techniques. You must fix XSS as well, or your CSRF tokens are only a speed bump.
| CSRF | XSS | |
|---|---|---|
| Attacker goal | Make your browser act for you | Run code as you on a trusted site |
| Needs your login | Yes, you must be logged in | Yes, usually, to steal the session |
| Attacker sees response | No | Yes |
| Main defense | CSRF tokens, SameSite cookies | Output encoding, content security policy |
| Can it steal data? | Not directly | Yes, cookies and page content |
If your site takes user input and shows it back to other users, you need both defenses. Our guide to spear phishing shows how attackers get victims to visit the malicious page in the first place.
Who is at risk from CSRF?
Any web application that relies on cookies for authentication and has state-changing actions is at risk. That covers most banks, shops, email services, social networks, admin panels, and routers with web interfaces.
Ordinary users are the victims, but developers own the fix. A user cannot reliably protect themselves against a vulnerable site, because the attack needs only one visit to a malicious page while logged in. Logging out before browsing elsewhere helps, but nobody does that for every site.
Small businesses are exposed in two ways. Their own web apps and admin panels may be vulnerable, and their staff may be tricked into visiting a malicious page while logged in to a supplier, bank, or cloud console. Either path can move money or change settings.
Single-page apps and APIs are not automatically safe. If your API relies on cookies, it needs CSRF protection too. If it uses bearer tokens in an Authorization header that JavaScript must add, the browser will not attach that header to a cross-site request on its own, which removes the classic CSRF path.
How do you prevent CSRF?
The strongest defense is a CSRF token: a secret, unpredictable value that the server puts in every form and checks on every state-changing request. An attacker on another site cannot read the token, because the same-origin policy blocks them, so they cannot build a valid request.
Use the synchronizer token pattern for stateful apps. The server generates a token once per session or per request, stores it with the session, and compares the token in the request with the stored value. Reject the request if the token is missing or does not match, and log the event as a possible attack.
For stateless apps, use the double submit cookie pattern, preferably the signed version that binds the token to the session with an HMAC. The naive version, where the cookie and the form value simply match, can be bypassed by an attacker who can write cookies on your domain, for example through a vulnerable subdomain.
Check your framework first. Most modern frameworks already include CSRF protection, including Django, Rails, Laravel, ASP.NET, and Spring. Turn it on and use it correctly rather than building your own token system, which is easy to get subtly wrong.
Add defense in depth. Mark session cookies SameSite=Lax or Strict, so the browser does not send them on cross-site requests. Verify the Origin or Referer header on state-changing requests. For modern browsers, check the Sec-Fetch-Site header, which tells you whether a request came from your own site, the same site, or a cross-site page.
For highly sensitive actions, ask the user to confirm again. Requiring a password, a one-time code, or a fresh login before a transfer, a password change, or a permission grant stops CSRF even if every other layer fails, because the attacker cannot supply that confirmation.
Never use GET for state-changing operations. And never put a CSRF token in a URL, where it can leak into browser history, logs, and Referer headers. Send it as a hidden form field or a custom header instead.
What can users do to protect themselves from CSRF?
Users cannot fix a vulnerable site, but a few habits reduce the risk. Log out of sensitive sites, especially banks and email, when you finish using them. A CSRF attack needs an active session, so a logged-out browser has nothing to abuse.
Be careful with links in emails and messages. If a page asks you to log in again after you clicked a link, stop and go to the site directly from a bookmark instead. That habit also protects you against phishing, which is often how victims reach the malicious page.
Use separate browser profiles or containers for sensitive sites. Firefox containers, for example, keep cookies separate, so a malicious tab in one container cannot use the session from another.
Keep your browser updated. Modern browsers support SameSite cookies and Fetch Metadata headers, which give sites the tools to defend themselves. An old browser may not send those signals at all.
If you run a website, you can check its headers and TLS setup with our free security headers checker. It does not test for CSRF directly, but it shows whether the basic hardening that supports CSRF defense is in place.
You can also check your TLS setup with the SSL checker.
For the full defense list, read the OWASP CSRF Prevention Cheat Sheet, which is the reference this article follows.
The weakness is also catalogued as CWE-352: Cross-Site Request Forgery, which defines it as the application failing to verify that a request was intentionally provided by the user who submitted it.
How do you test a site for CSRF?
Testing for CSRF is straightforward. Log in to the application, perform a state-changing action such as changing your email address, and capture the request. Then replay that request from a different origin, without the token, and see whether the server accepts it.
OWASP Web Security Testing Guide, section WSTG-SESS-05, describes the method: determine whether it is possible to initiate requests on a user behalf that are not initiated by the user. If a replayed request without a valid token succeeds, the site is vulnerable.
Test every state-changing endpoint, not just the obvious ones. Password changes, email changes, payment details, shipping addresses, permission grants, and account deletion are all targets. Also test API endpoints that accept cookies, because they are easy to miss.
Include CSRF testing in your regular security checks, and retest after every framework upgrade or authentication change. A framework update can silently change how tokens are validated, and a new endpoint can ship without protection.
The bottom line
CSRF works because browsers attach your session cookie to every request, including forged ones from a malicious page. The fix is to prove that you meant to send each state-changing request, with a token the attacker cannot guess.
Turn on your framework built-in CSRF protection, add SameSite cookies and origin checks, and require fresh confirmation for sensitive actions. Then check your site basic hardening with the free security headers checker.
Frequently asked questions
What does CSRF stand for?
CSRF stands for cross-site request forgery. It is also called XSRF or session riding. It is an attack in which a malicious site tricks your browser into sending an unwanted request to a site where you are already logged in.
Is CSRF the same as XSS?
No. CSRF forges a request from your browser to a trusted site, while XSS injects malicious code into a trusted site that runs in your browser. XSS can steal data and read CSRF tokens, which is why it defeats CSRF defenses.
Can CSRF steal my password?
Not directly. CSRF cannot read the response to a forged request, so it is used to change things, such as transferring money or changing an email address, rather than to steal data. A related variant, login CSRF, can log you in to an attacker account instead.
Do I need CSRF protection for an API?
Yes, if the API relies on cookies for authentication. If it uses bearer tokens that JavaScript must add in an Authorization header, the browser will not attach them to a cross-site request, which removes the classic CSRF path.
What is a CSRF token?
A CSRF token is a secret, unpredictable value that the server puts in forms and checks on every state-changing request. An attacker on another site cannot read it, so they cannot build a valid forged request.
What does SameSite do for CSRF?
The SameSite cookie attribute tells the browser not to send a cookie on cross-site requests. Marking session cookies SameSite=Lax or Strict stops many CSRF attacks, but it is a defense in depth layer, not a replacement for tokens.
Can a user protect themselves from CSRF?
Only partly. Logging out of sensitive sites after use and being careful with links in emails reduces the risk. The real fix belongs to the site developer, who must add CSRF tokens and related defenses.
Why should state-changing actions never use GET?
GET requests are easy to forge with a simple link or image tag, and browsers, crawlers, and caches may trigger them without the user meaning to act. State-changing actions should use POST or another method, protected by a CSRF token.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against the OWASP CSRF Prevention Cheat Sheet, the OWASP testing guide, and CWE-352.
How we write and fact-check →