JWT Algorithm Confusion

Forging a JWT by making the verifier run a different algorithm than the issuer intended, classically HS256 keyed with the server’s RSA/EC public key, so a value the attacker already holds becomes the HMAC secret.

How it works

The JOSE header is read before verification, and alg names the verification path. Libraries that expose an algorithm-agnostic verify(token, key) dispatch on that attacker-chosen value:

function verify(token, secretOrPublicKey){
  algorithm = token.getAlgHeader();
  if (algorithm == "RS256") { /* key used as RSA public key */ }
  else if (algorithm == "HS256") { /* same bytes used as HMAC secret */ }
}

An application that always passes its RSA public key reaches hmac.verify(public_key) when the token says HS256. HMAC accepts any byte string as a key, so an attacker who HMACs the forged header.payload with the same public-key bytes produces a matching tag.

Preconditions:

  • The verifier accepts both symmetric and asymmetric algorithms for the same key slot (explicitly via a mixed allow-list, or implicitly via alg dispatch).
  • The key is held as raw material (PEM string/bytes, raw JWK JSON) rather than a typed key object bound to one algorithm.
  • The attacker has the exact bytes the server uses: format (PEM vs JWK vs DER) and every non-printing byte, including trailing newlines, must match.

Getting the public key

  • Published: /jwks.json, /.well-known/jwks.json, OIDC jwks_uri, TLS certificate, mobile app bundles.
  • Derived from signatures: RSA moduli can be recovered from two tokens (rsa_sign2n / jwt_forgery.py, docker run --rm -it portswigger/sig2n <token1> <token2>); ECDSA yields two candidate public keys from a single signature. Both give candidate keys to try as HMAC secrets.
  • Encoding chain for JWKS-published keys: rebuild the JWK (n, e) as PEM, then base64-encode the whole PEM as the k of a symmetric JWK; using JWK components directly or skipping PEM yields different bytes and a non-matching HMAC. Burp JWT Editor: import JWK → “Copy Public Key as PEM” → Decoder → new symmetric key; keep kid, set alg: HS256 (juangrate). Try with and without the trailing newline, PEM vs DER, PKCS#1 vs PKCS#8 (jwt_tool <jwt> -X k -pk pub.pem) (robinx0).

Variants

VariantMechanism
RS256/PS256 → HS256RSA public key as HMAC secret (CVE-2016-5431 class)
ES256 → HS256EC public key (often recovered from one signature) as HMAC secret
Key-format guard bypassLibrary tries to detect “this is a public key” and refuse it as an HMAC secret, but the denylist misses an encoding (leading byte, OpenSSH, DER, JWK JSON)
Header-driven algorithmsApp code reads alg from the header and passes it into the allow-list (Parse Server)
Asymmetric-family swapRS256 vs PS256 on the same key; downgrade/quirk rather than direct forgery
none / unknown algSame root cause, different branch: see JWT None Algorithm
RSA JWK as empty HMAC keylibjwt: key compatibility checked against JWK alg hint, not kty, so an RSA JWK without alg becomes a zero-length HMAC key: see JWT Empty Key Forgery

Why “public key looks like PEM” guards keep failing

Libraries that keep a single raw-key API try to refuse asymmetric material as an HMAC key by string inspection. Each guard is an encoding denylist and each has been bypassed:

  • fast-jwt: CVE-2026-34950 added key.trim() before the anchored regex /^-----BEGIN(?: (RSA))? PUBLIC KEY-----/. trim() only strips ECMAScript whitespace, so any non-whitespace leading byte (control char, zero-width Unicode, # comment, PGP wrapper) falls through to the HMAC path: CVE-2026-107722, verified on 6.2.2, fixed in 6.3.0.
  • Authlib: prepending newlines/spaces to PEM defeated the check (CVE-2026-114615, incomplete fix of CVE-2024-37568); OpenSSH keys and certificates were still accepted as OctKey until 1.9.5.
  • python-jose: OpenSSH ECDSA keys accepted as HMAC secrets (CVE-2024-33663, same class as PyJWT CVE-2022-29217).
  • joserfc: DER-encoded RSA public keys imported as oct keys (GHSA-vvgp-wfq3-pjxj, fixed 1.7.6).
  • PyJWT: raw public JWK / JWKS JSON containers accepted as HMAC secrets when the allow-list mixes HS and RS/ES (CVE-2026-102268 family, fixed 2.14/2.15).

Python details live in Python JWT Library Vulnerabilities. The lesson generalises: content-sniffing is not a fix. Bind key type to algorithm.

Notable cases

  • Original disclosure (2015): node-jsonwebtoken, pyjwt, namshi/jose, php-jwt and jsjwt all let the token pick HS256 while the app passed an RSA public key. jsonwebtoken < 4.2.2 trusted the header by default; PyJWT < 1.5.0 used the header alg unless algorithms was passed.
  • xmidt-org/cjwt (CVE-2024-54150): cjwt_decode(text, len, opts, key, keylen, ...) takes no algorithm; identical call shape for RS and HS examples, alg read from header JSON.
  • Hono < 4.11.4 (CVE-2026-22817, CVSS 8.2): JWT middleware did not enforce the configured algorithm; RS256 deployments accepted HS256 signed with the public key. Affects Workers/Deno/Bun deployments.
  • Parse Server (CVE-2026-27804, CVSS 9.3): Google/Apple/Facebook OAuth adapters did const { alg } = getHeaderFromToken(token); jwt.verify(token, googlePublicKey, { algorithms: alg }), so none or HS256-with-Google’s-key worked; fix hardcodes ['RS256'].
  • PAN-OS / Panorama (CVE-2026-0265): pan_auth_verify in libpanmp_mp.so dispatched on header alg and accepted HS256 when Cloud Authentication Service was attached; pre-auth login as any user on GlobalProtect or the management interface.
  • Keycloak (CVE-2026-11800): algorithm confusion in the JWT Authorization Grant lets a client with valid credentials forge assertions and mint access tokens for any linked federated user.
  • Jitsi Meet < 2.0.5963 (GHSA-45ff-37jm-xjfx): Prosody module allowed symmetric algorithms when asymmetric signing was configured; public key as HMAC secret granted access to protected rooms.
  • lua-resty-jwt: forged tokens via public-key PEM text and arbitrary JWT forging via algorithm confusion (CWE-347), among seven bugs.
  • Microservice API gateway ($4,500 bounty): identity service issued RS256 and published the public key; the gateway’s verification did not pin the algorithm, so a guest account forged an HS256 admin token (Medium).

Detection and testing

  1. Determine the issuer’s algorithm family from the header; collect the public key (JWKS, OIDC discovery) or recover it from 1-2 tokens.
  2. Re-sign a tampered token with alg: HS256|HS384|HS512 using several encodings of the key: PEM with and without trailing \n, PKCS#1 vs SPKI, raw JWK JSON, DER. Burp JWT Editor: import JWK → export PEM → base64 → use as k of a new symmetric key.
  3. Also try the token-chosen allow-list path (none, unknown values) since it shares the root cause.
  4. In code review, flag any decode/verify call whose allow-list mixes HMAC with RS/ES/PS/EdDSA, any algorithms value derived from the token, and any raw string/bytes key shared across algorithm families.

Defenses

  • Pin algorithms from configuration, never from the token: jwt.verify(tok, pub, { algorithms: ['RS256'] }). RFC 8725 §3.1: libraries MUST let callers specify allowed algorithms and MUST NOT use others, and each key is used with exactly one algorithm.
  • Never mix HMAC and asymmetric algorithms on one verifier. If both are needed, route by kid to keys that carry their own algorithm.
  • Use typed key objects (KeyObject, RSAPublicKey, JWK with kty + alg) so the library can refuse cross-family use; validate the header alg against the key’s algorithm, not the other way round.
  • Library-side patterns that work: support one algorithm family only (brianvoe/sjwt, Corviz/jwt); require the caller’s algorithm and reject a mismatched header (garyf/json_web_token, nowakowskir/php-jwt); check header-key consistency and then verify with the developer-specified algorithm (auth0/java-jwt).
  • Prove the fix with negative tests: an HS256 token signed with the public key, none, and an unknown alg must all fail.

Sources