JWT PITFALLS
A token must not tell the verifier how to verify it
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.
// 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// The server decides; the token only gets checked
const claims = jwt.verify(token, publicKey, {
algorithms: ['RS256'], // pinned: never taken from the header
issuer: 'https://auth.example',
audience: 'billing', // a notes token is not a billing token
}); // exp and nbf checked when present
if (typeof claims.exp !== 'number') throw new Error('exp required');
// Short-lived access tokens (minutes), refresh tokens you can revoke,
// and one key per algorithm (select it by kid, never by alg). 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.
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
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.
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.
“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.
USED IN
Chapters that build on this lesson.