A cookie is a name=value pair the browser stores and attaches to later requests in a Cookie header. A Set-Cookie response header or an assignment to document.cookie creates it, and its attributes decide where it goes and for how long.
Which requests carry it. A cookie is sent only if all of these match:
- Domain. Without
Domainit's host-only: sent only to the exact host that set it.Domain=example.comsends it toexample.comand every subdomain. A host may name itself or a parent, never another site or a public suffix likeco.uk. - Path.
Path=/adminmatches/adminand/admin/users, not/administrator. The default is the directory of the URL that set it. Secure: HTTPS only, though browsers also treathttp://localhostas secure.SameSite, for requests from another site:Strictnever sends it;Laxsends it on top-level navigations with safe methods, like following a link;None(which requiresSecure) always does. Chrome treats a missingSameSiteasLax; Firefox 159 doesn't.- Expiry: it hasn't expired.
Ports don't count, and several matches are listed longest path first. These attributes also defend against CSRF and cookie theft, which is web-security ground.
For how long. With neither Expires (a date) nor Max-Age (seconds) it's a session cookie, kept until the browser decides the session is over. With both, Max-Age wins. Browsers cap either at 400 days. To delete a cookie, set it again with the same name, domain and path and Max-Age=0.
document.cookie returns only name=value pairs, never attributes and never HttpOnly cookies. Each assignment sets one cookie.
Partitioned cookies (CHIPS). A third-party cookie set with Partitioned and Secure is stored per top-level site, so an embedded widget gets a separate jar on each site that embeds it.