Skip to content

Commit 4ff30db

Browse files
authored
Merge commit from fork
An empty key let anyone forge HS256, HS384 and HS512 signatures. Require joserfc 1.6.8, which has the same check.
1 parent 9a86ff3 commit 4ff30db

4 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎authlib/jose/rfc7518/oct_key.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ def validate_raw_key(cls, key):
6565

6666
@staticmethod
6767
def _check_key(raw_key):
68+
# Per RFC 7518 §3.2, an HMAC key must be at least as long as the hash output.
69+
if not raw_key:
70+
raise ValueError("oct key material must not be empty")
71+
6872
# PEM parsers accept leading text, including comments and whitespace.
6973
if any(marker in raw_key for marker in POSSIBLE_UNSAFE_KEYS):
7074
raise ValueError("This key may not be safe to import")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Security
2+
~~~~~~~~
3+
4+
- ``authlib.jose`` rejects empty HMAC keys with a ``ValueError``. An empty key
5+
used to let anyone forge HS256, HS384 and HS512 signatures. The minimum
6+
``joserfc`` version is now 1.6.8, which has the same check.
7+
`GHSA-8m4g-j4wp-wgcg <https://github.com/authlib/authlib/security/advisories/GHSA-8m4g-j4wp-wgcg>`_

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ description = "The ultimate Python library in building OAuth and OpenID Connect
88
authors = [{name = "Hsiaoming Yang", email="[email protected]"}]
99
dependencies = [
1010
"cryptography>=45.0.1",
11-
"joserfc>=1.6.1",
11+
"joserfc>=1.6.8",
1212
]
1313
license = {text = "BSD-3-Clause"}
1414
requires-python = ">=3.10"

‎tests/jose/test_jwt.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ def test_reject_pem_key_as_hmac_secret(prefix, key_format):
5151
jwt.decode(forged, key)
5252

5353

54+
@pytest.mark.parametrize("alg", ["HS256", "HS384", "HS512"])
55+
@pytest.mark.parametrize(
56+
"key",
57+
[
58+
b"",
59+
"",
60+
{"kty": "oct", "k": ""},
61+
OctKey.import_key({"kty": "oct", "k": ""}),
62+
lambda header, payload: "",
63+
],
64+
ids=["bytes", "str", "jwk", "key", "callable"],
65+
)
66+
def test_reject_empty_hmac_key(alg, key):
67+
hash_alg = getattr(hashlib, f"sha{alg[2:]}")
68+
signing_input = (
69+
json_b64encode({"alg": alg}) + b"." + json_b64encode({"sub": "attacker"})
70+
)
71+
signature = hmac.new(b"", signing_input, hash_alg).digest()
72+
forged = signing_input + b"." + urlsafe_b64encode(signature)
73+
74+
with pytest.raises(ValueError, match="must not be empty"):
75+
jwt.decode(forged, key)
76+
77+
with pytest.raises(ValueError, match="must not be empty"):
78+
jwt.encode({"alg": alg}, {"sub": "alice"}, key)
79+
80+
5481
def test_encode_sensitive_data():
5582
# check=False won't raise error
5683
jwt.encode({"alg": "HS256"}, {"password": ""}, "k", check=False)

0 commit comments

Comments
 (0)