Skip to content

Commit 86d0091

Browse files
tonghuarootlepture
authored andcommitted
Reject empty oct key material and empty HMAC keys at sign/verify entry
An empty oct key produces a deterministic HMAC digest reproducible by any party, so accepting one in JWS verification lets an attacker forge HS256/ HS384/HS512 signatures with no secret knowledge. The path is reached whenever the application's secret resolves to "" or None (unset env var, missing config row, key finder fallback). Promote the existing SecurityWarning for keys smaller than 14 bytes to a hard reject at length == 0 inside OctKey.import_key, and add a defence-in-depth check in HMACAlgorithm.sign / .verify after get_op_key so OctKey instances constructed through non-import paths are also rejected. Add regression tests in test_oct_key.py (import-side) and test_compact.py (HMAC-side). Cross-language sibling of jwt/ruby-jwt CVE-2026-45363 / GHSA-c32j-vqhx-rx3x fix in lib/jwt/jwa/hmac.rb. Equivalent guards already exist in PyJWT 2.13.0, firebase/php-jwt, jjwt (256-bit minimum), and Microsoft IdentityModel (128-bit minimum). Signed-off-by: tonghuaroot <[email protected]>
1 parent 1e5b94d commit 86d0091

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎src/joserfc/_rfc7518/jws_algs.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,17 @@ def __init__(self, sha_type: t.Literal[256, 384, 512], recommended: bool = False
6262
def sign(self, msg: bytes, key: OctKey) -> bytes:
6363
# it is faster than the one in cryptography
6464
op_key = key.get_op_key("sign")
65+
if not op_key:
66+
# Defence-in-depth: OctKey.import_key rejects empty input, but
67+
# an OctKey can also be constructed directly through other
68+
# internal paths. An empty HMAC key produces a forgeable digest.
69+
raise ValueError("HMAC key must not be empty")
6570
return hmac.new(op_key, msg, self.hash_alg).digest()
6671

6772
def verify(self, msg: bytes, sig: bytes, key: OctKey) -> bool:
6873
op_key = key.get_op_key("verify")
74+
if not op_key:
75+
raise ValueError("HMAC key must not be empty")
6976
v_sig = hmac.new(op_key, msg, self.hash_alg).digest()
7077
return hmac.compare_digest(sig, v_sig)
7178

‎src/joserfc/_rfc7518/oct_key.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def import_key(
5858
password: Any = None,
5959
) -> "OctKey":
6060
key: OctKey = super(OctKey, cls).import_key(value, parameters, password)
61+
if not key.raw_value:
62+
# An empty oct key material produces a deterministic HMAC digest
63+
# that any party can reproduce, allowing trivial signature forgery
64+
# in JWS HS256/HS384/HS512 verification. Reject outright rather
65+
# than emitting a SecurityWarning that callers commonly suppress.
66+
raise ValueError("oct key material must not be empty")
6167
if len(key.raw_value) < 14:
6268
# https://csrc.nist.gov/publications/detail/sp/800-131a/rev-2/final
6369
warnings.warn("Key size should be >= 112 bits", SecurityWarning)

‎tests/jwk/test_oct_key.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ def test_generate_key_with_warnings(self):
110110
def test_import_key_with_warnings(self):
111111
self.assertWarns(SecurityWarning, OctKey.import_key, b"rfc")
112112

113+
def test_import_empty_key_rejected(self):
114+
# An empty oct key produces a deterministic HMAC digest reproducible
115+
# by any party, so accepting it would let an attacker forge HS256/
116+
# HS384/HS512 signatures whenever the application's secret resolves
117+
# to "" or None (unset env var, missing config, fallback).
118+
self.assertRaisesRegex(ValueError, "must not be empty", OctKey.import_key, "")
119+
self.assertRaisesRegex(ValueError, "must not be empty", OctKey.import_key, b"")
120+
113121
def test_key_eq(self):
114122
key1 = OctKey.generate_key()
115123
key2 = OctKey.import_key(key1.as_dict())

‎tests/jws/test_compact.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,19 @@ def test_detached_content(self):
105105
self.assertEqual(value, expected)
106106
obj = deserialize_compact(value, self.key, payload=b"foo")
107107
self.assertEqual(obj.payload, b"foo")
108+
109+
def test_hmac_rejects_empty_key(self):
110+
# An empty / None HMAC key produces a deterministic digest that any
111+
# attacker can reproduce, so signing and verifying must refuse the
112+
# key outright. Otherwise a misconfigured `JWT_SECRET=""` (or similar)
113+
# lets the attacker forge arbitrary HS256/HS384/HS512 tokens.
114+
self.assertRaisesRegex(ValueError, "must not be empty", OctKey.import_key, "")
115+
# Direct OctKey construction also blocked at the HMAC layer
116+
# (defence-in-depth): bypass OctKey.import_key by setting raw_value
117+
# post-hoc.
118+
empty_key = OctKey.import_key("rfc")
119+
empty_key._raw_value = b""
120+
from joserfc._rfc7518.jws_algs import HMACAlgorithm
121+
alg = HMACAlgorithm(256)
122+
self.assertRaisesRegex(ValueError, "must not be empty", alg.sign, b"msg", empty_key)
123+
self.assertRaisesRegex(ValueError, "must not be empty", alg.verify, b"msg", b"sig", empty_key)

0 commit comments

Comments
 (0)