Dev.to Security πŸ” Cybersecurity πŸ‘ 0 πŸ“– 2 min read

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=12 or /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=None let 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

  1. In DevTools, open Application > Cookies. Your session cookie should show HttpOnly, Secure and SameSite=Lax or Strict.
  2. List every route that changes data. None of them should use GET.
  3. Look at one POST request in the Network tab. Is there a CSRF token field or header, or does the server check the Origin header against your own domain?
  4. 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=Lax at minimum. It blocks most cross-site POSTs.
  • Use a framework CSRF token for form posts, or check that Origin (or Sec-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-With or 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.

πŸ“° Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.