Sessions done right: secure cookie settings explained
Day 10 of our web security series: every setting on a login cookie explained, and the server-side habits that keep a logged-in user safe.
On Day 3 we met the session cookie: the small random ticket your browser shows the server on every request, so you don’t have to type your password each time. We said then that the session ID is the key to the account.
Yesterday we made the login page harder to guess through. But once someone is logged in, an attacker doesn’t need the password any more. They only need that ticket. Today is about protecting it properly, from the moment it is created to the moment it is thrown away.
Anatomy of a well-set login cookie
When you log in, the server sends a header like this:
Set-Cookie: __Host-session=k7Q2vX9...; Path=/; Secure; HttpOnly; SameSite=Lax
Every part of that line has a job. Let’s go through them.
Secure
The cookie is only ever sent over HTTPS. Without it, a single visit to an http:// address (an old bookmark, a typed URL) could send the session ID across the network in plain text, where anyone on the same Wi-Fi might read it. With HTTPS everywhere and HSTS from Day 4, Secure closes the last gap.
HttpOnly
JavaScript running in the page cannot read the cookie. The browser still sends it with requests, but document.cookie won’t show it. This matters because if an attacker ever manages to run script on your site (cross-site scripting, coming on Day 19), the first thing they’ll try is to copy the session cookie. HttpOnly takes that prize off the table.
SameSite
This controls whether the browser sends the cookie when the request comes from another site:
Strict: never sent on cross-site requests, not even when you click a link from another site. Safest, but users arriving from an email link may appear logged out.Lax: sent when you follow a normal link to the site, but not on cross-site form posts or background requests. A good default for most login cookies.None: always sent, which is only needed for genuine cross-site embeds. It must be combined withSecure.
SameSite is a strong layer against cross-site request forgery (CSRF), the attack where another site makes your browser submit a request with your cookies attached. It isn’t the only layer we’ll want, but it removes most of the risk cheaply.
Domain: usually leave it out
If you set Domain=example.com, the cookie is also sent to every subdomain: blog.example.com, old-test.example.com, and anything else that ever exists there. If one of those is hosted somewhere less secure, or forgotten and taken over (remember dangling DNS records from Day 5), it can receive your users’ sessions.
Leaving Domain out makes the cookie host-only: sent to exactly the site that set it, and nowhere else.
Path
Path=/ means the cookie is sent for the whole site. It’s tempting to think a narrower path adds protection, but it doesn’t: other pages on the same origin can still reach it. Treat Path as tidiness, not security.
The __Host- prefix
Naming a cookie __Host-something is a promise the browser enforces: it will only accept the cookie if it has Secure, Path=/ and no Domain. That means a subdomain or a plain-HTTP page can’t plant or overwrite it. It’s one of the cheapest upgrades available, and most frameworks let you rename the session cookie with a single setting. (A weaker __Secure- prefix only requires Secure.)
Expires and Max-Age
Without either, the cookie is a session cookie in the browser sense and is meant to disappear when the browser closes (though many browsers restore it). With them, it persists. For a login cookie, keep the lifetime short, and let the server decide when a session really ends.
The server side matters more
Cookie settings protect the ticket in transit and in the browser. The rules for the ticket itself live on the server.
Make session IDs long and random. They should come from a secure random generator, long enough that guessing one is hopeless. Never build them from usernames, timestamps or counters. Your framework’s built-in session handling already does this, so use it rather than inventing your own.
Issue a new session ID at login. If the ID from before login carries on afterwards, an attacker who planted or saw that earlier ID gets a logged-in session for free. This is called session fixation, and the fix is simple: throw away the old ID and create a fresh one whenever someone logs in, or gains more privileges, such as entering an admin area.
Use two timeouts. An idle timeout ends a session after a period with no activity, for example 30 minutes for a bank, longer for a low-risk site. An absolute timeout ends it after a fixed time no matter what, so a stolen session can’t be kept alive forever.
Log out for real. Logging out must delete the session on the server, not just clear the cookie in the browser. Otherwise a copied cookie keeps working after the user thinks they’ve left.
Offer “log out of all devices”, and do it automatically when the password changes or is reset. If someone changes their password because they suspect a break-in, every existing session should end with it.
Keep session IDs out of URLs. Addresses end up in browser history, server logs, analytics and the Referer header sent to other sites. A session ID belongs in a cookie, nowhere else.
Don’t trust what the cookie says about the user. A cookie that contains role=admin or a user ID in plain text can simply be edited. Store the real details on the server and keep only the random ID in the cookie. If you use signed tokens instead, make sure the signature is always checked, and remember they are hard to revoke before they expire.
Why it matters for security
A stolen session is a full account takeover that skips the password, the rate limits and often two-factor login too, because all of those happen before the session exists. Session cookies are stolen through malware on the user’s device, script injection, unencrypted connections and leaky subdomains. Good cookie settings shut most of those routes. Good server habits limit the damage of the rest: a stolen session that expires in 30 minutes, or ends the moment the user changes their password, is worth far less to an attacker.
Check your own site
You can see your cookie settings in a minute:
- Log in to your site and press F12 to open developer tools.
- Go to Application (Chrome, Edge) or Storage (Firefox), then Cookies.
- Find the session cookie and check the columns: Secure and HttpOnly ticked, SameSite set to
LaxorStrict, and Domain showing your exact host.
Logout is harder to check from the browser alone, because the browser deletes its copy of the cookie either way. Ask whoever built the site to confirm that logging out deletes the session on the server, and that changing the password ends sessions on other devices.
What you can do today
If you use websites:
- Log out of banking, email and work accounts when you’re done, especially on shared or public computers.
- Use “log out of all devices” after you change a password or lose a phone or laptop.
- Be wary of browser extensions you don’t need. An extension with access to all sites can read cookies that ordinary pages can’t.
If you run a website:
- Set
Secure,HttpOnlyandSameSite=Lax(orStrict) on your session cookie, and leaveDomainout. - Rename it with the
__Host-prefix if your framework allows. - Regenerate the session ID at login and at any privilege change.
- Add both an idle and an absolute timeout, destroy sessions on the server at logout, and end all sessions when a password changes.
- Read the OWASP Session Management Cheat Sheet and MDN’s guide to cookies for the details.
Coming up next
On Day 11 we’ll look at the “Forgot password?” link: how password reset flows work, and how to build one that doesn’t become a back door into every account.