Cookie
A small piece of state that a server sets for a user agent to return with qualifying HTTP requests according to scope and security attributes.
Download Markdown (.md)Soroush’s Product Wiki · https://wiki.soroush.uk/topic/cookie/
01What it means
An HTTP cookie is a name-value pair plus attributes stored by a user agent. The browser attaches it to requests matching its domain, path, security, expiry, and SameSite rules. Cookies can carry session identifiers, preferences, or other state; they are not inherently authentication or consent mechanisms.[1][2]
- 01Set-Cookie responseValue, Domain, Path, expiry, Secure, HttpOnly, SameSite
- 02Browser storesOrigin scope and policy limits
- 03Qualifying requestBrowser attaches Cookie header automatically
- 04Server validatesIntegrity, session, authorization
- 05Rotate or expireServer instruction or age-out
Cookies can carry identifiers or preferences, but they are not inherently authentication or consent mechanisms.
02Why it matters
Cookies enable browser sessions and preferences but are sent automatically, creating privacy, cross-site request, theft, and scope risks. Secure attributes materially change which scripts, connections, and cross-site contexts can use them.
03When to use it
Use cookies for limited browser state that must accompany matching HTTP requests, especially opaque session identifiers. Prefer other storage or explicit request data when automatic transmission is unnecessary, and avoid sensitive data directly in cookie values.
04How it works
- A server sends Set-Cookie with a value and deliberate Domain, Path, expiry, Secure, HttpOnly, and SameSite attributes.
- The browser stores the cookie under its scope and policy limits.
- For a qualifying request, the browser adds the cookie to the Cookie header automatically.
- The server validates the value and applies session, integrity, and authorization checks.
- The server rotates or expires cookies and the browser removes them when instructed or when they age out.
05A practical example
A web app sets an opaque session cookie with Secure, HttpOnly, SameSite=Lax, a narrow Path, and an explicit lifetime. JavaScript cannot read it, insecure HTTP cannot carry it, and cross-site use is restricted.
06Where it breaks
Cookies have size and count limits, can be copied if exposed, and may be blocked or partitioned by privacy controls. HttpOnly reduces script theft but does not stop malicious scripts from sending authenticated requests.
Common mistakes
- Storing passwords, access rights, or sensitive personal data directly in a cookie.
- Omitting Secure, HttpOnly, or an intentional SameSite policy from session cookies.
- Setting broad Domain and Path values that expose a cookie unnecessarily.
- Assuming signed cookie data is confidential rather than merely integrity-protected.
07Sources
- HTTP State Management Mechanism (RFC 6265)RFC Editor · Internet StandardHTTP State Management Mechanism (RFC 6265)
- Using HTTP CookiesMDN Web Docs · Official technical documentationUsing HTTP Cookies