Why a localhost cookie never reaches thembase.localhost

2024/09/19
networkhttpcookiesamesitesecurity

You log in at localhost:3000, and the server sets a session cookie. Then you open thembase.localhost:3000. Same machine, same browser, same port, but the cookie is gone and the server says you're not logged in.

The first suspect is usually SameSite, but switching it to Lax, Strict or None changes nothing. So where did the cookie go?

How the browser decides whether to send a cookie

One scene runs through the article: an office building with many companies in it.

BuildingBrowser
a department's front deska host, such as www.example.com
a company that owns several desksa site, such as example.com
an access card a desk gives youa cookie
the building directory, listing names that belong to the building itself, not to a companythe Public Suffix List
a rule printed on the card: show it when another company brings you over, or notSameSite

The browser holds your cards. Each time you walk up to a desk, it picks the cards that desk recognizes. Here is how it picks.

Host

The host is the part of the URL after :// and before the port.

  • The host of http://localhost:3000 is localhost.
  • The host of http://thembase.localhost:3000 is thembase.localhost.

Same port and same machine don't matter: to the browser these are two different hosts, two different desks in the building.

Which hosts a cookie is sent to

When a server sets a cookie with Set-Cookie, the browser records which hosts the cookie belongs to. On every later request, it attaches only the cookies that belong to that request's host.

Say the server at myapp.test returns one of these headers, and the browser then sends requests to two hosts:

Header from myapp.testRequest to myapp.testRequest to sub.myapp.test
Set-Cookie: id=1sentnot sent
Set-Cookie: id=1; Domain=myapp.testsentsent

A cookie without Domain is host-only: it belongs only to the host that set it, and not even its subdomains get it. In building terms, a card from one desk works only at that desk. With Domain=myapp.test, the card works at every desk of the company.

Domain can't be too wide: the Public Suffix List

Domain can only name the host itself or a name above it, so sub.myapp.test can write Domain=myapp.test. But what if the name above is shared by many people?

youraccount.github.io and myaccount.github.io are two different people's sites. If youraccount.github.io could set a cookie with Domain=github.io, the browser would send it to every *.github.io, mine included.

The Public Suffix List (PSL) is the directory that prevents this. It lists domains anyone can register names under, such as com, co.uk, github.io and vercel.app. These are public suffixes: the building itself, not a company. RFC 6265bis §5.7 step 9 says that when Domain is a public suffix:

  • if it equals the host, the browser ignores Domain and the cookie becomes host-only;
  • otherwise, the browser drops the whole cookie.

So Domain=github.io from youraccount.github.io is dropped. Nobody can issue a card for the whole building.

The browser looks up the end of a host on the list. The part it finds is the eTLD (effective top-level domain). For www.example.com:

  • com: on the list, so it is the eTLD, the building itself
  • example.com: the eTLD plus one more name to the left, example, called the eTLD+1. This is the site, the company in the building
  • www.example.com: the full host, one desk of that company

If nothing is found on the list, the browser takes the rightmost dot-separated name as the eTLD. So an eTLD isn't always on the list: the eTLD of shop.unknowntld is unknowntld, which the list doesn't contain.

DomainFound on the listeTLDeTLD+1 (site)
www.example.comcomcomexample.com
blog.example.co.ukco.ukco.ukexample.co.uk
youraccount.github.iogithub.iogithub.ioyouraccount.github.io
shop.unknowntldnothingunknowntldshop.unknowntld

SameSite: requests started by another site

Host, Domain and the PSL together decide which hosts a cookie belongs to. Now suppose the cookie does belong to the host a request is going to, but the request was started by a page on another site. The browser then reads the cookie's SameSite. For example, if the server set the cookie with SameSite=Strict, this request goes out without it.

So the browser first has to tell whether the page and the request's URL are the same site. Same site is not the same as same origin:

Same SiteSame Origin
Used bythe SameSite cookie attribute, the Sec-Fetch-Site headerthe Same-Origin Policy (storage, fetch, postMessage)
CompareseTLD+1, plus the scheme in Chrome 91 and laterscheme, host and port, all exact
www.example.com vs blog.example.comsame sitedifferent origin
http://x.com vs https://x.comsame site by the old definition, cross-site in Chrome 91 and laterdifferent origin
example.com:80 vs example.com:8080same sitedifferent origin

In the building, same origin is the same desk through the same door; same site is the same company.

Back to localhost: why the cookie doesn't arrive

The cookie that was set:

{
  httpOnly: true,
  secure: false,
  sameSite: "lax",
  path: "/",
}

There is no Domain, so the cookie is host-only and belongs only to localhost. thembase.localhost is a different host, so the browser doesn't attach the cookie to requests going there.

What about adding Domain=localhost?

That doesn't work either. localhost is a name reserved by RFC 6761 §6.3 and isn't on the PSL. It has no dot, so its rightmost name is localhost itself, and that is its eTLD. An eTLD+1 needs one more name to the left of the eTLD, and localhost has nothing to its left, so it has no eTLD+1:

www.example.com      eTLD = com          eTLD+1 = example.com
shop.unknowntld      eTLD = unknowntld   eTLD+1 = shop.unknowntld
thembase.localhost   eTLD = localhost    eTLD+1 = thembase.localhost
localhost            eTLD = localhost    eTLD+1 = (none)

Whenever a host has no eTLD+1, Domain can only be the host itself and the cookie can only be host-only. IP addresses and co.uk itself have no eTLD+1 either, so the same rule applies to them. Neither browser's code has a rule specific to localhost:

  • Chrome finds no eTLD+1 for localhost (net/cookies/cookie_util.cc).
  • Firefox needs one more name to the left of localhost before there is an eTLD+1, as in thembase.localhost. Bare localhost returns NS_ERROR_INSUFFICIENT_DOMAIN_LEVELS, and on that error the cookie code only accepts a Domain identical to the host (netwerk/cookie/CookieCommons.cpp).

So Domain=localhost is ignored and the cookie stays host-only. In building terms, localhost is the lobby. The lobby has a desk, but it isn't a company; thembase.localhost is a company in the building.

Measured on Chrome 156 and Firefox 155:

Set onAttributesStored asDid the subdomain get it?
localhostno Domainbelongs to localhost onlyno, thembase.localhost got nothing
localhostDomain=localhostbelongs to localhost onlyno
localhostDomain=.localhostbelongs to localhost onlyno
thembase.localhostDomain=localhostdropped—
thembase.localhostDomain=thembase.localhostthembase.localhost and its subdomainsyes, a.thembase.localhost got it
myapp.testno Domainbelongs to myapp.test onlyno
myapp.testDomain=myapp.testmyapp.test and its subdomainsyes, sub.myapp.test got it

Domain=localhost and Domain=.localhost raise no error. The browser keeps the cookie and ignores Domain, so everything looks fine on localhost, and the cookie only turns out to be missing on another host.

Why changing SameSite doesn't help

SameSite only decides whether a cookie that already belongs to a host is attached. This cookie never belonged to thembase.localhost, so no SameSite value changes anything. With Lax, Strict, None; Secure, or no SameSite at all, the localhost cookie never reached thembase.localhost.

localhost and thembase.localhost are still two different sites, though. Following a link from one to the other, Chrome and Firefox both sent Sec-Fetch-Site: cross-site. In Chrome, a fetch() from thembase.localhost to localhost also carried only SameSite=None cookies. So SameSite does apply between them; it just isn't why the cookie went missing.

Fixing the localhost dev setup

Bare localhost is the lobby and can't issue a card for a whole company. Use a name that has an eTLD+1, such as myapp.test. Either of these works.

1. myapp.test via /etc/hosts. RFC 6761 reserves .test for exactly this, and unlike .local it doesn't collide with macOS mDNS. Point the names at loopback:

# /etc/hosts
127.0.0.1   myapp.test
127.0.0.1   sub.myapp.test

Or in one line:

echo "127.0.0.1 myapp.test sub.myapp.test" | sudo tee -a /etc/hosts

Develop at http://myapp.test:3000 and set the cookie with Domain=myapp.test; both hosts get it. myapp.test behaves like a production myapp.com.

One catch: myapp.test over plain HTTP is not a secure context, and cookies with Secure are dropped there. Chrome 156 and Firefox 155 dropped Secure, Secure; SameSite=Lax and SameSite=None; Secure cookies set over http://myapp.test, and kept the same cookies set over https://myapp.test. If your production cookies are Secure, run the dev server on HTTPS with a local certificate.

2. myapp.localhost and sub.myapp.localhost, no /etc/hosts needed. Chrome and Firefox both resolve any *.localhost name to loopback themselves. myapp.localhost is a company in the building, so a cookie set there with Domain=myapp.localhost reaches sub.myapp.localhost. thembase.localhost in the table above has the same structure, and its cookie reached a.thembase.localhost in Chrome 156 and Firefox 151 and 155. Safari wasn't tested.

Two things look like fixes and aren't:

  • Domain=.localhost or Domain=localhost. The browser ignores the attribute, and the subdomain still gets nothing.
  • SameSite=None; Secure. SameSite never makes a cookie belong to more hosts.

SameSite in detail

The three values

Set-Cookie: id=abc; SameSite=Strict
Set-Cookie: id=abc; SameSite=Lax
Set-Cookie: id=abc; SameSite=None; Secure
ValueCross-site subresource request (<img>, <iframe>, fetch)Cross-site top-level navigation
Strictnot sentNot sent. Even following a link from another site to yours, the first request has no cookie
Laxnot sentSent for GET (following a link). A cross-site POST form is not sent
Nonesent, but Firefox stores it per top-level site (see below)sent. Requires Secure

A top-level navigation changes the URL in the address bar: following a link, following a redirect, submitting a form. <img src>, <iframe>, <script>, fetch() and XHR don't change the address bar; those are subresource requests.

With a bank's session cookie set to Lax:

  • Sent. The user clicks a link to the bank in an email. That is a top-level GET navigation, so the user arrives logged in.
  • Not sent. An attacker's page includes <img src="https://yourbank.com/transfer?to=evil">. That is a subresource request, so it goes out without the session cookie and the CSRF fails. A hidden form that POSTs to your site is a top-level navigation, but not a GET, so Lax doesn't send it either.

Strict is the most conservative: even an email link lands logged out. Keep it for high-risk actions such as an admin panel or a bank's transfer confirmation.

When SameSite is missing

According to MDN's compat data, only Chrome treats a cookie with no SameSite as Lax (since Chrome 80); Firefox and Safari treat it as None. With Firefox's Total Cookie Protection turned off (network.cookie.cookieBehavior=0), Firefox 151 and 155 sent a cookie with no SameSite on a cross-site <img> request; Chrome 156 didn't. So write SameSite=Lax explicitly.

Chrome has one exception for such cookies: for the first two minutes after one is set, it is still sent on a cross-site top-level POST (RFC 6265bis calls this Lax-allowing-unsafe). On Chrome 156, the cookie went out on a cross-site POST 16 seconds after it was set and not 147 seconds after. A cookie with an explicit SameSite=Lax was sent neither time.

Firefox stores None cookies separately per site

Ad networks use SameSite=None cookies to recognize the same person. Say news.test and shop.test both show ads from ads.test. While the user browses news.test, ads.test sets a cookie id=42. Later the user browses shop.test, the browser sends id=42 back to ads.test, and ads.test knows the same person visited both sites.

Firefox's ad It's time to come back to Firefox brings back a group of early internet celebrities to dance together, and it is about how good the old internet was. The ad sells a feeling, but Firefox also changed a setting: since June 2022, Total Cookie Protection has been on by default for all desktop users. In Mozilla's words, trackers used to be able to link one person's behavior across many sites. With the feature on, ads.test treats the user on news.test and the user on shop.test as two unrelated people.

Chrome also planned to stop third-party cookies, but after years of delays, Google announced in April 2025 that it would keep the current approach. Third-party cookies are still sent, and users who want them off have to turn them off in settings.

Firefox's approach is to store cookies separately by which site the user was on at the time. The cookie ads.test set on news.test is sent only while the user is on news.test:

User is browsingCookie Chrome sends to ads.testCookie Firefox sends to ads.test
news.testid=42id=42
shop.testid=42none

That table only illustrates the rule; the ads.test case was not measured. The case below was measured, and Firefox follows the same rule:

myapp.test makes a chat widget, and other.test embeds it in its own page. The user first logs in on myapp.test and gets myapp.test's login cookie, then browses other.test. When other.test's page sends a request to myapp.test:

Chrome 156Firefox 155
Sends the cookie from the myapp.test loginyesno

The user got the login cookie while browsing myapp.test, so Firefox sends it only while the user is browsing myapp.test. If the widget relies on that cookie to know who is logged in, it shows as logged out in Firefox when embedded on other.test.

Following a link, though, makes Firefox send more. In the measurement, the user clicked a link on a localhost page to thembase.localhost, then thembase.localhost's page sent a request to localhost. By the rule above, the user is now browsing thembase.localhost and localhost's cookies shouldn't go along, but Firefox 155 sent them. The no-SameSite and SameSite=None cookies were sent; the Lax and Strict ones still were not, because the request is still cross-site. This exception was measured only on *.localhost.

Firefox probably does this for flows like "log in to site B with your site A account". The user follows a link from A to B, B's page asks A whether this person is logged in, and if that request carries no A cookie, A can only answer "not logged in".

Are http and https the same site: Schemeful same-site

Address bar: http://myapp.test
GET https://myapp.test/api
→ Cookie: id=abc     Firefox 155 (the cookie is SameSite=Lax)
→ (no Cookie)        Chrome 156

The domain is the same; only the page is http and the request is https. Since version 91, Chrome compares the scheme (http or https) too when deciding whether two sides are the same site, so Chrome treats this request as cross-site and sends neither Lax nor Strict cookies. Firefox compares only the eTLD+1 and treats it as same-site. The change is called Schemeful Same-Site. Serving the whole site over HTTPS avoids the case.

Why SameSite=None requires Secure

A SameSite=None cookie goes out on cross-site requests too, so it is sent far more often. Without Secure it would also travel over plain HTTP, where anyone on the same coffee-shop Wi-Fi could read it off the network. So browsers that enforce the rule reject such cookies outright:

Set-Cookie: id=abc; SameSite=None             rejected, no Secure
Set-Cookie: id=abc; SameSite=None; Secure     accepted, sent over HTTPS only

According to MDN's compat data, Chrome has enforced this since Chrome 80, Firefox since Firefox 131, and Safari doesn't enforce it. Measured: Chrome 156 and Firefox 151 and 155 all rejected SameSite=None without Secure.

The current best combination

Set-Cookie: session=...; HttpOnly; Secure; SameSite=Lax; Path=/
  • HttpOnly: JavaScript on the page can't read it from document.cookie, so an XSS payload can't copy the session ID out. XSS can still send requests from the page, though, and the browser still attaches the cookie to them.
  • Secure: HTTPS only. Chrome and Firefox accept it on http://localhost too.
  • SameSite=Lax: written out, because only Chrome defaults to it.
  • Path=/: the whole app.

Add Domain only when subdomains really need to share the cookie. Once you do, every subdomain gets it, including ones run by other teams or other programs.

Environment

ItemValue
SystemmacOS 15.6 (arm64)
Driverpuppeteer-core 25, headless
Chrome156.0.8078.49
Firefox151.0, 155.0
Results fromthe Cookie and Sec-Fetch-Site headers the server logged for each request
PSLthe version downloaded on 2026-10-28

References