Skip to content

Commit 790b699

Browse files
committed
Rename and simplify visit_child_role
Rename to DelegatedRole.is_in_trusted_paths and return a boolean flag instead of the role's name. Minor comments and code style improvements. Some code simplification. Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
1 parent 80da4e1 commit 790b699

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

tuf/api/metadata.py

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,48 +1033,29 @@ def to_dict(self) -> Dict[str, Any]:
10331033
res_dict["path_hash_prefixes"] = self.path_hash_prefixes
10341034
return res_dict
10351035

1036-
def visit_child_role(self, target_filepath: str) -> str:
1037-
"""Determines whether the given 'target_filepath' is an
1038-
allowed path of DelegatedRole"""
1036+
def is_in_trusted_paths(self, target_filepath: str) -> bool:
1037+
"""Determines whether the given 'target_filepath'
1038+
is in one of the trusted paths of DelegatedRole"""
10391039

10401040
if self.path_hash_prefixes is not None:
10411041
target_filepath_hash = _get_filepath_hash(target_filepath)
10421042
for path_hash_prefix in self.path_hash_prefixes:
1043-
if not target_filepath_hash.startswith(path_hash_prefix):
1044-
continue
1045-
1046-
return self.name
1043+
if target_filepath_hash.startswith(path_hash_prefix):
1044+
return True
10471045

10481046
elif self.paths is not None:
1049-
for path in self.paths:
1050-
# A child role path may be an explicit path or glob pattern (Unix
1051-
# shell-style wildcards). The child role 'child_role_name' is
1052-
# returned if 'target_filepath' is equal to or matches
1053-
# 'child_role_path'. Explicit filepaths are also considered
1054-
# matches. A repo maintainer might delegate a glob pattern with a
1055-
# leading path separator, while the client requests a matching
1056-
# target without a leading path separator - make sure to strip any
1057-
# leading path separators so that a match is made.
1047+
for pathpattern in self.paths:
1048+
# A delegated role path may be an explicit path or glob
1049+
# pattern (Unix shell-style wildcards). Explicit filepaths
1050+
# are also considered matches. Make sure to strip any leading
1051+
# path separators so that a match is made.
10581052
# Example: "foo.tgz" should match with "/*.tgz".
10591053
if fnmatch.fnmatch(
1060-
target_filepath.lstrip(os.sep), path.lstrip(os.sep)
1054+
target_filepath.lstrip(os.sep), pathpattern.lstrip(os.sep)
10611055
):
1056+
return True
10621057

1063-
return self.name
1064-
1065-
continue
1066-
1067-
else:
1068-
# 'role_name' should have been validated when it was downloaded.
1069-
# The 'paths' or 'path_hash_prefixes' fields should not be missing,
1070-
# so we raise a format error here in case they are both missing.
1071-
raise exceptions.FormatError(
1072-
repr(self.name) + " "
1073-
'has neither a "paths" nor "path_hash_prefixes". At least'
1074-
" one of these attributes must be present."
1075-
)
1076-
1077-
return None
1058+
return False
10781059

10791060

10801061
def _get_filepath_hash(target_filepath, hash_function="sha256"):

tuf/ngclient/updater.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,10 @@ def _preorder_depth_first_walk(self, target_filepath) -> Dict:
349349
# NOTE: This may be a slow operation if there are many
350350
# delegated roles.
351351
for child_role in child_roles:
352-
child_role_name = child_role.visit_child_role(
353-
target_filepath
354-
)
352+
if child_role.is_in_trusted_paths(target_filepath):
353+
child_role_name = child_role.name
354+
else:
355+
child_role_name = None
355356

356357
if child_role.terminating and child_role_name is not None:
357358
logger.debug(

0 commit comments

Comments
 (0)