Skip to content

Commit 4800843

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 062c9f6 commit 4800843

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
@@ -955,48 +955,29 @@ def to_dict(self) -> Dict[str, Any]:
955955
res_dict["path_hash_prefixes"] = self.path_hash_prefixes
956956
return res_dict
957957

958-
def visit_child_role(self, target_filepath: str) -> str:
959-
"""Determines whether the given 'target_filepath' is an
960-
allowed path of DelegatedRole"""
958+
def is_in_trusted_paths(self, target_filepath: str) -> bool:
959+
"""Determines whether the given 'target_filepath'
960+
is in one of the trusted paths of DelegatedRole"""
961961

962962
if self.path_hash_prefixes is not None:
963963
target_filepath_hash = _get_filepath_hash(target_filepath)
964964
for path_hash_prefix in self.path_hash_prefixes:
965-
if not target_filepath_hash.startswith(path_hash_prefix):
966-
continue
967-
968-
return self.name
965+
if target_filepath_hash.startswith(path_hash_prefix):
966+
return True
969967

970968
elif self.paths is not None:
971-
for path in self.paths:
972-
# A child role path may be an explicit path or glob pattern (Unix
973-
# shell-style wildcards). The child role 'child_role_name' is
974-
# returned if 'target_filepath' is equal to or matches
975-
# 'child_role_path'. Explicit filepaths are also considered
976-
# matches. A repo maintainer might delegate a glob pattern with a
977-
# leading path separator, while the client requests a matching
978-
# target without a leading path separator - make sure to strip any
979-
# leading path separators so that a match is made.
969+
for pathpattern in self.paths:
970+
# A delegated role path may be an explicit path or glob
971+
# pattern (Unix shell-style wildcards). Explicit filepaths
972+
# are also considered matches. Make sure to strip any leading
973+
# path separators so that a match is made.
980974
# Example: "foo.tgz" should match with "/*.tgz".
981975
if fnmatch.fnmatch(
982-
target_filepath.lstrip(os.sep), path.lstrip(os.sep)
976+
target_filepath.lstrip(os.sep), pathpattern.lstrip(os.sep)
983977
):
978+
return True
984979

985-
return self.name
986-
987-
continue
988-
989-
else:
990-
# 'role_name' should have been validated when it was downloaded.
991-
# The 'paths' or 'path_hash_prefixes' fields should not be missing,
992-
# so we raise a format error here in case they are both missing.
993-
raise exceptions.FormatError(
994-
repr(self.name) + " "
995-
'has neither a "paths" nor "path_hash_prefixes". At least'
996-
" one of these attributes must be present."
997-
)
998-
999-
return None
980+
return False
1000981

1001982

1002983
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
@@ -359,9 +359,10 @@ def _preorder_depth_first_walk(self, target_filepath) -> Dict:
359359
# NOTE: This may be a slow operation if there are many
360360
# delegated roles.
361361
for child_role in child_roles:
362-
child_role_name = child_role.visit_child_role(
363-
target_filepath
364-
)
362+
if child_role.is_in_trusted_paths(target_filepath):
363+
child_role_name = child_role.name
364+
else:
365+
child_role_name = None
365366

366367
if child_role.terminating and child_role_name is not None:
367368
msg = (

0 commit comments

Comments
 (0)