Validate JWT signature and claims
A JWT is only as trustworthy as its signature check. The classic bugs are accepting alg: none,
accepting any algorithm the token names, and parsing without verifying). Even a valid signature
proves nothing about audience or expiry unless those claims are checked too.
Procedure
Find where tokens are verified, and confirm the library call actually verifies:
rg -n "jwt\.(decode|verify)|jsonwebtoken|jose|PyJWT|jwt\.Parse" src/
In PyJWT, jwt.decode(token, key) verifies; jwt.decode(token, options={"verify_signature": False})
and a bare jwt.get_unverified_claims do not.
Pin the expected algorithm on the verify call; never let the token choose it:
# Python / PyJWT — explicit algorithms list
jwt.decode(token, key, algorithms=["RS256"], audience="api", issuer="https://auth.example.com")
# Node / jsonwebtoken
jwt.verify(token, publicKey, { algorithms: ["RS256"], audience: "api", issuer: "..." });
An empty or missing algorithms list permits none and HS/RS confusion.
Reject alg: none explicitly and reject a symmetric verify when you expect asymmetric: if the
token is signed HS256 with your public key as the HMAC secret, an attacker who has the public
key can forge tokens. Fix by fixing the algorithm, not by trusting the header.
Validate every relevant claim: exp (not expired), nbf (not premature), iat, aud (this
service, not another), iss (the expected issuer), and any azp.
Fetch keys from the issuer's JWKS over HTTPS and pin/refresh them; do not accept a jku or x5u
header pointing at an attacker-controlled URL, and validate kid against the known set.
Keep access-token lifetimes short (minutes) and pair with a checked refresh flow; verify the
signature on every request rather than caching "valid" forever.
Test with three degenerate tokens: alg:none, RS256→HS256 re-signed with the public key, and an
expired token. Each must be rejected.
alg=none token: eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiJ9.
python3 -c "import jwt; print(jwt.decode(open('/tmp/t.txt').read(), 'pub', algorithms=['HS256','RS256']))"
Pitfalls
jwt.decode(token, verify=False) / options={'verify_signature': False} disables the only check
that matters.
- Libraries that default to "any algorithm" are the root of alg-confusion CVEs; pass the list.
- Checking
exp in seconds vs milliseconds fails silently; know the unit.
aud is often omitted from validation, so a token minted for one service works on another.
kid path traversal (../../dev/null) can force an empty key if the verifier reads files.
- Base64url padding differences can make a round-trip comparison miss a tampered payload.
Verification
python3 - <<'PY'
import jwt
for name, tok in [("none","eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiJ9.")]:
try:
jwt.decode(tok, "k", algorithms=["RS256"], options={"verify_signature": True})
print(name, "ACCEPTED <-- BUG")
except jwt.InvalidTokenError as e:
print(name, "rejected:", type(e).__name__)
PY
Pass: the none and confusion tokens are rejected and the decode call names an explicit algorithm
list plus audience and issuer. Report the verify call, its algorithm pin, and the claims checked.
1---2name: validate-jwt-signature-and-claims3description: Use when a service trusts a JWT for identity or authorization. Checks signature verification, rejects alg=none and algorithm confusion, validates exp/aud/iss, and pins the key source.4metadata:5 source: https://skillmd.com/skills/jiayaoqijia/validate-jwt-signature-and-claims6---78# Validate JWT signature and claims910A JWT is only as trustworthy as its signature check. The classic bugs are accepting `alg: none`,11accepting any algorithm the token names, and parsing without verifying). Even a valid signature12proves nothing about audience or expiry unless those claims are checked too.1314## Procedure15161. Find where tokens are verified, and confirm the library call actually verifies:1718 rg -n "jwt\.(decode|verify)|jsonwebtoken|jose|PyJWT|jwt\.Parse" src/1920 In PyJWT, `jwt.decode(token, key)` verifies; `jwt.decode(token, options={"verify_signature": False})`21 and a bare `jwt.get_unverified_claims` do not.22232. Pin the expected algorithm on the verify call; never let the token choose it:2425 # Python / PyJWT — explicit algorithms list26 jwt.decode(token, key, algorithms=["RS256"], audience="api", issuer="https://auth.example.com")27 # Node / jsonwebtoken28 jwt.verify(token, publicKey, { algorithms: ["RS256"], audience: "api", issuer: "..." });2930 An empty or missing `algorithms` list permits `none` and HS/RS confusion.31323. Reject `alg: none` explicitly and reject a symmetric verify when you expect asymmetric: if the33 token is signed `HS256` with your *public* key as the HMAC secret, an attacker who has the public34 key can forge tokens. Fix by fixing the algorithm, not by trusting the header.35364. Validate every relevant claim: `exp` (not expired), `nbf` (not premature), `iat`, `aud` (this37 service, not another), `iss` (the expected issuer), and any `azp`.38395. Fetch keys from the issuer's JWKS over HTTPS and pin/refresh them; do not accept a `jku` or `x5u`40 header pointing at an attacker-controlled URL, and validate `kid` against the known set.41426. Keep access-token lifetimes short (minutes) and pair with a checked refresh flow; verify the43 signature on every request rather than caching "valid" forever.44457. Test with three degenerate tokens: `alg:none`, RS256→HS256 re-signed with the public key, and an46 expired token. Each must be rejected.4748 alg=none token: eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiJ9.49 python3 -c "import jwt; print(jwt.decode(open('/tmp/t.txt').read(), 'pub', algorithms=['HS256','RS256']))"5051## Pitfalls5253- `jwt.decode(token, verify=False)` / `options={'verify_signature': False}` disables the only check54 that matters.55- Libraries that default to "any algorithm" are the root of alg-confusion CVEs; pass the list.56- Checking `exp` in seconds vs milliseconds fails silently; know the unit.57- `aud` is often omitted from validation, so a token minted for one service works on another.58- `kid` path traversal (`../../dev/null`) can force an empty key if the verifier reads files.59- Base64url padding differences can make a round-trip comparison miss a tampered payload.6061## Verification6263 python3 - <<'PY'64 import jwt65 for name, tok in [("none","eyJhbGciOiJub25lIn0.eyJzdWIiOiJhZG1pbiJ9.")]:66 try:67 jwt.decode(tok, "k", algorithms=["RS256"], options={"verify_signature": True})68 print(name, "ACCEPTED <-- BUG")69 except jwt.InvalidTokenError as e:70 print(name, "rejected:", type(e).__name__)71 PY7273Pass: the `none` and confusion tokens are rejected and the decode call names an explicit algorithm74list plus audience and issuer. Report the verify call, its algorithm pin, and the claims checked.