Secure, HttpOnly, SameSite: Cookie Flags That Protect Sessions

The session cookie is often the most valuable thing your site hands to a browser. Whoever holds it is, as far as your server is concerned, the signed-in user. Yet it’s common to find session cookies set with nothing but a name and a value, relying on browser defaults that differ between browsers and have changed over the years.
Three attributes do most of the work: Secure, HttpOnly and SameSite. This article explains
what each one protects against, which values a session cookie should use, and a few less familiar
options, cookie prefixes and the Domain attribute, that decide how far a cookie travels.
The cookie you want, up front
For a typical session cookie on a site served entirely over HTTPS:
Set-Cookie: __Host-session=8f3a…; Path=/; Secure; HttpOnly; SameSite=Lax
The rest of this article explains each part, and when you’d change it.
Secure: HTTPS only
A cookie with Secure is only sent over HTTPS connections, and modern browsers don’t let plain-HTTP
pages set or overwrite a Secure cookie.
Without it, a session cookie is sent on any plain-HTTP request to your domain: a typed
http:// address before the redirect, an old link, an image tag someone inserted. On an untrusted
network, that’s enough to copy the session.
There’s no good reason to leave Secure off a session cookie on a site that runs on HTTPS. Pair it
with HSTS so browsers stop making plain-HTTP requests to your domain in the first
place.
HttpOnly: out of reach of scripts
HttpOnly hides the cookie from JavaScript: it doesn’t appear in document.cookie, and scripts
can’t read or change it. The browser still sends it with requests as usual.
This limits the damage of cross-site scripting. If an attacker manages to run script on your page, they can still act as the user while the page is open, but they can’t simply read the session token and use it from elsewhere later.
Set HttpOnly on every cookie your front-end code doesn’t need to read. That covers session
cookies, remember-me tokens and most authentication cookies. Cookies that front-end code reads by
design, such as a theme preference or a CSRF token that a script copies into a request header, can
stay readable, as long as they don’t grant access on their own.
SameSite: when cookies go along cross-site
SameSite controls whether the browser attaches the cookie to requests that start from another
site: a form on another domain that posts to yours, an image or iframe pointing at your URLs, or a
link the user clicks on another site.
Strict: the cookie is only sent on requests that start from your own site. A user following a link to your site from an email or a search result arrives without the cookie, as if signed out, until the next navigation.Lax: the cookie is sent on top-level navigations from other sites, such as following a link, as long as the method is safe (GET). It isn’t sent on cross-sitePOSTrequests, or when your pages are loaded in frames or as images by other sites.None: the cookie is sent on every cross-site request. Browsers requireSecurewithSameSite=Noneand reject the cookie otherwise.
Lax is the right choice for most session cookies. Users who follow a link to your site arrive
signed in, while the classic cross-site request forgery pattern, a hidden form on another site that
posts to yours, no longer carries the session.
Use Strict for cookies that only matter for sensitive actions, such as a separate cookie that
authorizes account changes. Use None only when your site really is embedded on other domains and
needs its cookies there, and expect browsers to restrict such third-party cookies
more over time.
Two caveats:
- Don’t rely on defaults. Chromium-based browsers treat a cookie without
SameSiteasLax, but not every browser does. Set the attribute explicitly. SameSiteisn’t a complete CSRF defense.Laxstill sends cookies on cross-siteGETnavigations, so any action that changes state through aGETrequest remains exposed. Keep state-changing actions onPOST, and keep CSRF tokens or anOrigincheck for them. “Same site” also means the same registrable domain: a compromised subdomain counts as same-site.
Cookie prefixes: rules the browser enforces
Attributes are set by the server, but nothing in the cookie itself tells the browser that they’re required. A cookie prefix does. Browsers enforce two of them:
__Secure-: the cookie must be set withSecurefrom an HTTPS page.__Host-: the cookie must be set withSecurefrom an HTTPS page, must havePath=/, and must not have aDomainattribute. That locks it to the exact host that set it.
A browser refuses to store a prefixed cookie that doesn’t meet the rules. The benefit is that a
different part of your domain, for instance an insecure subdomain or plain-HTTP traffic, can’t
overwrite or plant a cookie with the same name. For session cookies, __Host- is the strongest
option and costs nothing but a rename.
Domain and Path: how far a cookie travels
The Domain attribute is often set out of habit, but it widens the scope of a cookie rather than
restricting it:
- No
Domainattribute: the cookie goes only to the host that set it (www.example.com). Domain=example.com: the cookie also goes to every subdomain, including the marketing tool ongo.example.comand the forgotten staging host.
Leave Domain off unless several subdomains genuinely need to share the cookie, as with
single sign-on across hosts. Path is not a security boundary: pages under another path on the same
host can still read cookies in other ways, so use separate hosts if you need real separation.
Lifetime
A cookie with neither Expires nor Max-Age is a session cookie in the browser sense: it’s meant
to be discarded when the browser session ends, although browsers that restore sessions may keep it.
For authentication, the browser lifetime matters less than the server side. Expire sessions on the
server after inactivity and after an absolute maximum, issue a new session ID when the user signs
in (to prevent session fixation), and invalidate it on sign-out. Current browsers also cap cookie
lifetimes at 400 days, so very long Expires dates are shortened anyway.
Setting the flags in common stacks
PHP sessions, in php.ini or before session_start():
session.cookie_secure = 1
session.cookie_httponly = 1
session.cookie_samesite = "Lax"
session.use_strict_mode = 1
session.name = "__Host-PHPSESSID"
Express with express-session:
app.set('trust proxy', 1); // when behind a reverse proxy that terminates TLS
app.use(
session({
name: '__Host-session',
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { secure: true, httpOnly: true, sameSite: 'lax', path: '/' },
}),
);
Without trust proxy, an Express app behind a TLS-terminating proxy thinks requests are plain HTTP
and won’t send a Secure cookie at all.
nginx in front of an application you can’t change:
proxy_cookie_flags ~ secure httponly samesite=lax;
The proxy_cookie_flags directive (nginx 1.19.3 and later) adds flags to cookies set by the
proxied application. Fixing the application is better, but this is a useful stopgap.
Checking your own cookies
Open your browser’s developer tools, go to the storage or application panel, and look at the
cookies after signing in. Each attribute has its own column. Compare the raw Set-Cookie headers
in the network tab too, because the login response, a redirect and an API endpoint may each set
cookies differently.
Our cookie security guide lists what a passive check reports for each flag, and MDN’s Set-Cookie reference documents every attribute in detail.
The short version: Secure and HttpOnly on every session cookie, SameSite=Lax unless you have a
reason for something else, the __Host- prefix where you can use it, and no Domain attribute you
don’t need. That’s one line of configuration in most frameworks, and it closes off several of the
easiest ways to lose a session.