# 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` ([[raw/juangrate.cc/jwt-authentication-bypass-via-algorithm-confusion-15857|juangrate]]). Try with and without the trailing newline, PEM vs DER, PKCS#1 vs PKCS#8 (`jwt_tool <jwt> -X k -pk pub.pem`) ([[raw/robinx0.github.io/jwt-algorithm-confusion-none-hs256-rs256-mix-ups-and-kid-injection-15974|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 `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 ([[raw/medium.com/how-a-jwt-algorithm-confusion-vulnerability-allowed-a-4-500-authentica-15888|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. ## 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 - [[raw/portswigger.net/algorithm-confusion-attacks-web-security-academy-3849|Algorithm confusion attacks (PortSwigger)]] - [[raw/pentesterlab.com/how-jwt-libraries-block-algorithm-confusion-code-review-lessons-4723|How JWT Libraries Block Algorithm Confusion: Code Review Lessons]] - [[raw/pentesterlab.com/pentesterlab-another-jwt-algorithm-confusion-vulnerability-cve-2024-54-3830|PentesterLab: Another JWT Algorithm Confusion Vulnerability (CVE-2024-54150)]] - [[raw/pentesterlab.com/the-ultimate-guide-to-jwt-vulnerabilities-and-attacks-4105|The Ultimate Guide to JWT Vulnerabilities and Attacks]] - [[raw/workos.com/workos-jwt-algorithm-confusion-attacks-explained-3829|WorkOS: JWT algorithm confusion attacks explained]] - [[raw/auth0.com/auth0-critical-vulnerabilities-in-json-web-token-libraries-3833|Auth0: Critical vulnerabilities in JSON Web Token libraries]] - [[raw/advisories.gitlab.com/fast-jwt-incomplete-patch-re-enables-rsa-to-hs256-algorithm-confusion-14312|fast-jwt incomplete patch re-enables RSA to HS256 algorithm confusion (CVE-2026-107722)]] - [[raw/vulnerability.circl.lu/fast-jwt-rsa-to-hs256-algorithm-confusion-cve-2026-107722-12734|fast-jwt RSA-to-HS256 algorithm confusion (CVE-2026-107722)]] - [[raw/dev.to/cve-2026-22817-jwt-algorithm-confusion-in-hono-4966|CVE-2026-22817: JWT Algorithm Confusion in Hono]] - [[raw/dev.to/jwt-algorithm-confusion-attacks-cve-2026-22817-fix-guide-4366|JWT Algorithm Confusion Attacks: CVE-2026-22817 Fix Guide]] - [[raw/bishopfox.com/detecting-cve-2026-0265-at-scale-pan-os-cas-authentication-bypass-8655|Detecting CVE-2026-0265 at Scale: PAN-OS CAS Authentication Bypass]] - [[raw/github.com/keycloak-authentication-bypass-via-jwt-algorithm-confusion-cve-2026-11-14454|Keycloak authentication bypass via JWT algorithm confusion (CVE-2026-11800)]] - [[raw/hackerone.com/hackerone-1210502-jitsi-authentication-bypass-jwt-4553|HackerOne #1210502: Jitsi Authentication Bypass (JWT)]] - [[raw/brickell-tech.com/cve-2026-114615-authlib-jwt-algorithm-confusion-fix-bypass-13750|CVE-2026-114615: Authlib JWT algorithm confusion fix bypass]] - [[raw/sentinelone.com/cve-2024-33663-python-jose-algorithm-confusion-4103|CVE-2024-33663: Python-jose Algorithm Confusion]] - [[raw/github.com/joserfc-octkey-accepts-der-rsa-public-keys-as-hmac-secrets-hs256-forge-13751|joserfc OctKey accepts DER RSA public keys as HMAC secrets (HS256 forgery)]] - [[raw/cybersecurity-help.cz/multiple-vulnerabilities-in-lua-resty-jwt-14322|Multiple vulnerabilities in lua-resty-jwt]] - [[raw/blog.browserstay.com/the-jwt-algorithm-that-verified-nothing-alg-none-14010|The JWT algorithm that verified nothing (alg:none)]] - [[raw/lists.suse.com/pyjwt-hmac-forgery-with-known-public-key-cve-2026-102268-security-upda-14378|PyJWT HMAC forgery with known public key (CVE-2026-102268) security update]] - [[raw/github.com/authlib-accepts-openssh-keys-certs-as-hmac-secrets-14803|Authlib accepts OpenSSH keys/certs as HMAC secrets]] - [[raw/juangrate.cc/jwt-authentication-bypass-via-algorithm-confusion-15857|JWT authentication bypass via algorithm confusion]] - [[raw/medium.com/how-a-jwt-algorithm-confusion-vulnerability-allowed-a-4-500-authentica-15888|How a JWT Algorithm Confusion Vulnerability Allowed a $4,500 Authentication Bypass]] - [[raw/robinx0.github.io/jwt-algorithm-confusion-none-hs256-rs256-mix-ups-and-kid-injection-15974|JWT algorithm confusion: none, HS256/RS256 mix-ups, and kid injection]]