Explore our new Cybersecurity Terms Glossary, explained by our experts.

View the Glossary View the Glossary
PRIVIA

Cybersecurity Terms Glossary

What Is JWT Algorithm Confusion?

Algorithm confusion is an attack in which a token tells the server which algorithm to verify it with. Switch RS256 to HS256 and the server’s public key becomes the signing secret.

Illustration for the cybersecurity term JWT Algorithm Confusion
Home / Blog / Cybersecurity Terms / What Is JWT Algorithm Confusion?
17 November 2026

What Is JWT Algorithm Confusion?

What Is JWT Algorithm Confusion?

A JSON Web Token carries its own instructions. The header names the algorithm the verifier should use, and for years libraries did exactly as they were told. Algorithm confusion exploits that obedience: the attacker rewrites the header, the server switches verification method, and a token that should be unforgeable becomes trivial to produce.

The attack is a design flaw in how the standard was first implemented rather than a weakness in any algorithm. It persists because the vulnerable pattern — call verify(token, key) and let the library decide — still looks correct to a reviewer.

The Two Classic Variants

Setting alg to none

RFC 7519 defines none as a valid algorithm for unsecured tokens. An attacker changes the header to {"alg":"none"}, edits the claims to "role":"admin", removes the signature, and leaves the trailing full stop. A library that honours the header verifies nothing and returns success. This was disclosed across multiple implementations in 2015, including node-jsonwebtoken, and it still appears in bespoke verification code and in older forks.

Switching RS256 to HS256

The more interesting variant exploits the difference between asymmetric and symmetric verification. With RS256 the server verifies using the issuer’s public key, which is public by definition. With HS256 the server verifies using a shared secret — and the library takes whatever key material it was given.

So the attacker takes the public key, uses it as the HMAC secret, signs a forged payload with HS256, and sets the header accordingly. The server loads the same public key, sees alg: HS256, computes HMAC-SHA256 with it, and the signatures match. The attacker has produced a valid token for any identity, using only information that was published on purpose.

Finding the Public Key

The attack needs the exact key bytes in the exact format the server uses, which is usually easy:

Formatting matters to the attacker: PEM with or without a trailing newline, DER, or the JWK representation will produce different HMAC outputs, so exploitation involves trying each. Tooling such as Burp Suite’s JWT extensions and jwt_tool automates both the variants and the formats.

Related Header Attacks

HeaderAbuse
kidPath traversal to a predictable file such as /dev/null, giving an empty key; or SQL injection where keys are looked up in a database
jkuPoint the verifier at a key set on a host the attacker controls, or at an internal URL for server-side request forgery
x5u / x5cSupply an attacker-generated certificate and sign with the matching private key
jwkEmbed a public key in the token itself and sign with its private counterpart

All four share one root cause with algorithm confusion: the verifier is taking key selection instructions from the thing it is supposed to be checking.

How to Verify Safely

  1. Pin the algorithm server-side. Pass an explicit allowlist to the verification call — algorithms: ['RS256'] — and reject anything else before any cryptographic work happens. Every maintained library supports this, and most now require it.
  2. Never share key material between algorithm families. A key registered for RSA verification should not be usable as an HMAC secret; enforce that in the key loading code, keyed on the alg field of the JWK.
  3. Reject jku, x5u, and jwk, unless you have a documented need and a strict host allowlist.
  4. Treat kid as hostile input. Look it up in a fixed map of known identifiers; never interpolate it into a path or a query.
  5. Validate claims after the signature. Check iss, aud, exp, and nbf, and do not short-circuit any of them in a code path used by internal services.
  6. Use a maintained library. Libraries updated after 2015 generally refuse none outright and require an explicit algorithm list. Hand-rolled verification is where these flaws survive.

Because a successful exploit yields authentication bypass and privilege escalation in one step, we test JOSE token handling on every API engagement within our web application security services, and it is still a finding often enough to be worth checking in your own code this week.

FAQ – Frequently Asked Questions

How Do I Test Whether My API Is Vulnerable?

Take a valid token, change the header algorithm to none, strip the signature, and send it. Then fetch the public key from your JWKS endpoint, sign a modified payload with HS256 using that key as the secret, and send that. If either is accepted, the verifier is trusting the header. Tools such as jwt_tool automate both checks.

Is HS256 Unsafe?

No. HMAC-SHA256 is a sound construction, and HS256 is appropriate when the issuer and verifier are the same service and share a strong random secret. The danger is accepting several algorithm families with one key loading path, and using a weak or guessable secret, which brute-force tools will recover from a captured token.

Does Rotating Keys Fix the Problem?

No. Algorithm confusion works with whatever the current public key is, so rotation changes nothing. The fix is to pin accepted algorithms, separate keys by purpose, and ignore key selection hints in the token header. Rotation remains good practice for other reasons, but it is not a mitigation here.

Should I Stop Using JWTs?

Not necessarily, though opaque session tokens validated against server-side state are simpler and revocable, and are a better fit for first-party web sessions. JWTs earn their place in distributed systems where stateless verification matters. If you keep them, keep lifetimes short, verify strictly, and have a revocation path for compromise.

Related Terms

You May Be Interested In These

What Is the SSH Protocol?

Cybersecurity Terms

17 November 2026

What Is the SSH Protocol?

Read More
What Is Persistence in Cybersecurity?

Cybersecurity Terms

17 November 2026

What Is Persistence in Cybersecurity?

Read More
What Is the OWASP IoT Top 10?

Cybersecurity Terms

17 November 2026

What Is the OWASP IoT Top 10?

Read More