alg=none and algorithm confusion: why the header alg is not trustworthy
In one line: How alg=none skips signature checking, how algorithm confusion uses a public key as an HMAC secret, and how to defend against both.
The most frequent JWT library vulnerabilities came from verifiers trusting the header's alg. The header is attacker-controlled input, so the algorithm must be decided by the server.
alg=none
alg=none is a registered value meaning "no signature". If a verifier accepts it, anyone can rewrite the payload, blank the signature and be accepted. Some libraries also accepted case variants such as none, None and NONE.
Allowlist (recommended)
allowed = {"RS256", "ES256"}
if header.get("alg") not in allowed:
reject("unexpected alg")
Blocklist (risky)
if header.get("alg") == "none":
reject() # misses case and alias variants
Algorithm confusion
An attacker takes an RS256 token, rewrites the header to HS256, and targets a verifier that computes an HMAC whenever it sees HS256. The attacker then uses the public RSA key, which is public by definition, as the HMAC secret and produces a valid tag. The verifier never learns that the key was never meant for HMAC.
Defence has two halves. First, pin the algorithms the service accepts and compare the header value against that list. Second, check that the key type (kty) matches the algorithm family: HMAC only with a secret, RSA only with kty=RSA, EC only with kty=EC. With both checks, confusion cannot be built.
Checklist
| Item | Safe handling |
|---|---|
| alg | Proceed only when it is on the allowlist; reject none and unknown values |
| kty | Confirm the key type matches the algorithm family |
| kid | Look it up only in the trusted key set, reject when absent |
| Key input | Keep PEM and JWK away from HMAC secret inputs |
| Signature | Treat failure as an exception and default to deny |
JWTlab's audit follows the same order: alg=none is a risk, and when the algorithm family and key type disagree it reports "cannot verify" instead of passing the signature.
Decoding and signature checks run right here in your browser. The token you paste is not stored.
Open the JWTlab decoderFrequently asked questions
We only use RS256, but our code can also verify HS256. Is that a risk?
Yes. An unused verification path lets an attacker change the header to reach it. Declare the allowlist explicitly and remove the code paths for algorithms you do not support.
Does checking kid prevent algorithm confusion?
Only partly. kid selects a key; it does not pin an algorithm. You need the alg allowlist and the kty match in addition to kid.
Sources
Read next
- JWT structure and Base64URL: reading the three parts correctlyWhat the header, payload and signature parts carry, how Base64URL differs from Base64, and where people go wrong when reading a token.
- HS256, RS256, ES256, EdDSA: choosing a signature algorithmSymmetric HMAC versus asymmetric RSA, ECDSA and EdDSA, key and signature sizes, and when to prefer PS256 or ES256.
- Validating exp, nbf, iat, aud and iss, and handling clock skewThe order for validating time claims, audience and issuer, how to set a leeway, and what to do about a token without exp.