Home / patterns

Cookie-based login with no CSRF protection on state-changing requests

A laptop on a dark desk at night showing an account settings form, with a code editor on a monitor behind it and a coffee mug beside it
Quick answer: If your app logs users in with a cookie, the browser sends that cookie with every request to your site, including ones triggered by someone else's page. Without a CSRF defense, a malicious site can make a logged-in user's browser change their email, delete data or make a purchase. AI-built apps often miss this on custom API routes, especially ones that change data on a GET request.

Last updated November 11, 2026

Steps

  1. List every route that changes something: updates, deletes, payments, settings, invites. Make sure none of them run on GET. Move them to POST, PUT, PATCH or DELETE.
  2. Use your framework's built-in protection first. Next.js Server Actions only accept POST and compare the Origin header to the Host (or X-Forwarded-Host) header; if you sit behind a proxy, set serverActions.allowedOrigins instead of turning the check off.
  3. For custom API routes (Route Handlers, Express, etc.) that use cookie sessions, add a check: reject state-changing requests where Sec-Fetch-Site is cross-site, and when that header is missing, require a matching Origin header or a valid CSRF token.
  4. If you use tokens, use a framework-supported synchronizer token or a signed double-submit cookie tied to the session. Never put the token in the URL or logs.
  5. Set session cookies with SameSite=Lax or Strict, Secure and HttpOnly, and prefer the __Host- prefix. Treat this as extra protection, not the only one.
  6. Test it: from a page on a different origin, submit a form that POSTs to one of your routes while logged in. The request should be rejected.
  7. Fix any XSS first, too. OWASP warns that cross-site scripting can defeat every CSRF defense.

Why it matters

OWASP says a CSRF attack tricks an authenticated user's browser into performing an unwanted action on a trusted site, and that it works because browsers automatically include cookies, including session cookies. The attacker can do anything the victim is allowed to do, such as change a password or make a purchase. OWASP recommends using built-in framework protection first, never using GET for state-changing operations, and treating SameSite cookies as defense in depth, since SameSite=Lax still sends cookies on top-level GET navigations. The Next.js data security guide says Server Actions are invoked only with POST and check that the Origin matches the Host, which blocks most CSRF for Server Actions. Custom routes don't get that check automatically. MITRE tracks this weakness as CWE-352, Cross-Site Request Forgery.

Sources

OWASP Cheat Sheet Series: Cross-Site Request Forgery Prevention Cheat Sheet — https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html

Next.js docs: Data Security (Server Actions, allowed origins) — https://nextjs.org/docs/app/guides/data-security

MITRE CWE-352: Cross-Site Request Forgery (CSRF) — https://cwe.mitre.org/data/definitions/352.html

Related patterns

Found a problem you'd rather not fix yourself?

Tell us what's wrong. A StorenTech engineer reviews your app and replies with a fixed quote, so you know the cost before any work starts.

Get it fixed by StorenTech

Vibe Security Weekly

One email a week with new failure patterns and how to fix them.

By joining you agree we may email you this newsletter. See our privacy policy. Free. Unsubscribe anytime. We'll never sell your email.