# CSRF, CORS and SameSite cookies: what each actually protects

By Vishal Kumar · Published 2026-10-25 · Updated 2026-11-13
Source: https://fungyaan.com/p/csrf-cors-samesite
Tags: security, csrf, cors, cookies, web

**The verdict, which we'll earn step by step:** they're different tools for different problems.
- **The same-origin policy** (built into browsers) stops a page on one site from *reading* responses from another.
- **CORS** is how *your server relaxes* that rule for chosen origins. It protects nothing on its own; misconfigured, it opens holes.
- **CSRF** is an attack where another site makes the victim's browser *send* a request to your site, with your cookies attached. The same-origin policy doesn't stop sending, only reading.
- **SameSite cookies** stop browsers attaching cookies to most cross-site requests. **Fetch Metadata** headers tell your server where a request came from. A **CSRF token** proves the form came from your page. Use all three for cookie-based apps.

The case: a signed-in user visits some harmless-looking page, and moments later their account has changed something they never asked for. No password stolen, no session hijacked. The usual suspect is CORS ("didn't we configure that?"), and it has a perfect alibi: it was never guarding that door. These ideas get muddled constantly, because each answers a different quirk of how browsers grew up. Once you see which quirk, they fit together like a set of locks.

> **Case file**
>
> | | |
> |---|---|
> | **The case** | A signed-in user visits a harmless-looking page, and their account changes |
> | **Suspects** | CORS ("didn't we configure that?") |
> | **Culprit** | CSRF: another site makes the browser send a request with your cookies |
> | **The fix** | SameSite cookies, Fetch Metadata and Origin checks, a CSRF token |
> | **Evidence** | The blogging platform's checks on every POST |

## The crime, step by step

Here's the attack, and the surprising thing about it: the attacker never needs to see a single byte of your response.

```mermaid
sequenceDiagram
  participant V as Victim's browser
  participant E as evil.example
  participant B as bank.example
  V->>B: signs in#59; gets a session cookie
  V->>E: visits a page with a hidden auto-submitting form
  E-->>V: <form action="https://bank.example/transfer" method=POST> amount=10000 to=attacker
  V->>B: POST /transfer (browser attaches the bank cookie!)
  B-->>V: 200 transferred
  Note over V,B: evil.example never read anything#59; it only needed the request to be sent
```

## The locks, layer by layer

| Layer | What it does | Stops | Doesn't stop |
|---|---|---|---|
| **SameSite=Lax cookie** (Chromium's default when the attribute is missing; other browsers differ, so set it explicitly) | No cookie on cross-site POSTs, iframes or fetches; sent on top-level GET navigations | Most CSRF from other sites | Attacks from **same-site** subdomains (`evil.yourapp.com` counts as same-site); state-changing GETs; older browsers |
| **SameSite=Strict** | No cookie on any cross-site request, even links | More CSRF | Usability: arriving from an email link looks signed out |
| **Fetch Metadata** (`Sec-Fetch-Site`) | Browser tells you `same-origin`, `same-site`, `cross-site` or `none` | Reject anything not `same-origin` for state changes | Non-browser clients (who don't have the victim's cookies anyway) |
| **Origin / Referer check** | Compare the request's origin to your own | Cross-origin form posts | Requests where both headers are stripped (treat as reject) |
| **CSRF token** (synchronizer or double-submit) | A secret value in the form that another site can't read | All cross-site forgery, including same-site subdomains | XSS (script on your own page can read the token) |
| **CORS** | Lets chosen origins **read** responses | Nothing: it only grants | - |

The blogging platform I run uses four of these on every POST: Fetch Metadata, an Origin/Referer check, a synchronizer token tied to the session (a double-submit `__Host-` cookie before sign-in), and SameSite=Lax cookies. The beauty of layering is that no single check has to be perfect. Each blog lives on its own subdomain, which makes the "same-site" gap real: one blog's page must not be able to post to another blog or to the dashboard.

## The false lead: CORS, in one diagram

```mermaid
sequenceDiagram
  participant P as app.partner.example (page)
  participant API as api.yours.example
  P->>API: preflight OPTIONS /orders, Origin: https://app.partner.example, Access-Control-Request-Method: PUT
  API-->>P: Access-Control-Allow-Origin: https://app.partner.example, Allow-Methods: PUT, Allow-Credentials: true
  P->>API: PUT /orders/7 (with credentials)
  API-->>P: 200 + Access-Control-Allow-Origin: https://app.partner.example
  Note over P,API: Without the Allow-Origin header the browser still SENDS simple requests#59; it just hides the response from the page
```

Notice that CORS is a *permission slip*, not a lock. It decides who may *read*, and the attack never needed to read. The CORS rules that matter:
- **Never reflect any `Origin`** back with `Allow-Credentials: true`; that lets every site read your users' data. Use an explicit allow list.
- **`Access-Control-Allow-Origin: *`** is fine for public, cookie-less data (and browsers refuse it with credentials).
- **CORS is not CSRF protection:** a cross-site "simple" request (form-encoded POST) is sent without a preflight, whatever your CORS policy says.

## Which lock for which case

| App | What I'd use |
|---|---|
| **Server-rendered app with cookie sessions** | SameSite=Lax `__Host-` cookies, Fetch Metadata + Origin check, CSRF tokens in forms; no CORS at all |
| **SPA on the same origin as its API** | Same cookies; a CSRF token in a header (or rely on requiring a JSON `Content-Type`, which forces a preflight, plus Fetch Metadata); no CORS |
| **Public API with bearer tokens** | No cookies means no CSRF; CORS allow list (or `*` for public, unauthenticated data) |
| **Embeddable widget on customers' sites** | Bearer tokens scoped to the widget, explicit CORS origins per customer, never your main session cookie |

> **Remember this**
> - The same-origin policy stops reading, not sending: CSRF only needs sending.
> - CORS is a permission slip, not a lock.
> - Layer SameSite, Fetch Metadata and a CSRF token, and fix XSS first.

## What to do on Monday

1. **Cookies:** `__Host-` prefix, Secure, HttpOnly, SameSite=Lax (the [access and refresh tokens post](/p/access-refresh-tokens) covers what to keep in them, and it's the next case to read).
2. **On every state-changing request:** Fetch Metadata check, Origin check, and a CSRF token.
3. **No state changes on GET**, ever.
4. **CORS only when another origin must read responses**, with an explicit allow list.
5. **Fix XSS first:** it defeats every CSRF defence; a strict Content Security Policy helps.

I have real affection for this corner of the web platform. Each mechanism is someone smoothing off a sharp edge without breaking the pages already out there, and that's impressive work.
