opyebJWTlab

alg=none and algorithm confusion: why the header alg is not trustworthy

Security · 7 min read

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

ItemSafe handling
algProceed only when it is on the allowlist; reject none and unknown values
ktyConfirm the key type matches the algorithm family
kidLook it up only in the trusted key set, reject when absent
Key inputKeep PEM and JWK away from HMAC secret inputs
SignatureTreat 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 decoder

Frequently 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