You're on free cafe Wi-Fi, you log in to a site, then keep scrolling. Someone at the next table can watch the same network. Can they see your login ticket (your session ID)? The answer isn't in your password. It's in a few small words the server attaches to the cookie.
A cookie is a small piece of data the browser stores and sends back to the server on every request. Picture an entry wristband at an event: the staff put it on your wrist, and you show it each time you come in. The wristband is the cookie, the staff are the server, and the one wearing the wristband is the browser. A cookie flag is a rule printed on that wristband, for example when and through which gate it may be used. Without the right flags, a cookie is easy to steal or gets sent to the wrong place.
Cookie: sent on every request
The server sets a cookie with the Set-Cookie header, and the browser sends it back on every request to that site. What the cookie carries should be only a ticket number (a session ID), not important data. The three flags below decide how safe that cookie is.
HttpOnly: JavaScript cannot read it
The HttpOnly flag makes the cookie unreadable by JavaScript through document.cookie (the way JavaScript reads a page's cookies). The point: if your site is hit by XSS, the attacker's script still cannot steal the session ID.
Set-Cookie: sessionId=abc123; HttpOnly
- Without HttpOnly,
document.cookiecan readsessionId, and one XSS gap is enough to steal it. - With HttpOnly,
document.cookiedoes not showsessionIdat all. So the flag that stops JavaScript from reading a cookie is HttpOnly.
Secure: only over HTTPS
The Secure flag makes the cookie only be sent over an HTTPS connection. On a plain http:// connection the cookie is not sent, so someone listening on the network cannot catch it. The flag that makes a cookie go only over HTTPS is Secure.
Set-Cookie: sessionId=abc123; HttpOnly; Secure
SameSite: when the cookie is sent cross-site
The SameSite flag controls whether the cookie is sent when the request comes from another site (cross-site). This flag is one layer of protection against CSRF, an attack that uses the victim's cookie through a request from another site (covered in the CSRF Attack lesson). Before you look at the table, guess: you click a link on another site that goes to your bank's site. Is your bank cookie with SameSite=Lax sent along or not?
It has three values. Assume the connection is already HTTPS:
| Request type | Strict | Lax | None |
|---|---|---|---|
| From your own site (same-site) | sent | sent | sent |
| Click a link from another site (top-level GET navigation) | no | sent | sent |
| POST or fetch from another site | no | no | sent |
- Strict is the strictest: the cookie is never sent for a cross-site request, not even when you click a link from another site. The strictest SameSite value is Strict.
- Lax: the cookie is sent for a top-level GET navigation, which is when the browser moves to a page of your site because you clicked a link. It is not sent for a cross-site POST or fetch (a request sent by JavaScript). So the answer to the guess: with
SameSite=Lax, clicking a link from another site still sends the cookie, because that is a top-level GET navigation. Lax is the default in Chrome and Edge whenSameSiteis not written. Firefox and Safari do not use it as the default, so always writeSameSiteyourself. - None: the cookie is always sent, including on a cross-site POST. For that reason
SameSite=Nonemust come withSecure. WithoutSecure, the browser rejects the cookie.
Set-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Lax
Cookie prefixes
Modern browsers support prefixes (the first part of a name) in cookie names for extra safety:
__Secure-: the cookie must have theSecureflag.__Host-: must beSecure, have noDomain(the attribute that shares the cookie with subdomains), and havePath=/(valid for the whole site). This locks the cookie to one host (exactly the same site name, with no subdomains).
Set-Cookie: __Host-sessionId=abc123; Secure; HttpOnly; Path=/
⚠️ The flags people forget most
- A session cookie without HttpOnly. The moment there is XSS, the session ID can be stolen. Set HttpOnly on sensitive cookies.
- Forgetting Secure in production. Production is the server that real users use. Without Secure, the cookie can leak over an http connection. Always set Secure in production.
- Using SameSite=None without Secure. The browser will quietly reject the cookie. None always pairs with Secure.
- Storing sensitive data directly in the cookie. Store only the session ID, with the real data on the server.
TL;DR: A cookie (a small piece of data the browser stores) is sent to the server on every request, so its flags (rules attached to the cookie) decide its safety. HttpOnly stops JavaScript from reading the cookie, so it cannot be stolen through XSS (the attacker's JavaScript running on your page). Secure makes the cookie go only over HTTPS. SameSite controls whether the cookie is sent when a request comes from another site: Strict never sends it, Lax sends it only for a link click (top-level GET navigation), and None always sends it but must be Secure. For a strong session cookie: HttpOnly; Secure; SameSite=Lax (or Strict), and consider the __Host- prefix (a special start for the cookie name), which locks the cookie to one site name.
⚡ HttpOnly: JavaScript can't read it
Write:
Set-Cookie: sessionId=abc123; HttpOnly
Meaning: The server sets the cookie sessionId with the HttpOnly flag (rule). Now document.cookie, the way JavaScript reads cookies, doesn't show sessionId at all. If your site is hit by XSS (the attacker's JavaScript running on your page), the attacker's script still can't steal the session ID.
⚡ Secure: only over HTTPS
Write:
Set-Cookie: sessionId=abc123; HttpOnly; Secure
Meaning: With Secure, the cookie is only sent over HTTPS. On a plain http:// connection the cookie isn't sent, so someone listening on the network can't catch it.
⚡ SameSite: when it's sent cross-site
Write:
Set-Cookie: sessionId=abc123; HttpOnly; Secure; SameSite=Lax
Guess first: SameSite=Lax holds the cookie back on some requests from other sites. You click a link on another site that goes to your bank's site. Is your bank cookie with SameSite=Lax sent along?
It is. Clicking a link is a top-level GET navigation (the browser moves to a new page), and Lax sends the cookie for that. What goes without the cookie is a POST or fetch from another site.
Meaning: Strict is the strictest: the cookie is never sent cross-site. None: the cookie is always sent, and it must pair with Secure.
⚡ It only holds a ticket number
Meaning: The cookie only carries the session ID (the login ticket number), and the real data is on the server. For a strong session cookie: HttpOnly; Secure; SameSite=Lax, and consider the __Host- prefix, which locks the cookie to one exact site name.