Skip to content

Commit 9c33cd3

Browse files
author
Kairo de Araujo
committed
explicit encode role names
This commit explicitly encodes role names. Mostly this encoding is already happening in ``requests`` for what is not a URL. The "/" in a role name will now be encoded. Also, a slight change in the RepositorySimulator will align with the tests. This commit partially covers issue theupdateframework#1634 Signed-off-by: Kairo de Araujo <[email protected]>
1 parent f172972 commit 9c33cd3

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

tests/repository_simulator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def fetch_metadata(self, role: str, version: Optional[int] = None) -> bytes:
259259
If version is None, non-versioned metadata is being requested.
260260
"""
261261
self.fetch_tracker.metadata.append((role, version))
262+
# decode role for the metadata
263+
role = parse.unquote(role, encoding="utf-8")
262264

263265
if role == Root.type:
264266
# return a version previously serialized in publish_root()

tests/test_updater_delegation_graphs.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,21 @@ def test_fishy_rolenames(self) -> None:
346346
fishy_rolenames = DelegationsTestCase(delegations)
347347
self._init_repo(fishy_rolenames)
348348
updater = self._init_updater()
349+
updater.refresh()
349350

350-
# trigger updater to fetch the delegated metadata, check filenames
351+
# trigger updater to fetch the delegated metadata
352+
self.sim.fetch_tracker.metadata.clear()
351353
updater.get_targetinfo("anything")
354+
355+
# assert that local delegated metadata filenames are expected
352356
local_metadata = os.listdir(self.metadata_dir)
353357
for fname in roles_to_filenames.values():
354358
self.assertTrue(fname in local_metadata)
355359

360+
# assert that requested URLs are quoted without extension
361+
exp_calls = [(quoted[:-5], 1) for quoted in roles_to_filenames.values()]
362+
self.assertListEqual(self.sim.fetch_tracker.metadata, exp_calls)
363+
356364

357365
class TestTargetFileSearch(TestDelegations):
358366
r"""

tuf/ngclient/updater.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,11 @@ def _download_metadata(
290290
self, rolename: str, length: int, version: Optional[int] = None
291291
) -> bytes:
292292
"""Download a metadata file and return it as bytes"""
293+
encoded_name = parse.quote(rolename, "")
293294
if version is None:
294-
url = f"{self._metadata_base_url}{rolename}.json"
295+
url = f"{self._metadata_base_url}{encoded_name}.json"
295296
else:
296-
url = f"{self._metadata_base_url}{version}.{rolename}.json"
297+
url = f"{self._metadata_base_url}{version}.{encoded_name}.json"
297298
return self._fetcher.download_bytes(url, length)
298299

299300
def _load_local_metadata(self, rolename: str) -> bytes:

0 commit comments

Comments
 (0)