Identity and access
What is a Cookie?
Definition
A cookie is a small piece of data that a server asks a browser to store and send back on subsequent requests to the same site, most often to identify a session.
Also known as: HTTP cookie, Browser cookie
Cookie in more detail
HTTP forgets everything between requests, so a cookie is how continuity is bolted on. The server sends a Set-Cookie header, the browser stores the name and value with the rules attached to it, and every later matching request includes a Cookie header automatically. That automatic inclusion is both what makes cookies convenient and what makes CSRF possible.
The attributes are the security model. HttpOnly hides the cookie from JavaScript, which stops an XSS payload from reading a session id. Secure means it is only ever sent over HTTPS. SameSite controls whether it rides along on cross-site requests: Lax, the default in Chrome and Edge, sends it on top-level navigations but not on cross-site form posts or subresource requests, and Strict withholds it entirely. Domain and Path narrow where it applies, and Max-Age or Expires decide whether it survives the browser closing.
A cookie is not a session and not a login. It is a transport for a small value; the meaning lives elsewhere. Usually that value is an opaque random session id whose data sits on the server, which is the safer arrangement because the browser holds nothing of value and the server can revoke instantly. Third-party cookies — set by a domain other than the one in the address bar — are what made cross-site tracking work, and are now blocked or partitioned by default in most browsers.
Cookie example
Server sets it
Set-Cookie: session=8f2c19ab; HttpOnly; Secure;
SameSite=Lax; Path=/; Max-Age=1209600
Browser returns it on every matching request
GET /account HTTP/1.1
Host: example.com
Cookie: session=8f2c19ab
What each attribute buys you
HttpOnly JavaScript cannot read it (blunts XSS theft)
Secure never sent over plain HTTP
SameSite=Lax not sent on cross-site POSTs (blunts CSRF)
SameSite=None sent cross-site; requires Secure
Max-Age seconds to live; omit for a session cookieCookie — common questions
- What is the difference between a cookie and a session?
- A cookie is the storage and transport mechanism in the browser. A session is the server-side state the cookie's value points at. The cookie usually holds nothing but a random id; the shopping basket lives on the server.
- Are cookies a security risk?
- Cookies carrying credentials are worth protecting, which is what HttpOnly, Secure and SameSite are for. Without HttpOnly a cross-site scripting bug can steal the session; without SameSite the cookie is attached to cross-site requests, enabling CSRF.
- What is a third-party cookie?
- One set by a domain different from the site you are visiting, typically by an embedded ad, analytics script or widget. Because the same domain can set and read it across many sites, it enables cross-site tracking — which is why browsers now block or partition them.
Keep reading
Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.
Browse the A-Z glossary