Skip to content

Commit af6272c

Browse files
committed
Test now uses monkeypatch and checks error message text
1 parent 5b23bd3 commit af6272c

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

tests/test_jwe.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -527,33 +527,26 @@ def test_kid_header_not_present_when_not_provided(self):
527527
assert "kid" not in header
528528

529529
@pytest.mark.skipif(AESKey is None, reason="No AES backend")
530-
def test_jwe_with_excessive_data(self):
530+
def test_jwe_with_excessive_data(self, monkeypatch):
531531
enc = ALGORITHMS.A256CBC_HS512
532532
alg = ALGORITHMS.RSA_OAEP_256
533-
import jose.constants
534-
old_limit = jose.constants.JWE_SIZE_LIMIT
535-
try:
536-
jose.constants.JWE_SIZE_LIMIT = 1024
537-
encrypted = jwe.encrypt(b"Text"*64*1024, PUBLIC_KEY_PEM, enc, alg)
538-
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
539-
with pytest.raises(JWEError):
540-
actual = jwe.decrypt(encrypted, PRIVATE_KEY_PEM)
541-
finally:
542-
jose.constants.JWE_SIZE_LIMIT = old_limit
533+
monkeypatch.setattr('jose.constants.JWE_SIZE_LIMIT', 1024)
534+
encrypted = jwe.encrypt(b"Text"*64*1024, PUBLIC_KEY_PEM, enc, alg)
535+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
536+
with pytest.raises(JWEError) as excinfo:
537+
actual = jwe.decrypt(encrypted, PRIVATE_KEY_PEM)
538+
assert 'JWE string' in str(excinfo.value)
539+
assert 'bytes exceeds' in str(excinfo.value)
543540

544541
@pytest.mark.skipif(AESKey is None, reason="No AES backend")
545-
def test_jwe_zip_with_excessive_data(self):
542+
def test_jwe_zip_with_excessive_data(self, monkeypatch):
546543
# Test that a fix for CVE-2024-33664 is in place.
547544
enc = ALGORITHMS.A256CBC_HS512
548545
alg = ALGORITHMS.RSA_OAEP_256
549-
import jose.constants
550-
old_limit = jose.constants.JWE_SIZE_LIMIT
551-
try:
552-
jose.constants.JWE_SIZE_LIMIT = 1024
553-
encrypted = jwe.encrypt(b"Text"*64*1024, PUBLIC_KEY_PEM, enc, alg, zip=ZIPS.DEF)
554-
assert len(encrypted) < jose.constants.JWE_SIZE_LIMIT
555-
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
556-
with pytest.raises(JWEError):
557-
actual = jwe.decrypt(encrypted, PRIVATE_KEY_PEM)
558-
finally:
559-
jose.constants.JWE_SIZE_LIMIT = old_limit
546+
monkeypatch.setattr('jose.constants.JWE_SIZE_LIMIT', 1024)
547+
encrypted = jwe.encrypt(b"Text"*64*1024, PUBLIC_KEY_PEM, enc, alg, zip=ZIPS.DEF)
548+
assert len(encrypted) < jose.constants.JWE_SIZE_LIMIT
549+
header = json.loads(base64url_decode(encrypted.split(b".")[0]))
550+
with pytest.raises(JWEError) as excinfo:
551+
actual = jwe.decrypt(encrypted, PRIVATE_KEY_PEM)
552+
assert 'Decompressed JWE string exceeds' in str(excinfo.value)

0 commit comments

Comments
 (0)