What Is a CSRF Attack? How to Stop It in Your App
CSRF makes your own browser send a request to a site you are logged into. Here is how it works, which defenses hold up, and which popular fixes do not.
A CSRF attack, short for cross-site request forgery, tricks a logged-in user's browser into sending a request to a site the user trusts, and the site cannot tell the forged request from a real one. The attacker cannot read your account. They can only trigger actions you are already allowed to take, such as changing an email address or transferring credit.
How a CSRF attack works
Browsers attach cookies to requests automatically, including your session cookie. OWASP puts it plainly: browser requests automatically include all cookies, including session cookies. So if you are logged into an app and then open a hostile page, that page can make your browser submit a form to the app, and the cookie goes along for the ride.
A forged request can be as small as this:
<form action="https://app.example.com/account/email" method="POST">
<input name="email" value="attacker@example.net">
</form>
<script>document.forms[0].submit()</script>If the app only checks the session cookie, it sees a valid logged-in request and changes the email. The attacker then uses "forgot password" to take the account.
The defenses that hold up
The OWASP prevention cheat sheet lists these main controls:
Defense | What it does |
|---|---|
Synchronizer token | The server issues a secret per session or request and rejects state-changing requests without it |
Custom request header | For API calls, require a header such as X-CSRF-Token, which browsers will not send cross-origin without a preflight |
Origin or Referer check | Compare the header with your own origin and reject a mismatch |
Fetch Metadata | Block cross-site unsafe requests using the Sec-Fetch-Site header, with a fallback for browsers that lack it |
SameSite cookies | Limit when the browser sends the cookie cross-site |
OWASP calls SameSite a useful defense-in-depth control, not a replacement for a proper CSRF defense. Use it as one layer.
Three fixes that do not work
**A CAPTCHA.** OWASP says not to use one as a CSRF defense, because it is built to stop bots, not to authorise a request.
**A naive double-submit cookie.** The guide discourages it, since an attacker who can write cookies on your domain can bypass it. A signed, session-bound variant is the recommended form.
**Treating a missing Origin header as safe.** Missing headers and an Origin of null are not evidence of a same-origin request.
Two more traps: any GET endpoint that changes data escapes SameSite=Lax, so keep GET, HEAD and OPTIONS free of side effects. And cross-site scripting defeats every CSRF mitigation, which is why cross-site scripting in AI-generated code matters here too.
What to ask your AI coding agent for
If your app uses cookie-based sessions, put these in the prompt:
Require a CSRF token on every POST, PUT, PATCH and DELETE route.
Set the session cookie with SameSite and HttpOnly.
Verify Origin on state-changing requests.
Never change data from a GET route.
Then test it yourself. Log in, copy a form submission, and replay it from a page on a different origin or with the token removed. It should fail. An app that authenticates with an Authorization header rather than a cookie is not exposed to the cookie-riding trick in the same way, but check how your framework actually sends credentials rather than assuming.
Defenses stack. Add rate limiting on sensitive routes and pair CSRF protection with two-factor authentication for sensitive actions, and see what a CVE is for how these weaknesses get tracked. The wider map is in the full picture of AI risks.
FAQ
What does CSRF stand for?
Cross-site request forgery. An attacker causes a victim's browser to send an unwanted request to a site where the victim is logged in.
Is SameSite enough to stop CSRF?
Not by itself. OWASP treats it as defense in depth. It does not cover same-origin requests, client-side CSRF, or sibling subdomains that count as the same site.
Do APIs need CSRF protection?
If authentication rides on cookies, yes. If every request carries a token in a header that the browser does not attach on its own, the classic attack does not apply.
Can AI-generated code be vulnerable to CSRF?
Any code can be. Generated code often covers the happy path first, so ask for the protections above and test them.
How did this land?
About the author

Staff Engineer, Platform
Carlo works on the platform that turns prompts into running apps. He writes the engineering deep dives and the changelog notes worth reading.


