Skip to content

Commit

Permalink
explicit encode role names
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Kairo de Araujo committed Jan 17, 2022
1 parent f172972 commit aa6d28f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 2 additions & 0 deletions tests/repository_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def fetch_metadata(self, role: str, version: Optional[int] = None) -> bytes:
If version is None, non-versioned metadata is being requested.
"""
self.fetch_tracker.metadata.append((role, version))
# decode role for the metadata
role = parse.unquote(role, encoding="utf-8")

if role == Root.type:
# return a version previously serialized in publish_root()
Expand Down
21 changes: 15 additions & 6 deletions tests/test_updater_delegation_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@ def test_invalid_metadata(self, test_data: DelegationsTestCase) -> None:
finally:
self.teardown_subtest()

def test_fishy_rolenames(self) -> None:
# Test that delegated roles filenames are safely encoded
# when persisted to the file system.
def test_safely_encoded_rolenames(self) -> None:
"""Test that delegated roles names are safely encoded in the filenames
and URLs.
"""

roles_to_filenames = {
"../a": "..%2Fa.json",
Expand All @@ -343,16 +344,24 @@ def test_fishy_rolenames(self) -> None:
for rolename in roles_to_filenames:
delegations.append(TestDelegation("targets", rolename))

fishy_rolenames = DelegationsTestCase(delegations)
self._init_repo(fishy_rolenames)
delegated_rolenames = DelegationsTestCase(delegations)
self._init_repo(delegated_rolenames)
updater = self._init_updater()
updater.refresh()

# trigger updater to fetch the delegated metadata, check filenames
# trigger updater to fetch the delegated metadata
self.sim.fetch_tracker.metadata.clear()
updater.get_targetinfo("anything")

# assert that local delegated metadata filenames are expected
local_metadata = os.listdir(self.metadata_dir)
for fname in roles_to_filenames.values():
self.assertTrue(fname in local_metadata)

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


class TestTargetFileSearch(TestDelegations):
r"""
Expand Down
5 changes: 3 additions & 2 deletions tuf/ngclient/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,11 @@ def _download_metadata(
self, rolename: str, length: int, version: Optional[int] = None
) -> bytes:
"""Download a metadata file and return it as bytes"""
encoded_name = parse.quote(rolename, "")
if version is None:
url = f"{self._metadata_base_url}{rolename}.json"
url = f"{self._metadata_base_url}{encoded_name}.json"
else:
url = f"{self._metadata_base_url}{version}.{rolename}.json"
url = f"{self._metadata_base_url}{version}.{encoded_name}.json"
return self._fetcher.download_bytes(url, length)

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

0 comments on commit aa6d28f

Please sign in to comment.