Skip to content

Commit dcdd332

Browse files
committed
Add basic input validation to {Meta,Target}File
Add basic checks for allowed input values during objects' serialization. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 22facb7 commit dcdd332

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tuf/api/metadata.py

+14
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,13 @@ def from_dict(cls, meta_dict: Dict[str, Any]) -> "MetaFile":
719719
version = meta_dict.pop("version")
720720
length = meta_dict.pop("length", None)
721721
hashes = meta_dict.pop("hashes", None)
722+
723+
# Do some basic input validation
724+
if version <= 0:
725+
raise ValueError(f"Metafile version must be > 0, got {version}")
726+
if length is not None and length <= 0:
727+
raise ValueError(f"Metafile length must be > 0, got {length}")
728+
722729
# All fields left in the meta_dict are unrecognized.
723730
return cls(version, length, hashes, meta_dict)
724731

@@ -1019,6 +1026,13 @@ def from_dict(cls, target_dict: Dict[str, Any]) -> "TargetFile":
10191026
"""Creates TargetFile object from its dict representation."""
10201027
length = target_dict.pop("length")
10211028
hashes = target_dict.pop("hashes")
1029+
1030+
# Do some basic validation checks
1031+
if length <= 0:
1032+
raise ValueError(f"Targetfile length must be > 0, got {length}")
1033+
if not hashes:
1034+
raise ValueError("Missing targetfile hashes")
1035+
10221036
# All fields left in the target_dict are unrecognized.
10231037
return cls(length, hashes, target_dict)
10241038

0 commit comments

Comments
 (0)