okay small quiz, no googling. your page is on https://shop.example.com and the server sends these Set-Cookie lines. which ones does the browser keep?
1. sid=abc; Path=/; Secure; HttpOnly; SameSite=Lax
2. pref=1; SameSite=None
3. cart=1; Domain=example.com; Secure
4. __Host-sid=1; Secure; Path=/; Domain=example.com
5. all=1; Domain=com
6. theme=dark; Max-Age=2592000
7. theme=; Max-Age=0
and one bonus, sid=abc; Secure but this time the response came over plain http.
take a minute, answers are below the dots so you don't see them by accident
.
.
.
ok so
1 is kept. for shop.example.com only though, and only till the browser closes because there's no Expires or Max-Age on it. no Domain means exact host, not www, not api, nothing else.
2 is dropped. the whole cookie, not just the SameSite part. SameSite=None without Secure and current browsers just throw it away. and the browser shows no error to the user anywhere, the login simply doesn't stick and you sit there checking your backend for an hour. this is the one i'd check first every time
3 kept, and now for example.com and all its subdomains. people add Domain thinking it makes the cookie stricter, it's actually the opposite, Domain widens it
4 dropped. a __Host- name needs Secure, Path=/ and no Domain, all three. this one has a Domain so it's gone. that's the whole point of the prefix btw, a subdomain or an http page can't overwrite it, so it's really nice for session cookies
5 dropped, com is a public suffix. you can't set one cookie for every .com site, obviously
6 kept for 30 days, 2592000 seconds is 30 days. side note, if you ask for 5 years you won't get it, browsers cap it at 400 days (about 13 months) whatever Max-Age or Expires says
7 not kept, Max-Age=0 means already expired. that's literally how logout deletes a cookie. and if a line has both Expires and Max-Age, Max-Age wins
bonus, rejected. a Secure cookie set over http is not stored
so how many did you get? be honest
i built a Set-Cookie analyzer on my site for exactly this, you paste the lines plus the page address and it tells you per cookie what's kept, what's ignored and why something got dropped. when i was building it i ran the parser against the http-state test suite (the one written for browsers) and it passes all 218 enabled cases. i built this one so ya i'm biased, but paste your own login cookie in once and see: https://theomnikit.com/dev/set-cookie-analyzer?utm_source=devto&utm_medium=article&utm_campaign=devto-set-cookie-quiz
(AI helped me tidy this post, the examples and rules are from the page and RFC 6265 / the 6265bis draft.)
Top comments (0)