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

Free lesson

JWTs checked properly

After this lesson: I can make an API refuse a forged or misdirected JWT.

60 min lesson12 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 with nothing at all

A JWT library once read the token's own header to decide how to check it. When a token said its algorithm was "none", the library checked nothing, and the API let it in.

Mistakes like this have appeared in real libraries and real APIs. In this lesson you forge two tokens for Ben, a teacher, and watch the weak Noticeboard hand you every student's marks.

Only your own lab

Warning

Forge tokens only for the Noticeboard in your own lab, which has no way out to the internet. Sending one to a system 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 make an API refuse a forged or misdirected JWT.

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 means
Bearer tokena token that works for whoever presents it
Signatureproof that a key holder made these exact bytes
alg nonea JWT header saying the token is not signed
Algorithm confusionchecking a token with a different algorithm from the one meant
Pinningthe verifier fixing the algorithm and key itself

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

An API checks every request's token

An API receives a bearer token in the Authorization header and believes nothing until it has checked it. RS256 means an RSA signature over SHA-256: the provider signs with its private key, and anybody can check with the public key from the JWKS.

First get a real token for Amira, meant for the Noticeboard, and send it to the safe copy.

Get Amira a token for the Noticeboard

Command
docker exec tools python3 steps/token.py amira noticeboard

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

Send it to the safe copy's API

Command
docker exec tools python3 steps/api.py safe amira

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

alg none: a token with no signature

A JWT header carries alg. The JWT rules allow alg none, an unsigned token, for cases where something else already protects it. A verifier that reads alg from the token and obeys it will accept a token with no signature at all.

forge.py writes one: claims for Ben, a teacher, with the right issuer and audience, and an empty signature.

Forge an unsigned token for Ben

Command
docker exec tools python3 steps/forge.py none

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

Send it to the weak copy

Command
docker exec tools python3 steps/api.py weak none

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

Send it to the safe copy

Command
docker exec tools python3 steps/api.py safe none

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

Try it: pin the algorithm

Code: python
# A verifier decides the algorithm itself. It never asks the token.
ALLOWED = {"RS256"}

headers = [{"alg": "RS256", "kid": "k1"}, {"alg": "none"}, {"alg": "HS256", "kid": "k1"}]
for header in headers:
    if header.get("alg") in ALLOWED:
        print(header["alg"], "-> check the signature with the RSA key named by kid")
    else:
        print(header.get("alg"), "-> refuse before reading anything else")
# Add "HS256" to ALLOWED. Which forged token from the lesson would now be checked with a key?

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

Algorithm confusion: the public key as a secret

HS256 is an HMAC: one shared secret both makes and checks it. If a verifier is given the provider's public key and lets the token choose HS256, it uses that public key as the HMAC secret.

The public key is public. So anybody can sign with it, and the weak verifier will agree. This is algorithm confusion. The fix is pinning: the verifier decides RS256 and which key, never the token.

Sign Ben a token with the public key

Command
docker exec tools python3 steps/forge.py hs256

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

Send it to the weak copy

Command
docker exec tools python3 steps/api.py weak hs256

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

Send it to the safe copy

Command
docker exec tools python3 steps/api.py safe hs256

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

A common mistake

Warning

Many people think a token is safe if its signature checks out. A valid signature only proves who made it. If the API also skips the audience, a genuine token meant for another app gets in. Check aud, iss and exp too.

A genuine token for the wrong app

Dex signs tokens for every app it serves. A token Amira got for the Timetable has a perfect signature and aud timetable. If the Timetable were careless or hostile, it could replay its users' tokens to the Noticeboard.

The safe copy checks that aud holds noticeboard, that iss is Dex and that exp has not passed, with 60 seconds' leeway for clocks.

Get Amira a token for the Timetable

Command
docker exec tools python3 steps/token.py amira

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

Replay it to the weak copy

Command
docker exec tools python3 steps/api.py weak amira-timetable

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

Replay it to the safe copy

Command
docker exec tools python3 steps/api.py safe amira-timetable

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: pin RS256 and pick the key by kid from the provider's JWKS, then check iss, aud and exp. Keep access tokens short-lived, as Dex's ten minutes are. Use a maintained library and tell it the expected algorithm.

To spot attacks, log every refusal with its reason. A token with alg none or HS256 at an RS256 API is never an accident. The app's log has a line for each token you sent.

Find the forged tokens in the log

Command
docker logs app

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

Assignment: a pinned verifier

Write verify_hs256(token, key, audience, now) for an API whose tokens are signed with HS256 and a secret key (bytes). Return the claims as a dictionary when the token is good. Otherwise raise ValueError with one of these messages, checking in this order:

  • "format" when the token is not three parts joined by dots, or a part is not base64url JSON.
  • "alg" when the header's alg is not exactly "HS256", whatever else it says.
  • "signature" when the HMAC-SHA256 of the first two parts, with the dot, under key is not the third part. Compare with hmac.compare_digest.
  • "aud" when aud (a string or a list) does not hold audience.
  • "exp" when there is no exp or it is 60 seconds or more before now.
Code: python
def verify_hs256(token, key, audience, now):
    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.