JSON Web Tokens (JWT) are widely used for stateless authentication and API authorization across single-page applications and microservices. Because a JWT carries its own header and claims from the client, subtle mistakes in how the server verifies the signature or checks claims can lead to authentication bypass and cross-service privilege escalation.
Four high-impact JWT implementation mistakes
During API security assessments and code reviews, four verification weaknesses account for the majority of JWT findings:
- Unrestricted `alg` headers and the `none` algorithm: older or misconfigured JWT libraries that trust the token header's `alg` field without an explicit server-side algorithm allowlist can accept unsigned `alg: "none"` tokens.
- Asymmetric/symmetric algorithm confusion (RS256 to HS256): if a server verifies tokens using a public RSA key but allows the caller to specify `HS256`, an attacker can sign a forged token using the public RSA key string as an HMAC secret.
- Weak HMAC signing secrets: signing `HS256` tokens with short, guessable strings or default development placeholders allows offline brute-force cracking from a single captured token.
- Missing `iss`, `aud`, and `exp` claim validation: verifying the cryptographic signature without checking the issuer (`iss`) and audience (`aud`) allows a valid token issued for one tenant, environment, or microservice to be replayed against another.
Designing safe token lifetimes and revocation
Because a self-contained JWT remains valid until its `exp` timestamp, long-lived access tokens cannot be revoked immediately upon logout, password reset, or role downgrade unless the server checks revocation state. Keep access token lifetimes short (5 to 15 minutes), store refresh tokens in `Secure`, `HttpOnly` cookies scoped to the refresh endpoint, and maintain a server-side session or token-version check for sensitive privilege changes.
In your JWT verification middleware, always hardcode the expected signing algorithm (for example, `algorithms: ['RS256']`), pin keys to a trusted internal JWKS store rather than arbitrary header-supplied `jku` URLs, and require strict `iss`, `aud`, and `exp` validation on every protected route.
Hardcode allowed signing algorithms in your JWT verification call, validate iss and aud claims explicitly, and keep access token lifetimes short.