JWT Security Pitfalls: alg=none, Weak Secrets, Storage and Revocation
Avoid common JSON Web Token mistakes: algorithm confusion, weak HMAC secrets, missing claim validation, localStorage storage, long lifetimes.
Published December 4, 2026
JSON Web Tokens are everywhere in modern authentication, and so are the mistakes made with them. A JWT is just a signed (sometimes encrypted) set of claims; its security depends entirely on how you create, validate, store and expire it. Most real-world JWT vulnerabilities come from a handful of repeatable errors.
The concept
Validation errors are the most dangerous. Libraries that trust the alg header can be tricked: alg: none disables signature checks, and algorithm confusion attacks make a server verify an RS256 token as HS256 using the public key as an HMAC secret. Always pin the expected algorithm(s) and key. Also validate exp, nbf, iss and aud; a valid signature on a token meant for another service or that expired last week is still a rejection.
Lifecycle choices matter too. JWTs are stateless, so they cannot be revoked before expiry without extra machinery. Keep access tokens short-lived (minutes), use refresh tokens with rotation stored server-side or in httpOnly cookies, and maintain a denylist or token version for emergency revocation. Never put secrets in the payload: it is only base64url-encoded and readable by anyone.
Pitfalls and fixes
| Pitfall | Attack / risk | Fix |
|---|---|---|
| Trusting the alg header | alg=none or RS/HS confusion forgeries | Pin algorithms in verify() |
| Weak HS256 secret | Offline brute force, then forge tokens | ≥256-bit random secret or asymmetric keys |
| Not checking exp/aud/iss | Replay of expired or cross-service tokens | Validate all registered claims |
| Sensitive data in payload | Anyone can decode it | Only non-secret claims |
| Long-lived access tokens | Stolen tokens valid for days | 5-15 minute access tokens + refresh |
| Tokens in localStorage | Theft via XSS | httpOnly Secure cookies or in-memory tokens |
Token lifecycle
Revocation strategies
| Revocation approach | How | Trade-off |
|---|---|---|
| Short expiry | Tokens die quickly | Simple; delayed revocation |
| Denylist (jti) | Store revoked token IDs until expiry | Small lookup per request |
| Token version per user | Bump version on logout-all/password change | One cached lookup |
| Opaque tokens + introspection | Server-side state | Full control; more lookups |
Practical examples
Pin algorithms and validate claims (jose)
Never let the token choose how it is verified.
import { jwtVerify } from 'jose'
const { payload } = await jwtVerify(token, publicKey, {
algorithms: ['RS256'],
issuer: 'https://auth.example.com',
audience: 'orders-api',
clockTolerance: 30,
})PHP with firebase/php-jwt
Key objects bind the algorithm to the key.
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
$decoded = JWT::decode($jwt, new Key($publicKey, 'RS256'));
if ($decoded->aud !== 'orders-api' || $decoded->iss !== 'https://auth.example.com') {
throw new UnexpectedValueException('invalid token');
}Generate a strong HS256 secret
If you must use HMAC, use enough entropy.
openssl rand -base64 48Common mistakes
- Verifying with libraries in a mode that reads alg from the token.
- Short, human-chosen HMAC secrets.
- Using JWTs as long-lived sessions with no revocation plan.
- Accepting tokens issued for a different audience.
Key takeaways
- Pin algorithms and keys; validate exp, iss and aud.
- Use strong secrets or asymmetric keys.
- Keep access tokens short; rotate refresh tokens.
- Plan revocation and never put secrets in payloads.
FAQ
Are JWTs better than sessions?
Not inherently. Server-side sessions are simpler to revoke; JWTs help when many services must verify identity without a shared session store.
Should I encrypt JWTs?
Use JWE only if the payload must be confidential; otherwise keep payloads non-sensitive and rely on TLS.
How do I inspect a token safely?
Decode locally with a trusted tool; never paste production tokens into unknown websites.