Skip to main content
Security guideReviewed

Cookie security: what each Set-Cookie attribute does, and how to choose them

Every cookie your server sets travels in a Set-Cookie header, and its attributes decide who can read the cookie, over what connection, to which hosts and paths, for how long and in which cross-site situations.

This guide explains each attribute in plain terms with the standard behind it, and comes with a Set-Cookie decision table for six kinds of cookie and a verification pack you run with Node.js alone.

Set-Cookie decision table and cookie verification pack

An illustration on invented hosts. Part 1 is a Set-Cookie decision table for six kinds of cookie a business site sends, with the choice and a sourced reason for every attribute and prefix, and the rules every header has to meet.

How to use it

Parts 2 to 4 are a Node.js lab with no installs: an invented site in a careless and a careful version, a checker that reads each Set-Cookie header from the login, logout, subdomain and cross-site flows and compares it with the table and the cookie draft's rules, and a regression test that fails on the careless version. Part 5 lists steps to confirm in your own browser what it stores and sends, Part 6 points the test at your own site, and Part 7 is a cookie register. Use it for learning and for checking the cookies your own site sets.

cookie-security.txt

Introduction5 lines

ILLUSTRATION ONLY. The hosts app.example.com, shop.example.com, widget.example.net and partner.example.org, every cookie and every cookie value below are invented for this page. The lab runs on your own machine and binds to 127.0.0.1 on a port the system picks. Its responses only set made-up cookies, which a scripted client reads and never stores. Use it for learning and for checking the cookies your own site sets. What this asset is, and what it is not. Part 1 is a decision table: for six kinds of cookie a business site sends, the choice for each attribute and prefix, with the reason and its source. Parts 2 to 4 are a lab. An invented site sends its cookies in a careless and a careful version, and a checker reads each Set-Cookie header that the login, logout, subdomain and cross-site flows send. For each header it reports, attribute by attribute, what is present, what the decision table expects for that cookie, and whether the cookie draft's prefix rules and other storage rules hold. The checker judges header text against written rules. It is not a browser: it stores no cookies and cannot show what any browser keeps or sends. Part 5 is how to see that in your own browser, Part 6 is how to point the test at your own site, and Part 7 is a cookie register to keep. Written and run on: Node.js 26.0.0, the only version it was run on. It uses node:http, node:crypto, node:test and node:assert, which ship with Node.js, so there is nothing to install. Every output below comes from that run; cookie values and timings will differ on your machine.

What each attribute narrows, and why the defaults are not the safe choice

Each attribute narrows one thing about a cookie.

Read together, they answer five questions.

  • Who can read it. HttpOnly forbids page script from reading the cookie (MDN Set-Cookie). Without it, script on the page can read and change it.
  • Over what connection. Secure limits the cookie to secure channels, typically HTTPS (RFC 6265 4.1.2.5).
  • To which hosts. Domain widens the cookie from the one host that set it to a whole domain and its subdomains (RFC 6265 4.1.2.3).
  • To which paths. Path limits the cookie to URLs at or below a path, as a matter of scope rather than security (RFC 6265 4.1.2.4).
  • For how long. Expires gives a date and Max-Age a number of seconds; with neither, the browser keeps the cookie until the current session is over, as the browser defines it (RFC 6265 4.1.2.1 and 4.1.2.2).
  • In which cross-site situations. SameSite decides whether the cookie goes with cross-site requests (cookie draft 4.1.2.7), and Partitioned asks the browser to store it separately for each top-level site your content is embedded in (MDN Set-Cookie; MDN CHIPS).

Why leaving attributes out is not the safe choice for a session cookie

Leaving an attribute out does not give you the cautious setting across the board. Two defaults are narrow. With no Domain the cookie goes only to the host that set it, and with no Path it is limited to the directory of the URL that set it (RFC 6265 4.1.2.3 and 4.1.2.4).

The others are wide. With no Secure the cookie travels over plain HTTP as well as HTTPS. With no HttpOnly, script can read it.

With no SameSite the result depends on the browser: MDN says some browsers use Lax as the default value, and the CSRF guide sets out how they differ.

The cookie draft puts the principle in its security considerations: by default, cookies provide neither confidentiality nor integrity against network attackers, even when used with HTTPS, so a cookie needs to state any protective attribute explicitly. Its example is a cookie set as a=b with no Secure attribute, which is therefore accessible on both secure and insecure connections, whatever connection it was created on (cookie draft 8.1).

RFC 6750, the OAuth bearer token standard, says bearer tokens must not be stored in cookies that can be sent in the clear, and calls that the default transmission mode for cookies.

A session cookie needs the same care: the OWASP Session Management Cheat Sheet says that once a session is authenticated, its ID is temporarily equivalent to the strongest authentication method the application uses.

So for a session cookie, every attribute that is wide by default has to be set on purpose, and the narrow defaults are worth keeping.

Secure: keeping the cookie off plain HTTP

RFC 6265 says a cookie with the Secure attribute is included in a request only if the request is transmitted over a secure channel, typically HTTPS, and the cookie draft keeps that wording.

MDN adds that sites on plain http: cannot set cookies with Secure, and that a Secure cookie is never sent with unsecured HTTP except on localhost, an exception its cookies guide says is not supported by Safari.

Serving the site over HTTPS only is not a substitute for the attribute. The OWASP Session Management Cheat Sheet explains that forcing a site to use only HTTPS, even with plain HTTP closed on the server, does not protect against session ID disclosure if the session cookie lacks Secure: the browser can be deceived into disclosing the session ID over an unencrypted HTTP connection, because an attacker who can intercept and manipulate the victim's traffic can inject an unencrypted HTTP reference to the site that forces the browser to submit the session ID in the clear.

The cookie draft's webmail example makes the same point: the browser includes the cookies in the redirected plain-HTTP request even if the server is not listening for HTTP connections, and the attacker can intercept and replay them.

It concludes that if a server does not set Secure, the protection provided by the secure channel will be largely moot (cookie draft 8.3).

What Secure does not do matters as much. RFC 6265 says it protects only the cookie's confidentiality: an active network attacker can overwrite Secure cookies from an insecure channel, disrupting their integrity. The cookie draft narrows that gap for browsers that follow it. A response over an insecure connection cannot set a Secure cookie, and cannot set a non-Secure cookie that would overlay an existing Secure cookie of the same name, which the draft describes as some mitigation against cookie-fixing attacks (cookie draft 5.7, steps 13 and 16).

MDN also warns not to assume Secure prevents all access: someone with access to the client's hard disk, or script on the page when HttpOnly is not set, can still read and change the cookie.

For whole-domain transport rules, HSTS is the companion control, and the Domain section below says when the sources require it.

HttpOnly: keeping the cookie away from page script

RFC 6265 says HttpOnly limits the cookie to HTTP requests: it tells the browser to leave the cookie out when giving cookies to non-HTTP APIs, such as a browser API that exposes cookies to scripts.

It is independent of Secure, so a cookie can carry both. What HttpOnly means when script is injected, and what it does not stop, is covered in the XSS guide.

The cookie draft gives it a second job. A browser that follows the draft ignores an HttpOnly cookie that script tries to create, and ignores a cookie from script that would replace a stored HttpOnly cookie with the same name, domain, host-only flag and path (cookie draft 5.7, steps 15 and 23). So HttpOnly also keeps page script from overwriting the cookie.

ASVS 5.0 requirement 3.3.4, at Level 2, asks for more than the flag. A cookie whose value is not meant for client-side script, such as a session token, must have HttpOnly set, and the same value must only reach the browser through the Set-Cookie header.

A session token that also comes back in a JSON body, or is written into the page, does not meet the requirement, whatever flags the cookie carries.

Leave HttpOnly off only for a cookie your own script has to read, such as a theme choice, and keep such a cookie free of anything sensitive. ASVS 5.0 requirement 14.3.3, at Level 2, asks that data in browser storage, cookies included, contains no sensitive data apart from session tokens.

SameSite in brief: which cross-site requests carry the cookie

SameSite controls whether a cookie goes with cross-site requests.

With Strict it goes only with same-site requests, with Lax it also goes with cross-site top-level navigations that use a safe method, and with None it goes with both (cookie draft 4.1.2.7 and 5.6.7.1). MDN says None also needs the Secure attribute, and a browser that follows the draft ignores a None cookie without it (cookie draft 5.7, step 19).

How SameSite behaves against forged requests, what counts as the same site, and how browsers treat a cookie with no SameSite value are the CSRF guide's subject.

Two rules from the standards are enough to choose a value. ASVS 5.0 requirement 3.3.2, at Level 2, asks that each cookie's SameSite value is set according to the purpose of the cookie. The OWASP Session Management Cheat Sheet says session cookies must set Strict, which it prefers, or Lax explicitly, and must not rely on the browser default, which it says varies across browsers and versions. The decision table applies both rules, cookie by cookie.

SameSite also limits where a cookie can be set, not only where it is sent. The cookie draft says cookies that assert Lax or Strict cannot be set in responses to cross-site subresource requests or cross-site nested navigations, though they can be set on any top-level navigation (cookie draft 4.1.2.7).

That is why a widget inside another site's page needs None. The draft says content intended for embedding in cross-site contexts will not have access to same-site cookies, and that cookies required in those situations should be marked None (cookie draft 8.8.3).

Domain: why leaving it out is narrower than setting it

The Domain attribute names the hosts a cookie goes to.

RFC 6265's example: with Domain=example.com, the browser includes the cookie in requests to example.com, www.example.com and www.corp.example.com. If the server leaves the attribute out, the browser returns the cookie only to the origin server. MDN calls that a host-only cookie and says it is more restrictive than setting the host name, because the cookie is not made available to subdomains of the host.

Setting Domain=app.example.com from app.example.com therefore widens the cookie to every host under app.example.com, while leaving it out keeps the cookie on app.example.com alone.

A server can name only its own domain or a parent of it. MDN's example: a response from api.example.com can set Domain=api.example.com or Domain=example.com, but not Domain=beta.api.example.com, Domain=other.example.com or Domain=com, because a public suffix is not allowed either, and cookies that break these rules are ignored.

A leading dot, as in .example.com, is ignored. RFC 6265 carries a warning that the cookie draft repeats: some existing user agents treat an absent Domain attribute as if it named the current host, and would send a cookie set by example.com to www.example.com as well. Part 5 of the asset shows how to see what your own browser does.

A wide Domain carries risk in both directions. A cookie set for example.com reaches every subdomain, and the OWASP Session Management Cheat Sheet says a too permissive Domain lets an attacker attack session IDs across hosts on the same domain: its example is that vulnerabilities in www.example.com might allow an attacker to get access to the session IDs from secure.example.com.

In the other direction, the cookie draft says cookies give no integrity guarantee between sibling domains. foo.site.example can set a cookie with Domain=site.example, possibly overwriting one bar.site.example set, and in the worst case bar.site.example will be unable to tell it from a cookie it set itself (cookie draft 8.6).

The cheat sheet recommends leaving Domain unset on session cookies and not mixing web applications of different security levels on the same domain.

When a cookie does need to reach several subdomains, as in row 5 of the decision table, set Domain to the parent you control on purpose, give the cookie a __Secure- name, keep anything that grants access out of it, and treat its value as untrusted input on every host that reads it.

WSTG-SESS-09, OWASP's session hijacking test, says full HSTS adoption is required when session cookies are issued with the Domain attribute set, and ASVS 5.0 requirement 3.4.1 asks, from Level 2, for an HSTS policy that applies to all subdomains.

Path: scope, not a security boundary

Path limits a cookie to part of a site.

The browser includes the cookie only when the path of the request matches the cookie's Path or is a subdirectory of it, with the slash as the directory separator (RFC 6265 4.1.2.4). MDN's example: with Path=/docs, the request paths /docs, /docs/, /docs/Web/ and /docs/Web/HTTP match, and /, /docsets and /fr/docs do not.

Leave Path out and the browser uses the directory of the request URL's path as the default (RFC 6265 4.1.2.4 and 5.1.4). That default is easy to miss. In the lab, cookies the careless version sets from /account/login without a Path get the path /account, so a sign-out that clears them with Path=/ does not match them.

Path is not a security measure. RFC 6265 says that although it seems useful for isolating cookies between paths on one host, the Path attribute cannot be relied upon for security. The cookie draft gives two reasons. Some user agents expose cookies through non-HTTP APIs such as document.cookie without isolating resources received from different paths, so a page on one path might be able to read cookies stored for another (cookie draft 8.5).

And the browser accepts an arbitrary Path in a Set-Cookie header, so a response for /foo/bar can set a cookie for /qux (cookie draft 8.6).

Its conclusion is that servers should not both run mutually distrusting services on different paths of the same host and use cookies to store security-sensitive information. MDN says the path attribute is not intended as a security measure.

Use Path to keep a cookie out of parts of a site that do not need it. WSTG-SESS-02, OWASP's cookie attributes test, warns that a Path set too loosely could leave an application open to attacks by other applications on the same server. Put applications that should not trust each other on separate hostnames instead: ASVS 5.0 requirement 3.5.4, at Level 2, asks for separate applications on different hostnames so they can rely on the same-origin policy and hostname-based restrictions on cookies.

A __Host- cookie has to use Path=/ in any case. Ports do not separate cookies either: the cookie draft says a cookie readable by a service on one port is also readable by a service on another port of the same server, and a cookie writable on one port is also writable on another (cookie draft 8.5).

Expires and Max-Age: how long the browser keeps a cookie

Expires gives the date and time a cookie expires, and Max-Age the number of seconds until it does.

Both state a maximum lifetime, not a promise: RFC 6265 says the browser is not required to keep the cookie that long, and that user agents often evict cookies due to memory pressure or privacy concerns. When a cookie has both, Max-Age takes precedence.

When it has neither, the browser keeps it until the current session is over, as the browser defines it (RFC 6265 4.1.2.1 and 4.1.2.2), and MDN calls it a session cookie.

Max-Age is the easier one to get right. An Expires date is read against the browser's clock, which may differ from the server's, and the cookie draft says servers must not depend on cookies being evicted exactly at the date and time of the server's clock (cookie draft 4.1.2.1).

MDN's cookies guide calls Max-Age less error-prone for that reason, and its Set-Cookie reference says Firefox and Chromium-based browsers adjust for clock difference using the response's Date header. RFC 6265 also notes that some existing user agents do not support Max-Age and ignore it.

Long lifetimes are capped. The cookie draft says a browser must limit the lifetime of a cookie set with Expires or Max-Age, that the limit should not be more than 400 days in the future, and that a longer value must be reduced to the limit (cookie draft 5.5).

It also asks servers to select reasonable expiration periods based on the purpose of the cookie rather than gratuitously long ones (cookie draft 7.4). The careless version of the lab's remember-this-device cookie asks for far more than 400 days, and the checker flags it.

A cookie's expiry is an instruction to the browser, not a session control. A copy of the value can be sent whatever the expiry says, so the server has to enforce its own limits; the session management guide covers timeouts.

Removing a cookie uses the same attributes. RFC 6265's example removes a cookie by sending it again with an Expires date in the past, and says the server will succeed only if the Path and Domain attributes match the values used when the cookie was created.

RFC 6265 and the cookie draft both give a server's Max-Age, in their grammar, as a number starting with a digit from 1 to 9. MDN's Set-Cookie reference says a zero or negative Max-Age expires the cookie immediately, and the draft's rules for browsers treat zero or less as expired (cookie draft 5.6.2). The logout flow in the lab shows what a mismatched Path or Domain does.

Partitioned cookies for embedded widgets

A cookie set by content embedded in another site's page, such as a chat or booking widget in an iframe, is what the cookie draft calls a third-party cookie, and browsers handle those differently.

The draft says that most user agents now limit third-party cookies in a variety of ways: some block them, some partition them by the first-party context so that different cookies are sent depending on the site being browsed, and some block them under a cookie policy or user controls. It concludes that resources cannot rely on third-party cookies being treated consistently (cookie draft 7.1).

The Partitioned attribute lets a server opt in to partitioning. MDN's Set-Cookie reference says it indicates the cookie should be stored using partitioned storage, and that Secure must also be set. MDN's page on Cookies Having Independent Partitioned State (CHIPS) explains the effect: a partitioned cookie is double-keyed, by the origin that sets it and by the top-level page, so it can only be read within the context of the top-level site it was set on, and a widget embedded on a second site no longer reaches the cookie set under the first.

MDN marks the feature Baseline 2025, newly available since December 2025, and says it might not work in older devices or browsers. Partitioned is not defined in revision 22 of the cookie draft.

For a widget, row 6 of the decision table sets SameSite=None and Secure, so the cookie can be sent in a cross-site frame at all, and Partitioned when the widget's state belongs to each embedding site separately. MDN's CHIPS page recommends the __Host- prefix on partitioned cookies if you do not need to share them between subdomains.

Then confirm in each browser your visitors use whether the cookie is stored and sent, using Part 5 of the asset.

What your framework sets by default

Frameworks set some attributes for you and leave others off, and their defaults differ.

Two examples, from each project's own documentation as read on 14 November 2026:

  • express-session for Node.js: the README gives the session cookie's default settings as path '/', httpOnly true, secure false and maxAge null. By default SameSite is not set and no Domain is set, and the cookie is named connect.sid. The README calls secure: true a recommended option, says it requires an HTTPS-enabled website, and says that if you use it behind a proxy you need to set Express's trust proxy setting.
  • Django 6.1: SESSION_COOKIE_SECURE defaults to False, SESSION_COOKIE_HTTPONLY to True, SESSION_COOKIE_SAMESITE to 'Lax', SESSION_COOKIE_DOMAIN to None and SESSION_COOKIE_PATH to '/'. For the CSRF cookie, CSRF_COOKIE_SECURE and CSRF_COOKIE_HTTPONLY default to False and CSRF_COOKIE_SAMESITE to 'Lax'. The settings reference says leaving SESSION_COOKIE_SECURE off isn't a good idea, because an attacker could capture an unencrypted session cookie with a packet sniffer and use the cookie to hijack the user's session.

Read the headers your production site sends

In both frameworks Secure is off until you turn it on, so a production site has to set it in configuration. The Next.js authentication guide recommends setting session cookies on the server with the HttpOnly, Secure and SameSite options, an expiry and a path. Whatever the framework, check the Set-Cookie headers your production site sends, because the headers are what browsers act on.

The OWASP Session Management Cheat Sheet also recommends changing a framework's default session cookie name to a generic one, because names such as PHPSESSID, JSESSIONID and ASP.NET_SessionId can reveal the technology behind the site. A generic name with a __Host- prefix, as in the lab's __Host-sid, does both jobs.

Running the cookie lab on your own machine

The lab is an illustration.

An invented site, app.example.com with a widget host at widget.example.net, sends its cookies in a careless version and a careful version. The server binds to 127.0.0.1, lets the system pick a free port, keeps nothing between requests and uses only modules that ship with Node.js, so there is nothing to install.

It was written and run on Node.js 26.0.0, and every output in the asset was copied from that run. Use it for learning and for checking the cookies your own site sets.

Be exact about what it proves. A scripted client sends the requests of four flows, login, logout, subdomains and cross-site, over plain HTTP to 127.0.0.1, naming the invented host in each request and recording the https URL each step stands for. The checker reads every Set-Cookie header that comes back and reports, attribute by attribute, what is present, what the decision table expects for that cookie, and whether the prefix rules and the cookie draft's other storage rules hold for that header text, judged as if it came from that https URL.

It is not a browser.

It stores no cookies and cannot show what any browser keeps or sends; Part 5 of the asset gives the steps to see that in your own browser's developer tools.

  • Save the five files into an empty folder and open a terminal there.
  • Run node run-flows.mjs. It prints every Set-Cookie header from the four flows for the careless version and then the careful version, with the checker's findings and a summary. The careless version has 30 attribute differences or broken rules, 3 cookies its sign-out does not clear and 1 cookie in scope beyond app.example.com that is not meant to be shared. The careful version has none of each.
  • Run node cookie-flags.test.mjs. Node's built-in test runner runs nine tests against the careful version and all pass. Run node cookie-flags.test.mjs careless and eight fail, each naming the attributes that differ. The Node.js documentation says the runner sets the exit code to 1 when any test fails, so a pipeline step running the test fails.

What each flow checks

Login: the sign-in response and the first page set five cookies, one for each of rows 1 to 5 of the decision table, and each is compared with its row. Logout: for each cookie sign-out should clear, the clearing header is compared with the cookie as it was set.

It has to repeat the name, Path and Domain, because RFC 6265 says a server removes a cookie only if the Path and Domain match the values used when it was created, and the cookie draft replaces a stored cookie only when the name, domain, host-only flag and path all match (cookie draft 5.7, step 23).

Subdomains: for each login cookie, the hosts it is in scope for under the draft's domain-match rule, and whether a sibling subdomain could set a cookie of the same name that app.example.com would receive.

Under the draft's storage model, such a cookie is kept beside an original that is host-only, because the two differ in domain and host-only flag, and replaces an original that has Domain=example.com and the same path (cookie draft 5.7, steps 23 and 24).

Cross-site: the widget cookie set in an iframe on another site, compared with row 6 and with the draft's rules for cross-site responses.

For subdomain and cross-site behaviour, the lab shows the headers and what the specification says follows from them. What a browser actually stores and sends depends on that browser and its settings, so Part 5 of the asset lists steps to confirm it on your own site, in each browser you care about.

Where an outside test fits

Your own checks cover the cookies you know about.

The Clever Ops website security check, which does not sign in, reviews the cookie settings anyone can see under WSTG-SESS-02, where a good result is Secure on every cookie, HttpOnly on any cookie that identifies a session, and a SameSite value chosen deliberately rather than left to the browser.

It reads the cookies issued before sign-in. A web application and API penetration test covers authentication and sessions, and an authenticated test follows the session through sign-in, use, sign-out and timeout.

Each finding comes with evidence, a fix written for your stack and the criteria the retest will use. If Clever Ops built or hosts your site, the work is a security review rather than an independent test, and the report says who built it and who hosts it.

Frequently asked questions

Setting each cookie's attributes so the browser sends it only where, when and to whom it should. Secure keeps it off plain HTTP, HttpOnly keeps it from page script, SameSite limits cross-site requests, Domain and Path set its scope, Expires or Max-Age its lifetime, and the __Host- and __Secure- prefixes let the server rely on some of those attributes. The cookie draft says that by default cookies provide neither confidentiality nor integrity against network attackers, so each protective attribute has to be stated.

The decision table's session row: a __Host- name with Path=/, Secure, HttpOnly and SameSite set to Lax or Strict, no Domain attribute, and no Expires or Max-Age unless the cookie has to outlive a browser restart. WSTG-SESS-02 gives __Host-SID with path=/, Secure, HttpOnly and SameSite=Strict as its most secure configuration, and the OWASP Session Management Cheat Sheet recommends the __Host- prefix for session IDs.

It keeps the cookie out of reach of page script. RFC 6265 says the browser omits it when providing cookies to non-HTTP APIs, and MDN gives document.cookie as the example. The cookie is still sent with requests that script makes. ASVS 5.0 requirement 3.3.4 asks for it on any cookie whose value is not meant for script, such as a session token.

__Secure- requires the Secure attribute. __Host- requires Secure, a Path of / and no Domain attribute, which locks the cookie to the host that set it. A browser that supports prefixes rejects a prefixed cookie that breaks its rules, and the cookie draft requires browsers to match the prefix case-insensitively. ASVS 5.0 requirement 3.3.3 asks for __Host- unless a cookie is explicitly designed to be shared with other hosts.

Only if the cookie has to reach other hosts. Leaving Domain out makes the cookie host-only, which MDN calls more restrictive than setting the host name. Setting it sends the cookie to that domain and all its subdomains, and the cookie draft warns that a sibling subdomain can set a cookie that another host may be unable to tell from its own.

No. RFC 6265 says the Path attribute cannot be relied upon for security. The cookie draft explains that in some user agents script on one path may be able to read cookies stored for another, and that a response can set a cookie for any path. Put applications that should not trust each other on separate hostnames.

A clearing header removes only a cookie with the same name, Path and Domain: RFC 6265 says the server succeeds only if the Path and Domain match the values used when the cookie was created. A cookie set without a Path gets the directory of the URL that set it, so check the path it actually has. For a __Host- or __Secure- cookie, the clearing header has to meet the prefix rules too. And clearing the cookie does not end the session on the server.

Cookies set with the Partitioned attribute, which MDN says are stored using partitioned storage and must also be Secure. MDN's CHIPS page says they are double-keyed, by the origin that sets them and by the top-level page, so a widget embedded on two sites gets a separate cookie on each. MDN marks the feature Baseline 2025, newly available since December 2025, and says it might not work in older devices or browsers.

Scope the right test for your application

Tell us what you have built, who uses it and when it needs to launch. You will get a plain-English recommendation: a website audit, a web application test or a referral to a specialist.