Header guide · Set-Cookie

Secure cookies: Secure, HttpOnly and SameSite explained

What Secure, HttpOnly and SameSite do on a Set-Cookie header, how WebSec0 scores cookies, and tested fixes in the app or on Nginx, Apache and Caddy.

  • By
  • Updated
  • 6 min read
  • Tested on nginx 1.28 to 1.31, Apache httpd 2.4.58 to 2.4.69 and Caddy 2.9 to 2.11
A session cookie set with Secure, HttpOnly and SameSite=Lax: Secure keeps it off plain http:// requests, HttpOnly hides it from document.cookie, and SameSite=Lax keeps it off POST requests from other sites.SET-COOKIEsession_id=7f3a…; Secure; HttpOnly; SameSite=LaxATTRIBUTEKEPT AWAY FROMSecurehttp:// requestsHttpOnlydocument.cookieSameSite=Laxcross-site POST
In short4 points
  • Give every cookie Secure and a SameSite value, and add HttpOnly to every cookie that scripts do not need to read, starting with the session cookie.
  • SameSite=Lax suits most cookies. SameSite=None is for cookies used inside other sites, and requires Secure.
  • WebSec0 grades the cookies set on your home page: −5 per cookie without Secure (−10 at most), −3 per cookie without SameSite, −3 per session cookie without HttpOnly.
  • Fix cookies where they are created, in the application. As a fallback, Nginx's proxy_cookie_flags, Apache's Header edit and Caddy's header_down add missing attributes at the proxy.

A cookie is secure when its Set-Cookie header carries three attributes: Secure, so it never travels over plain HTTP; HttpOnly, so JavaScript cannot read it; and SameSite, so other sites cannot make the browser send it with their requests. Without them, a session cookie can be read on an open Wi-Fi network, stolen by an XSS bug, or used in a cross-site request forgery.

HTTP
Set-Cookie: session_id=7f3a…; Path=/; Secure; HttpOnly; SameSite=Lax

The attributes cost nothing on most cookies. The exceptions are a cookie that your own JavaScript must read, which cannot be HttpOnly, and a cookie used inside other sites, which needs SameSite=None.

What each attribute does

Attribute What it does When to leave it out
Secure The browser sends the cookie only over HTTPS, and an http:// page cannot set it Never on a site served over HTTPS
HttpOnly document.cookie cannot read it; the browser still sends it with fetch() and forms Cookies your scripts read, such as a CSRF token or a theme setting
SameSite=Lax Sent when the user follows a link from another site, not on that site’s POST forms, frames or scripted requests When another site must embed your page with the user logged in
SameSite=Strict Never sent on a request that starts on another site, links included When users should arrive logged out from external links
SameSite=None Sent on every request; MDN states it must come with Secure Unless the cookie works inside other sites

MDN notes that some browsers treat a cookie without SameSite as Lax, with a more permissive variant for recently set cookies. Others do not, so set the value explicitly. HttpOnly limits what an XSS bug can steal, but a script running in your page can still send requests with the cookie: a Content-Security-Policy remains the main defence against XSS.

How WebSec0 grades cookies

Cookies are not one of the six graded headers. WebSec0 applies penalties to the headers score instead, from the cookies set on the response to your home page, after following redirects on the same host:

Problem Penalty
Cookie without Secure −5 per cookie, −10 at most
Cookie without a SameSite attribute −3 per cookie
Session cookie without HttpOnly −3 per cookie

Any SameSite value counts, None included. A cookie counts as a session cookie when its name contains session, auth, token, sid, jwt or csrf, in any case: PHPSESSID, connect.sid and XSRF-TOKEN all match. In the report, a cookie without Secure is shown as failing, and one that only lacks SameSite or HttpOnly as a warning.

To see the effect, we ran the scanner’s own scoring code on a response with all six security headers passing and these five cookies:

HTTP
Set-Cookie: session_id=abc; Path=/
Set-Cookie: theme=dark; Path=/
Set-Cookie: XSRF-TOKEN=x1; Path=/; Secure; SameSite=Lax
Set-Cookie: embed_pref=1; Path=/; SameSite=None
Set-Cookie: auth=ok; Path=/; Secure; HttpOnly; SameSite=Strict

The headers score dropped from 100 to 78 (B): −10 for the three cookies without Secure (capped), −6 for the two without SameSite, and −6 for session_id and XSRF-TOKEN without HttpOnly. With the Apache or Caddy fix below, it rose to 97 (A+). The remaining −3 is XSRF-TOKEN, which the application’s JavaScript has to read, so it stays without HttpOnly by design.

Fix cookies in the application

Cookies are created by your application or framework, so that is the right place to set their attributes: the application knows which cookies scripts read and which must work inside other sites. PHP is a common example. In our test, PHP 8.3 and 8.4 sent the session cookie with no attributes at all:

HTTP
Set-Cookie: PHPSESSID=2f6825df80a108ffa47e154855127e44; path=/

The scoring code gave that response 89 (A): −5, −3 and −3, since PHPSESSID contains sid. Three settings in php.ini fix it:

ini
session.cookie_secure = 1
session.cookie_httponly = 1
session.cookie_samesite = Lax

Our test then returned PHPSESSID=…; path=/; secure; HttpOnly; SameSite=Lax, and the scoring code gave 100. Most frameworks have equivalent settings for their session and CSRF cookies; search their documentation for “session cookie secure”.

Add the attributes at the proxy

When you cannot change the application, the server in front of it can add the missing attributes. These snippets were tested with the five cookies above, on a page and on a 302 redirect, which often sets the session cookie after a login.

Nginx

proxy_cookie_flags (nginx 1.19.3 and later) changes the cookies of a proxied application. The first directive whose cookie name matches applies:

Nginx
location / {
    proxy_pass http://app;
    proxy_cookie_flags session_id secure httponly samesite=lax;
    proxy_cookie_flags ~ secure;
}

On nginx 1.28, 1.30 and 1.31, this added Secure, HttpOnly and SameSite=Lax to session_id, and Secure to every other cookie, keeping their own SameSite. theme had none and still had none, which costs 3 points: the scoring code gave 94 (A). Nginx cannot add SameSite only where it is missing, so give such cookies their own proxy_cookie_flags line with the value they need, or set it in the application. Be careful with samesite= on a catch-all rule: with proxy_cookie_flags ~ secure httponly samesite=lax;, our test turned the application’s SameSite=Strict and SameSite=None into SameSite=Lax, and made XSRF-TOKEN unreadable by scripts.

Apache

Header edit rewrites each Set-Cookie header with a regular expression. These lines add an attribute only when it is missing, and skip HttpOnly on XSRF-TOKEN:

Apache
<VirtualHost *:443>
    # ...
    ProxyPass "/" "http://127.0.0.1:8080/"
    Header edit Set-Cookie "(?i)^((?!.*;\s*secure(;|$)).*)$" "$1; Secure"
    Header edit Set-Cookie "(?i)^((?!.*;\s*samesite=).*)$" "$1; SameSite=Lax"
    Header edit Set-Cookie "(?i)^((?!xsrf-token=)(?!.*;\s*httponly(;|$)).*)$" "$1; HttpOnly"
</VirtualHost>

On httpd 2.4.58, 2.4.62 and 2.4.69, every cookie came out with Secure and a SameSite, the application’s Strict and None were kept, and only XSRF-TOKEN stayed readable by scripts. Use Header edit, not Header always edit: with mod_proxy_http, the application’s cookies are in the default table, and in our test Header always edit left them unchanged.

Caddy

Caddy’s regular expressions have no lookahead, so each attribute takes two header_down lines: one appends it, the next removes the copy when the cookie already had it. They run in the order written:

Caddyfile
example.com {
	reverse_proxy 127.0.0.1:8080 {
		header_down Set-Cookie "$" "; Secure"
		header_down Set-Cookie "(?i)(;\s*secure\b.*); Secure$" "${1}"
		header_down Set-Cookie "$" "; SameSite=Lax"
		header_down Set-Cookie "(?i)(;\s*samesite=.*); SameSite=Lax$" "${1}"
		header_down Set-Cookie "^(session_id=.*)$" "${1}; HttpOnly"
		header_down Set-Cookie "(?i)(;\s*httponly\b.*); HttpOnly$" "${1}"
	}
}

On Caddy 2.9, 2.10 and 2.11, every cookie came out with Secure and one SameSite, the application’s Strict and None were kept, and session_id got HttpOnly. The last pair targets session_id by name; adapt it to your session cookie.

Traefik

The headers middleware of Traefik sets or removes whole headers; it has no option to edit the attributes of each cookie. Fix the cookies in the application.

A cookie whose name starts with __Host- is accepted by the browser only if it has Secure, Path=/ and no Domain attribute, and was set over HTTPS:

HTTP
Set-Cookie: __Host-session=7f3a…; Path=/; Secure; HttpOnly; SameSite=Lax

Without a Domain attribute, the cookie belongs to your exact host, so a subdomain cannot set a cookie under that name for it. __Secure- only requires Secure and HTTPS. MDN warns that browsers without prefix support do not enforce these rules. WebSec0 does not score prefixes; they are a hardening step once the three attributes are in place.

Verify it

List the cookies your home page sets, with their attributes. Use your own URL:

Shell
curl -sI https://example.com/ | grep -i "^set-cookie"

Each line should contain Secure and a SameSite, and session cookies HttpOnly. Repeat for the login page and the redirect after login, which WebSec0 does not grade but which usually set the session cookie. Then scan the site with WebSec0: the headers report lists each cookie with the attributes it found.

Common mistakes

Mistake What happens Fix
SameSite=None without Secure MDN states None requires Secure Add Secure, or use Lax.
HttpOnly on a cookie scripts must read The feature that reads it breaks, such as CSRF protection in a single-page app Leave HttpOnly off that cookie only.
A catch-all samesite= at the proxy The application’s Strict or None is replaced Set SameSite per cookie, or in the application.
Header always edit behind mod_proxy_http The application’s cookies are not changed Use Header edit.
Only the home page checked The login response sets the session cookie without attributes Check every response that sets a cookie.
Relying on HSTS alone A cookie without Secure is exposed before HSTS applies, and on subdomains it does not cover Add Secure as well as HSTS.

Questions

Frequently asked questions

What are the secure cookie attributes?

Secure, which keeps the cookie off plain HTTP; HttpOnly, which hides it from JavaScript; and SameSite, which controls whether it is sent on requests from other sites. A session cookie should have all three.

Which SameSite value should I use?

Lax for most cookies, including sessions: the cookie follows links from other sites but not their forms or embedded requests. Strict drops it even on links. None sends it everywhere and requires Secure.

Why does WebSec0 flag my CSRF or XSRF token cookie?

WebSec0 treats any cookie whose name contains session, auth, token, sid, jwt or csrf as a session cookie, and expects HttpOnly on it. A CSRF token that JavaScript must read cannot have it, so that −3 is expected; keep Secure and SameSite on it.

Does WebSec0 check every cookie on my site?

No. It reads the cookies set on the response to your home page, after following redirects on the same host. Cookies set after login or on other pages are not graded, but deserve the same attributes.

Can I add Secure and HttpOnly to cookies in Nginx?

Yes, for proxied applications, with proxy_cookie_flags (nginx 1.19.3 and later). Be careful with samesite=: it replaces the value the application set, including Strict and None.

Do I still need Secure if I use HSTS?

Yes. HSTS stops browsers from making plain HTTP requests to your domain once they know the policy. Secure protects the cookie even before that, and on any subdomain the policy does not cover.

References

Sources

  1. Set-Cookie headerMDN Web Docs
  2. Using HTTP cookiesMDN Web Docs
  3. Apache Module mod_headersApache HTTP Server Project

About the author

Portrait of Joshua Martinelle

Security engineer at Tenable

Security engineer at Tenable and web security researcher. He has disclosed more than 60 CVEs since 2020 and builds WebSec0.

Verify the change

Deployed it? Check it.

WebSec0 grades your TLS setup and security headers, including Set-Cookie, in a few seconds. Free, no account.

Scan your site