SameSite Cookies Explained
What SameSite=Strict, Lax and None mean, worked through with one example, and why Lax is the right default for a login cookie.

Every login cookie should have a SameSite setting, and most of them end up
as SameSite=Lax. It's one word in a Set-Cookie header, but it decides
whether another website can make requests to yours as your logged-in
user.
This post explains what the three settings do, using one example all the way through. It follows on from Sessions Explained, where the session cookie looks like this:
Set-Cookie: sid=a8f3c9...; HttpOnly; Secure; SameSite=Lax; Path=/; Max-Age=604800The problem SameSite solves
Your browser sends a site's cookies with every request to that site, whichever page started the request. That's what keeps you logged in. It's also a problem: if another site makes your browser send a request to your bank, your bank's cookie goes along with it.
Using that to act as someone without their knowledge is called cross-site
request forgery (CSRF). SameSite tells the browser when it should leave the
cookie out.
What "same site" means
Two addresses are the same site if they share the scheme and the
registrable domain: the part someone actually buys, like bank.com.
https://bank.comandhttps://app.bank.com: same site.https://bank.comandhttps://cute-cats.com: different sites.https://bank.comandhttp://bank.com: different sites (the scheme differs).https://alice.github.ioandhttps://bob.github.io: different sites. Browsers keep a list of domains likegithub.iowhere anyone can get a subdomain, and treat each subdomain there as its own site.
A request is cross-site when a page on one site makes the browser send a request to a different site.
The three settings
Strict: never send the cookie on a cross-site request.Lax: send it only when another site opens a page on yours with aGET, like a link click. (AGETform or a redirect counts too.) Leave it out of everything else that another site starts:POSTforms, images, iframes,fetch.None: always send it. This requiresSecure.
"Lax" just means "relaxed": less strict than Strict, but still protective.
The example below shows why the difference matters.
One example, three settings
Alice logs in to bank.com. Her browser now has the cookie sid=a8f3c9 for
bank.com.
Case 1: an attacker's site tries to move her money
Later, Alice visits cute-cats.com, which is secretly run by an attacker. The
page has a hidden form that submits itself:
<form action="https://bank.com/transfer" method="POST"> <input name="to" value="attacker" /> <input name="amount" value="1000" /></form><script> document.forms[0].submit();</script>Alice's browser sends POST https://bank.com/transfer. Does it attach her
bank.com cookie?
None: yes. The bank sees alice's session and sends the $1000.Lax: no, because it's a formPOSTstarted by another site. The bank sees no session and answers401. The attack fails.Strict: no. The attack fails.
Case 2: Alice clicks a link in an email
Alice gets an email from her bank and clicks a link to
https://bank.com/statements. The click comes from her email, which is a
different site.
None: yes. She lands on her statements, logged in.Lax: yes, because it's a link click that opens a page. She lands on her statements, logged in.Strict: no. The bank shows its login page, even though she's logged in. If she refreshes, she's logged in again: the browser started that request itself, not another site, so it counts as same-site.
Side by side
None: the attack works, and the link click works.Lax: the attack is blocked, and the link click works.Strict: the attack is blocked, but the link click looks logged out.
Lax is the only setting that gets both cases right. That's why it's the
usual choice for a login cookie.
The rule Lax depends on: GET never changes anything
Lax still sends the cookie when another site opens a page on yours, by a
link, a GET form or a redirect. Those are all GET requests. So if your app
changes data on a GET, Lax doesn't protect it:
<!-- On cute-cats.com. With Lax, the cookie IS sent when this is clicked. --><a href="https://bank.com/transfer?to=attacker&amount=1000">See more cats</a>The fix isn't a different cookie setting. It's the rule HTTP already has:
GET requests only read. Anything that changes data (transfers, deletes,
logging out, changing a password) uses POST, PUT, PATCH or DELETE.
When to use Strict
Use Strict when nobody should ever arrive at your site already logged in
from somewhere else, like an admin panel or a banking app that's fine with an
extra login screen. Some sites use two cookies: a Lax one for normal
browsing, and a Strict one that's required for sensitive actions.
When to use None
Use None only when your cookie really must work inside other sites, such as
a chat widget, an embedded video player, or a payment form in an iframe.
It must come with Secure:
Set-Cookie: widget_sid=k29xq...; Secure; SameSite=NoneMany browsers now block or restrict third-party cookies, so even None isn't
guaranteed to work there. A normal login cookie for your own site never needs
it.
Setting it in Express
res.cookie("sid", sessionId, { httpOnly: true, secure: true, sameSite: "lax", // or "strict" / "none" maxAge: 7 * 24 * 60 * 60 * 1000,});Most frameworks and auth libraries have the same option under a similar name.
SameSite isn't the whole defense
SameSite=Lax stops most CSRF attacks, but not all of them:
- Subdomains count as the same site. If an attacker controls
blog.bank.com(say, through a forgotten subdomain), requests from it tobank.comare same-site, and the cookie is sent. - Old browsers ignore it. A browser that doesn't understand
SameSitesends the cookie everywhere. GETrequests that change data aren't protected, as shown above.
For actions that really matter, add a second check: a CSRF token in the form,
or checking that the request's Origin header is your own site.
The short version
- Browsers send a site's cookies on every request to it, even ones another
site starts.
SameSitecontrols that. Strict: never on cross-site requests.Lax: only when another site opens a page on yours with aGET, like a link.None: always, and needsSecure.- Use
Laxfor login cookies. It blocks hiddenPOSTforms and still keeps people logged in when they follow a link. - Never change data on a
GETrequest, becauseLaxlets those through. - For important actions, add a CSRF token or check the
Originheader too.