Skip to content

Commit 74be971

Browse files
committed
fix(jwe): raise DecodeError when encrypted_key is None
1 parent f409f8f commit 74be971

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎src/joserfc/_rfc7516/message.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ def decrypt_recipient(alg: JWEAlgModel, enc: JWEEncModel, recipient: Recipient[A
243243

244244
# 8. When Key Agreement with Key Wrapping is employed, the agreed upon key
245245
# will be used to decrypt the JWE Encrypted Key.
246-
assert recipient.encrypted_key is not None
246+
if recipient.encrypted_key is None:
247+
raise DecodeError("Invalid encrypted key")
247248
cek = alg.unwrap_cek_with_auk(recipient.encrypted_key, agreed_upon_key)
248249
else:
249250
# 9. When Key Wrapping, Key Encryption, or Key Agreement with Key
@@ -256,5 +257,7 @@ def decrypt_recipient(alg: JWEAlgModel, enc: JWEEncModel, recipient: Recipient[A
256257
# to decrypt one of the per-recipient JWE Encrypted Key values to
257258
# obtain the CEK value.
258259
assert isinstance(alg, (JWEKeyWrapping, JWEKeyEncryption))
260+
if recipient.encrypted_key is None:
261+
raise DecodeError("Invalid encrypted key")
259262
cek = alg.decrypt_cek(recipient)
260263
return cek

‎tests/jwe/test_json.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ def test_decode_multiple_recipients(self):
7373
registry=registry,
7474
)
7575

76+
def test_general_json_without_recipients(self):
77+
obj = GeneralJSONEncryption({"enc": "A128CBC-HS256"}, b"i")
78+
obj.add_recipient({"alg": "RSA-OAEP"}, self.rsa_key)
79+
value = jwe.encrypt_json(obj, None)
80+
value["recipients"] = []
81+
self.assertRaises(DecodeError, jwe.decrypt_json, value, self.rsa_key)
82+
83+
def test_general_json_without_encrypted_key(self):
84+
obj = GeneralJSONEncryption({"enc": "A128CBC-HS256"}, b"i")
85+
obj.add_recipient({"alg": "RSA-OAEP"}, self.rsa_key)
86+
value = jwe.encrypt_json(obj, None)
87+
del value["recipients"][0]["encrypted_key"]
88+
self.assertRaises(DecodeError, jwe.decrypt_json, value, self.rsa_key)
89+
7690
def test_flattened_encryption(self):
7791
key = OctKey.generate_key(128)
7892
protected = {"enc": "A128CBC-HS256"}
@@ -88,3 +102,11 @@ def test_flattened_encryption(self):
88102
value = jwe.encrypt_json(obj0, None)
89103
obj3 = jwe.decrypt_json(value, key)
90104
self.assertEqual(obj3.plaintext, plaintext)
105+
106+
def test_flattened_json_without_encrypted_key(self):
107+
key = OctKey.generate_key(128)
108+
obj = FlattenedJSONEncryption({"enc": "A128CBC-HS256"}, b"hello world")
109+
obj.add_recipient({"alg": "A128KW"}, key)
110+
value = jwe.encrypt_json(obj, None)
111+
del value["encrypted_key"]
112+
self.assertRaises(DecodeError, jwe.decrypt_json, value, key)

0 commit comments

Comments
 (0)