Skip to content

Commit 75f6b00

Browse files
committed
Handle consistent targets same as legacy updater
The definition of consistent targets in the spec is ambiguous: "consistent target files should be written to non-volatile storage as digest.filename.ext" Additionally, the specification describes consistent targets when the client builds the download URL as follows: "The filename is of the form HASH.FILENAME.EXT". The issue is about how we interpreted those quotes. The legacy updater has decided this means a target path "a/b" will translate to a download url path "a/{HASH}.b". The ngclient however translates the target path "a/b" to a download url path "{HASH}.a/b". We decided we want to follow the same approach taken from the legacy updater and thus change how we construct the consistent targets. Additionally, we want to make sure we test for cases when the TARGETPATH is an empty string or points to a directory. Signed-off-by: Martin Vrachev <[email protected]>
1 parent bb1ed9f commit 75f6b00

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

tests/test_updater_with_simulator.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def test_refresh(self):
8282
def test_targets(self):
8383
targets = {
8484
"targetpath": b"content",
85-
"åäö": b"more content"
85+
"åäö": b"more content",
86+
"dir/targetpath": b"dir target content"
8687
}
8788

8889
# Add targets to repository
@@ -110,8 +111,13 @@ def test_targets(self):
110111
with open(local_path, "rb") as f:
111112
self.assertEqual(f.read(), content)
112113

113-
# TODO: run the same download tests for target paths like "dir/file2")
114-
# This currently fails because issue #1576
114+
if "/" in targetpath:
115+
# assert local_path != targetpath because of the URL encoding
116+
# make target_path absolute as local_path
117+
target_path = os.path.join(self.targets_dir, targetpath)
118+
self.assertNotEqual(target_path, local_path)
119+
120+
115121

116122
def test_keys_and_signatures(self):
117123
"""Example of the two trickiest test areas: keys and root updates"""

tuf/ngclient/updater.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ def download_target(
230230
download URL. Default is the value provided in Updater()
231231
232232
Raises:
233+
ValueError: If target_base_url and self._target_base_url are
234+
simultaniosly None.
233235
TODO: download-related errors
234236
TODO: file write errors
235237
@@ -252,8 +254,9 @@ def download_target(
252254
consistent_snapshot = self._trusted_set.root.signed.consistent_snapshot
253255
if consistent_snapshot and self.config.prefix_targets_with_hash:
254256
hashes = list(targetinfo.hashes.values())
255-
target_filepath = f"{hashes[0]}.{target_filepath}"
256-
full_url = parse.urljoin(target_base_url, target_filepath)
257+
dirname, sep, basename = target_filepath.rpartition("/")
258+
target_filepath = f"{dirname}{sep}{hashes[0]}.{basename}"
259+
full_url = f"{target_base_url}{target_filepath}"
257260

258261
with self._fetcher.download_file(
259262
full_url, targetinfo.length

0 commit comments

Comments
 (0)