JWT Security Attacks

JSON Web Tokens (JWTs) are widely used for authentication and authorization, but they are secure only when implemented correctly. Many real-world security breaches occur due to misconfiguration, incorrect assumptions, or incomplete validation.

This page explains the most common JWT security attacks, how attackers exploit them, and how to properly defend against them in production systems.


1. alg=none Attack

JWTs include a header that specifies the signing algorithm. In early or poorly implemented libraries, servers may accept alg=none, meaning no signature verification is performed.

An attacker can modify the token payload, set the algorithm to none, and the server may trust the token as valid.

Real-World Impact

Mitigation


2. Token Replay Attack

JWTs are bearer tokens. Anyone who possesses a valid token can use it until it expires. If a token is stolen, it can be replayed multiple times.

Common Token Theft Vectors

Real-World Impact

Mitigation


3. Weak Signing Keys

JWTs signed with weak secrets (for example, short strings or common words) can be brute-forced by attackers. This is especially dangerous when using symmetric algorithms like HS256.

Attack Scenario

An attacker captures a JWT and attempts to guess the signing key offline. If the key is weak, the attacker can generate valid tokens.

Mitigation


4. Algorithm Confusion Attack

In some implementations, servers incorrectly allow both symmetric and asymmetric algorithms. An attacker can trick the server into verifying a token using the wrong algorithm.

Example

A server expects RS256 but accepts HS256. The attacker uses the public key as an HMAC secret to forge tokens.

Mitigation


5. Missing Claim Validation

Many systems verify the JWT signature but fail to validate critical claims such as:

This allows attackers to reuse expired tokens or tokens issued for a different service.

Mitigation


6. Storing JWTs Insecurely

Storing JWTs in localStorage or sessionStorage exposes them to XSS attacks. If JavaScript can access the token, so can injected malicious code.

Mitigation


7. Overloaded JWT Payloads

Developers sometimes store sensitive or excessive data inside JWT payloads, assuming they are encrypted. JWT payloads are only Base64 encoded, not encrypted.

Real-World Impact

Mitigation


Best Practices Summary


Final Thoughts

JWTs are powerful but unforgiving. Most JWT vulnerabilities are not flaws in the standard, but mistakes in implementation.

A secure JWT implementation requires correct cryptography, strict validation, and secure client-side handling.