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

PitfallAttack / riskFix
Trusting the alg headeralg=none or RS/HS confusion forgeriesPin algorithms in verify()
Weak HS256 secretOffline brute force, then forge tokens≥256-bit random secret or asymmetric keys
Not checking exp/aud/issReplay of expired or cross-service tokensValidate all registered claims
Sensitive data in payloadAnyone can decode itOnly non-secret claims
Long-lived access tokensStolen tokens valid for days5-15 minute access tokens + refresh
Tokens in localStorageTheft via XSShttpOnly Secure cookies or in-memory tokens

Token lifecycle

call /refreshissueAccess token10 minExpiresRefresh tokenhttpOnly cookie, rotatedNew access tokenold refresh invalidated
Short access tokens plus rotating refresh tokens limit exposure.

Revocation strategies

Revocation approachHowTrade-off
Short expiryTokens die quicklySimple; delayed revocation
Denylist (jti)Store revoked token IDs until expirySmall lookup per request
Token version per userBump version on logout-all/password changeOne cached lookup
Opaque tokens + introspectionServer-side stateFull 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 48

Common 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.

More Security articles