From algorithm confusion to token theft — learn the most dangerous JWT vulnerabilities and the exact patterns to prevent them.
Some early JWT libraries accepted "alg": "none", meaning no signature required. An attacker could forge any token.
Fix: Always explicitly whitelist allowed algorithms:
jwt.verify(token, secret, { algorithms: ["HS256"] });
If your server uses RS256 (asymmetric), an attacker might send a HS256 token signed with your public key as the HMAC secret — since the public key is, well, public.
Fix: Never auto-detect the algorithm from the token header. Hardcode it server-side.
HS256 is only as strong as your secret. Short secrets can be brute-forced offline.
# Generate a strong secret
openssl rand -hex 32
A JWT without exp is valid forever. If stolen, there's no way to invalidate it.
Fix: Always set short expiration (15 min for access tokens) and use refresh tokens.
iss and aud claims