CORS: requests to another origin
IntermediateWhy a page can’t read just any server’s answers, how a server allows it with Access-Control-Allow-Origin, and when the browser sends a preflight OPTIONS request first.Builds on HTTP requests and responses
OPTIONS /notes/42 HTTP/1.1Host: api.example.comOrigin: https://app.example.comAccess-Control-Request-Method: PUTAccess-Control-Request-Headers: content-type HTTP/1.1 204 No ContentAccess-Control-Allow-Origin: https://app.example.comAccess-Control-Allow-Methods: GET, PUT, DELETEAccess-Control-Allow-Headers: content-typeAccess-Control-Max-Age: 600 PUT /notes/42 HTTP/1.1Host: api.example.comOrigin: https://app.example.comContent-Type: application/json HTTP/1.1 200 OKAccess-Control-Allow-Origin: https://app.example.comEmpty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
3 min read
On this page
What is CORS?
Your browser keeps you logged in to many sites at once. If any page could read any server's answers, a page you opened by accident could read your email or your bank statement through your own browser. The same-origin policy prevents that: a page may freely read answers only from its own .
CORS (Cross-Origin Resource Sharing) is how a server makes an exception. It names, in response headers, the origins whose pages may read its answers. The browser checks those headers and either hands the response to the page or blocks it.
How do simple requests and preflights work?
- The origin.
https://app.example.comandhttps://api.example.comare different origins, and so arehttp://andhttps://, or ports3000and4000onlocalhost. - Simple requests. A
GET,HEADorPOSTwith only ordinary headers and a form-likeContent-Typeis something any HTML form could already send. The browser sends it straight away with anOriginheader, then checksAccess-Control-Allow-Originin the response. - Preflighted requests. Anything else, such as
PUT,DELETE, a JSONContent-Typeor anAuthorizationheader, is first asked about with anOPTIONSrequest. The server answers withAccess-Control-Allow-MethodsandAccess-Control-Allow-Headers, and the real request leaves only if they allow it.Access-Control-Max-Agelets the browser remember the answer. - Cookies. Cross-origin
fetchsends no cookies unless the page asks withcredentials: 'include'. The server must then answer withAccess-Control-Allow-Credentials: trueand name the exact origin, not*.
What do people get wrong about CORS?
Mini example
A cross-origin request that carries a login cookie. The server must name the exact origin, allow
credentials, and say with Vary: Origin that the answer depends on who asks:
GET /me HTTP/1.1Host: api.example.comOrigin: https://app.example.comCookie: sid=k8Fq2xW7pL0aZ3 HTTP/1.1 200 OKAccess-Control-Allow-Origin: https://app.example.comAccess-Control-Allow-Credentials: trueVary: OriginContent-Type: application/jsonQuick check
A page on http://localhost:3000 calls fetch('http://localhost:4000/notes'). Is this a cross-origin request?
Further reading
All steps as text
- Step 1:
The browser loads a notes app from https://app.example.com. That scheme, host and port together are the page’s origin.
- Step 2:
The app’s JavaScript fetches the notes from api.example.com, a different host and so a different origin. The browser sends the request and adds an
Originheader. - Step 3:
By default a page may not read another origin’s answers: that is the same-origin policy. Here the API opts in with
Access-Control-Allow-Origin, naming the page’s origin. - Step 4:
Now the app saves a note with
PUTand a JSON body. An old server might never expect that from another site, so the browser asks first, with anOPTIONSpreflight.Lines 1–2
- Step 5:
The preflight carries no note. It only asks: may a page from this origin send a
PUTwith acontent-typeheader?Lines 3–5
- Step 6:
app.example.com is on the API’s list, so it says yes: this origin, these methods, this header. The
204has no body; the headers are the answer.Lines 7–10
- Step 7:
Access-Control-Max-Age: 600lets the browser remember this answer for 10 minutes, so the next PUTs skip the preflight.Line 11
- Step 8:
Only now does the real
PUTleave, with the note in its body and theOriginheader again.Lines 13–16
- Step 9:
The response must name the origin again: the preflight allowed the request, and this header allows the page to read the answer.
Lines 18–19
- Step 10:
Next, the app tries to fetch data from a third-party API at api.other.example. That is another origin again, so the browser adds
Originonce more. - Step 11:
The server answers normally, but without any
Access-Control-Allow-Originheader. The request reached it and the data came back. - Step 12:
But the browser won’t hand it to the page:
fetchrejects, and the console shows a CORS error. The block happens in the browser, after the answer arrived. - Step 13:
The fix belongs to that API, which has to add the header, or to your own server, which can call the API for you. The page alone can’t switch CORS off.
- Step 14:
Pages can send requests anywhere, but read only the answers whose server names their origin. Requests a plain HTML form couldn’t send are preflighted first.