What Is CORS? Cross-Origin Resource Sharing Explained
On this page
Learning Objective
Explain CORS as a server-controlled relaxation of the Same-Origin Policy's read restriction, enforced by the browser on behalf of the user, and articulate why it is not a mechanism that protects a server from unwanted requests.
Concept Explanation
Cross-Origin Resource Sharing is a set of HTTP response headers by which a server tells the browser "it is acceptable for script on this specific other origin to read my response." That is the entire scope of the mechanism. It does not authenticate anyone, it does not authorize anything, and it does not block any request from arriving. It is a note from the resource owner to the browser, and the browser is the only party that enforces it.
The most important consequence of "the browser is the only enforcer" is that CORS has no effect whatsoever on any client that is not a browser. curl, a Python script, a mobile app, a server-side HTTP client, and Postman all ignore CORS headers completely, because there is no user whose ambient credentials need protecting and no other origin to protect them from. If an API endpoint returns sensitive data to any caller who asks, a restrictive CORS policy does not protect it; it simply makes the data inconvenient to fetch from a browser. Authentication and authorization remain the server's job and CORS is not a substitute for either.
It is worth being precise about whom CORS protects. It protects the user, not the server. The scenario it addresses is: a user is logged into their bank, they visit a malicious page, and that page tries to read the bank's API with the user's cookies attached. The bank's server sees a perfectly valid authenticated request and answers it. CORS is what stops the malicious page from seeing the answer. The resource owner declares who may read, and the browser enforces that declaration on the user's behalf, which is why an overly permissive CORS policy is dangerous: the server is waiving a protection that exists for its users.
Before CORS, developers worked around the read restriction with JSONP, which exploited the fact that <script> tags may be loaded cross-origin. The client added a script tag pointing at the API with a callback parameter, and the server responded with JavaScript that invoked that named callback with the data as its argument. It worked, and it was a catastrophe in security terms: the response executes as code in the consuming page, so the consumer grants the provider full script execution rights, the request can only ever be a GET, and there is no way to restrict who may call. JSONP endpoints also became one of the most reliable ways to bypass a Content Security Policy, which this course covers in its third phase. CORS exists so that none of this is necessary.
Finally, CORS is opt-in and default-deny, which is the right shape for a security mechanism. A server that says nothing gets the old behavior: requests are still sent, responses are still received by the browser, and script is refused access. Everything in the rest of this phase is about the vocabulary a server uses to carve out precise exceptions to that default, and about the ways teams accidentally carve out far more than they intended.
| Claim about CORS | True or false | Why |
|---|---|---|
| CORS stops attackers from calling my API | False | Non-browser clients ignore CORS entirely; the request always reaches the server |
| CORS protects my server | False | It protects the user by controlling what script on another origin may read |
| CORS prevents CSRF | False | Simple requests are still sent with credentials and still take effect |
| A missing CORS header blocks the request | False | The request is sent and answered; only the response is withheld from script |
| CORS is a server-declared, browser-enforced read permission | True | The server declares, the browser enforces, and only for script in a browser |
Worked Example: The Same Endpoint Through Three Clients
Nothing illustrates the scope of CORS faster than issuing one request three ways. The endpoint behaves identically each time; only the browser applies a policy to what it does with the answer.
# 1. curl: CORS headers are present in the response and completely ignored.
$ curl -i https://api.example.com/v1/me -H 'Cookie: session=abc'
HTTP/1.1 200 OK
Content-Type: application/json
Access-Control-Allow-Origin: https://app.example.com
{"id":42,"email":"user@example.com"} <-- curl reads it fine
# 2. Browser script on https://app.example.com: origin matches, read allowed.
fetch('https://api.example.com/v1/me', { credentials: 'include' })
.then(r => r.json()) // resolves
# 3. Browser script on https://evil.example: the request is STILL SENT and
# the server STILL PROCESSES IT, but the browser refuses the read.
fetch('https://api.example.com/v1/me', { credentials: 'include' })
.then(r => r.json())
// TypeError: Failed to fetch
// The server saw the request. It just cannot be read by this origin.Real-World Examples
- JSONP was the dominant pre-CORS workaround and is still deployed on major platforms; Google, Facebook, and Flickr all shipped JSONP endpoints in the late 2000s, and the residual endpoints on widely allowlisted domains later became one of the most reliable Content Security Policy bypass techniques documented in Google's 2016 CCS paper "CSP Is Dead, Long Live CSP!", since an allowlisted host serving a JSONP endpoint effectively grants arbitrary script execution.
- The CORS specification was first published as a W3C Working Draft in 2009 and later folded into the WHATWG Fetch Standard, which is now the single normative definition of the algorithm every browser implements; that consolidation matters practically, because behaviors teams often treat as browser quirks, such as which request headers count as safelisted or why a wildcard cannot be combined with credentials, are explicitly specified there rather than left to implementations.
Review Questions & Answers
They have confused a browser-enforced read restriction with network-level access control. Any client that is not a browser, curl, a script, a mobile app, a compromised server on the same network, will receive the full response regardless of the CORS headers, because nothing outside a browser consults them. The CORS policy protects a browser user from a malicious page reading the response with their credentials; it does nothing at all to restrict who can reach the endpoint.
The threat CORS addresses is a malicious page reading an authenticated response using a victim's ambient cookies. The server itself is in no additional danger from a permissive policy, since the requests were always going to arrive. What changes is that any origin may now read the data that comes back with the user's credentials attached, so the harm lands on users whose sessions can be exercised by a page they merely visited.
JSONP delivers the response as executable JavaScript that runs in the consumer's own page, so the consumer grants the provider, and anyone who can influence the provider's response, full script execution rights in their origin. CORS delivers data as data, with the browser mediating whether script may read it. JSONP also cannot express anything but a GET, cannot restrict which origins may call, and cannot be audited, and its surviving endpoints are a well-documented way to bypass a Content Security Policy.
The browser sends the request, the server receives and processes it normally with whatever credentials were included, and the server sends a response. The browser then finds no Access-Control-Allow-Origin header that matches the requesting origin and rejects the fetch promise without exposing the status, headers, or body to script. The absence of CORS headers is a refusal to grant read access, not a refusal to perform the request.