Highlights

Contents

Part III
03

CORS credentials and caching

We cover the CORS settings that decide whether cookies travel and how often the browser asks. Credentialed CORS is where real data leaks happen, and wrong caching makes correct configurations fail at random. We will go through the credentials rules, choosing an origin from an allowlist, Vary: Origin, preflight caching and the null origin.

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 8Credentials and the wildcard

By default a cross-origin fetch sends no cookies, and the response it gets is anonymous. Wren’s app needs the user’s session, so it opts in with fetch(url, {credentials: "include"}). The server must also opt in. It returns Access-Control-Allow-Credentials: true, and its Access-Control-Allow-Origin must name the exact requesting origin. If the server answers a credentialed request with Access-Control-Allow-Origin: *, the browser refuses to expose the response. The same rule bans wildcards in Allow-Headers, Allow-Methods and Expose-Headers whenever credentials are involved. In those headers * loses its special meaning and is read as a literal header name.

WITH CREDENTIALS, THE WILDCARD IS REFUSED: NAME THE ORIGINWITH CREDENTIALS, THE WILDCARD IS REFUSED: NAME THE ORIGINrejected by the browserACAO: * + ACAC: trueany site could read alogged-in user's dataacceptedACAO: https://app.wren.exampleACAC: true · Vary: Originapp.wren
Figure 2.10Credentials plus the wildcard is refused. The server must name the origin it trusts.

The rule exists because a credentialed response is personal. GET /me with user 42’s cookie returns their email, phone and saved address. If * worked with credentials, every website user 42 visited could quietly fetch that data. Forcing the server to name one origin makes whoever configures it decide whom they trust with logged-in data. A public, uncredentialed endpoint, such as Wren’s menu, can safely use *, because it returns the same thing to everyone.

There is a second check on the browser side that people forget. Third-party cookie blocking applies here too. If the app and API were on different sites, Safari and Firefox would refuse to send the API’s cookies on the app’s fetch at all, whatever the CORS headers say. Wren avoids that by keeping both under wren.example, so the request is cross-origin but same-site.

#Section 9Origin allowlists and Vary: Origin

Wren has to allow two origins, its own app and the partner widget, but ACAO holds only one value. So the server reads the request’s Origin header, compares it with an allowlist and, if it matches, echoes it back in ACAO. This dynamic origin reflection is safe only with an exact match against a fixed set of strings. Parse the Origin, compare the whole string, and send no CORS headers at all for anything that does not match. Sending a fixed “default” origin to strangers is harmless but confusing, while sending nothing makes the browser’s error message say plainly that the origin was not allowed.

SAFE DYNAMIC CORS: EXACT STRING MATCH AGAINST A SETSAFE DYNAMIC CORS: EXACT STRING MATCH AGAINST A SETOrigin: partner.exampleOrigin: evil.exampleALLOWED sethttps://app.wren.examplehttps://partner.exampleecho it back+ Vary: Originno CORS headersparse, then compare whole strings; never startswith, endswith or an unanchored regex
Figure 2.11A set lookup on the full origin string. Anything not in the set gets no CORS headers.

A dynamic ACAO creates a caching hazard. Suppose a CDN caches /menu right after a request from the partner, so the stored response carries ACAO: https://partner.example. The next request comes from the app, and the CDN serves the stored copy. The app’s browser sees the partner’s origin in ACAO and blocks the response. Users of the app see a broken menu, but only sometimes, depending on who filled the cache last, which makes the bug look random.

The fix is Vary: Origin on every response whose CORS headers depend on the request’s Origin. That includes responses to disallowed origins that carry no CORS headers, because otherwise a cached “no headers” copy gets served to an allowed origin. Browsers have their own HTTP cache, and the same bug happens there without a CDN involved.

WITHOUT Vary: Origin, A CACHE SERVES ONE ORIGIN'S CORS ANSWER TO ANOTHERWITHOUT Vary: Origin, A CACHE SERVES ONE ORIGIN'S CORS ANSWER TO ANOTHERpartnerapp.wrenCDN cache/menuACAO: partner1st: Origin partner2nd: Origin app.wrengets ACAO: partner, browser blocks itwith Vary: Origin,one entry per origin
Figure 2.12One cached answer, two origins. Vary: Origin keeps one entry per origin.

#Section 10Preflight caching and the null origin

Each preflight costs a round trip before the real request. Wren’s app makes 120 PUT requests to the same URL during a 10-minute editing session. Without preflight caching that is 240 HTTP requests, half of them OPTIONS. With Access-Control-Max-Age: 600, the browser keeps the first preflight answer for 10 minutes, and the session needs 121 requests.

Browsers cap the value. Chromium allows at most 7,200 seconds, and Firefox 86,400. A larger value is silently clipped. The cache is also keyed by origin, URL and request shape. If the 120 PUTs go to 120 different order URLs, such as /orders/1 to /orders/120, each needs its own preflight, and caching saves nothing. APIs with ids in their paths pay the preflight on almost every write. Some teams avoid it by serving the API under the app’s origin through a reverse proxy, which makes the requests same-origin and removes CORS entirely.

120 PUTS IN 10 MINUTES: PREFLIGHT CACHING HALVES THE REQUESTS120 PUTS IN 10 MINUTES: PREFLIGHT CACHING HALVES THE REQUESTSno Max-Age240Max-Age: 600121orange: OPTIONS preflights · grey: the PUTs (first 40 shown)Chromium caps Max-Age at 7,200 s; the cache is per origin, URL and request shape
Figure 2.13One preflight instead of 120. The cache is per URL, so ids in paths reduce its value.

Some requests carry Origin: null. Sandboxed iframes, pages opened from file://, data: URLs and some cross-origin redirects all produce it. A developer testing from a local HTML file sees null in the logs, adds it to the allowlist so the test works, and ships it. That is dangerous, because an attacker can produce a null origin on purpose. A sandboxed iframe on evil.example sends Origin: null, so allowing null with credentials lets any website read user data.

MANY UNRELATED CONTEXTS SEND Origin: nullMANY UNRELATED CONTEXTS SEND Origin: nullsandboxed iframefile:// pagesome redirectsdata: URLOrigin: nullan attacker can produce it on purpose; never put "null" in the allowlist
Figure 2.14Four unrelated sources of the same null value. It identifies nothing.