Cookie Security

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…

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

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
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:

Set-Cookie: __Host-sessionId=abc123; Secure; HttpOnly; Path=/

⚠️ The flags people forget most

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.

What you will learn