Pointer Walk

Sessions, Cookies and Tokens

HTTP forgets you after every request. Two ways for a site to remember who you are: look you up each time, or hand you a signed pass.

SecurityIntermediate9 min read

The server forgets you

HTTP is stateless. Each request stands alone. When you log in to a site and then click to another page, that second request arrives with no connection to the first. As far as the protocol is concerned, you are a stranger again.

Typing your password on every click is not an option. So after a successful login the server gives the browser something to show on later requests, as proof that it already logged in.

There are two main designs for that "something".

Sessions: a ticket number

The server creates a record, the session, and gives the browser its id.

Sessions: look the user up each timeInteractive
Browser
Server
Session store
empty

A login, and then the next request from the same browser.

  1. On login, the server creates a random session id

    A long, unguessable string. It contains no information.

  2. It stores the id with the user's details

    In a database or an in-memory store.

  3. It sends the id to the browser in a cookie

    A cookie is a small value the browser keeps for a site and sends back automatically with every request to that site.

  4. On every request, the server looks the id up

    Finding the session tells it who is asking.

It is a coat check. You hand over your coat and get a numbered ticket. The ticket is worthless to anyone who reads it. It only means something to the attendant with the rack.

Tokens: a signed pass

The other design stores nothing on the server. Instead, the server writes the facts down, signs them, and hands the whole thing to the browser.

Tokens: a signed pass that carries its own factsInteractive
Browser
Server
Session store
not used

A login, and then the next request from the same browser.

The most common format is a JWT (JSON Web Token). It has three parts: a header, the claims (who you are, when it expires), and a signature made with a key only the server holds.

On each request the server recomputes the signature. If it matches, the contents have not been altered, so they can be trusted without consulting any database.

It is a festival wristband. Staff do not look you up on a list. They look at the wristband, see that it is genuine, and read your access level straight off it.

The trade-off

Sessions: look the user up each timeInteractive
Method
Browser
Server
Session store
empty

A login, and then the next request from the same browser.

SessionToken
The browser holdsA meaningless idThe facts themselves, signed
Server-side storageNeeded, and read on every requestNone
Many serversAll must share the session storeAny server with the key can verify
Log a user out immediatelyDelete the session. DoneHard: the token stays valid until it expires
Change a user's permissionsTakes effect on the next requestTakes effect when the token is replaced
Size sent with each requestTinyLarger

The row that matters most is logging out. With a session, deleting the record ends it at once. With a token, there is nothing to delete. A stolen or cancelled token keeps working until its expiry time.

That is why tokens are given short lives, often minutes, paired with a longer-lived refresh token that is checked against the server when a new short one is needed. That check is where a logout or a ban takes effect.

Which to use

  1. A normal website with its own login: sessions

    Simple, instantly revocable, and one lookup per request is cheap.

  2. Many separate services that must each trust a login: tokens

    A token issued by one service can be verified by another without calling back.

  3. Access for another company's app: tokens

    This is what "Log in with ..." systems hand out.

Keeping either one safe

Whatever the browser holds is, in effect, the user's identity. Anyone who copies it becomes that user.

  • Always use HTTPS, so it cannot be read in transit.
  • Mark cookies HttpOnly, so scripts on the page cannot read them. This limits the damage of a script injected into your page.
  • Mark cookies Secure, so they are never sent over plain HTTP.
  • Set SameSite, so other sites cannot make the browser send your cookie along with a request they trigger.
  • Expire them. A session that lasts for ever is a stolen laptop waiting to happen.
  • Issue a new id at login, so one planted before login is useless afterwards.

Key takeaways

  • HTTP does not remember users, so the browser must present proof of login on each request.
  • A session id is a meaningless ticket. The server looks it up to find out who you are.
  • A token carries the facts with a signature, so the server can trust it without a lookup.
  • Sessions are easy to cancel and need shared storage. Tokens need no storage and are hard to cancel.
  • Either way, protect it in transit and in the browser, and make it expire.

Your progress

Check your progress

0%

  1. Read the page50% of this page

    When you have gone through the page and its animations, tick it off.

  2. Take the quiz50% of this page

    7 questions, from easy to hard. Take it as often as you like: the questions change, and only your best score counts.

Saved on this device only. See all your progress