From 2c39fa2214fbe1ebaf420fd8123eec2284a8275f Mon Sep 17 00:00:00 2001 From: Riyaz Faizullabhoy Date: Thu, 17 Dec 2015 15:47:38 -0800 Subject: [PATCH] simplify and clean logic, gofmt Signed-off-by: Riyaz Faizullabhoy --- tuf/data/roles.go | 12 ++++++++---- tuf/data/roles_test.go | 12 ++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/tuf/data/roles.go b/tuf/data/roles.go index 83eb3f3daf..0faf870be0 100644 --- a/tuf/data/roles.go +++ b/tuf/data/roles.go @@ -2,9 +2,9 @@ package data import ( "fmt" - "strings" - "regexp" "path/filepath" + "regexp" + "strings" ) // Canonical base role names @@ -116,9 +116,13 @@ func ValidRole(name string) bool { // IsDelegation checks if the role is a delegation or a root role func IsDelegation(role string) bool { targetsBase := fmt.Sprintf("%s/", ValidRoles[CanonicalTargetsRole]) - whitelistedChars, _ := regexp.MatchString("^[a-zA-Z0-9_/]*$", role) + whitelistedChars, err := regexp.MatchString("^[a-zA-Z0-9_/]*$", role) + if err != nil { + return false + } + // Removes ., .., extra slashes, and trailing slash isClean := filepath.Clean(role) == role - return strings.HasPrefix(role, targetsBase) && !strings.HasSuffix(role, "/") && whitelistedChars && isClean + return strings.HasPrefix(role, targetsBase) && whitelistedChars && isClean } // RootRole is a cut down role as it appears in the root.json diff --git a/tuf/data/roles_test.go b/tuf/data/roles_test.go index d65dad2b79..bc3b1edd80 100644 --- a/tuf/data/roles_test.go +++ b/tuf/data/roles_test.go @@ -197,25 +197,25 @@ func TestIsDelegation(t *testing.T) { assert.False(t, IsDelegation(filepath.Join(CanonicalTargetsRole, "level1")+"/")) assert.False(t, IsDelegation( - filepath.Join(CanonicalTargetsRole, "directory") + "/../../traversal")) + filepath.Join(CanonicalTargetsRole, "directory")+"/../../traversal")) assert.False(t, IsDelegation( - filepath.Join(CanonicalTargetsRole) + "///test/middle/slashes")) + filepath.Join(CanonicalTargetsRole)+"///test/middle/slashes")) assert.False(t, IsDelegation( - filepath.Join(CanonicalTargetsRole) + "/./././")) + filepath.Join(CanonicalTargetsRole)+"/./././")) assert.False(t, IsDelegation( filepath.Join(" ", CanonicalTargetsRole, "level1"))) assert.False(t, IsDelegation( - filepath.Join(" " + CanonicalTargetsRole, "level1"))) + filepath.Join(" "+CanonicalTargetsRole, "level1"))) assert.False(t, IsDelegation( - filepath.Join(CanonicalTargetsRole, "level1" + " "))) + filepath.Join(CanonicalTargetsRole, "level1"+" "))) assert.False(t, IsDelegation( - filepath.Join(CanonicalTargetsRole, "white space" + "level2"))) + filepath.Join(CanonicalTargetsRole, "white space"+"level2"))) } func TestValidRoleFunction(t *testing.T) {