Highlights

Contents

Part II
02

CORS: simple and preflight requests

We learn how a server grants other origins permission to read its responses. Wren’s web app and its API live on different origins, so without CORS the app cannot read a single API response. We will cover why CORS exists, simple requests, preflight requests and the response headers that make up the protocol.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Origins and thesame-origin policy2CORS: simple andpreflight requests3CORS credentials andcaching4CORS vulnerabilities5Cookies6SameSite and third-partycookies7CSRF8The browser boundary endto end

#Section 4Why CORS exists

Wren’s web app runs at https://app.wren.example and calls the API at https://api.wren.example. Those are different origins, so the same-origin policy hides every API response from the app’s scripts, and the developer sees “blocked by CORS policy” in the console on the very first call. Wren has two ways out. It could put the API under the app’s origin, for example by having the app’s server proxy /api/* to the API. That works for its own app. But partners such as https://partner.example embed a Wren menu widget on their own pages, and their scripts also need to read the API. Wren needs a way to say “these other origins may read my responses”.

CORS (Cross-Origin Resource Sharing) is that way. When a script makes a cross-origin request, the browser adds an Origin header naming the script’s origin. The server answers with Access-Control-Allow-Origin (ACAO) naming the origin it allows. The browser compares the two, and only on a match does it hand the response to the script. If the header is missing or names a different origin, the script gets a network error and nothing else, not even the status code, so it cannot probe the server.

CORS IS THE SERVER TELLING THE BROWSER WHICH OTHER ORIGINS MAY READ ITS ANSWERCORS IS THE SERVER TELLING THE BROWSER WHICH OTHER ORIGINS MAY READ ITS ANSWERhttps://app.wren.examplefetch(api...)api.wren.exampleGET /me + Origin: app.wren...Access-Control-Allow-Origin: https://app.wren.examplebrowser compares, thenhands the body to the script
Figure 2.5The server states who may read. The browser enforces it.

Two facts follow from this design. CORS never blocks anything. It is a controlled relaxation of the SOP, so a server that sends no CORS headers at all is as locked down as it gets. And the server never enforces CORS itself. It only declares a policy, and browsers carry it out. Before CORS was standardised around 2014, sites worked around the SOP with JSONP, hidden iframes and server-side proxies, all of which were either insecure or awkward.

#Section 5Simple requests

Some cross-origin requests were possible long before CORS, because an HTML form can send them. A form can send GET or POST with a body of type application/x-www-form-urlencoded, multipart/form-data or text/plain, to any origin. Servers have always had to cope with such requests arriving from anywhere. So CORS does not ask permission before sending them. It treats requests that a form could have sent as simple requests. The browser sends a simple request immediately with an Origin header, and it checks ACAO only when the response comes back.

A SIMPLE REQUEST GOES STRAIGHT OUT; THE CHECK HAPPENS ON THE WAY BACKA SIMPLE REQUEST GOES STRAIGHT OUT; THE CHECK HAPPENS ON THE WAY BACKscriptbrowserapi.wren.examplefetch GET /menuGET /menu, Origin: https://partner.example200 + ACAO: https://partner.exampleorigins match?response visiblethe server already ran the request; CORS only controls who sees the result
Figure 2.6The request reaches the server and runs. The browser only decides whether the script sees the answer.

The precise rule has three conditions. The method must be GET, HEAD or POST. Every header the script set must be on the CORS safelist, which includes Accept, Accept-Language, Content-Language and a restricted Content-Type, with value limits of 128 bytes. And the Content-Type, if any, must be one of the three form types. A request that meets all three skips the preflight.

The important consequence is that a simple POST runs on the server before the browser decides anything. If Wren answers without a matching ACAO, the browser hides the response from the script, but the order was still placed and the email was still changed. CORS is not a write protection, and treating “CORS blocked it” as “the server rejected it” is a common misreading of the console error.

#Section 6Preflight requests

Anything a form could not send gets a preflight, a permission request sent before the real one. Suppose the app wants to send PUT /orders/123 with a JSON body. Before sending it, the browser sends OPTIONS /orders/123 with Origin: https://app.wren.example, Access-Control-Request-Method: PUT and Access-Control-Request-Headers: content-type. The preflight carries no cookies and no body. The server answers with the origins, methods and headers it allows. Only if that answer covers the planned request does the browser send the real PUT. If not, the PUT never leaves the browser, and the server never sees it.

A PREFLIGHT ASKS PERMISSION BEFORE THE REAL REQUEST IS SENTA PREFLIGHT ASKS PERMISSION BEFORE THE REAL REQUEST IS SENTbrowserapi.wren.exampleOPTIONS /orders/123Access-Control-Request-Method: PUT; -Headers: content-type204 + Allow-Origin, -Methods: PUT, -Headers, Max-Age: 600PUT /orders/123 (application/json)200 + Allow-Originif the preflight says no, the PUT is never sent
Figure 2.7The preflight asks first. A refusal means the real request is never sent.

Three questions decide whether a request needs a preflight. Is the method GET, HEAD or POST? Are all the headers safelisted? Is the Content-Type one of the three form types? A single “no” triggers one. In practice almost every call from a modern single-page app is preflighted, because Content-Type: application/json and Authorization are not on the safelist, and most APIs use PUT, PATCH and DELETE.

IS A PREFLIGHT NEEDED? THREE QUESTIONSIS A PREFLIGHT NEEDED? THREE QUESTIONSGET, HEAD or POST?noonly safelisted headers?noform, multipart or text/plain?nopreflight OPTIONS firstall yes: simple request, no preflightJSON bodies and Authorizationheaders always trigger it
Figure 2.8JSON bodies, custom headers and methods such as PUT and DELETE all trigger a preflight.

Preflights cost a round trip each. At Wren’s 40 ms round trip, an uncached preflight doubles the latency of a small API call. Part III shows how to cache them, and why the cache helps less than people expect.

#Section 7CORS response headers

The server’s side of the protocol is six response headers. Access-Control-Allow-Origin names the single origin allowed to read, or * for any origin. On preflight responses, Access-Control-Allow-Methods lists the allowed methods, such as GET, PUT, DELETE, and Access-Control-Allow-Headers lists the allowed request headers, such as content-type, x-request-id. Access-Control-Allow-Credentials: true allows the browser to send cookies and to expose the response that came back with them.

Access-Control-Expose-Headers lists the response headers a script may read beyond the few safelisted ones, which are Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified and Pragma. If Wren wants the app to log X-Request-Id or read a pagination Link header, it must expose them. Otherwise response.headers.get("x-request-id") returns null even though the header is visible in the network tab, which wastes many afternoons. Access-Control-Max-Age says how many seconds the browser may cache this preflight answer.

THE SIX CORS RESPONSE HEADERS AND WHAT EACH ONE UNLOCKSTHE SIX CORS RESPONSE HEADERS AND WHAT EACH ONE UNLOCKSHTTP/2 204Allow-Origin: https://app.wren.examplewho may readAllow-Methods: GET, PUT, DELETEmethods allowedAllow-Headers: content-type, x-request-idrequest headers allowedAllow-Credentials: truecookies sent and readExpose-Headers: x-request-idheaders scripts can seeMax-Age: 600seconds to cache the preflightall prefixed Access-Control-
Figure 2.9Wren’s preflight response. Each header unlocks one specific thing.

ACAO accepts exactly one value. A header like Access-Control-Allow-Origin: https://app.wren.example, https://partner.example is invalid, and browsers reject it. Supporting several origins means choosing one per request, which Part III covers. ACAO must also appear on the real response, not only on the preflight. Forgetting it on error responses, such as a 500 from an exception handler that bypasses the CORS middleware, makes the browser report a CORS error instead of the real failure.