CORS Preflight and SameSite Cookie Boundaries
Fetch-layer cross-origin read gating via OPTIONS preflight handshakes, paired with SameSite cookie attributes that restrict cross-site credential attachment.
About this page
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).
Where this connects
Prerequisites
Beacon API and visibilitychange Lifecycle
Cross-origin sendBeacon payloads with non-simple MIME types trigger CORS preflights.
Next & related
Cross-Origin Isolation with COOP and COEP
COEP relies on CORS or Cross-Origin-Resource-Policy opt-ins before loading cross-origin subresources.
Simulated DOM (JSDOM/HappyDOM) vs Real Browser Engines
Simulated fetch mocks bypass browser CORS preflight and SameSite cookie enforcement.
Mechanical Comparisons
- Contrasts WithCORS Preflight and SameSite Cookie Boundaries vs Cross-Origin Isolation with COOP and COEP
CORS governs JS response readability; COEP/CORP governs embedding binary resources in isolated processes.