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
algdispatch). - 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, OIDCjwks_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 thekof 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; keepkid, setalg: 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
| Variant | Mechanism |
|---|---|
| RS256/PS256 → HS256 | RSA public key as HMAC secret (CVE-2016-5431 class) |
| ES256 → HS256 | EC public key (often recovered from one signature) as HMAC secret |
| Key-format guard bypass | Library 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 algorithms | App code reads alg from the header and passes it into the allow-list (Parse Server) |
| Asymmetric-family swap | RS256 vs PS256 on the same key; downgrade/quirk rather than direct forgery |
none / unknown alg | Same root cause, different branch: see JWT None Algorithm |
| RSA JWK as empty HMAC key | libjwt: 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
OctKeyuntil 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
algunlessalgorithmswas 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,algread 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 }), sononeor HS256-with-Google’s-key worked; fix hardcodes['RS256']. - PAN-OS / Panorama (CVE-2026-0265):
pan_auth_verifyinlibpanmp_mp.sodispatched on headeralgand 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
- Determine the issuer’s algorithm family from the header; collect the public key (JWKS, OIDC discovery) or recover it from 1-2 tokens.
- Re-sign a tampered token with
alg: HS256|HS384|HS512using 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 askof a new symmetric key. - Also try the token-chosen allow-list path (
none, unknown values) since it shares the root cause. - In code review, flag any
decode/verifycall whose allow-list mixes HMAC with RS/ES/PS/EdDSA, anyalgorithmsvalue 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
kidto keys that carry their own algorithm. - Use typed key objects (
KeyObject,RSAPublicKey, JWK withkty+alg) so the library can refuse cross-family use; validate the headeralgagainst 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.
Related
- JSON Web Tokens
- JWT None Algorithm
- JWT Header Injection
- JWT Empty Key Forgery
- JWT Secret Cracking
- Python JWT Library Vulnerabilities
- OpenID Connect Security
Sources
- Algorithm confusion attacks (PortSwigger) · original
- How JWT Libraries Block Algorithm Confusion: Code Review Lessons · original
- PentesterLab: Another JWT Algorithm Confusion Vulnerability (CVE-2024-54150) · original
- The Ultimate Guide to JWT Vulnerabilities and Attacks · original
- WorkOS: JWT algorithm confusion attacks explained · original
- Auth0: Critical vulnerabilities in JSON Web Token libraries · original
- fast-jwt incomplete patch re-enables RSA to HS256 algorithm confusion (CVE-2026-107722) · original
- fast-jwt RSA-to-HS256 algorithm confusion (CVE-2026-107722) · original
- CVE-2026-22817: JWT Algorithm Confusion in Hono · original
- JWT Algorithm Confusion Attacks: CVE-2026-22817 Fix Guide · original
- Detecting CVE-2026-0265 at Scale: PAN-OS CAS Authentication Bypass · original
- Keycloak authentication bypass via JWT algorithm confusion (CVE-2026-11800) · original
- HackerOne #1210502: Jitsi Authentication Bypass (JWT) · original
- CVE-2026-114615: Authlib JWT algorithm confusion fix bypass · original
- CVE-2024-33663: Python-jose Algorithm Confusion · original
- joserfc OctKey accepts DER RSA public keys as HMAC secrets (HS256 forgery) · original
- Multiple vulnerabilities in lua-resty-jwt · original
- The JWT algorithm that verified nothing (alg:none) · original
- PyJWT HMAC forgery with known public key (CVE-2026-102268) security update · original
- Authlib accepts OpenSSH keys/certs as HMAC secrets · original
- JWT authentication bypass via algorithm confusion · original
- How a JWT Algorithm Confusion Vulnerability Allowed a $4,500 Authentication Bypass · original
- JWT algorithm confusion: none, HS256/RS256 mix-ups, and kid injection · original