The browser blocks Set-Cookie because its Domain attribute is invalid
Set-Cookie was blocked because its Domain attribute was invalid with regards to the current host urlShort answer
A browser accepts a Domain attribute only when it is the host that sent the Set-Cookie header or a parent domain of it, and it is not a public suffix such as com or co.uk. A cookie that breaks this is ignored. Fix the domain option of res.cookie(), or leave it out so the cookie is tied to the sending host.
Fix it fast
1Find out which cause you have
Read the Set-Cookie header the server really sends and compare its Domain value with the host name you requested. Look at the header, not at the code that was meant to build it.
curl -s -D - -o /dev/null https://api.example.com/login | grep -i '^set-cookie'2Match what you see, then jump to its fix
- 1
The request goes to one host (api.example.com) and the header says Domain=a different domain (example.org, or a sibling subdomain)The Domain names a site the sending host does not belong toFix - 2
The header shows Domain=com, Domain=co.uk, or a shared hosting suffix such as github.ioThe Domain is a public suffixFix - 3
The header shows Domain=https://example.com, Domain=example.com:3000, or two names in one attributeThe domain value is not a plain domain nameFix - 4
The cookie name starts with __Host- and the header also holds a Domain attributeA __Host- cookie is given a DomainFix
- 1
What does the error look like?
Set-Cookie was blocked because its Domain attribute was invalid with regards to the current host urlres.cookie() runs without error, but no cookie appears in the browser's storageThe same code works when the Domain option is removed
Cause 1 of 4
The Domain names a site the sending host does not belong to
1Check
The value must be the domain of the server that sent the header, or a parent of it. A response from api.example.com can set Domain=api.example.com or Domain=example.com, but not Domain=beta.api.example.com or Domain=other.example.com. If the domain does not match the request host, the cookie is ignored entirely.
grep -rn "domain:" --include="*.js" --exclude-dir=node_modules .
curl -s -D - -o /dev/null https://api.example.com/login | grep -i '^set-cookie'2Fix
Set the domain option to the host that answers or to its parent. To share one cookie between app.example.com and api.example.com, use the parent, example.com. If you do not need sharing, leave domain out: the cookie then becomes a host-only cookie, returned only to the host that sent it.
res.cookie('session', token, {
domain: 'example.com', // parent of api.example.com
httpOnly: true,
secure: true,
});3Verify
The header shows the domain you set, and the cookie shows in the browser's storage for that site.
curl -s -D - -o /dev/null https://api.example.com/login | grep -i '^set-cookie'Confirmed by developer.mozilla.org
Cause 2 of 4
The Domain is a public suffix
1Check
A cookie cannot be set for a public suffix such as com, co.uk or github.io. RFC 6265 tells the browser to ignore the cookie entirely in that case, unless the value equals the request host.
grep -rn "domain:" --include="*.js" --exclude-dir=node_modules .2Fix
Use your own registered domain (example.co.uk, not co.uk). On a shared suffix like github.io, drop the domain option, so the cookie is bound to your own host.
res.cookie('session', token, { domain: 'example.co.uk' });
// shared suffix: no domain option
res.cookie('session', token);3Verify
The response header carries the cookie and the browser stores it.
curl -s -D - -o /dev/null https://www.example.co.uk/login | grep -i '^set-cookie'Confirmed by developer.mozilla.org
Cause 3 of 4
The domain value is not a plain domain name
1Check
The value must be a domain. Multiple host or domain values are not allowed, and a leading dot is ignored, so .example.com and example.com mean the same. A URL or a host with a port is neither. It can happen when a config value such as BASE_URL is reused as the domain.
node -e "console.log(process.env.COOKIE_DOMAIN)"2Fix
Store a bare host name in the setting and pass only that to the domain option. Strip the scheme and the port when you derive it from a URL: URL.hostname does not include the port.
const cookieDomain = new URL(process.env.BASE_URL).hostname;
res.cookie('session', token, { domain: cookieDomain });3Verify
The header shows Domain=example.com and nothing else in that attribute.
curl -s -D - -o /dev/null https://example.com/login | grep -i '^set-cookie'Confirmed by developer.mozilla.org
Cause 4 of 4
A __Host- cookie is given a Domain
1Check
A cookie whose name starts with __Host- must be set with Secure from a secure page, must have Path=/, and must not have a Domain attribute. With a Domain it is rejected.
curl -s -D - -o /dev/null https://example.com/login | grep -i '^set-cookie: __Host-'2Fix
Remove the domain option from that cookie, and keep secure: true and path: '/'.
res.cookie('__Host-session', token, {
secure: true,
path: '/',
httpOnly: true,
});3Verify
The header for the cookie has no Domain attribute, and the browser stores it.
curl -s -D - -o /dev/null https://example.com/login | grep -i '^set-cookie: __Host-'Confirmed by developer.mozilla.org
How do I stop it coming back?
- Leave the domain option out unless you need to share the cookie with subdomains; a host-only cookie cannot fail the domain check.
- Derive the cookie domain from one config value, and keep it a bare host name.
- Test cookies against the real host names that production uses, not a different one.
- Check the Set-Cookie header with curl before you debug the browser.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Express sends Set-Cookie but the browser never stores the cookie
req.cookies is empty on the next request
res.cookie only writes a Set-Cookie header.
4 causes, each with a fixServers
Cookies from a PHP backend vanish behind an nginx proxy
Login works when you call the backend directly but fails through nginx
nginx forwards Set-Cookie headers from the backend unchanged, so lost cookies usually come from the cookie itself: a domain or path that does not match the public URL, or Secure and SameSite flags the browser rejects.
4 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fix