-
Notifications
You must be signed in to change notification settings - Fork 22.8k
Editorial review: Document __Http- and __Host-Http- cookie prefixes #40959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
chrisdavidmills
merged 4 commits into
mdn:main
from
chrisdavidmills:Http-and-HostHttp-cookie-prefixes
Sep 1, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8938b69
Document __Http- and __Host-Http- cookie prefixes
chrisdavidmills 669b7cc
Fixes for yoavweiss review comments
chrisdavidmills e3b4ee4
Add security boundary line to __Host- description
chrisdavidmills 9d6f7a7
Fixes for bsmth review comments
chrisdavidmills File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,21 +60,14 @@ Set-Cookie: <cookie-name>=<cookie-value>; Domain=<domain-value>; Secure; HttpOnl | |
- : Defines the cookie name and its value. | ||
A cookie definition begins with a name-value pair. | ||
|
||
A `<cookie-name>` can contain any US-ASCII characters except for: control characters ({{Glossary("ASCII")}} characters 0 up to 31 and ASCII character 127) or separator characters (space, tab and the characters: `( ) < > @ , ; : \ " / [ ] ? = { }`) | ||
A `<cookie-name>` can contain any US-ASCII characters except for control characters ({{Glossary("ASCII")}} characters 0 up to 31 and ASCII character 127) or separator characters (space, tab and the characters: `( ) < > @ , ; : \ " / [ ] ? = { }`) | ||
|
||
A `<cookie-value>` can optionally be wrapped in double quotes and include any US-ASCII character excluding control characters (ASCII characters 0 up to 31 and ASCII character 127), {{glossary("Whitespace")}}, double quotes, commas, semicolons, and backslashes. | ||
|
||
**Encoding**: Many implementations perform {{Glossary("Percent-encoding", "percent-encoding")}} on cookie values. | ||
However, this is not required by the RFC specification. | ||
The percent-encoding does help to satisfy the requirements of the characters allowed for `<cookie-value>`. | ||
**Encoding**: Many implementations perform {{Glossary("Percent-encoding", "percent-encoding")}} on cookie values. However, this is not required by the RFC specification. The percent-encoding does help to satisfy the requirements of the characters allowed for `<cookie-value>`. | ||
|
||
> [!NOTE] | ||
> Some `<cookie-name>` have a specific semantic: | ||
> | ||
> **`__Secure-` prefix**: Cookies with names starting with `__Secure-` (dash is part of the prefix) must be set with the `secure` flag from a secure page (HTTPS). | ||
> | ||
> **`__Host-` prefix**: Cookies with names starting with `__Host-` are sent only to the host subdomain or domain that set them, and not to any other host. | ||
> They must be set with the `secure` flag, must be from a secure page (HTTPS), must not have a domain specified, and the path must be `/`. | ||
> Some cookie names contain prefixes that impose specific restrictions on the cookie's attributes in supporting user-agents. See [Cookie prefixes](#cookie_prefixes) for more information. | ||
|
||
- `Domain=<domain-value>` {{optional_inline}} | ||
- : Defines the host to which the cookie will be sent. | ||
|
@@ -163,6 +156,18 @@ Set-Cookie: <cookie-name>=<cookie-value>; Domain=<domain-value>; Secure; HttpOnl | |
> | ||
> Insecure sites (`http:`) cannot set cookies with the `Secure` attribute. The `https:` requirements are ignored when the `Secure` attribute is set by localhost. | ||
|
||
## Cookie prefixes | ||
|
||
Some cookie names contain prefixes that impose specific restrictions on the cookie's attributes in supporting user-agents. All cookie prefixes start with a double-underscore (`__`) and end in a dash (`-`). The following prefixes are defined: | ||
|
||
- **`__Secure-`**: Cookies with names starting with `__Secure-` must be set with the `Secure` attribute by a secure page (HTTPS). | ||
- **`__Host-`**: Cookies with names starting with `__Host-` must be set with the `Secure` attribute by a secure page (HTTPS). In addition, they must not have a `Domain` attribute specified, and the `Path` attribute must be set to `/`. This guarantees that such cookies are only sent to the host that set them, and not to any other host on the domain. It also guarantees that they are set host-wide and cannot be overridden on any path on that host. This combination yields a cookie that is as close as can be to treating the origin as a security boundary. | ||
- **`__Http-`**: Cookies with names starting with `__Http-` must be set with the `Secure` flag by a secure page (HTTPS) and in addition must have the `HttpOnly` attribute set to prove that they were set via the `Set-Cookie` header (they can't be set or modified via JavaScript features such as `Document.cookie` or the [Cookie Store API](/en-US/docs/Web/API/Cookie_Store_API)). | ||
- **`__Host-Http-`**: Cookies with names starting with `__Host-Http-` must be set with the `Secure` flag by a secure page (HTTPS) and must have the `HttpOnly` attribute set to prove that they were set via the `Set-Cookie` header. In addition, they also have the same restrictions as `__Host-`-prefixed cookies. This combination yields a cookie that is as close as can be to treating the origin as a security boundary while at the same time ensuring developers and server operators know that its scope is limited to HTTP requests. | ||
|
||
> [!WARNING] | ||
> You cannot count on these additional assurances on browsers that don't support cookie prefixes; in such cases, prefixed cookies will always be accepted. | ||
|
||
## Examples | ||
|
||
### Session cookie | ||
|
@@ -205,12 +210,11 @@ Set-Cookie: sessionId=e8bb43229de9; Domain=foo.example.com | |
|
||
### Cookie prefixes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually missed that this section is under "Examples", so there's some small duplication, but it's also okay in this state, IMO. |
||
|
||
Cookie names prefixed with `__Secure-` or `__Host-` can be used only if they are set with the `secure` attribute from a secure (HTTPS) origin. | ||
Cookie names prefixed with `__Secure-` or `__Host-` can be used only if they are set with the `Secure` attribute from a secure (HTTPS) origin. | ||
|
||
In addition, cookies with the `__Host-` prefix must have a path of `/` (meaning any path at the host) and must not have a `Domain` attribute. | ||
Cookie names prefixed with `__Http-` or `__Host-Http-` can be used only if they are set with the `Secure` attribute from a secure (HTTPS) origin and in addition must have the `HttpOnly` attribute set to prove that they were set via the `Set-Cookie` header and not on the client-side via JavaScript. | ||
|
||
> [!WARNING] | ||
> For clients that don't implement cookie prefixes, you cannot count on these additional assurances, and prefixed cookies will always be accepted. | ||
In addition, cookies with the `__Host-` or `__Host-Http-` prefix must have a path of `/` (meaning any path at the host) and must not have a `Domain` attribute. | ||
|
||
```http | ||
// Both accepted when from a secure origin (HTTPS) | ||
|
@@ -225,6 +229,10 @@ Set-Cookie: __Host-id=1; Secure | |
|
||
// Rejected due to setting a Domain | ||
Set-Cookie: __Host-id=1; Secure; Path=/; Domain=example.com | ||
|
||
// Only settable via Set-Cookie | ||
Set-Cookie: __Http-ID=123; Secure; Domain=example.com | ||
Set-Cookie: __Host-Http-ID=123; Secure; Path=/ | ||
``` | ||
|
||
### Partitioned cookie | ||
|
@@ -234,7 +242,7 @@ Set-Cookie: __Host-example=34d8g; SameSite=None; Secure; Path=/; Partitioned; | |
``` | ||
|
||
> [!NOTE] | ||
> Partitioned cookies must be set with `Secure`. In addition, it is recommended to use the `__Host` prefix when setting partitioned cookies to make them bound to the hostname and not the registrable domain. | ||
> Partitioned cookies must be set with `Secure`. In addition, it is recommended to use a `__Host` or `__Host-Http-` prefix when setting partitioned cookies to make them bound to the hostname and not the registrable domain. | ||
|
||
## Specifications | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.