# CORS Preflight and SameSite Cookie Boundaries

- **Canonical URL:** https://frontend.rocks/concepts/cors-preflight-samesite/
- **Domain:** Security & Accessibility
- **Baseline / Maturity Status:** Baseline · Widely
- **Last Verified:** 2026-11-15
- **TL;DR:** Fetch-layer cross-origin read gating via OPTIONS preflight handshakes, paired with SameSite cookie attributes that restrict cross-site credential attachment.

## Primary Sources
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS
- https://fetch.spec.whatwg.org/#http-cors-protocol

---
## TL;DR

CORS relaxes the Same-Origin Policy by letting servers declare which origins may read responses via `Access-Control-Allow-Origin`, preceded by an `OPTIONS` preflight for non-simple requests. `SameSite` cookies control whether credentials attach to cross-site requests in the first place.

## Interactive Lab

Step through a credentialed cross-origin `POST` request, its `OPTIONS` preflight, and `SameSite` cookie evaluation.

## Under the Hood

CORS is a bouncer checking whether JavaScript may *read* the response; `SameSite` is the lock on your wallet deciding whether cookies even leave your pocket on a cross-site trip.

1. **Simple vs Preflight**: `GET`/`POST` with `text/plain` or `application/x-www-form-urlencoded` skips preflight; `application/json` or `Authorization` triggers an `OPTIONS` handshake first, including on cross-origin [Beacon API](/concepts/beacon-api-visibilitychange) calls.
2. **Wildcard Ban with Credentials**: When `credentials: 'include'` is set, `Access-Control-Allow-Origin: *` is rejected; the server must echo the exact verified origin and set `Vary: Origin`.
3. **Site vs Origin**: `SameSite` checks the **eTLD+1** (`app.example.com` and `api.example.com` are *same-site*, though *cross-origin*), whereas CORS and [Cross-Origin Isolation](/concepts/cross-origin-isolation-coop-coep) enforce strict `(scheme, host, port)` origin equality.

```http
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Max-Age: 7200
Vary: Origin
```

## Problem Genealogy

- **1995 (Same-Origin Policy)**: Blocked cross-origin DOM/XHR reads, forcing brittle `JSONP` `<script>` tag hacks.
- **2009–2014 (CORS W3C Recommendation)**: Standardized `OPTIONS` preflights and `Access-Control-Allow-*` headers.
- **2020 (`SameSite=Lax` Default)**: Browsers switched default cookies from `None` to `Lax`, cutting off classic cross-site CSRF vectors.

## Production Pitfalls

- **Missing `Vary: Origin`**: Caching a response with `Access-Control-Allow-Origin: https://a.test` on a CDN without `Vary: Origin` breaks `https://b.test` with mysterious CORS errors.
- **Zero `Access-Control-Max-Age`**: Omitting `Max-Age` forces an `OPTIONS` round-trip before every single API call; set `7200` (2 hours, Chromium's cap).
- **When not to use `SameSite=None`**: Never set `SameSite=None` on session cookies unless a third-party iframe genuinely requires them; `SameSite=Lax` protects standard first-party apps against CSRF.
