On this page
Same-Origin Policy & CORS
Prev: security | Up: security | Next: CSP, Cookies & XSS/CSRF Mitigations
Rel: Fetch & Networking, Python Web Development & APIs
Ref: MDN Web Docs — Cross-Origin Resource Sharing (CORS)
Tags: #public #web #platform #security #http #cors
Contents
What Defines an Origin
Two URLs share the same origin only when scheme, host, and port all match exactly. The browser's Same-Origin Policy (SOP) blocks JavaScript from reading responses across origins unless the server explicitly opts in via CORS headers.
Simple vs. Preflighted (OPTIONS) Requests
- Simple requests (
GET/HEAD/POSTwith only safelisted headers likeAcceptorContent-Type: text/plain) are sent directly; the browser checksAccess-Control-Allow-Originbefore exposing the response to JS. - Preflighted requests (e.g.,
Content-Type: application/json, custom headers likeAuthorization, orPUT/DELETE) first trigger an automaticOPTIONSrequest to verify permission.
Key CORS Response Headers
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Authorization
Access-Control-Allow-Credentials: true
Access-Control-Max-Age: 600
Note: Access-Control-Allow-Origin: * cannot be combined with Access-Control-Allow-Credentials: true (or fetch(url, { credentials: "include" })); the server must echo an explicit trusted origin and include Vary: Origin.
Linked from
Related notes
- tuts/web/security Linked here; links to this page; shared tags: #platform, #security, #web
- tuts/web/security/csp_cookies_and_mitigations Linked here; links to this page; shared tags: #platform, #security, #web
- tuts/web/apis/fetch_and_networking Linked here; shared tags: #platform, #web
- tuts/python3/tutorials/popular_application_domains/web_development_and_apis Linked here; shared tags: #web
- tuts/web shared tags: #platform, #web
- tuts/web/apis shared tags: #platform, #web
- tuts/web/apis/observers_and_workers shared tags: #platform, #web
- tuts/web/apis/storage_and_state shared tags: #platform, #web
- tuts/web/dom shared tags: #platform, #web
- tuts/web/dom/events_and_delegation shared tags: #platform, #web