# 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.

- Type: artifact
- Domain: Engineering & architecture
- Level: Foundational
- Last reviewed: 2026-09-23
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/cookie/>)

## What 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.

## Why 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.

## When 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.

## How it works

1. A server sends Set-Cookie with a value and deliberate Domain, Path, expiry, Secure, HttpOnly, and SameSite attributes.
2. The browser stores the cookie under its scope and policy limits.
3. For a qualifying request, the browser adds the cookie to the Cookie header automatically.
4. The server validates the value and applies session, integrity, and authorization checks.
5. The server rotates or expires cookies and the browser removes them when instructed or when they age out.

## A 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.

## Where 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.

## Visualization

[View the visualization on the website](<https://wiki.soroush.uk/topic/cookie/#visualization-cookie>)

## Sources

1. [HTTP State Management Mechanism \(RFC 6265\)](<https://www.rfc-editor.org/rfc/rfc6265>) — RFC Editor · Internet Standard
2. [Using HTTP Cookies](<https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Cookies>) — MDN Web Docs · Official technical documentation

## Related topics

- [Session](<https://wiki.soroush.uk/topic/session/>)
- [Same-Origin Policy](<https://wiki.soroush.uk/topic/same-origin-policy/>)
- [Cross-Site Request Forgery \(CSRF\)](<https://wiki.soroush.uk/topic/cross-site-request-forgery/>)
