How it works
A JSON Web Token (JWT) is a signed piece of data that an API uses to recognize a caller. It has three parts separated by dots: a header, a payload with claims about the user, and a signature. The header names the signing algorithm in a field called alg.
The flaw appears when the server reads alg from the incoming token and obeys it. Until the signature is checked, the whole token is attacker-controlled input. A server that asks the token how to check the token has handed that decision to the attacker.
Two algorithm families matter here:
- RS256 is asymmetric. The issuer signs with a private key, and anyone can verify with the matching public key, which is published openly.
- HS256 is symmetric. A single shared secret is used for signing and for verifying.
Suppose the server is set up for RS256 and holds the public key. The attacker creates a token whose header says HS256 and signs it with that public key, treating the key file as an HMAC secret. If the server's code hands its key to a library that follows the header, the library computes an HMAC with the same public key. The signatures match, and the forged token is accepted.
A related mistake is accepting alg set to none. The JWT standard defines this value for unsigned tokens, and a server that allows it will trust a token that carries an empty signature.
What it looks like
The legitimate token for user_a decodes to this header and payload:
{ "alg": "RS256", "typ": "JWT" }
{ "sub": "user_a", "groups": ["viewer"] }The attacker builds a new token with these values instead:
{ "alg": "HS256", "typ": "JWT" }
{ "sub": "user_a", "groups": ["owner"] }They sign it with HMAC-SHA256, using the server's public key file as the secret, and send it as a normal bearer token. A vulnerable API now treats user_a as an owner. The result is account takeover or privilege escalation without stealing anything secret.
The vulnerable pattern in code, and the fix:
# Vulnerable: the algorithm comes from the token the attacker sent.
header = jwt.get_unverified_header(token)
claims = jwt.decode(token, VERIFY_KEY, algorithms=[header["alg"]])
# Fixed: the server decides. One algorithm, one key of the matching type.
claims = jwt.decode(token, RSA_PUBLIC_KEY, algorithms=["RS256"], audience="api.acme.test")How to test for it
- Decode a valid token and note the algorithm. The attack applies when it is asymmetric, such as RS256 or ES256.
- Obtain the public key. It is often published at a well-known URL such as
/.well-known/jwks.json. - Forge a token with
algchanged to HS256, a modified claim, and an HMAC signature made with the public key. The key bytes must match the server's copy exactly, including line breaks. - Send it to an endpoint that requires authentication. A normal response means the server is vulnerable.
- Test the unsigned case. Set
algtonone, remove the signature and try variations in capitalization.
Use a test account throughout.
How to fix it
Decide the algorithm on the server, and never from the token.
- Name the allowed algorithms in the verification call. One entry is best.
- Bind each key to one algorithm. An RSA public key should be usable only for RSA verification. Many libraries now refuse to treat an asymmetric key as an HMAC secret. Do not disable that check.
- Reject
none. No production token should be unsigned. - Do not let the token choose its key. Header fields such as
jkuandkidpoint at keys. Resolve keys from your own configuration only.
How it differs from related JWT problems
Algorithm confusion is a signature verification failure: the server checks a signature in the wrong way. It is separate from weak HMAC secrets, where the algorithm is right and the secret can be guessed, and from code that decodes the payload and never checks the signature at all.
A correctly verified token still only proves who the caller is. What that caller may do is a separate question, covered under broken function level authorization and, more generally, API security.