Course contentsShow
← Security
Lesson 726 of 3,05215. Web Security - Authentication and SessionsFree lesson

Cookie Prefixes (__Secure- and __Host-)

Using cookie name prefixes to enforce Secure flag and restrict Domain/Path for enhanced cookie security.

Cookie Prefixes (__Secure- and __Host-)

You'll learn how special cookie name prefixes enforce security requirements automatically, preventing misconfiguration vulnerabilities.

The Problem with Trust

Even though you can set cookie flags like Secure and HttpOnly, nothing stops a subdomain or even client-side JavaScript from creating a new cookie with the same name but weaker security settings. Cookie prefixes solve this by making the browser enforce certain security properties based on the cookie's name.

The Two Security Prefixes

__Secure- Prefix: When you name a cookie starting with __Secure-, browsers will only accept it if:

  • The Secure flag is set (HTTPS-only transmission)

Example: __Secure-SessionID must be sent over HTTPS with the Secure flag, or the browser rejects it entirely.

__Host- Prefix (Stricter): Cookies starting with __Host- enforce even tighter restrictions:

  • Must have the Secure flag
  • Must not specify a Domain attribute (binds to exact hostname only)
  • Must have Path=/ (applies to entire site)

Example: __Host-SessionID cannot be overwritten by subdomains and is locked to the exact origin and full path.

Why This Matters

Think of prefixes as tamper-proof seals. Without them, malicious.example.com could set a weak SessionID cookie that overwrites your secure one. With __Host-SessionID, the browser blocks any attempt to create a cookie with that name unless it meets strict criteria—even from your own subdomains.

This prevents session fixation via subdomain takeover and cookie injection attacks where attackers set cookies with looser security.

Key Takeaway:
Use __Secure- to enforce HTTPS transmission and __Host- for maximum protection, binding cookies to a single hostname with no subdomain sharing—browser-enforced security you can trust.