Skip to content

Commit f36420c

Browse files
authored
Merge commit from fork
Signed-off-by: Radoslav Dimitrov <radoslav@stacklok.com>
1 parent f95222b commit f36420c

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

metadata/metadata.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -562,28 +562,30 @@ func isTargetInPathPattern(targetpath string, pathpattern string) bool {
562562

563563
// GetRolesForTarget return the names and terminating status of all
564564
// delegated roles who are responsible for targetFilepath
565-
func (role *Delegations) GetRolesForTarget(targetFilepath string) map[string]bool {
566-
res := map[string]bool{}
567-
// standard delegations
565+
// Note the result should be an ordered list, ref. https://github.com/theupdateframework/go-tuf/security/advisories/GHSA-4f8r-qqr9-fq8j
566+
func (role *Delegations) GetRolesForTarget(targetFilepath string) []RoleResult {
567+
var res []RoleResult
568+
// Standard delegations
568569
if role.Roles != nil {
569570
for _, r := range role.Roles {
570571
ok, err := r.IsDelegatedPath(targetFilepath)
571572
if err == nil && ok {
572-
res[r.Name] = r.Terminating
573+
res = append(res, RoleResult{Name: r.Name, Terminating: r.Terminating})
573574
}
574575
}
575576
} else if role.SuccinctRoles != nil {
576577
// SuccinctRoles delegations
577578
res = role.SuccinctRoles.GetRolesForTarget(targetFilepath)
578579
}
580+
// We preserve the same order as the actual roles list
579581
return res
580582
}
581583

582584
// GetRolesForTarget calculate the name of the delegated role responsible for "targetFilepath".
583585
// The target at path "targetFilepath" is assigned to a bin by casting
584586
// the left-most "BitLength" of bits of the file path hash digest to
585-
// int, using it as bin index between 0 and “2**BitLength - 1“.
586-
func (role *SuccinctRoles) GetRolesForTarget(targetFilepath string) map[string]bool {
587+
// int, using it as bin index between 0 and “2**BitLength-1”.
588+
func (role *SuccinctRoles) GetRolesForTarget(targetFilepath string) []RoleResult {
587589
// calculate the suffixLen value based on the total number of bins in
588590
// hex. If bit_length = 10 then numberOfBins = 1024 or bin names will
589591
// have a suffix between "000" and "3ff" in hex and suffixLen will be 3
@@ -604,8 +606,8 @@ func (role *SuccinctRoles) GetRolesForTarget(targetFilepath string) map[string]b
604606
// add zero padding if necessary and cast to hex the suffix
605607
suffix := fmt.Sprintf("%0*x", suffixLen, binNumber)
606608
// we consider all succinct_roles as terminating.
607-
// for more information read TAP 15.
608-
return map[string]bool{fmt.Sprintf("%s-%s", role.NamePrefix, suffix): true}
609+
// for more information, read TAP 15.
610+
return []RoleResult{{Name: fmt.Sprintf("%s-%s", role.NamePrefix, suffix), Terminating: true}}
609611
}
610612

611613
// GetRoles returns the names of all different delegated roles

metadata/types.go

+6
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,9 @@ type SuccinctRoles struct {
171171
NamePrefix string `json:"name_prefix"`
172172
UnrecognizedFields map[string]any `json:"-"`
173173
}
174+
175+
// RoleResult represents the name and terminating status of a delegated role that is responsible for targetFilepath
176+
type RoleResult struct {
177+
Name string
178+
Terminating bool
179+
}

metadata/updater/updater.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,14 @@ func (update *Updater) preOrderDepthFirstWalk(targetFilePath string) (*metadata.
550550
// after pre-order check, add current role to set of visited roles
551551
visitedRoleNames[delegation.Role] = true
552552
if targets.Signed.Delegations != nil {
553-
childRolesToVisit := []roleParentTuple{}
553+
var childRolesToVisit []roleParentTuple
554554
// note that this may be a slow operation if there are many
555555
// delegated roles
556556
roles := targets.Signed.Delegations.GetRolesForTarget(targetFilePath)
557-
for child, terminating := range roles {
558-
log.Info("Adding child role", "role", child)
559-
childRolesToVisit = append(childRolesToVisit, roleParentTuple{Role: child, Parent: delegation.Role})
560-
if terminating {
557+
for _, rolesForTarget := range roles {
558+
log.Info("Adding child role", "role", rolesForTarget.Name)
559+
childRolesToVisit = append(childRolesToVisit, roleParentTuple{Role: rolesForTarget.Name, Parent: delegation.Role})
560+
if rolesForTarget.Terminating {
561561
log.Info("Not backtracking to other roles")
562562
delegationsToVisit = []roleParentTuple{}
563563
break

0 commit comments

Comments
 (0)