Skip to content

Commit 7108ea2

Browse files
author
Jussi Kukkonen
authored
Merge pull request #1454 from sechkova/hashes-handle-sslib-errors
BaseFile._verify_hashes: handle sslib errors
2 parents 97da5ab + 752a741 commit 7108ea2

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

tests/test_api.py

+9
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,15 @@ def test_length_and_hash_validation(self):
541541
self.assertRaises(exceptions.LengthOrHashMismatchError,
542542
snapshot_metafile.verify_length_and_hashes, data)
543543

544+
snapshot_metafile.hashes = {'unsupported-alg': "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab"}
545+
self.assertRaises(exceptions.LengthOrHashMismatchError,
546+
snapshot_metafile.verify_length_and_hashes, data)
547+
548+
# Test wrong algorithm format (sslib.FormatError)
549+
snapshot_metafile.hashes = { 256: "8f88e2ba48b412c3843e9bb26e1b6f8fc9e98aceb0fbaa97ba37b4c98717d7ab"}
550+
self.assertRaises(exceptions.LengthOrHashMismatchError,
551+
snapshot_metafile.verify_length_and_hashes, data)
552+
544553
# test optional length and hashes
545554
snapshot_metafile.length = None
546555
snapshot_metafile.hashes = None

tuf/api/metadata.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,20 @@ def _verify_hashes(
679679
"""Verifies that the hash of 'data' matches 'expected_hashes'"""
680680
is_bytes = isinstance(data, bytes)
681681
for algo, exp_hash in expected_hashes.items():
682-
if is_bytes:
683-
digest_object = sslib_hash.digest(algo)
684-
digest_object.update(data)
685-
else:
686-
# if data is not bytes, assume it is a file object
687-
digest_object = sslib_hash.digest_fileobject(data, algo)
682+
try:
683+
if is_bytes:
684+
digest_object = sslib_hash.digest(algo)
685+
digest_object.update(data)
686+
else:
687+
# if data is not bytes, assume it is a file object
688+
digest_object = sslib_hash.digest_fileobject(data, algo)
689+
except (
690+
sslib_exceptions.UnsupportedAlgorithmError,
691+
sslib_exceptions.FormatError,
692+
) as e:
693+
raise exceptions.LengthOrHashMismatchError(
694+
f"Unsupported algorithm '{algo}'"
695+
) from e
688696

689697
observed_hash = digest_object.hexdigest()
690698
if observed_hash != exp_hash:
@@ -797,7 +805,7 @@ def verify_length_and_hashes(self, data: Union[bytes, BinaryIO]):
797805
data: File object or its content in bytes.
798806
Raises:
799807
LengthOrHashMismatchError: Calculated length or hashes do not
800-
match expected values.
808+
match expected values or hash algorithm is not supported.
801809
"""
802810
if self.length is not None:
803811
self._verify_length(data, self.length)
@@ -1094,7 +1102,7 @@ def verify_length_and_hashes(self, data: Union[bytes, BinaryIO]):
10941102
data: File object or its content in bytes.
10951103
Raises:
10961104
LengthOrHashMismatchError: Calculated length or hashes do not
1097-
match expected values.
1105+
match expected values or hash algorithm is not supported.
10981106
"""
10991107
self._verify_length(data, self.length)
11001108
self._verify_hashes(data, self.hashes)

0 commit comments

Comments
 (0)