Sub topic 15
Same-origin policy and CORS
This chapter assumes you know how a browser sends HTTP requests, how headers work, and how a browser loads a page that runs JavaScript. Here you will learn why a browser blocks some cross-site requests and how a server can allow them.
What an origin is
An origin is the combination of scheme, host, and port. Two URLs share an origin only if all three match.
| URL | Same origin as `https://example.com/app`? |
|---|---|
| `https://example.com/other` | Yes |
| `http://example.com/app` | No, the scheme differs |
| `https://api.example.com/app` | No, the host differs |
| `https://example.com:8443/app` | No, the port differs |
Why the same-origin policy exists
Your browser attaches cookies to every request for a site. Suppose you are logged in to https://bank.example. A script on https://evil.example could then call fetch("https://bank.example/account"), and the browser would send your session cookie along. Without a restriction, the script could read your account data.
The same-origin policy prevents this. Scripts can send requests to other origins, but the browser will not let them read the response unless the other origin agrees. The policy is enforced by the browser. It does not stop curl or a server from making the same request.
How CORS relaxes the policy
Cross-Origin Resource Sharing (CORS) is the mechanism that lets a server opt in to being read by another origin. The browser adds an Origin header to cross-origin requests. The server answers with Access-Control-Allow-Origin to name the origins it trusts.
GET /data HTTP/1.1
Host: api.example.com
Origin: https://app.example.com
HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://app.example.com
Content-Type: application/jsonIf the header is missing or does not match, the browser still receives the response. It then refuses to give it to your script and logs a CORS error in the console.
Preflight requests
Some requests could change data on the server, so the browser first asks permission. This applies to methods such as PUT and DELETE, and to requests with custom headers or a JSON Content-Type. The browser sends an OPTIONS request called a preflight.
The server must answer the preflight with the permissions it grants:
HTTP/1.1 204 No Content
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Allow-Methods: GET, PUT, DELETE
Access-Control-Allow-Headers: Content-Type
Access-Control-Max-Age: 600Access-Control-Max-Age lets the browser cache the answer, so it skips repeat preflights for 600 seconds.
Credentials and common errors
By default, cross-origin fetch calls do not send cookies. To include them, set credentials: "include" in the request. The server must then respond with Access-Control-Allow-Credentials: true. It must also name an exact origin, because the browser rejects the wildcard * when credentials are involved.
Common mistakes:
- Treating a CORS error as a server failure. The request often succeeded, and the browser only hid the result.
- Setting
Access-Control-Allow-Origin: *on an API that uses cookies. This fails, and echoing back anyOriginvalue without checking it is unsafe. - Handling
GETbut notOPTIONS, so the preflight fails. - Forgetting
Vary: Originwhen the allowed origin changes per request. Caches can then serve the wrong header.
Exercise
A page at https://app.example.com calls fetch("https://api.example.com/items", { method: "DELETE" }). The server's DELETE handler works when tested with curl, but the browser shows a CORS error. What is the most likely cause, and what should the server do?
Show answer
The cross-origin DELETE triggers a preflight OPTIONS request, and the server does not answer it with the right CORS headers. The server should respond to OPTIONS with Access-Control-Allow-Origin: https://app.example.com, Access-Control-Allow-Methods including DELETE, and any needed Access-Control-Allow-Headers. It should also send Access-Control-Allow-Origin on the DELETE response itself.