simplify and clean logic, gofmt

Signed-off-by: Riyaz Faizullabhoy <riyaz.faizullabhoy@docker.com>
This commit is contained in:
Riyaz Faizullabhoy 2015-12-17 15:47:38 -08:00
parent e82371e687
commit 2c39fa2214
2 changed files with 14 additions and 10 deletions

View File

@ -2,9 +2,9 @@ package data
import ( import (
"fmt" "fmt"
"strings"
"regexp"
"path/filepath" "path/filepath"
"regexp"
"strings"
) )
// Canonical base role names // 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 // IsDelegation checks if the role is a delegation or a root role
func IsDelegation(role string) bool { func IsDelegation(role string) bool {
targetsBase := fmt.Sprintf("%s/", ValidRoles[CanonicalTargetsRole]) 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 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 // RootRole is a cut down role as it appears in the root.json

View File

@ -197,25 +197,25 @@ func TestIsDelegation(t *testing.T) {
assert.False(t, IsDelegation(filepath.Join(CanonicalTargetsRole, "level1")+"/")) assert.False(t, IsDelegation(filepath.Join(CanonicalTargetsRole, "level1")+"/"))
assert.False(t, IsDelegation( assert.False(t, IsDelegation(
filepath.Join(CanonicalTargetsRole, "directory") + "/../../traversal")) filepath.Join(CanonicalTargetsRole, "directory")+"/../../traversal"))
assert.False(t, IsDelegation( assert.False(t, IsDelegation(
filepath.Join(CanonicalTargetsRole) + "///test/middle/slashes")) filepath.Join(CanonicalTargetsRole)+"///test/middle/slashes"))
assert.False(t, IsDelegation( assert.False(t, IsDelegation(
filepath.Join(CanonicalTargetsRole) + "/./././")) filepath.Join(CanonicalTargetsRole)+"/./././"))
assert.False(t, IsDelegation( assert.False(t, IsDelegation(
filepath.Join(" ", CanonicalTargetsRole, "level1"))) filepath.Join(" ", CanonicalTargetsRole, "level1")))
assert.False(t, IsDelegation( assert.False(t, IsDelegation(
filepath.Join(" " + CanonicalTargetsRole, "level1"))) filepath.Join(" "+CanonicalTargetsRole, "level1")))
assert.False(t, IsDelegation( assert.False(t, IsDelegation(
filepath.Join(CanonicalTargetsRole, "level1" + " "))) filepath.Join(CanonicalTargetsRole, "level1"+" ")))
assert.False(t, IsDelegation( assert.False(t, IsDelegation(
filepath.Join(CanonicalTargetsRole, "white space" + "level2"))) filepath.Join(CanonicalTargetsRole, "white space"+"level2")))
} }
func TestValidRoleFunction(t *testing.T) { func TestValidRoleFunction(t *testing.T) {