JWT PITFALLS

A token must not tell the verifier how to verify it

L216 MINSECURITY
PREDICT

Your API verifies RS256 tokens with the auth server's RSA public key, which anyone can download. The verifier reads alg from the token header and calls verify(token, publicKey). An attacker sends a token with alg HS256. What happens?

A JWT is three base64url parts: a header, the claims, and a signature over the first two. The header has an alg field, and the trap is believing it. If the verifier lets the token choose, alg: none means nothing gets checked, and alg: HS256 turns your public RSA key into an HMAC secret the attacker already holds.

A valid signature only tells you who issued the token. It does not say the token is still live or meant for you. So the server pins the algorithm and key in its own config, then checks exp, nbf, iss and aud. Keep access tokens short-lived, because you cannot take one back once it is issued.

▶ SIMULATION
AUTH SERVER
signs with: RS256 private key
public key: published at the JWKS URL
BILLING API
verifier: reads alg from the header; "none" means no signature to check; no claim checks
expects: alg RS256 · iss https://auth.example · aud billing
tokens it should have refused but accepted: 0
ATTACKER
has: the public key, old and cross-service tokens
acting as: nobody
last—
✓ billing accepts only live tokens its own issuer made for billing✓ a valid token is still accepted
SCHEDULE
01[BILLING]billing verifier: reads alg from the header; "none" means no signature to check; no claim checks
02[USER]user calls GET /invoices with a fresh billing token
03[ATTACKER]attacker sends a token with alg "none" and sub: admin
04[BILLING]report
Start of trusts the header scenario. 4 steps.
// The token tells the verifier how to verify it
const header = JSON.parse(b64(token.split('.')[0]));
const claims = jwt.verify(token, publicKey);   // old libraries: alg from the header
// alg: none  -> no signature checked
// alg: HS256 -> HMAC with publicKey as the secret, and the attacker has publicKey
// exp, iss, aud never looked at

Watch each row’s alg, iss, aud and exp against what billing expects. A red row is a token billing accepted that it should have refused; the invariant strip fails on the first one.

BREAK IT NOT YET

Billing's verifier was patched after the alg-none reports: it rejects "none". On a quiet day it accepts the user's token. Inject one attacker token that billing accepts anyway.

GOAL: billing accepts a token it should have refused

FIX IT NOT YET

Write verify(token, config) for the billing API. Return the claims object to accept, or null (or throw) to reject. config has alg ('RS256'), publicKey, issuer, audience ('billing'), now (seconds), decode(part) (base64url JSON to object), and crypto.verify(alg, key, signingInput, sig), where the signing input is the first two parts joined by a dot. Rules (RFC 7519 makes exp optional; this API requires it as its own policy): reject when now >= exp or exp is missing, reject when now < nbf, and aud may be a string or an array.

Mental model:

The token is untrusted input, header included. The server decides the algorithm and the key; the token only gets checked against that decision. A good signature answers “who made this?”, and the claims answer the rest: is it still live (exp, nbf), from the issuer I trust (iss), and meant for me (aud)?

IN PRODUCTION

Every API that accepts bearer tokens: OAuth access tokens and OIDC ID tokens, service-to-service tokens in a mesh, webhook and session tokens. Configure the library with an explicit algorithms list, the issuer and the audience (in jsonwebtoken that is algorithms, issuer and audience); fetch keys from your issuer's JWKS and select them by kid, never by alg. RFC 8725, the JWT Best Current Practices, is the checklist: it requires the caller to choose the algorithms, one algorithm per key, and an aud check when a token can reach more than one service.

IN INTERVIEWS

“How do you validate a JWT?” Do not stop at checking the signature. Say you pin the algorithm and key server-side (and why: alg none, RS256/HS256 confusion), then validate exp, nbf, iss and aud, and that aud stops a token for one service being replayed at another. Finish with lifetime: short access tokens, revocable refresh tokens, and a deny-list for the rare emergency.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: IDOR →