Amblem
Furkan Baytekin

Cookie Prefixes Explained: `__Secure-` and `__Host-`

Cookie security prefixes guide

Cookie Prefixes Explained: `__Secure-` and `__Host-`
2
3 minutes

Modern web apps rely heavily on cookies for sessions, preferences, and authentication. But cookies are also a common attack surface, especially in XSS and session hijacking scenarios. To reduce risk, browsers support special cookie prefixes that enforce security rules automatically: __Secure- and __Host-.

If you’re building anything that handles user accounts, tokens, or sessions, these prefixes are your friends.


A cookie prefix is simply a naming convention. When a cookie starts with __Secure- or __Host-, the browser expects it to follow certain rules. If the rules aren’t met, the browser refuses to set the cookie.

This adds an extra layer of protection that developers can’t accidentally misconfigure.


What it requires:

That’s it. Browsers will block the cookie if it’s set over HTTP or without Secure.

Why it matters:

Secure ensures the cookie is never leaked over plaintext connections. Combined with HTTPS, it protects session cookies from basic network sniffing and MITM attacks.

Example:

http
Set-Cookie: __Secure-sessionId=abc123; Secure; HttpOnly; Path=/; SameSite=Lax

__Host- is the strictest cookie prefix. Think of it like a hardened version of __Secure-.

What it requires:

Why these rules matter

This makes it ideal for:

Example:

http
Set-Cookie: __Host-authToken=xyz789; Secure; HttpOnly; Path=/; SameSite=Strict

If you try to add Domain=example.com, browsers will reject it. That’s the whole point.


When to Use Which?

Use __Secure- when:

Use __Host- when:

For authentication, __Host- is the best choice.


SEO Angle: Why Search Engines Care

Search engines reward secure sites. Using cookie prefixes helps ensure:

Security → trust → stronger SEO.


Best Practices

Prefixes help, but they don’t replace secure coding.


Final Thoughts

__Secure- and __Host- prefixes give you built-in safety nets against common cookie misconfigurations. They’re simple to use, harden your session management, and reduce the risk of accidental exposure.

If you handle logins or sessions, just use them - they’re basically free security.


Album of the blog:

Suggested Blog Posts