Cookies and sessions
IntermediateHow a site remembers that you logged in: a session id in a cookie, sent back with every request, and what HttpOnly, Secure and SameSite protect you from.Builds on HTTP requests and responses
POST /login HTTP/1.1Host: example.comContent-Type: application/x-www-form-urlencoded username=sara&password=s3cret-pass HTTP/1.1 303 See OtherLocation: /accountSet-Cookie: sid=k8Fq2xW7pL0aZ3; Max-Age=86400; Path=/; HttpOnly; Secure; SameSite=Lax GET /account HTTP/1.1Host: example.comCookie: sid=k8Fq2xW7pL0aZ3 HTTP/1.1 200 OKContent-Type: text/html; charset=UTF-8Empty
Spaceplay or pause←→previous or next stepRback to the start
Explanation
3 min read
On this page
What are cookies and sessions?
HTTP has no memory: every request stands on its own. To know that two requests come from the same
person, a site needs something the browser sends back every time. That is a cookie: a small
name=value pair that a server sets with a Set-Cookie header, and that the browser returns in a
Cookie header with every later request to that site.
A session is what most sites put in it. Instead of storing "user: sara" in the cookie, where anyone could edit it, the server makes a long random , keeps "this id belongs to Sara" on its side, and puts only the id in the cookie.
How do the cookie's attributes protect a session?
Max-AgeorExpiressay how long the browser keeps the cookie. Without either, it is a session cookie, dropped when the browser decides the session is over.PathandDomainsay which requests get the cookie. WithoutDomain, only the exact host that set it does, not its subdomains.Securesends the cookie over HTTPS only, so it never crosses the network in plain text.HttpOnlyhides it from JavaScript (document.cookie), so a script injected into the page (XSS) cannot read the session id and send it elsewhere.SameSitecontrols requests that come from other sites.Strictnever sends the cookie with them,Laxsends it only when you follow a link (a top-levelGET), andNonealways does, and then requiresSecure. Chrome and Edge treat a cookie withoutSameSiteasLax, but not every browser does, so always set it.
What do people get wrong about cookies?
Mini example
Not every cookie is a login. Here the server remembers a display preference for a year, readable by the page's JavaScript because it is not secret:
POST /settings/theme HTTP/1.1Host: example.comContent-Type: application/x-www-form-urlencoded theme=dark HTTP/1.1 303 See OtherLocation: /settingsSet-Cookie: theme=dark; Max-Age=31536000; Path=/; Secure; SameSite=LaxQuick check
A script injected into the page runs document.cookie. Which cookie can it not see?
Further reading
All steps as text
- Step 1:
HTTP has no memory: each request stands alone, and the server doesn’t know it came from the same person as the last one. Sara opens the login form.
- Step 2:
She submits the form. The browser sends a POST whose body is the form’s fields, encoded like a URL query. HTTPS keeps the password private on the way.
Lines 1–5
- Step 3:
The server checks the password against the hash it stored when Sara signed up. It never keeps the password itself.
- Step 4:
It’s right, so the server starts a session: a long random id, saved with who it belongs to. Nobody can guess it.
- Step 5:
The response sends Sara on to her account page and carries a
Set-Cookieheader with the session id. The browser keeps it in its cookie jar for example.com.Lines 7–9
- Step 6:
Max-Age=86400keeps the cookie for one day, andPath=/sends it with every page of the site.Securemeans only over HTTPS.Line 9
- Step 7:
HttpOnlyhides the cookie from the page’s JavaScript, so a script slipped into the page can’t steal it.SameSite=Laxmatters in a moment.Line 9
- Step 8:
The browser follows the redirect with a GET, and adds the cookie on its own in a
Cookieheader. Neither Sara nor the page’s code had to do anything.Lines 11–13
- Step 9:
The server looks the id up in the session store and finds Sara. To the server, whoever sends this id is Sara.
- Step 10:
So it answers with Sara’s account page. Every later request to example.com carries the cookie the same way, and she stays logged in.
Lines 15–16
- Step 11:
Later Sara opens another site. Its page hides a form that posts to example.com/account/delete and submits itself as soon as the page loads.
- Step 12:
The form sends a POST to example.com from another site. Because of
SameSite=Lax, the browser leaves the cookie out of this cross-site POST. - Step 13:
With no session id, the request is from nobody, so the server refuses it. Without SameSite, the cookie would have gone along and the account would be gone.
- Step 14:
Back on example.com, Sara logs out. The server deletes the session from its store, so the id is worthless even if someone copied it.
- Step 15:
The response also clears the cookie: a
Set-Cookiewith the same name andMax-Age=0tells the browser to throw it away. - Step 16:
A session is a random id the server remembers, carried in a cookie the browser sends back by itself. HttpOnly, Secure and SameSite keep that id out of the wrong hands.