Skip to content

Commit 03f39b0

Browse files
committed
Add hash and length validation
- valid length: greater than zero - valid hashes: a non-empty dictionary of type Dict[str, str] Checking the validity of hash algorithms is not part of the metadata input validation and is done by securesystemslib during hash verification. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 39ed706 commit 03f39b0

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

tuf/api/metadata.py

+25-12
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,19 @@ def _verify_length(
702702
f"expected length {expected_length}"
703703
)
704704

705+
@staticmethod
706+
def _validate_hashes(hashes: Dict[str, str]) -> None:
707+
if not hashes:
708+
raise ValueError("Hashes must be a non empty dictionary")
709+
for key, value in hashes.items():
710+
if not (isinstance(key, str) and isinstance(value, str)):
711+
raise TypeError("Hashes items must be strings")
712+
713+
@staticmethod
714+
def _validate_length(length: int) -> None:
715+
if length <= 0:
716+
raise ValueError(f"Length must be > 0, got {length}")
717+
705718

706719
class MetaFile(BaseFile):
707720
"""A container with information about a particular metadata file.
@@ -730,6 +743,14 @@ def __init__(
730743
hashes: Optional[Dict[str, str]] = None,
731744
unrecognized_fields: Optional[Mapping[str, Any]] = None,
732745
) -> None:
746+
747+
if version <= 0:
748+
raise ValueError(f"Metafile version must be > 0, got {version}")
749+
if length is not None:
750+
self._validate_length(length)
751+
if hashes is not None:
752+
self._validate_hashes(hashes)
753+
733754
self.version = version
734755
self.length = length
735756
self.hashes = hashes
@@ -742,12 +763,6 @@ def from_dict(cls, meta_dict: Dict[str, Any]) -> "MetaFile":
742763
length = meta_dict.pop("length", None)
743764
hashes = meta_dict.pop("hashes", None)
744765

745-
# Do some basic input validation
746-
if version <= 0:
747-
raise ValueError(f"Metafile version must be > 0, got {version}")
748-
if length is not None and length <= 0:
749-
raise ValueError(f"Metafile length must be > 0, got {length}")
750-
751766
# All fields left in the meta_dict are unrecognized.
752767
return cls(version, length, hashes, meta_dict)
753768

@@ -1033,6 +1048,10 @@ def __init__(
10331048
hashes: Dict[str, str],
10341049
unrecognized_fields: Optional[Mapping[str, Any]] = None,
10351050
) -> None:
1051+
1052+
self._validate_length(length)
1053+
self._validate_hashes(hashes)
1054+
10361055
self.length = length
10371056
self.hashes = hashes
10381057
self.unrecognized_fields = unrecognized_fields or {}
@@ -1049,12 +1068,6 @@ def from_dict(cls, target_dict: Dict[str, Any]) -> "TargetFile":
10491068
length = target_dict.pop("length")
10501069
hashes = target_dict.pop("hashes")
10511070

1052-
# Do some basic validation checks
1053-
if length <= 0:
1054-
raise ValueError(f"Targetfile length must be > 0, got {length}")
1055-
if not hashes:
1056-
raise ValueError("Missing targetfile hashes")
1057-
10581071
# All fields left in the target_dict are unrecognized.
10591072
return cls(length, hashes, target_dict)
10601073

0 commit comments

Comments
 (0)