Catalog My learning Blog
Browser Security Headers: CORS, CSP and Cookies Explained · Phase 4 · Lesson 18 · 6 min read

Cookie Security Attributes: HttpOnly, Secure, Prefixes

On this page
  1. Learning Objective
  2. Concept Explanation
  3. Worked Example: A Session Cookie and the Cookie-Tossing Attack
  4. Real-World Examples
  5. Review Questions & Answers

Learning Objective

State exactly what Secure, HttpOnly, Domain, Path, and the __Host- and __Secure- prefixes each enforce, and explain the cookie-tossing attack that the __Host- prefix exists to prevent.

Concept Explanation

Cookies are the oldest state mechanism on the web and, as the first lesson in this course established, they are scoped by domain and path rather than by origin. Every security attribute on a cookie is an attempt to retrofit stronger boundaries onto that model, and knowing precisely which gap each one closes is what separates a correct Set-Cookie header from a superstitious one.

Secure means the cookie is only sent over HTTPS. Without it, a cookie set on an HTTPS page is still transmitted in plaintext if anything causes a request to the same host over HTTP, including an image reference, a mistyped link, or an attacker-forced navigation, which hands the session to any network observer. It is worth knowing the asymmetry: Secure controls sending, not setting. An attacker with a plaintext HTTP position could historically set or overwrite a Secure cookie for the same host, which is part of why HSTS, covered later in this course, is a necessary companion rather than an optional extra.

HttpOnly means the cookie is invisible to document.cookie and to any JavaScript in the page. Its purpose is narrow and valuable: it means an XSS payload cannot simply read the session cookie and exfiltrate it for offline reuse. It does not prevent the XSS from acting as the user, since requests made from the page still carry the cookie automatically, but it does prevent the session from being stolen and used elsewhere, which is a meaningful difference between a transient compromise and a durable one. There is no legitimate reason for a session cookie to be readable by script.

Domain and Path both widen rather than narrow. Omitting Domain produces a host-only cookie, sent only to the exact host that set it, which is the tighter and usually correct choice. Specifying Domain=example.com sends the cookie to every subdomain, which means any subdomain, including a compromised one, a user-content host, or a dangling record someone else has claimed, both receives and can overwrite it. Path is not a security boundary at all, because same-origin script can read cookies from any path via an iframe, so it should be treated as an organizational convenience only.

The cookie prefixes exist because a server receiving a cookie cannot tell how it was set: the Cookie request header carries only names and values, with no attributes, no origin, and no indication of whether the cookie came from the legitimate application or from a compromised sibling subdomain overwriting it. That attack is called cookie tossing. The prefixes solve it by making the name itself carry a verifiable guarantee. A cookie named with a __Secure- prefix is only accepted by the browser if it was set with Secure over HTTPS. A cookie named with a __Host- prefix is only accepted if it was set with Secure, over HTTPS, with Path=/, and with no Domain attribute at all, which makes it strictly host-only and therefore impossible for a sibling subdomain to set or overwrite. For session and CSRF cookies, __Host- should be the default choice.

Attribute or prefixWhat it enforcesWhat it does not do
SecureSent only over HTTPSDoes not prevent an HTTP position from setting the cookie
HttpOnlyInvisible to document.cookieDoes not stop an XSS from acting as the user in-page
Domain omitted (host-only)Sent only to the exact host that set itNothing else; this is the tight default
Domain=example.comWidens to every subdomainLets any subdomain read and overwrite it
Path=/adminOrganizational scoping onlyNot a security boundary; same-origin script can read any path
__Secure- prefixRejected unless Secure and set over HTTPSDoes not restrict Domain
__Host- prefixRejected unless Secure, HTTPS, Path=/, and no DomainCannot be used where a cookie must span subdomains

The sequence below shows why the prefix matters. The compromised subdomain sets a cookie the application cannot distinguish from its own, until the prefix makes the distinction enforceable by the browser.

YAML
# WEAK: readable by script, sent over plaintext, shared with every subdomain
Set-Cookie: session=abc123; Domain=example.com; Path=/

# COOKIE TOSSING: a compromised or user-content subdomain does this...
#   (from https://uploads.example.com)
Set-Cookie: session=attacker-chosen-value; Domain=example.com; Path=/
# ...and https://app.example.com now receives:
#   Cookie: session=attacker-chosen-value
# The server sees only a name and a value. It cannot tell which host set it,
# which path it claimed, or whether Secure was used. This is session fixation
# performed from a sibling subdomain.

# STRONG: host-only, HTTPS-only, script-invisible, and verifiable by name
Set-Cookie: __Host-session=abc123; Secure; HttpOnly; Path=/; SameSite=Lax

# The browser now REJECTS any attempt to set __Host-session with a Domain
# attribute, from any host, so uploads.example.com cannot toss it:
Set-Cookie: __Host-session=evil; Domain=example.com; Path=/   -> rejected
Set-Cookie: __Host-session=evil; Path=/admin                  -> rejected (Path)
Set-Cookie: __Host-session=evil; Secure; Path=/  (over HTTP)   -> rejected
A cookie-tossing attack from a sibling subdomain reaching the main application, with the __Host- prefix shown as a browser-enforced rejection point that makes the cookie name itself a guarantee about how it was set.
A cookie-tossing attack from a sibling subdomain reaching the main application, with the __Host- prefix shown as a browser-enforced rejection point that makes the cookie name itself a guarantee about how it was set.

Real-World Examples

  • The Firesheep Firefox extension, released in October 2010, made session hijacking over open wireless networks a point-and-click operation by capturing cookies sent without the Secure attribute over plaintext HTTP; it worked against Facebook, Twitter, and many other major sites at the time, and the resulting publicity is widely credited with accelerating the industry-wide move to HTTPS everywhere and to marking session cookies Secure by default.
  • Cookie prefixes were standardized precisely because the Cookie request header is attribute-free: RFC 6265bis specifies __Secure- and __Host- so that a server can derive a guarantee about how a cookie was set from its name alone, and all major browsers enforce the rejection rules, which makes __Host- the only mechanism by which an application can be certain a cookie was not written by a sibling subdomain.
Best PracticeUse __Host- as the default prefix for session and CSRF cookies, combined with Secure, HttpOnly, Path=/ and an appropriate SameSite value. Omit the Domain attribute unless a cookie genuinely must span subdomains, because specifying it hands every subdomain, including ones you do not control, the ability to both read and overwrite it.

Review Questions & Answers

Because the guarantee becomes verifiable by the server from the name alone. The Cookie request header transmits only names and values, with no attributes and no indication of which host set the cookie, so a server receiving a cookie cannot tell whether it was set correctly by itself or tossed by a compromised sibling subdomain. The browser enforces the __Host- rules at set time and rejects any violation, so the presence of the name is proof of how it was set.

It prevents the payload from reading the cookie value and exfiltrating it, so the attacker cannot take the session away and reuse it from their own machine or after the page is closed. It does not prevent the payload from acting as the user while the page is open, because requests issued from the page carry the cookie automatically. The distinction is between a transient in-page compromise and a durable, portable session theft.

Because cookie path scoping is only about which requests include the cookie, not about isolation between contexts. Same-origin script can read cookies from any path by creating an iframe at that path and reading its document.cookie, since the origin is the same. Path is useful for keeping unrelated cookies out of unrelated requests, but treating it as a barrier between parts of the same origin is a mistake.

Every subdomain of example.com, present and future, can now both read and overwrite that cookie. That includes marketing microsites, status pages, hosts run by third-party vendors, staging environments with weaker controls, user-content subdomains, and any dangling DNS record an attacker can claim. A compromise of any one of them becomes a session-theft or session-fixation path against the main application, which is why a host-only cookie plus an explicit token exchange is usually the better architecture.