Details
Steps to reproduce
pip install authlib==1.8.0 cryptography flask requests- Generate an RSA-2048 keypair; serialise the public key to PEM and prepend a single newline (or indent it, or prepend a BOM or a comment line).
- Confirm
authlib.jose.rfc7518.oct_key.POSSIBLE_UNSAFE_KEYSdoes not match those bytes withstartswith, whilecryptography.hazmat.primitives.serialization.load_pem_public_keyloads them. from authlib.jose import jwtthentok = jwt.encode({"alg":"HS256"}, {"sub":"attacker","role":"admin"}, mutated_pem)jwt.decode(tok, mutated_pem)— verification succeeds and the claims are returned.
Impact
Cryptographic signature-verification bypass leading to authentication bypass (CWE-347, algorithm
confusion). An unauthenticated attacker who knows only the issuer's public verification key — public by
definition, normally served at /.well-known/jwks.json or in IdP metadata — forges tokens with
arbitrary claims, including administrative roles and arbitrary subjects. In the PoC this yields an HTTP
200 and the application's admin secrets for sub=attacker.
Authlib had 103,356,930 PyPI downloads in the last month (pypistats /api/packages/authlib/recent,
read 2026-10-01), which is why I am reporting a bypass of a guard rather than only the documented
footgun it backstops.
Two notes for triage, in the interest of completeness:
authlib.joseemitsAuthlibDeprecationWarning("please use joserfc instead. It will be compatible before version 2.0.0"). It is deprecated but still shipped, still documented indocs/jose/jwt.rst, and still carries the download volume above.- joserfc is not affected by this mechanism. 1.7.5 was checked: keys carry an explicit
ktyin the data model, sojwt.decodewith anRSAKey/ECKeyandalg=HS256raisesInvalidKeyTypeErrorfor every one of the seven loader-accepted forms. Typing the key rather than inspecting its bytes is what makes the class unrepresentable there.
Suggested remediation
Either of these closes the class rather than one more byte form:
- Decide key family by type, not by bytes. Attempt
load_pem_public_key/load_pem_x509_certificate/load_ssh_public_key/load_der_public_keyon the candidate and reject it as anoctkey if any of them succeeds. This is the approach that makes joserfc immune and it cannot drift out of step with the loader, because it *is* the loader. - If a cheap recogniser must be kept, make it a substring scan rather than an anchored prefix check (
any(m in raw_key for m in POSSIBLE_UNSAFE_KEYS)) and add the-----BEGIN CERTIFICATE-----and DERSubjectPublicKeyInfocases. This is strictly weaker than (1): the recogniser can drift again the next timecryptographybecomes more tolerant.
Separately, and independent of this report: giving JsonWebToken.decode an algorithms= parameter
would let callers of the documented module-level object apply the mitigation the docs recommend without
constructing a second JsonWebToken.
Credit
Found by zer0d4y5. Happy to test a patch branch before release.