diff --git a/tests/test_metadata_serialization.py b/tests/test_metadata_serialization.py index c10798f611..1bfa9fa708 100644 --- a/tests/test_metadata_serialization.py +++ b/tests/test_metadata_serialization.py @@ -399,6 +399,10 @@ def test_delegated_role_serialization(self, test_case_data: str) -> None: "terminating": false, "threshold": 1}', "invalid path_hash_prefixes type": '{"keyids": ["keyid"], "name": "a", "path_hash_prefixes": [1,2,3], \ "terminating": false, "threshold": 1}', + "paths is a string": '{"keyids": ["keyid"], "name": "a", "paths": "fn1", \ + "terminating": false, "threshold": 1}', + "path_hash_prefixes is a string": '{"keyids": ["keyid"], "name": "a", "path_hash_prefixes": "h1", \ + "terminating": false, "threshold": 1}', } @utils.run_sub_tests_with_dataset(invalid_delegated_roles) diff --git a/tuf/api/_payload.py b/tuf/api/_payload.py index 28be84fa61..c7ed365d0d 100644 --- a/tuf/api/_payload.py +++ b/tuf/api/_payload.py @@ -1099,12 +1099,18 @@ def __init__( # noqa: PLR0917 "Only one of (paths, path_hash_prefixes) must be set" ) - if paths is not None and any(not isinstance(p, str) for p in paths): - raise ValueError("Paths must be strings") - if path_hash_prefixes is not None and any( - not isinstance(p, str) for p in path_hash_prefixes + # A str is iterable too, so it needs to be rejected explicitly: its + # characters would otherwise be used as separate patterns/prefixes + if paths is not None and ( + not isinstance(paths, list) + or any(not isinstance(p, str) for p in paths) ): - raise ValueError("Path_hash_prefixes must be strings") + raise ValueError("Paths must be a list of strings") + if path_hash_prefixes is not None and ( + not isinstance(path_hash_prefixes, list) + or any(not isinstance(p, str) for p in path_hash_prefixes) + ): + raise ValueError("Path_hash_prefixes must be a list of strings") self.paths = paths self.path_hash_prefixes = path_hash_prefixes