~/posts/security/jwt-security-pitfalls.md

JWT Security Pitfalls Everyone Falls Into

The 'alg: none' attack, algorithm confusion, storing JWTs in localStorage — the implementation mistakes that turn a secure spec into a live vulnerability.

7 min read by admin authentication jwt security vulnerabilities
~/posts/security/jwt-security-pitfalls.md $ cat jwt-security-pitfalls.md

The alg:none Attack

The JWT spec allows alg: none — a token with no signature. If your library honours this without explicit blocking, an attacker can forge arbitrary tokens by setting the algorithm to none and stripping the signature.

Algorithm Confusion

If your server accepts both RS256 and HS256, an attacker can take your public RSA key, use it as an HMAC secret, sign a token with HS256, and your server may accept it.

localStorage is Not Safe

JWTs in localStorage are accessible to any JavaScript on the page. XSS = token theft = account takeover. Prefer HttpOnly cookies — inaccessible to JavaScript, still sent automatically on requests.