Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Sessions and cookies done safely

After this lesson: I can judge whether a web app keeps its sessions safely.

60 min lesson6 commandsIdentity and access lab: OpenID Connect, OAuth 2.0 and JWTs

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

Signed in on the library computer

Amira signs in to her college noticeboard on a library computer, reads a message and presses Log out. Ten minutes later someone else sits down at the same computer.

Whether they can get into her account depends on how the app keeps her session: the record that says this browser belongs to Amira. This lesson looks at that record and three ways to get it wrong.

Only your own lab

Warning

Every attack here is against the weak copy of the Noticeboard in your own lab, which has no way out to the internet. Trying it on a site you do not own, without written permission, is a crime in the UK under the Computer Misuse Act 1990.

By the end of this lesson

Note

I can judge whether a web app keeps its sessions safely.

Starter

Have a go

Starter

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Key words

WordWhat it meansExample
Session ida random value that names one signed-in browser
Cookie flagan attribute that limits where a cookie goesHttpOnly
Session fixationmaking a victim sign in on an id you already know
Rotationissuing a new session id at sign-in
Idle timeoutending a session nobody has used for a while

The cells students fill in are left blank in this preview.

Three flags limit where the key goes

A session cookie is sent with every request, so its flags decide where the key can travel. Secure sends it over HTTPS only. HttpOnly hides it from script, so a cross-site scripting bug cannot read it. SameSite=Lax keeps it off most requests started by other sites.

A name starting __Host- makes the browser insist on Secure, Path=/ and no Domain. Then no other site under the same domain can set it. The lab runs the app twice: port 8000 is weak, port 8001 safe.

Read both copies' session cookies

Command
docker exec tools python3 steps/cookies.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Worked example: what cookies.py printed

CopySet-Cookie lineMissing
weak, port 8000sid=<random>; Path=/Secure, HttpOnly, SameSite
safe, port 8001__Host-sid=<random>; Path=/; Secure; HttpOnly; SameSite=Laxnothing

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Try it: spot a missing flag

Code: python
# Which of the three flags does this Set-Cookie line carry?
line = "sid=Zq81; Path=/; HttpOnly"

parts = [one.strip() for one in line.split(";")]
names = [one.split("=")[0].lower() for one in parts[1:]]
for flag in ["Secure", "HttpOnly", "SameSite"]:
    if flag.lower() in names:
        print(flag, "present")
    else:
        print(flag, "MISSING")
# Add "; Secure; SameSite=Lax" to the line and run it again.

Make a free account to save your work and track your progress.

A new id at sign-in stops fixation

Many apps hand every visitor a session before they sign in, for a basket or a form. Session fixation abuses that. Dan visits and gets an id, then gets that same id into Amira's browser, through a shared computer or a page that sets cookies. When Amira signs in, a weak app marks that session as hers, and Dan holds it.

The fix is rotation: throw the old id away at sign-in and issue a new one. Do the same whenever a user gains rights.

Fix a session on the weak copy

Command
docker exec tools python3 steps/fixation.py weak

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try the same on the safe copy

Command
docker exec tools python3 steps/fixation.py safe

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A common mistake

Warning

Many people think a random session id is safe whoever chose it. Randomness stops guessing, not planting: if the id existed before sign-in, whoever knew it then knows it now. Issue a new id at every sign-in.

Log out on the server, and time out

Pressing Log out must end the session on the server. Telling the browser to forget the cookie is not enough: a copy taken earlier, from a shared computer or a stolen laptop, would still work.

Two timers limit what a stolen id is worth. An idle timeout ends a session nobody has used, often after 15 to 30 minutes. An absolute limit ends every session after a fixed time, such as 8 hours. The safe copy ends a session after 15 idle minutes.

Log out, then reuse the cookie

Command
docker exec tools python3 steps/logout.py weak

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The same on the safe copy

Command
docker exec tools python3 steps/logout.py safe

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Fixing it and spotting it

The fixes are all in the safe copy. It sets the three flags and a __Host- name and issues a new id at sign-in. It deletes the session at Log out and ends idle sessions.

To spot an attack, log each sign-in with a short prefix of the session id. One id that was handed to one browser and signed in from another is a warning. The app's log shows every decision both copies made, each line starting with a time.

Read the app's log

Command
docker logs app

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: check a Set-Cookie line

A Set-Cookie line is a name and value, then attributes separated by ;. Write two functions.

parse_cookie(line) returns a dictionary with name and value, plus each attribute in lower case: a flag such as Secure becomes "secure": True, and a pair such as SameSite=Lax becomes "samesite": "Lax". Strip the spaces round each part.

problems(line) returns a list of what a session cookie lacks: "Secure", "HttpOnly" and "SameSite", in that order, for each that is missing. Then, if the name starts with __Host-, add "__Host- rules" when the cookie is not Secure, its path is not /, or it has a Domain.

Code: python
def parse_cookie(line):
    return {}


def problems(line):
    return []

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.