Skip to content

Commit 1022006

Browse files
committed
Raise exception for invalid signature enc_str
1 parent 03d8ed3 commit 1022006

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

common/exceptions.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,25 @@ class InvalidEncodedFragment(DSKEException):
321321

322322
def __init__(self, encoded_fragment: str):
323323
super().__init__(
324-
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
324+
status_code=status.HTTP_400_BAD_REQUEST
325325
message="Invalid encoded fragment.",
326326
details={"encoded_fragment": encoded_fragment},
327327
)
328328

329329

330+
class InvalidEncodedSignature(DSKEException):
331+
"""
332+
Exception raised when trying to parse an encoded signature string that is invalid.
333+
"""
334+
335+
def __init__(self, encoded_signature: str):
336+
super().__init__(
337+
status_code=status.HTTP_400_BAD_REQUEST
338+
message="Invalid encoded fragment.",
339+
details={"encoded_signature": encoded_signature},
340+
)
341+
342+
330343
class EncryptorNotRegisteredForClientError(DSKEException):
331344
"""
332345
Exception raised when an encryptor is not registered for a client.

common/signature.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from typing import Optional
6+
from .exceptions import InvalidEncodedSignatureError
67
from .utils import bytes_to_str, str_to_bytes
78

89
HEADER_NAME = "DSKE-Signature"
@@ -51,7 +52,7 @@ def from_enc_str(cls, enc_str: str) -> "Signature":
5152
"""
5253
split_str = enc_str.split(_ENCODING_SEPARATOR)
5354
if len(split_str) != 2:
54-
assert False # TODO: Raise an exception instead
55+
raise InvalidEncodedSignatureError(enc_str)
5556
allocation_enc_str = split_str[0]
5657
signature_data_str = split_str[1]
5758
signature_data = str_to_bytes(signature_data_str)

0 commit comments

Comments
 (0)