AleannLab

CSRF in 2026: What SameSite Cookies Fixed and What They Did Not

AleannLab team3 min read

Cross-site request forgery (CSRF) is when another website makes your logged-in user's browser send a request to your app, and the browser attaches their session cookie. Modern browsers treat cookies as SameSite=Lax by default, which stops the classic hidden-form attack. It does not cover state changes done with GET, cookies set to SameSite=None, or attacks from your own subdomains. Keep changes on POST, check the Origin header on the server, and leave your framework's protection on.

On this page
  1. The attack
  2. What SameSite does
  3. The gaps
  4. What to do
  5. What frameworks already do
  6. Test it

CSRF only applies when the browser sends credentials on its own, which means cookies. An API that reads a bearer token from an Authorization header set by your JavaScript is not exposed to it. Moving sessions into cookies, as we recommend in session tokens in localStorage, is what brings CSRF into scope.

The attack

A page on any site contains:

HTML
<form action="https://yourapp.com/api/account/email" method="POST" id="f">
  <input name="email" value="attacker@evil.example">
</form>
<script>document.getElementById('f').submit()</script>

If your user visits it while logged in to your app, and the session cookie goes along, their account email changes. A password reset finishes the takeover.

What SameSite does

  • Strict: the cookie is never sent on requests that start from another site, including when the user clicks a link to you. Safest, but a user arriving from an email link appears logged out on the first page.
  • Lax (the default in current browsers when the attribute is missing): sent on top-level navigations with safe methods such as GET, not on cross-site POSTs, images, iframes or fetch.
  • None: sent everywhere. Requires Secure.

With Lax, the form above arrives without the cookie and fails.

The gaps

State changes on GET. Lax still sends the cookie when the user is navigated to a URL. So these remain exploitable with a plain link or redirect:

code
GET /api/account/delete
GET /unsubscribe?all=true
GET /api/invite/accept?team=42&role=admin
GET /logout

AI-generated route handlers put actions on GET more often than you would expect, because it is easy to test in a browser.

SameSite=None. Set when the frontend and API are on different sites, or when the app is embedded in an iframe. All the old attacks work again, and the cookie attribute gives no protection.

Subdomains count as same-site. blog.yourapp.com, status.yourapp.com and a forgotten staging.yourapp.com are the same site as app.yourapp.com. An XSS on any of them, or a dangling DNS record someone else takes over, can send cross-origin but same-site requests that carry Lax and Strict cookies.

Relying on a default. The Lax default is browser behaviour, and it has had exceptions. Set the attribute explicitly.

What to do

1. Never change state on GET. Use POST, PUT, PATCH or DELETE. Links in emails that perform an action should land on a page with a confirm button.

2. Set cookie attributes explicitly.

code
Set-Cookie: session=...; HttpOnly; Secure; SameSite=Lax; Path=/

3. Check where the request came from. Browsers send an Origin header on cross-origin and state-changing requests, and a page cannot forge it.

TypeScript
const ALLOWED = new Set(['https://yourapp.com', 'https://app.yourapp.com']);

export function assertSameOrigin(req: Request) {
  if (['GET', 'HEAD', 'OPTIONS'].includes(req.method)) return;
  const origin = req.headers.get('origin');
  if (!origin || !ALLOWED.has(origin)) {
    throw new HttpError(403, 'Cross-site request blocked');
  }
}

The newer Sec-Fetch-Site header gives the same information directly: reject cross-site on state-changing requests.

4. Use a CSRF token when cookies must be SameSite=None, or when you want a layer that does not depend on headers. The server issues a random value tied to the session, the page sends it back in a header or hidden field, and the server compares.

5. Require JSON. A cross-site HTML form cannot send Content-Type: application/json, and a cross-site fetch that tries triggers a CORS preflight. Rejecting other content types on JSON endpoints removes the simplest attacks. This holds only while your CORS policy is strict: see CORS wide open.

6. Re-authenticate for the dangerous few. Changing email or password, deleting the account, and payouts should ask for the password or a fresh login.

What frameworks already do

  • Next.js Server Actions accept only POST and compare the Origin header with the host, rejecting mismatches. Route handlers in app/api have no such check. You add it.
  • Auth.js (NextAuth) uses a CSRF token on its own sign-in and sign-out routes.
  • Django, Rails, Laravel include token-based protection by default. The usual bug is someone disabling it on a route (@csrf_exempt) to fix an error.
  • Supabase and Firebase from the browser use tokens in headers, not cookies, so classic CSRF does not apply to those calls.

Test it

Save this as a local HTML file, open it while logged in to your app, and see whether the action happens:

HTML
<form action="https://yourapp.com/api/account/email" method="POST">
  <input name="email" value="csrf-test@example.com">
  <button>Send</button>
</form>

Then list every GET route that changes something:

bash
grep -rnE "export async function GET" app/api -A12 | grep -E "\.(update|delete|create|upsert)\(|sendEmail|stripe\."

Each result is worth reading.

Cookie and request handling are part of what we review in our app rescue and security audit. The full list is in the security checklist for vibe-coded apps.

All articles →

Gotowy, aby omówić swój projekt z nami?

idea
*
*
*
Interesuje mnie...
Tworzenie stron internetowych
Tworzenie aplikacji
Projektowanie UX/UI
DevOps
Sztuczna inteligencja i uczenie maszynowe
Testowanie QA
Tworzenie aplikacji mobilnych
Konsultacja
Outsourcing
Budżet projektu (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Prześlij plik