{"canonical_url":"https://engineer.company/glossary/cookie/","content_markdown":"# Cookie\n\n## What it is\n\nA cookie, or HTTP cookie, is a short name and value that a web server sends to\nthe browser in a response header, and that the browser returns with its later\nrequests to the same server. Cookies are how HTTP, a protocol that treats every\nrequest as a stranger, gets a memory: a login that survives the next click, a\nbasket still full at the checkout, a language chosen once and kept.\n\nThe server sets each cookie with its own `Set-Cookie` header, and the browser\nsends back every cookie that applies in one `Cookie` header. Here the first\nthree lines are a response and the rest is a later request from the same\nbrowser:\n\n```http\nHTTP/1.1 200 OK\nSet-Cookie: __Host-sid=4f2c9a71e0b8; Path=/; Secure; HttpOnly; SameSite=Lax\nSet-Cookie: lang=da; Max-Age=31536000; Path=/\n\nGET /account/ HTTP/1.1\nHost: shop.example\nCookie: __Host-sid=4f2c9a71e0b8; lang=da\n```\n\nEverything after the first semicolon is an attribute, an instruction about how\nlong to keep the cookie and where to send it. `Expires` or `Max-Age` sets a\nmaximum lifetime; with neither, the cookie lasts until the browser decides the\nsession is over. `Domain` widens the cookie to a whole domain and its\nsubdomains, and without it the cookie goes back only to the host that set it.\n`Path` narrows it to part of the site. `Secure` keeps it off unencrypted\nconnections, `HttpOnly` hides it from scripts that read `document.cookie`, and\n`SameSite` decides whether it rides along on requests another site starts.\n\nThe return trip carries names and values only, so the server cannot tell from it\nwhen a cookie expires, whether it was set with `Secure`, or who last wrote it.\n\nThe rules are in RFC 6265, published in 2011. Its revision, known as rfc6265bis,\nadds `SameSite`, the `__Secure-` and `__Host-` name prefixes and a requirement\nthat browsers cap every lifetime at no more than 400 days. At the time of\nwriting it has been approved and is waiting for the RFC Editor to publish it as\nthe replacement for RFC 6265.\n\n## Why it matters\n\nA cookie is usually the only thing that makes a signed-in request different from\nan anonymous one. After a login, the session identifier in the cookie is the\nuser as far as the server is concerned: whoever presents it gets the account,\nand no password is asked for. Most of what goes wrong with cookies is somebody\nelse reading, copying or using one.\n\nThe browser makes the last of those easy. It attaches cookies to every request\nthat matches them, whoever started that request. A form on another site that\nposts to a shop arrives carrying the visitor's cookies, and the server sees a\nrequest from a signed-in user. RFC 6265 calls this ambient authority and names\nthe attacks it invites: cross-site request forgery and the confused deputy.\n\nCookies are also the oldest tracking tool on the web. A script embedded on\nthousands of sites can set a cookie from its own domain, which the browser sends\nback from every one of those sites, so one company can line up a person's visits\nacross all of them. The revision describes this use of third-party cookies,\nnotes that most browsers now block or partition them, and recommends a policy as\nrestrictive as compatibility allows.\n\nThe law follows the storage rather than the technology. In the EU, Article 5(3)\nof the ePrivacy Directive allows a site to store information on a visitor's\ndevice, or to read what is stored there, only with consent given after clear and\ncomprehensive information. The exceptions are storage used solely to transmit a\ncommunication, and storage strictly necessary for a service the visitor\nexplicitly asked for. That article is where consent banners come from, and it\nnever mentions cookies by name. The European Data Protection Board's guidelines\non its technical scope apply it to tracking pixels and tracking links too.\n\n## What it looks like in practice\n\nThis site sets none. Its [cookies page](/cookies/) explains why: there are no\naccounts, no basket and nothing to follow a visitor with, so there is no consent\nbanner, because there is nothing to consent to. It also lists the other places a\nsite can leave something behind, from `localStorage` to a service worker, and\nsays none of them is used either.\n\nGoing without cookies changes how a site learns about its readers.\n[Counting readers from the access log alone](/portfolio/measured-13-addresses-with-no-tracking/)\nmeasures readership with no cookie, script or pixel, from the web server's own\nlog, and drops the `Cookie` request header from every line as it is written,\nalong with the authorization header. The result is coarse by design: unique\nvisitors and a visitor's path through the site are out of reach, and the page\nprints those limits beside the numbers.\n\nWhere an application does need a cookie, the useful question is what it is\ntrusted with. On a platform where one person acts for several organizations,\n[the active organization is kept in the browser, and a server-side cookie mirrors it](/portfolio/built-organization-context-switching-for-users/)\nso that pages rendered on the server show the same context. Neither copy decides\naccess. The server checks authorization on every request, so someone who edits\nthe value changes what their own screen shows and nothing more. That is the safe\nshape for any cookie: the value may choose what to render, and the decision\nabout what someone may do stays on the server.\n\n## Where it goes wrong\n\n**Script on the page reads the session.** Without `HttpOnly`, a script an\nattacker injects can read `document.cookie` and send the session identifier\nanywhere. `HttpOnly` takes the cookie out of that API. It stops theft, not\nmisuse: the injected script can still send requests, and the browser still\nattaches the cookie, so `HttpOnly` sits beside a Content Security Policy rather\nthan replacing one.\n\n**The cookie crosses plain HTTP.** A cookie without `Secure` goes out on any\n`http://` request to its host, readable by anyone on the network path. Under RFC\n6265, `Secure` guards confidentiality alone, and an attacker on the network can\nstill overwrite a secure cookie from an insecure connection. The revision\nnarrows that gap by ignoring `Secure` cookies set over plain HTTP and refusing a\nplain-HTTP cookie that would overlay a secure one.\n\n**A sibling subdomain sets the cookie.** `Domain=example.com` sends a cookie to\nevery host under that domain, and any of those hosts can set a cookie for the\nwhole domain, which the main site then reads as its own. `Path` is no security\nboundary either. The `__Host-` prefix answers this: a browser accepts such a\ncookie only when it is `Secure`, has `Path=/` and has no `Domain`, which locks\nit to the one host that set it.\n\n**Another site starts the request.** `SameSite=Strict` keeps the cookie to\nrequests made from the same site, `Lax` adds top-level navigations with safe\nmethods, such as following a link, and `None` sends it everywhere and is ignored\nunless the cookie is also `Secure`. The revision treats a cookie with no\n`SameSite` much like `Lax`; a server that depends on that should say so. A site,\nfor this purpose, is the scheme plus the registrable domain, so `SameSite` does\nnothing against a hostile sibling subdomain.\n\n**The server believes the value.** Anything in a cookie can be edited by the\nperson whose browser holds it. RFC 6265 recommends that servers encrypt and sign\ncookie contents, and warns that this still leaves a cookie open to replay. The\nsturdier pattern is a random session identifier pointing at state kept on the\nserver, replaced at login, so that an identifier an attacker planted beforehand,\nthe session fixation the RFC warns about, means nothing afterwards.\n\n**The browser lets it go.** An expiry date is a maximum. Browsers evict cookies\nto stay within their limits, and an attacker can push cookies out by setting\nmany of its own. Servers should not rely on browsers keeping cookies. Deleting\none has a trap too: a `Set-Cookie` with an expiry in the past removes the cookie\nonly when its `Path` and `Domain` match the ones it was created with.\n\n## Further reading\n\n- [RFC 6265: HTTP State Management Mechanism](https://www.rfc-editor.org/rfc/rfc6265)\n  — the standard for `Set-Cookie` and `Cookie`, with the attribute rules and a\n  security section on ambient authority, weak integrity and session identifiers.\n- [Cookies: HTTP State Management Mechanism (rfc6265bis)](https://datatracker.ietf.org/doc/draft-ietf-httpbis-rfc6265bis/)\n  — the approved revision, with `SameSite`, the `__Secure-` and `__Host-`\n  prefixes, the 400-day cap and a section on third-party cookies.\n- [Directive 2002/58/EC, consolidated text](https://eur-lex.europa.eu/legal-content/EN/TXT/?uri=CELEX:02002L0058-20091219)\n  — the ePrivacy Directive as amended in 2009, with Article 5(3) on storing and\n  reading information on a user's device.\n- [EDPB Guidelines 2/2023 on the technical scope of Article 5(3)](https://www.edpb.europa.eu/our-work-tools/our-documents/guidelines/guidelines-22023-technical-scope-art-53-eprivacy-directive_en)\n  — how the rule reaches tracking pixels, tracking links, local processing and\n  other techniques beyond cookies.\n\n\u003chttps://engineer.company/glossary/cookie/\u003e\n","date_modified":"2026-10-20T21:59:50Z","description":"How a cookie travels between server and browser, what Secure, HttpOnly and SameSite change, and when European law asks a site for consent first.","formats":{"gemtext":"https://engineer.company/glossary/cookie/index.gmi","gopher":"https://engineer.company/glossary/cookie/gophermap","html":"https://engineer.company/glossary/cookie/","json":"https://engineer.company/glossary/cookie/index.json","markdown":"https://engineer.company/glossary/cookie/index.md","pdf":"https://engineer.company/glossary/cookie/index.pdf"},"kind":"page","language":"en","title":"What is a cookie?","url":"https://engineer.company/glossary/cookie/","version":1}