First-party cookies are HTTP cookies that belong to the same site the user is currently visiting. When the domain and the scheme of a cookie match the site shown in the browser’s address bar, the cookie is seen as a first-party cookie.
In practical terms, a first-party cookie is set by the website itself (or by scripts running in its context) and is sent back only to that site or to subdomains the cookie’s Domain attribute permits. These cookies support core website operations such as keeping a user logged in, remembering preferences, or maintaining the contents of a shopping cart.
The fine line between first-party and third-party cookies is contextual rather than inherent to the cookie file. The same cookie name-value pair can be first-party on one page and third-party on another, depending on whether the page’s top-level site matches the cookie’s domain. This definition is reflected in current browser documentation, including the MDN explanation of third-party cookies.
Cookies themselves are defined by RFC 6265 (HTTP State Management Mechanism).
How Browsers Determine First-Party Context
A cookie is connected with a particular domain and optionally a scheme. Browsers compare that domain with the site the users are actively viewing:
- If the cookie’s domain matches the top-level site (the URL in the address bar), the cookie is first-party.
- If the cookie’s domain differs from the top-level site, the cookie is third-party (also called a cross-site cookie).
For instance, when a user visits https://shop.example, any cookies set with a domain of shop.example are first-party. A cookie set by an embedded resource from ads. An example would be third-party in that context.
The comparison uses the concept of “site,” which is usually based on the registrable domain plus the scheme. Subdomains of the same registrable domain are usually considered same-site for this purpose, subject to the cookie’s Domain attribute and browser rules.
How First-Party Cookies Are Set and Sent
First-party cookies are based on the traditional cookie cycle described in RFC 6265:
- The server sends one or more Set-Cookie headers in an HTTP response, or the JavaScript running on the page sets the cookie using the document. cookie API.
- The browser stores the cookie and all the associated attributes (Domain, Path, Secure, HttpOnly, SameSite, Expires/Max-Age, etc.).
- When the browser sends requests to a host and path that match, it sends the cookie within the Cookie header of the request.
Since the domain is the same as the top-level domain of the site, the browser considers the cookie first-party and applies the less restrictive policy for same-site cookies. Attributes like SameSite=Lax and SameSite=Strict determine if the cookie should be sent in certain cross-site navigations, but the cookie is not affected by the more restrictive third-party blocking rules of many browsers.
Common Uses of First-Party Cookies
First-party cookies provide the following functionalities that are specific to one website:
- Session management – Saving a session ID to enable the server to identify an existing session on subsequent visits from the user without re-authenticating on each page.
- Authentication – Saving the authentication state for the user on subsequent visits to the page as well as browser visits if the persistent cookie is set.
- Preferences – Saving user preferences like language, theme, density, etc.
- Cart and flow state – State saving across pages on the same website.
- Analytics – Tracking page views and events restricted to the website setting the cookie.
- Signals for consent and preferences – Saving consent to the user’s non-essential cookies or features.
For this use, the cookie needs to be set on each visit of the user to the website. No access to other domains is necessary.
Relationship to Cookie Attributes
First-party cookies are still governed by the standard attributes defined for all cookies.
- Domain & Path – Control the hosts and paths that will set the cookie.
- Secure – Allows the setting of the cookie through HTTPS only.
- HttpOnly – Ensures that the cookie cannot be accessed using client-side JavaScript, which reduces the vulnerability in cases where a cross-site scripting attack is possible.
- SameSite – Controls how cookies are sent on cross-site requests. Options for this field include Strict, Lax, and None (the last one requires the attribute Secure).
- Expires / Max-Age – These attributes control whether the cookie is a session cookie or a persistent cookie.
As first-party cookies work in the same-site environment, the developers can make use of the stricter SameSite options without having to hinder the core workings of the website.
First-Party Cookies and Browser Privacy Features
The majority of modern browsers apply different default policies to first-party and third-party cookies. Third-party cookies are increasingly restricted or partitioned to limit cross-site tracking. First-party cookies stay widely available because they are required for basic website operation.
Some browser features still affect first-party cookies indirectly:
- Storage partitioning and related mechanisms can isolate certain forms of state.
- Lifetime limits or additional restrictions may apply in specific privacy modes.
- Users can clear cookies or block them entirely through browser settings.
Limitations
First-party cookies are restricted to the website (and subdomains authorized by the domain) from which they were set. Without any other means, first-party cookies alone cannot be used to track users across unrelated websites.
Similar to all cookies, first-party cookies are constrained by the maximum size of a cookie, the maximum number of cookies stored per domain in the browser, and the potential that users may delete or block them. First-party cookies must not be the only source for storing important data, and important values must be secured with the Secure and HttpOnly flags.
First-party cookies do not offer confidentiality from the website setting the cookies and from network snooping if the Secure flag is missing. Encryption of the connection (HTTPS) is still needed.