CSRF in AI-built apps: when your session cookie lets other sites act as your users
CSRF (cross-site request forgery) is when another website makes the user's browser send a request to your app, and your app accepts it because the browser attached the login cookie automatically. AI-generated apps often
CSRF (cross-site request forgery) is when another website makes the user's browser send a request to your app, and your app accepts it because the browser attached the login cookie automatically. AI-generated apps often skip the defence because the demo works without it.
When it matters
CSRF only applies when authentication rides on cookies that the browser sends by itself. If your API reads a token from an Authorization header that your own JavaScript sets, a random site cannot forge that request. If your app uses a session cookie, it can.
The three ways it goes wrong
-
State changes on GET. A link like
/delete?id=12or/api/transfer?to=...that acts when loaded. An<img src>on another site can trigger it. -
Cookies with no SameSite. Older defaults or an explicit
SameSite=Nonelet the cookie travel on cross-site requests. - POST routes with no token and no origin check. Forms and JSON endpoints that accept any request that carries a valid cookie.
How to check your own app
- In DevTools, open Application > Cookies. Your session cookie should show
HttpOnly,SecureandSameSite=LaxorStrict. - List every route that changes data. None of them should use GET.
- Look at one POST request in the Network tab. Is there a CSRF token field or header, or does the server check the
Originheader against your own domain? - Try the request from a different origin (a local HTML file with a form pointing at your endpoint while you are logged in on a test account). It should be rejected.
The fix
- Set the session cookie to
SameSite=Laxat minimum. It blocks most cross-site POSTs. - Use a framework CSRF token for form posts, or check that
Origin(orSec-Fetch-Site) matches your domain on every state-changing request. - Never change data on GET.
- For JSON APIs, require a custom header such as
X-Requested-Withor use bearer tokens in a header instead of cookies.
What an outside scan can and cannot tell you
A public scan can read your Set-Cookie flags and see missing security headers. It cannot log in, so it cannot prove that your POST routes reject forged requests. Treat the cookie check as a floor, not a pass.
Check your own app from the outside, free. Run the free ProofForge scan on your live URL. No account needed for the first scan. It looks at your public site the way a stranger would and says pass, fail or inconclusive, so you know what is confirmed and what it could not see. One-time reports start at $9: see pricing. Building with an AI agent? There is an MCP server so it can check before it deploys.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.