Lesson 3 of 9 · 20m
Key Confusion, JWKS, and Key Rotation
How services learn which keys to trust, how kid lookup goes wrong, and how to rotate signing keys without logging everyone out.
Every JWT system eventually becomes a key-management system. The issuer rotates signing keys for hygiene and incident response; verifiers need to keep up without downtime and without trusting keys they shouldn't. The standard mechanism is JWKS — and it has its own failure modes.
JWKS: the trust distribution channel
The issuer publishes its public keys at a well-known HTTPS endpoint:
// GET https://auth.example.io/.well-known/jwks.json
{"keys": [
{"kty": "RSA", "kid": "key-2024-11", "use": "sig", "alg": "RS256",
"n": "0vx7agoebGc...", "e": "AQAB"},
{"kty": "RSA", "kid": "key-2025-01", "use": "sig", "alg": "RS256", ...}
]}The verifier flow: read kid from the token header, look it up in the cached JWKS, verify. On cache miss, refetch once (the key may be new) — then fail if still absent.
Where kid lookup goes wrong
kid is attacker-controlled input — the attacker writes the header. Real findings I've seen:
- Path traversal: a verifier doing
open(f"keys/{kid}.pem")—kid: ../../../etc/passwdor, worse, a path to a file whose contents are a valid key the attacker controls. - SQL injection:
kidinterpolated into a key-store query. - X5c confusion: the
x5cheader embeds a certificate chain in the token itself. Verifiers that trust it are letting the attacker supply the verifying key. RFC 7515 allows it; you must not. - jku/x5u SSRF: headers pointing at attacker-hosted JWKS URLs. Same story — key source must be pinned by config, never taken from the token.
The rule is absolute: the set of acceptable keys comes from configuration or a pinned, authenticated JWKS URL. Nothing in the token influences where keys come from — only which key within that trusted set is selected.
Rotation without the thundering logout
The safe rotation dance:
1. Generate key B. Publish B in JWKS alongside A. (verifiers see both)
2. Wait for verifiers' cache TTL to pass. (everyone trusts B)
3. Start signing new tokens with B. (kid flips to B)
4. Wait for the max lifetime of A-signed tokens. (old tokens die naturally)
5. Remove A from JWKS.Skip step 4 and every active session dies at once — I've watched a company log out 40k users mid-checkout because someone rotated "instantly." Skip step 2 and verifiers with stale caches reject valid tokens, same outcome, more confusion.
Field notes
- Cache JWKS aggressively but bound the refetch-on-miss: attackers can spam unknown
kidvalues to force fetch loops — rate-limit refetches per interval. - If the JWKS endpoint is compromised, everything is compromised — it deserves the same protection as the signing key distribution plane: separate credentials, tight monitoring, immutable-ish deploys.
- Keep an emergency revocation story: document how you force-rotate in incident response before you need it. Rehearse it once.
- Verifying
issincludes the trailing-slash problem:https://auth.example.iovshttps://auth.example.io/are different strings to a strict comparison but the same issuer to a human. Decide once, everywhere.
Was this lesson helpful?