From 9933902013db7bbc517382946cbd3d47344fe48d Mon Sep 17 00:00:00 2001 From: Bahaa Mattar Date: Thu, 19 Nov 2026 15:13:31 +0200 Subject: [PATCH] Metadata API: Reject non-list paths and path_hash_prefixes DelegatedRole checked that every item of paths and path_hash_prefixes is a string, but a plain str is iterable too: a delegation with "paths": "files/*.zip" was accepted and every character was then used as a separate path pattern. The "*" character matches any single-component target path, so unrelated targets were delegated to the role while the intended "files/x.zip" was not. A string path_hash_prefixes behaved similarly, matching far more hash bins than intended. Require both attributes to be lists of strings. Fixes #3052 Signed-off-by: Bahaa Mattar --- tests/test_metadata_serialization.py | 4 ++++ tuf/api/_payload.py | 16 +++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) 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