JWT Security Pitfalls Every Developer Should Know
JWTs are everywhere in modern applications. Here are the vulnerabilities that trip up even experienced teams.
JSON Web Tokens (JWTs) are the de-facto standard for stateless authentication in web applications. They’re easy to generate, easy to consume — and easy to get wrong.
The alg: none Attack
The JWT spec allows an algorithm value of none, meaning no signature is required. If your library doesn’t explicitly reject this, an attacker can craft a token with any payload and set alg to none.
{
"alg": "none",
"typ": "JWT"
}
Fix: Explicitly specify allowed algorithms in your library’s configuration. Never accept none.
Algorithm Confusion (RS256 → HS256)
Some libraries that support both RSA (RS256) and HMAC (HS256) can be confused by an attacker who:
- Knows the server’s public key
- Creates a token signed with that public key using HS256
If the server is configured to “auto-detect” the algorithm from the token header, it may verify the HS256 signature using the public key — which the attacker already knows.
Fix: Pin the algorithm server-side. Never trust the alg header in the token.
Weak Signing Secrets
An HMAC-signed JWT is only as strong as its secret. Secrets like secret, password, or company names are routinely cracked offline once a valid token is obtained.
# An attacker can bruteforce the secret if they have a token
hashcat -a 0 -m 16500 token.jwt wordlist.txt
Fix: Use a cryptographically random secret of at least 256 bits. For high-value tokens, prefer RS256 or ES256 so the signing key is never shared.
Missing Expiry Claims
A JWT without an exp claim is valid forever. If a token is leaked — through logs, a compromised client, or a shoulder-surf — there’s no way to invalidate it short of rotating the signing key.
Fix: Always set exp. For sensitive operations, keep lifetimes short (minutes, not days) and use refresh tokens.
Storing Tokens in localStorage
localStorage is accessible to any JavaScript on the page. A single XSS vulnerability hands an attacker all stored tokens.
Fix: Store tokens in HttpOnly cookies. They’re not accessible to JavaScript and are automatically sent with requests.
JWTs aren’t inherently insecure — they’re a tool that requires careful configuration. The JWT Inspector on this site can help you audit tokens from your own applications.