{"id":"GHSA-7vr2-2fgp-9w7p","summary":"Authlib incomplete algorithm-confusion fix permits HMAC forgery with prefixed public keys","details":"## Summary\n\n`OctKey.import_key()` carries the only guard that stops an asymmetric public key from being used as an\nHMAC secret in `authlib.jose`. That guard is `raw_key.startswith(POSSIBLE_UNSAFE_KEYS)` — anchored at\nbyte 0 — while the loader that consumes the identical bytes on the asymmetric path\n(`cryptography.hazmat.primitives.serialization.load_pem_public_key` / `load_pem_x509_certificate`)\nskips anything before the `-----BEGIN` line. A PEM public key or X.509 certificate carrying **one\nleading byte** — a newline, a space, four spaces of indentation, a UTF-8 BOM, or a comment line — is\ntherefore not recognised, falls through, and becomes the HMAC secret. An attacker holding only the\nissuer's public key then mints an `HS256` token with arbitrary claims that `jwt.decode()` accepts as\nauthentic.\n\nThis is an incomplete fix of **CVE-2024-37568 / GHSA-5357-c2jx-v7qh**. That advisory's fix is exactly\nthis guard (`git log -S 'POSSIBLE_UNSAFE_KEYS' -- authlib/jose/rfc7518/oct_key.py` returns a single\ncommit, `3bea812` \"fix: prevent OctKey to import ssh/rsa/pem keys\", referencing issue #654), and the\noriginal attack works again unchanged whenever the stored PEM does not start at offset 0.\n\nReproduced end-to-end on **1.8.0**, the newest release (published 2026-08-30), and on `origin/main`\n(`c529a61`), with `cryptography` 50.0.2.\n\n## Preconditions\n\nBoth are deployment properties; neither is attacker-controlled at request time, and the attacker needs\nno secret and no user interaction.\n\n1. **The relying party uses the documented module-level `jwt` object.** `from authlib.jose import jwt`\n   is built at `authlib/jose/__init__.py:55-72` with the full list\n   `[\"HS256\",\"HS384\",\"HS512\",\"RS256\",...,\"EdDSA\"]`, and `JsonWebToken.decode`\n   (`authlib/jose/rfc7519/jwt.py:80`) exposes **no `algorithms` parameter**, so a caller of the\n   documented object has no way to narrow it. This is the first code sample in\n   `docs/jose/jwt.rst:21-31`.\n\n   This hazard is already documented: `docs/jose/jwt.rst:73-79` says\n   \"This decoding method is insecure. By default `jwt.decode` parses the alg header. This allows\n   symmetric macs and asymmetric signatures.\", and `docs/jose/jwt.rst:94-98` says \"You should never\n   combine symmetric (HS) and asymmetric (RS, ES, PS) signature schemes.\" The point of this report is\n   **not** that the mixed list exists — it is that the `oct_key.py:81` backstop shipped for precisely\n   that documented hazard does not hold.\n\n2. **The verification key is passed as PEM/certificate text whose bytes do not begin at offset 0 with\n   `-----BEGIN `.** Each of the following forms was confirmed to forge, and each also still verifies a\n   genuine RS256/ES256 token with the identical bytes, so nothing in the deployment looks broken:\n   * one leading newline — what a triple-quoted Python literal produces\n   * one leading space\n   * four-space indentation — an indented YAML/JSON/TOML config block\n   * a UTF-8 BOM — a Windows-editor artifact\n   * a leading comment line\n   * all of the above applied to an X.509 `CERTIFICATE` PEM instead of a `PUBLIC KEY` PEM\n\n   A raw DER SPKI blob also bypasses the guard, but DER cannot be used for genuine asymmetric\n   verification through this path, so I exclude it as a non-deployable configuration and mention it\n   only as further evidence of the recogniser/loader gap.\n\n## Details\n\n`authlib/jose/rfc7518/oct_key.py:10-17`\n\n```python\nPOSSIBLE_UNSAFE_KEYS = (\n    b\"-----BEGIN \",\n    b\"---- BEGIN \",\n    b\"ssh-rsa \",\n    b\"ssh-dss \",\n    b\"ssh-ed25519 \",\n    b\"ecdsa-sha2-\",\n)\n```\n\n`authlib/jose/rfc7518/oct_key.py:78-84`\n\n```python\nraw_key = to_bytes(raw)\n\n# security check\nif raw_key.startswith(POSSIBLE_UNSAFE_KEYS):\n    raise ValueError(\"This key may not be safe to import\")\n\nkey = cls(raw_key=raw_key, options=options)\n```\n\n`bytes.startswith` is anchored. The loader on the asymmetric path is not:\n`authlib/jose/rfc7517/_cryptography_key.py:22-29` calls `load_pem_public_key` /\n`load_pem_x509_certificate`, both of which scan forward for the armour header. So the set of byte\nstrings the recogniser rejects is strictly smaller than the set the loader accepts, and the difference\nis exploitable.\n\nReach, for a caller of the documented object:\n\n* `authlib/jose/rfc7518/jws_algs.py:59` — `HMACAlgorithm.prepare_key(raw_data)` returns\n  `OctKey.import_key(raw_data)`; this is the whole of the HMAC key handling.\n* `authlib/jose/rfc7519/jwt.py:80` — `JsonWebToken.decode(self, s, key, claims_cls=None,\n  claims_options=None, claims_params=None)`; the algorithm comes from the token header and is checked\n  only against the instance's list.\n* `authlib/jose/__init__.py:55-72` — that list contains both HS\\* and RS\\*/ES\\*/PS\\*/EdDSA.\n\nSo `oct_key.py:81` is the only thing between a mixed allow-list and using the public key as an HMAC\nsecret, and it is bypassed by a single leading byte.\n\n## PoC\n\n`poc-authlib-e2e-http.py` stands up a Flask API whose only authentication is the documented\n`jwt.decode(token, PUBLIC_KEY)`, where `PUBLIC_KEY` is the issuer's RSA public key embedded as a\ntriple-quoted Python literal (hence one leading newline). The attacker is given **only** that public\nkey.\n\n```\nauthlib=1.8.0 cryptography=50.0.2 flask=3.1.3\nguard  = authlib/jose/rfc7518/oct_key.py:81   raw_key.startswith(POSSIBLE_UNSAFE_KEYS)\ncall   = authlib/jose/rfc7519/jwt.py:80       JsonWebToken.decode(s, key)  [no `algorithms` param]\nalgs   = authlib/jose/__init__.py:55+         module-level jwt = JsonWebToken([HS256..RS256..ES256..])\n\n[baseline 1] genuine RS256 token, role=user            -\u003e (403, '{\"error\":\"not admin\",\"sub\":\"alice\"}')\n[baseline 2] genuine RS256 token, role=admin           -\u003e (200, '{\"data\":\"DB_ROOT_PASSWORD=hunter2; STRIPE_SK=sk_live_deadbeef\",\"ok\":true,\"sub\":\"root\"}')\n[CONTROL 1 ] payload tampered, RS256 sig kept          -\u003e (401, '{\"error\":\"BadSignatureError: bad_signature: \"}')\n[CONTROL 2 ] HS256 forged with CANONICAL pubkey        -\u003e BLOCKED at key import: ValueError: This key may not be safe to import\n[CONTROL 3 ] JsonWebToken(['RS256']) vs forged token   -\u003e REJECTED: UnsupportedAlgorithmError\n\nATTACKER INPUT: only IDP_PUB_CANONICAL (public by definition), no private key.\nforged token header: {'alg': 'HS256', 'typ': 'JWT'}\n[ATTACK    ] HS256 forged with the PUBLIC key          -\u003e (200, '{\"data\":\"DB_ROOT_PASSWORD=hunter2; STRIPE_SK=sk_live_deadbeef\",\"ok\":true,\"sub\":\"attacker\"}')\n```\n\nThe three controls establish that the guard does work on a canonical PEM (so the leading byte is\nload-bearing), that ordinary signature verification is intact, and that pinning the algorithm is an\neffective mitigation.\n\nPer-form matrix, both key types, with a control proving genuine asymmetric verification still succeeds\non the exact same bytes:\n\n```\n### RSA-2048/RS256\n  canonical PEM  (CONTROL A)   loader=load_pem_public_key  startswith_guard=True  RS/ES-verify=OK   BLOCKED:ValueError\n  indented PEM   (4 spaces)    loader=load_pem_public_key  startswith_guard=False RS/ES-verify=OK   FORGED sub=superadmin role=admin\n  UTF-8 BOM prefix             loader=load_pem_public_key  startswith_guard=False RS/ES-verify=OK   FORGED sub=superadmin role=admin\n  leading comment line         loader=load_pem_public_key  startswith_guard=False RS/ES-verify=OK   FORGED sub=superadmin role=admin\n  leading blank line           loader=load_pem_public_key  startswith_guard=False RS/ES-verify=OK   FORGED sub=superadmin role=admin\n  leading single space         loader=load_pem_public_key  startswith_guard=False RS/ES-verify=OK   FORGED sub=superadmin role=admin\n  DER (SPKI) bytes             loader=load_der_public_key  startswith_guard=False RS/ES-verify=FAIL  FORGED sub=superadmin role=admin\n  [CONTROL C] JsonWebToken(['RS256']).decode(forged HS256) -\u003e REJECTED:UnsupportedAlgorithmError\n\n### EC-P256/ES256\n  identical results for all seven rows\n\nSame five leading-byte forms applied to an X.509 CERTIFICATE PEM: also FORGED, RS256-verify=OK.\n```\n\n### Steps to reproduce\n\n1. `pip install authlib==1.8.0 cryptography flask requests`\n2. Generate an RSA-2048 keypair; serialise the public key to PEM and prepend a single newline\n   (or indent it, or prepend a BOM or a comment line).\n3. Confirm `authlib.jose.rfc7518.oct_key.POSSIBLE_UNSAFE_KEYS` does not match those bytes with\n   `startswith`, while `cryptography.hazmat.primitives.serialization.load_pem_public_key` loads them.\n4. `from authlib.jose import jwt` then\n   `tok = jwt.encode({\"alg\":\"HS256\"}, {\"sub\":\"attacker\",\"role\":\"admin\"}, mutated_pem)`\n5. `jwt.decode(tok, mutated_pem)` — verification succeeds and the claims are returned.\n\n## Impact\n\nCryptographic signature-verification bypass leading to authentication bypass (CWE-347, algorithm\nconfusion). An unauthenticated attacker who knows only the issuer's public verification key — public by\ndefinition, normally served at `/.well-known/jwks.json` or in IdP metadata — forges tokens with\narbitrary claims, including administrative roles and arbitrary subjects. In the PoC this yields an HTTP\n200 and the application's admin secrets for `sub=attacker`.\n\nAuthlib had **103,356,930** PyPI downloads in the last month (pypistats `/api/packages/authlib/recent`,\nread 2026-10-01), which is why I am reporting a bypass of a guard rather than only the documented\nfootgun it backstops.\n\nTwo notes for triage, in the interest of completeness:\n\n* `authlib.jose` emits `AuthlibDeprecationWarning` (\"please use joserfc instead. It will be compatible\n  before version 2.0.0\"). It is deprecated but still shipped, still documented in `docs/jose/jwt.rst`,\n  and still carries the download volume above.\n* **joserfc is not affected by this mechanism.** 1.7.5 was checked: keys carry an explicit `kty` in the\n  data model, so `jwt.decode` with an `RSAKey`/`ECKey` and `alg=HS256` raises `InvalidKeyTypeError` for\n  every one of the seven loader-accepted forms. Typing the key rather than inspecting its bytes is what\n  makes the class unrepresentable there.\n\n## Suggested remediation\n\nEither of these closes the class rather than one more byte form:\n\n1. Decide key family by **type, not by bytes**. Attempt `load_pem_public_key` /\n   `load_pem_x509_certificate` / `load_ssh_public_key` / `load_der_public_key` on the candidate and\n   reject it as an `oct` key if any of them succeeds. This is the approach that makes joserfc immune and\n   it cannot drift out of step with the loader, because it *is* the loader.\n2. If a cheap recogniser must be kept, make it a substring scan rather than an anchored prefix check\n   (`any(m in raw_key for m in POSSIBLE_UNSAFE_KEYS)`) and add the `-----BEGIN CERTIFICATE-----` and\n   DER `SubjectPublicKeyInfo` cases. This is strictly weaker than (1): the recogniser can drift again\n   the next time `cryptography` becomes more tolerant.\n\nSeparately, and independent of this report: giving `JsonWebToken.decode` an `algorithms=` parameter\nwould let callers of the documented module-level object apply the mitigation the docs recommend without\nconstructing a second `JsonWebToken`.\n\n## Credit\n\nFound by zer0d4y5. Happy to test a patch branch before release.","aliases":["CVE-2026-114615"],"modified":"2026-10-27T15:47:24.216169990Z","published":"2026-10-27T15:38:48Z","database_specific":{"github_reviewed_at":"2026-10-27T15:38:48Z","nvd_published_at":"2026-10-22T22:18:04Z","cwe_ids":["CWE-184","CWE-347"],"severity":"HIGH","github_reviewed":true},"references":[{"type":"WEB","url":"https://github.com/authlib/authlib/security/advisories/GHSA-7vr2-2fgp-9w7p"},{"type":"ADVISORY","url":"https://nvd.nist.gov/vuln/detail/CVE-2026-114615"},{"type":"WEB","url":"https://github.com/authlib/authlib/pull/945"},{"type":"WEB","url":"https://github.com/authlib/authlib/commit/e3bd41cb29dcfee434453b135ffb487884baeedf"},{"type":"PACKAGE","url":"https://github.com/authlib/authlib"},{"type":"WEB","url":"https://github.com/authlib/authlib/releases/tag/v1.9.0"}],"affected":[{"package":{"name":"authlib","ecosystem":"PyPI","purl":"pkg:pypi/authlib"},"ranges":[{"type":"ECOSYSTEM","events":[{"introduced":"1.3.1"},{"fixed":"1.9.0"}]}],"versions":["1.3.1","1.3.2","1.4.0","1.4.1","1.5.0","1.5.1","1.5.2","1.6.0","1.6.1","1.6.10","1.6.11","1.6.12","1.6.2","1.6.3","1.6.4","1.6.5","1.6.6","1.6.7","1.6.8","1.6.9","1.7.0","1.7.1","1.7.2","1.8.0"],"database_specific":{"source":"https://github.com/github/advisory-database/blob/main/advisories/github-reviewed/2026/10/GHSA-7vr2-2fgp-9w7p/GHSA-7vr2-2fgp-9w7p.json","last_known_affected_version_range":"\u003c= 1.8.0"}}],"schema_version":"1.9.2","severity":[{"type":"CVSS_V3","score":"CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:N"}]}