Skip to content

Lesson 2 of 7

Sessions

Listen to an AI summary of Sessions

0:00 / 0:31

How a server knows who sent a request

A request on its own does not say who sent the request.

An event with tickets has the same problem. Staff at the door check Sam's ticket once and give Sam a wristband with a number. Staff inside look only at the wristband.

Signing in is showing the ticket, and the server is the staff. The wristband is a cookie.

What is a session id

The number on the wristband is the session id. A session id is a long random value in a cookie that tells the server which session a request belongs to. The server keeps a record of each session that says who signed in.

  1. 1.Sam signs in

    with a username and a password

  2. 2.The server sends the cookie

    session=7f3a9c

  3. 3.The browser sends the cookie back

    with every later request

The server reads the session id in each request and finds Sam's record.

A wristband is hard to copy. A session id is only a value, so a copy works as well as the original. Anyone who has Sam's session id can send requests as Sam.

How a session id is made

An attacker who can guess a session id needs no password. The two lines of JavaScript each make a session id for Sam.

// Weakness: made from the username and the date
const sessionId = username + '-' + today

// Safer: a secure random number, written as letters and digits
const sessionId = crypto.randomBytes(32).toString('hex')

The first line joins the username and today's date. An attacker who knows the username sam can build the same session id and send the session id in a cookie. The server then treats the attacker as Sam.

The second line makes a secure random number, a number made in a way that nobody can predict.

  1. session=sam-2026-11-13made from the username and the date
  2. session=7f3a9c41d2e8b05f6amade from a secure random number
An attacker can work out the first session id and cannot guess the second.

A framework's session manager makes each session id from a secure random number and keeps the records. A reviewer reads closely any other code that makes a session id.

The settings on the cookie

The server sets the cookie with a header of the response.

Set-Cookie: session=7f3a9c; HttpOnly; Secure; SameSite=Lax

  1. Set-Cookie:the name of the header
  2. session=7f3a9cthe cookie, with the session id
  3. HttpOnlyJavaScript cannot read the cookie
  4. Securesent over HTTPS only
  5. SameSite=Laxkept off most requests other websites start
The Set-Cookie header, with three settings after the cookie.

HttpOnly is a cookie setting that stops JavaScript in the page reading the cookie. An attacker's JavaScript can get into a page, an attack named cross-site scripting that a later lesson teaches. With HttpOnly the attacker's JavaScript cannot read the cookie. The JavaScript can still send requests, and the browser adds Sam's cookie.

Secure is a cookie setting that makes the browser send the cookie only over HTTPS. Another person on the same network can read a cookie sent over plain HTTP.

SameSite is a cookie setting that tells the browser whether to send the cookie with requests started by other websites. The values Lax and Strict keep the cookie off most of those requests, which limits an attack named cross-site request forgery, where another website sends requests as Sam. A later lesson teaches the attack.

The line of JavaScript sets the same cookie, with one word for each setting.

res.cookie('session', sessionId, { httpOnly: true, secure: true, sameSite: 'lax' })

A framework's configuration can turn on the same settings (Framework configuration). A session id never goes in a URL, because a URL stays in the browser's history.

Ending a session

A server ends a session by deleting the record of the session. A session id with no record no longer works. Three events end a session:

  • The person signs out.
  • Nobody uses the session for a set time, such as 30 minutes.
  • A fixed time passes since sign-in, such as eight hours, so a stolen session id stops working.

Each piece of JavaScript runs when Sam signs out. sessions is where the server keeps every record.

// Weakness: the browser deletes the cookie, and the server keeps the session
res.clearCookie('session')

// Safer: the server deletes the session as well
sessions.delete(sessionId)
res.clearCookie('session')

The first piece deletes only the cookie in Sam's browser, so a copy of the session id still works. The second piece deletes the record on the server as well.

Two attacks on a session

Session hijacking is an attacker stealing a session id and using the session id to act as that person. An attacker can steal a session id through cross-site scripting or over plain HTTP. The defences are HttpOnly, Secure and HTTPS on every page.

Session fixation is an attacker giving a person a session id the attacker already knows, then waiting for the person to sign in. The sports shop gives every visitor a session id before sign-in, to keep the basket. An attacker gets a session id from the shop. Where the shop also accepts a session id in a URL, the attacker sends Sam a link with that session id. Sam opens the link, and the shop uses that session id for Sam.

Sam signs in. A server that keeps the same session id now treats the attacker as Sam. The defence is a new session id at every sign-in.

Examples of what to look for

  • A session id built from a username, the time or Math.random(), whose numbers can be predicted.
  • A session cookie set without httpOnly, secure or sameSite, or a session id in a URL.
  • Sign-in code that keeps the session id the visitor had before sign-in.
  • Sign-out code that deletes the cookie and leaves the session on the server.
  • A session with no time limit.
  • JavaScript in a page that reads the session cookie.

Check your understanding

  1. State what a session id is and where the browser keeps the session id.

    Show the answer

    A session id is a long random value that tells the server which session a request belongs to. The browser keeps the session id in a cookie.

  2. The sports shop sends Set-Cookie: session=7f3a9c; HttpOnly; SameSite=Lax. Which setting is missing, and what does the setting do?

    Show the answer

    Secure is missing. Secure makes the browser send the cookie only over HTTPS.

  3. Give one reason why a session id made from a username is a weakness.

    Show the answer

    An attacker who knows the username can work out the session id. The server then treats the attacker as that person.

Practise it

Challenges with a weakness this lesson teaches.