The Domain
and Path
attributes define the scope of a cookie: what URLs the cookies should be sent to.
Domain attribute
The Domain
attribute specifies which hosts can receive a cookie. If unspecified, the attribute defaults to the same host that set the cookie, excluding subdomains. If Domain
is specified, then subdomains are always included. Therefore, specifying Domain
is less restrictive than omitting it. However, it can be helpful when subdomains need to share information about a user.
For example, if you set Domain=mozilla.org
, cookies are available on subdomains like developer.mozilla.org
.
Path attribute
The Path
attribute indicates a URL path that must exist in the requested URL in order to send the Cookie
header. The %x2F
("/") character is considered a directory separator, and subdirectories match as well.
For example, if you set Path=/docs
, these request paths match:
/docs
/docs/
/docs/Web/
/docs/Web/HTTP
But these request paths don't:
SameSite attribute
The SameSite
attribute lets servers specify whether/when cookies are sent with cross-site requests (where Site is defined by the registrable domain and the scheme: http or https). This provides some protection against cross-site request forgery attacks (CSRF). It takes three possible values: Strict
, Lax
, and None
.
With Strict
, the cookie is only sent to the site where it originated. Lax
is similar, except that cookies are sent when the user navigates to the cookie's origin site. For example, by following a link from an external site. None
specifies that cookies are sent on both originating and cross-site requests, but only in secure contexts (i.e., if SameSite=None
then the Secure
attribute must also be set). If no SameSite
attribute is set, the cookie is treated as Lax
.
Here's an example:
Note: The standard related to SameSite
recently changed (MDN documents the new behavior above). See the cookies Browser compatibility table for information about how the attribute is handled in specific browser versions:
-
SameSite=Lax
is the new default if SameSite
isn't specified. Previously, cookies were sent for all requests by default. - Cookies with
SameSite=None
must now also specify the Secure
attribute (they require a secure context). - Cookies from the same domain are no longer considered to be from the same site if sent using a different scheme (
http:
or https:
).
Cookie prefixes
Because of the design of the cookie mechanism, a server can't confirm that a cookie was set from a secure origin or even tell where a cookie was originally set.
A vulnerable application on a subdomain can set a cookie with the Domain
attribute, which gives access to that cookie on all other subdomains. This mechanism can be abused in a session fixation attack. See session fixation for primary mitigation methods.
As a defense-in-depth measure, however, you can use cookie prefixes to assert specific facts about the cookie. Two prefixes are available:
__Host-
-
If a cookie name has this prefix, it's accepted in a Set-Cookie
header only if it's also marked with the Secure
attribute, was sent from a secure origin, does not include a Domain
attribute, and has the Path
attribute set to /
. This way, these cookies can be seen as "domain-locked".
__Secure-
-
If a cookie name has this prefix, it's accepted in a Set-Cookie
header only if it's marked with the Secure
attribute and was sent from a secure origin. This is weaker than the __Host-
prefix.
The browser will reject cookies with these prefixes that don't comply with their restrictions. Note that this ensures that subdomain-created cookies with prefixes are either confined to the subdomain or ignored completely. As the application server only checks for a specific cookie name when determining if the user is authenticated or a CSRF token is correct, this effectively acts as a defense measure against session fixation.
Note: On the application server, the web application must check for the full cookie name including the prefix. User agents do not strip the prefix from the cookie before sending it in a request's Cookie
header.
For more information about cookie prefixes and the current state of browser support, see the Prefixes section of the Set-Cookie reference article.
JavaScript access using Document.cookie
You can create new cookies via JavaScript using the Document.cookie
property. You can access existing cookies from JavaScript as well if the HttpOnly
flag isn't set.
document.cookie = "yummy_cookie=choco";
document.cookie = "tasty_cookie=strawberry";
console.log(document.cookie);
Cookies created via JavaScript can't include the HttpOnly
flag.
Please note the security issues in the Security section below. Cookies available to JavaScript can be stolen through XSS.