alg none and algorithm confusion: two attacks that still work
Two JWT attacks have been known for years and still find targets: the unsigned token, where the header says alg: none, and algorithm confusion, where a token signed for one algorithm is verified as another. Neither breaks a cryptographic primitive. Both exploit a verifier that lets the token decide how it should be checked.
The common root is a generic verification API that takes the algorithm from the token header. That design is convenient because one function can handle many issuers. It is also dangerous because the attacker controls the header. The secure design is the opposite: the verifier is configured with an algorithm and a key, and the token's header is only compared against that configuration.
The unsigned token
In the none case the header is {"alg":"none","typ":"JWT"} and the signature segment is empty, so the token ends with a dot. The specification defines none for unsecured JWS, where integrity is provided by some other means such as a signed channel. It is not a valid choice for an access token on the open internet.
A vulnerable verifier sees alg none, skips the signature check, and accepts the claims. The payload can say anything. Some libraries require an explicit opt-in to accept none; others historically accepted it by default. The defence is to reject none unconditionally unless a specific application has a documented reason, and to test that rejection with a real token.
Algorithm confusion in detail
Suppose a service publishes an RSA public key and verifies RS256. The public key is public by design. An attacker takes the token, changes the header to {"alg":"HS256"}, and computes an HMAC over header.payload using the public key bytes as the secret. If the verifier follows the header, imports its configured key as an HMAC secret and compares, the MAC matches. The token is forged, but every check the verifier performs succeeds.
The attack generalises. Any pair of algorithms that share a key representation at the API boundary can be confused: RSA public key to HMAC secret, or an EC public key used as a shared secret. The conditions are a verifier that trusts the header algorithm and a key that can be reinterpreted as raw bytes. Both are avoidable.
| Attack | What the attacker changes | What the verifier must do |
|---|---|---|
| alg none | Header alg, empty signature | Reject none unless explicitly enabled |
| RS256→HS256 | Header alg, HMAC with public key | Fix the algorithm in config; check key type |
| PS256→RS256 | Header alg | Require the exact configured algorithm |
| ES256→ES384 | Header alg and curve assumptions | Match the key curve to the algorithm |
| Key injection via jku | Header jku/x5u URL | Ignore key URLs in the header; use a local JWKS |
The defence in code
Write the verification call with an explicit algorithm list: for example, verify with RS256 only, using this key, and require iss and aud. Do not pass algorithms=[header.alg]. Do not use a function that reads the algorithm from the token. In libraries that require a key resolver, resolve by kid within a trusted key set and assert that the resolved key type matches the algorithm.
Add a negative test for each attack. A token with alg none must fail. A token signed with HS256 using the public key text must fail. A token whose header names an algorithm you do not use must fail. These tests are cheap, they document the intent, and they catch a dependency upgrade that changes a default.
What about jku, jwk and x5u?
Some headers can point a verifier at a key: jku and x5u are URLs, and jwk embeds a key directly. A verifier that follows these is accepting a key chosen by the attacker, which is equivalent to no verification at all. Unless you implement a strict allowlist of key URLs and validate the fetched key, ignore those headers and use a key set you control.
The same principle applies to kid. It is a selector, not a locator with authority. If the key set has no matching kid, the correct response is to refresh once from the configured JWKS and then reject. It is not to fetch a URL derived from the kid or to fall back to the first key in the list.
Why this keeps happening
JWT libraries are often adopted through a quickstart that shows a one-line verify call. The quickstart works, the tests pass, and the algorithm policy is never revisited. Years later the service accepts more than its authors think. Security reviews that read the configuration but not the verification call miss it, because the configuration looks strict while the call passes the header algorithm through.
A useful audit question is: if I changed the alg header to every value in the registry, which ones would this service accept? The answer should be exactly the set you intended, and it should be a set of one or two, not a family. If the answer is hard to determine from the code, the code is too permissive.
The token is input. It may describe how it wants to be checked, but it must never decide.
Sources
- RFC 8725 §3.1 — Perform algorithm verification
- RFC 7515 §4.1.1 — alg header parameter
- OWASP JWT cheat sheet — none and confusion
Questions
Does alg none still work against real systems?
It still appears in audits and in libraries with permissive defaults, especially in code that passes the token's algorithm to a generic verify function. Most mainstream libraries now reject it by default, but the pattern survives in hand-written verification and in older or lightly maintained packages.
How does RS256 to HS256 confusion work?
The attacker changes alg to HS256 and signs the token with the RSA public key text as the HMAC secret. If the verifier selects the algorithm from the token and treats its configured public key as raw bytes, the HMAC check passes. The fix is to choose the algorithm from configuration and to import the key as the type that algorithm requires.
Is checking the alg header enough?
No. Checking that alg equals a configured value is necessary but not sufficient: the key must also match that algorithm's family, the issuer and audience must be validated, and the token must be current. Defence is a sequence of checks, not one comparison.
The audit flags alg none immediately; the verifier never follows the header algorithm on its own.
Open the verifierWritten against the JOSE specifications (RFC 7515–7519), RFC 7636 and RFC 8037; every example can be reproduced in the verifier linked above.
Back to the guides