Skip to content

Commit

Permalink
Update ngclient to skip visited nodes on delegation tree traversal
Browse files Browse the repository at this point in the history
This change edits the ngclient `Updater` to traverse the delegation
tree on nodes, instead of edges in order to skip already visited
nodes.

For more detailed clarification, please review
theupdateframework/specification#177

Fixes #1528

Signed-off-by: Ivana Atanasova <iyovcheva@iyovcheva-a02.vmware.com>
  • Loading branch information
Ivana Atanasova committed Nov 18, 2021
1 parent bb15ecf commit f5ee005
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions tuf/ngclient/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
import logging
import os
import tempfile
from typing import Optional, Set, Tuple
from typing import Optional, Set
from urllib import parse

from securesystemslib import util as sslib_util
Expand Down Expand Up @@ -401,17 +401,19 @@ def _preorder_depth_first_walk(
# List of delegations to be interrogated. A (role, parent role) pair
# is needed to load and verify the delegated targets metadata.
delegations_to_visit = [("targets", "root")]
visited_role_names: Set[Tuple[str, str]] = set()
visited_role_names: Set[str] = set()
number_of_delegations = self.config.max_delegations

# Preorder depth-first traversal of the graph of target delegations.
while number_of_delegations > 0 and len(delegations_to_visit) > 0:

# Pop the role name from the top of the stack.
role_name, parent_role = delegations_to_visit.pop(-1)
if parent_role not in visited_role_names:
visited_role_names.add(parent_role)

# Skip any visited current role to prevent cycles.
if (role_name, parent_role) in visited_role_names:
if role_name in visited_role_names:
logger.debug("Skipping visited current role %s", role_name)
continue

Expand All @@ -427,7 +429,7 @@ def _preorder_depth_first_walk(
return target

# After preorder check, add current role to set of visited roles.
visited_role_names.add((role_name, parent_role))
visited_role_names.add(role_name)

# And also decrement number of visited roles.
number_of_delegations -= 1
Expand Down

0 comments on commit f5ee005

Please sign in to comment.