Skip to content

Commit 0eef15a

Browse files
kamui-finjku
authored andcommitted
fix: parse manually and handle url edge cases
Signed-off-by: Kamui <fin-kamui@pm.me>
1 parent 1e47e39 commit 0eef15a

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

tests/test_api.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,36 @@ def test_targetfile_hash_prefix_paths(self) -> None:
731731
)
732732
self.assertEqual(sorted(target.get_prefixed_paths()), ["public/path/abc.file.ext", "public/path/def.file.ext"])
733733

734+
target = TargetFile(
735+
100, {"sha256": "abc", "md5": "def"}, ""
736+
)
737+
self.assertEqual(sorted(target.get_prefixed_paths()), [])
738+
739+
target = TargetFile(
740+
100, {"sha256": "abc", "md5": "def"}, "public/path/"
741+
)
742+
self.assertEqual(sorted(target.get_prefixed_paths()), [])
743+
744+
target = TargetFile(
745+
100, {"sha256": "abc", "md5": "def"}, "file.ext"
746+
)
747+
self.assertEqual(sorted(target.get_prefixed_paths()), ["abc.file.ext", "def.file.ext"])
748+
749+
target = TargetFile(
750+
100, {"sha256": "abc", "md5": "def"}, "public/path/.ext"
751+
)
752+
self.assertEqual(sorted(target.get_prefixed_paths()), ["public/path/abc..ext", "public/path/def..ext"])
753+
754+
target = TargetFile(
755+
100, {"sha256": "abc"}, "/root/file.ext"
756+
)
757+
self.assertEqual(sorted(target.get_prefixed_paths()), ["/root/abc.file.ext"])
758+
759+
target = TargetFile(
760+
100, {"sha256": "abc"}, "/"
761+
)
762+
self.assertEqual(sorted(target.get_prefixed_paths()), [])
763+
734764
def test_is_delegated_role(self) -> None:
735765
# test path matches
736766
# see more extensive tests in test_is_target_in_pathpattern()

tuf/api/metadata.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,13 +1740,28 @@ def verify_length_and_hashes(self, data: Union[bytes, IO[bytes]]) -> None:
17401740

17411741
def get_prefixed_paths(self) -> List[str]:
17421742
"""
1743-
Returns hash-prefixed paths for the given target file path.
1743+
Returns hash-prefixed paths for the given target file path. Empty result in the case of an invalid path.
1744+
1745+
Expects self.path to be a URL path fragment, not a filesystem path.
17441746
"""
17451747
paths = []
1746-
path = pathlib.Path(self.path)
1747-
name, parent = path.name, path.parent
1748+
try:
1749+
if not self.path:
1750+
raise ValueError
1751+
elif "/" not in self.path:
1752+
parent, name = None, self.path
1753+
else:
1754+
parent, name = self.path.rsplit("/", 1)
1755+
if name == "":
1756+
raise ValueError
1757+
except ValueError:
1758+
return paths
1759+
17481760
for hash in self.hashes.values():
1749-
paths.append(str(parent.joinpath(f"{hash}.{name}")))
1761+
path = f"{hash}.{name}"
1762+
if parent is not None:
1763+
path = f"{parent}/{path}"
1764+
paths.append(path)
17501765
return paths
17511766

17521767

0 commit comments

Comments
 (0)