CORS Preflight and SameSite Cookie Boundaries
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.
CORS Preflight & Credentialed Fetch Flow
Trace how a cross-origin JSON POST triggers an OPTIONS preflight and SameSite cookie check.
fetch('https://api.example.com/v1', { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json' } }) is non-simple because of application/json.
fetch('https://api.example.com/v1', { method: 'POST', credentials: 'include', ... });- simpleRequest
- false
- preflightRequired
- true
- cookiesAttached
- PENDING
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.
- Simple vs Preflight:
GET/POSTwithtext/plainorapplication/x-www-form-urlencodedskips preflight;application/jsonorAuthorizationtriggers anOPTIONShandshake first, including on cross-origin Beacon API calls. - Wildcard Ban with Credentials: When
credentials: 'include'is set,Access-Control-Allow-Origin: *is rejected; the server must echo the exact verified origin and setVary: Origin. - Site vs Origin:
SameSitechecks the eTLD+1 (app.example.comandapi.example.comare same-site, though cross-origin), whereas CORS and Cross-Origin Isolation enforce strict(scheme, host, port)origin equality.
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: OriginProblem Genealogy
- 1995 (Same-Origin Policy): Blocked cross-origin DOM/XHR reads, forcing brittle
JSONP<script>tag hacks. - 2009–2014 (CORS W3C Recommendation): Standardized
OPTIONSpreflights andAccess-Control-Allow-*headers. - 2020 (
SameSite=LaxDefault): Browsers switched default cookies fromNonetoLax, cutting off classic cross-site CSRF vectors.
Production Pitfalls
- Missing
Vary: Origin: Caching a response withAccess-Control-Allow-Origin: https://a.teston a CDN withoutVary: Originbreakshttps://b.testwith mysterious CORS errors. - Zero
Access-Control-Max-Age: OmittingMax-Ageforces anOPTIONSround-trip before every single API call; set7200(2 hours, Chromium's cap).
- When not to use
SameSite=None: Never setSameSite=Noneon session cookies unless a third-party iframe genuinely requires them;SameSite=Laxprotects standard first-party apps against CSRF.
Connections & Logical Next Steps
COEP relies on CORS or Cross-Origin-Resource-Policy opt-ins before loading cross-origin subresources.
Simulated fetch mocks bypass browser CORS preflight and SameSite cookie enforcement.
Cross-origin sendBeacon payloads with non-simple MIME types trigger CORS preflights.
Cross-origin sendBeacon with application/json triggers a CORS preflight or fails if disallowed.
CORS governs JS response readability; COEP/CORP governs embedding binary resources in isolated processes.