Skip to content

Commit b603ea5

Browse files
committed
fix: use UnsupportedAlgorithmError instead of ValueError in JWS/JWE registry
1 parent 04dd9e7 commit b603ea5

6 files changed

Lines changed: 30 additions & 18 deletions

File tree

src/joserfc/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class ConflictAlgorithmError(JoseError):
6060
error = "conflict_algorithm"
6161

6262

63+
class UnsupportedAlgorithmError(JoseError):
64+
error = "unsupported_algorithm"
65+
66+
6367
class MissingEncryptionError(JoseError):
6468
error = "missing_encryption"
6569
description = 'Missing "enc" value in header'

src/joserfc/rfc7515/registry.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22
from typing import Dict
33
from .model import JWSAlgModel
4+
from ..errors import UnsupportedAlgorithmError
45
from ..registry import (
56
JWS_HEADER_REGISTRY,
67
Header,
@@ -49,14 +50,14 @@ def get_alg(self, name: str) -> JWSAlgModel:
4950
:param name: value of the ``alg``, e.g. ``HS256``, ``RS256``
5051
"""
5152
if name not in self.algorithms:
52-
raise ValueError(f'Algorithm of "{name}" is not supported')
53+
raise UnsupportedAlgorithmError(f'Algorithm of "{name}" is not supported')
54+
5355
if self.allowed:
54-
allowed = self.allowed
56+
if name not in self.allowed:
57+
raise UnsupportedAlgorithmError(f'Algorithm of "{name}" is not allowed')
5558
else:
56-
allowed = self.recommended
57-
58-
if name not in allowed:
59-
raise ValueError(f'Algorithm of "{name}" is not allowed')
59+
if name not in self.recommended:
60+
raise UnsupportedAlgorithmError(f'Algorithm of "{name}" is not recommended')
6061
return self.algorithms[name]
6162

6263
def check_header(self, header: Header) -> None:

src/joserfc/rfc7516/registry.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22
import typing as t
33
from .models import JWEAlgModel, JWEEncModel, JWEZipModel
4+
from ..errors import UnsupportedAlgorithmError
45
from ..registry import (
56
Header,
67
HeaderRegistryDict,
@@ -101,15 +102,14 @@ def get_zip(self, name: str) -> JWEZipModel:
101102

102103
def _check_algorithm(self, name: str, registry: dict[str, t.Any]) -> None:
103104
if name not in registry:
104-
raise ValueError(f'Algorithm of "{name}" is not supported')
105+
raise UnsupportedAlgorithmError(f'Algorithm of "{name}" is not supported')
105106

106107
if self.allowed:
107-
allowed = self.allowed
108+
if name not in self.allowed:
109+
raise UnsupportedAlgorithmError(f'Algorithm of "{name}" is not allowed')
108110
else:
109-
allowed = self.recommended
110-
111-
if name not in allowed:
112-
raise ValueError(f'Algorithm of "{name}" is not allowed')
111+
if name not in self.recommended:
112+
raise UnsupportedAlgorithmError(f'Algorithm of "{name}" is not recommended')
113113

114114

115115
default_registry = JWERegistry()

tests/jwe/test_errors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
InvalidKeyTypeError,
77
InvalidKeyLengthError,
88
DecodeError,
9+
UnsupportedAlgorithmError,
910
)
1011
from tests.base import load_key
1112

@@ -67,7 +68,7 @@ def test_invalid_alg(self):
6768
protected = {"alg": "INVALID", "enc": "A128CBC-HS256"}
6869
key = OctKey.import_key("secret")
6970
self.assertRaises(
70-
ValueError,
71+
UnsupportedAlgorithmError,
7172
jwe.encrypt_compact,
7273
protected, b"i", key
7374
)

tests/jwe/test_example.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from joserfc.rfc7516.message import perform_encrypt
88
from joserfc.rfc7516.compact import represent_compact
99
from joserfc.rfc7516.json import represent_general_json
10+
from joserfc.errors import UnsupportedAlgorithmError
1011
from tests.base import load_key
1112

1213

@@ -209,7 +210,7 @@ def test_A2(self):
209210
self.assertEqual(represent_compact(obj), to_bytes(expected))
210211

211212
# RSA1_5 is not allowed by default
212-
self.assertRaises(ValueError, decrypt_compact, expected, key)
213+
self.assertRaises(UnsupportedAlgorithmError, decrypt_compact, expected, key)
213214
_registry = JWERegistry(algorithms=['RSA1_5', 'A128CBC-HS256'])
214215
jwe_data = decrypt_compact(expected, key, registry=_registry)
215216
self.assertEqual(jwe_data.plaintext, plaintext)

tests/jws/test_compact.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
from unittest import TestCase
22
from joserfc.jws import JWSRegistry, serialize_compact, deserialize_compact
33
from joserfc.jwk import OctKey, RSAKey, KeySet
4-
from joserfc.errors import BadSignatureError, DecodeError, MissingAlgorithmError
4+
from joserfc.errors import (
5+
BadSignatureError,
6+
DecodeError,
7+
MissingAlgorithmError,
8+
UnsupportedAlgorithmError,
9+
)
510

611

712
class TestCompact(TestCase):
@@ -19,10 +24,10 @@ def test_bad_signature_error(self):
1924
value = b'eyJhbGciOiJIUzI1NiJ9.Zm9v.0pehoi-RMZM1jl-4TP_C4Y6BJ-bcmsuzfDyQpkpJkh0'
2025
self.assertRaises(BadSignatureError, deserialize_compact, value, key)
2126

22-
def test_raise_none_supported_alg(self):
27+
def test_raise_unsupported_algorithm_error(self):
2328
key = OctKey.import_key("secret")
24-
self.assertRaises(ValueError, serialize_compact, {"alg": "HS512"}, b"foo", key)
25-
self.assertRaises(ValueError, serialize_compact, {"alg": "NOT"}, b"foo", key)
29+
self.assertRaises(UnsupportedAlgorithmError, serialize_compact, {"alg": "HS512"}, b"foo", key)
30+
self.assertRaises(UnsupportedAlgorithmError, serialize_compact, {"alg": "NOT"}, b"foo", key)
2631

2732
def test_invalid_length(self):
2833
key = OctKey.import_key("secret")

0 commit comments

Comments
 (0)