Cookies in depth: every Set-Cookie attribute and when to use it
What Domain, Path, Expires, Max-Age, Secure, HttpOnly, SameSite and Partitioned really do, the __Host- prefix, size limits, and where third-party cookies stand in each browser today.
Comes up in: Web fundamentals, Recruiter screen
Learn#
Aditya’s banking demo stores the session token in a cookie. A cross-site scripting bug lets an attacker’s script read document.cookie and send the token away. One attribute would have stopped it.
A cookie is a small name and value that the browser keeps for a site. It sends it back in the Cookie header. The server controls it with attributes:
Set-Cookie: __Host-session=8f2c; Path=/; Max-Age=604800; Secure; HttpOnly; SameSite=LaxMax-Age or Expires keeps it after the browser closes. Without them, it is a session cookie. Secure sends it only over HTTPS. HttpOnly hides it from JavaScript, which stops scripts from stealing it. SameSite controls whether it goes with requests started by other sites. Strict means never. Lax means only when the user follows a link to you. None means always, and it requires Secure.
A first-party cookie belongs to the site in the address bar; a third-party cookie belongs to another site embedded in the page, such as an ad frame.
Network
- POST /login → 200
- Set-Cookie: __Host-session=…; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age=604800
Console
empty
Login succeeds. The server sends Set-Cookie with __Host-, Secure, HttpOnly and SameSite=Lax.
Read the steps as text
- Login succeeds. The server sends Set-Cookie with __Host-, Secure, HttpOnly and SameSite=Lax.
- Your own pages send the cookie with every request to this exact host.
- A script injected into the page tries document.cookie. HttpOnly hides the session from JavaScript.
- Another site auto-submits a form POST to your /transfer. SameSite=Lax keeps the cookie off that cross-site POST.
Predict#
Predict first
In Chrome, a cookie is set with no SameSite attribute. Another site submits a form POST to yours. Is the cookie sent?
Chromium-based browsers default to Lax since Chrome 80; Firefox only behind a setting, and Safari does not. So the answer depends on the browser, and you should always set SameSite yourself.
Interview angle#
Say it like this — about 30 seconds
“HttpOnly hides a cookie from JavaScript, so an XSS bug cannot read it. Secure sends it only over HTTPS. SameSite controls cross-site sending: Strict never, Lax only on top-level navigations, None always but only with Secure. Chromium treats a missing SameSite as Lax, other browsers may not, so I set it explicitly. For sessions I use the __Host- prefix, which forces Secure, Path=/ and no Domain. Third-party cookies are blocked or partitioned in Safari and Firefox; Chrome still allows them.”
Follow-up What do HttpOnly, Secure and SameSite do?
Follow-up What does the Domain attribute change?
Follow-up What is CHIPS?
Practice#
Guided
Write a Set-Cookie builder that refuses unsafe combinations, the rules browsers enforce.
Done when
- Attributes appear in the given order, only when set.
__Host-cookies must be Secure, havePath=/and no Domain.SameSite=NoneandPartitionedboth require Secure.
Remember#
Try to answer each card before you open it. Then add them to your reviews.
What does HttpOnly do?
Hides the cookie from JavaScript (document.cookie).
SameSite=Lax: when is the cookie sent cross-site?
Only on top-level navigations such as following a link (safe methods).
What does the __Host- prefix require?
Secure, Path=/ and no Domain attribute.
What does SameSite=None require?
The Secure attribute.
Go deeper
Four layers. Stop at the one your interview needs; each ends with a quick check.
The core ideaEvery round
Max-Age wins over Expires if both are set. Chrome caps lifetimes at 400 days. Path limits which URLs get the cookie. It is not a security wall: other pages on the same origin can still read it.
Check yourself: What is a session cookie?
A cookie with no Expires or Max-Age; it is deleted when the browser session ends.
How it works underneathSenior rounds
__Host-… is accepted only if it is Secure, has Path=/ and no Domain, so a subdomain or plain-HTTP page cannot plant or overwrite it. __Secure- only requires Secure Source: MDN: Set-Cookie.Check yourself: Why use __Host- for a session cookie?
It cannot be set by subdomains or over HTTP, so it cannot be overwritten that way.
Edge cases and trapsWhere people slip
Partitioned (CHIPS) stores a third-party cookie per top-level site: Chrome 114, Firefox 141, Safari 26.2.Check yourself: About how large can one cookie be?
About 4 KB for name plus value.
In productionStaff-level answers
Check yourself: Does Chrome block third-party cookies by default today?
No. Chrome allows them by default; Safari blocks them and Firefox partitions them.
Finished the five steps? Mark it, and its cards join your reviews.
Last checked 2026-11-01. Spot a mistake? Use “Report an error” below — fixes are listed on the corrections page.
Interview questions this answers
- What do
HttpOnly,SecureandSameSitedo?