Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,11 @@ func DefaultStoreOptions(rootless bool, rootlessUID int) (StoreOptions, error) {
}
if storageOpts.GraphRoot == "" {
storageOpts.GraphRoot = defaultRootlessGraphRoot
} else if storageOpts.RootlessStoragePath != "" {
splitPaths := strings.SplitAfter(storageOpts.RootlessStoragePath, "$")
validEnv := regexp.MustCompile(`^(HOME|USER|UID)[^a-zA-Z]`).MatchString
if len(splitPaths) > 1 {
for _, path := range splitPaths {
if !validEnv(path) {
return storageOpts, errors.Errorf("Unrecognized environment variable")
}
}
}
if storageOpts.RootlessStoragePath != "" {
if err = validRootlessStoragePathFormat(storageOpts.RootlessStoragePath); err != nil {
return storageOpts, err
}

rootlessStoragePath := strings.Replace(storageOpts.RootlessStoragePath, "$HOME", homedir.Get(), -1)
rootlessStoragePath = strings.Replace(rootlessStoragePath, "$UID", strconv.Itoa(rootlessUID), -1)
usr, err := user.LookupId(strconv.Itoa(rootlessUID))
Expand Down Expand Up @@ -271,3 +265,21 @@ func DefaultStoreOptions(rootless bool, rootlessUID int) (StoreOptions, error) {
}
return storageOpts, nil
}

// validRootlessStoragePathFormat checks if the environments contained in the path are accepted
func validRootlessStoragePathFormat(path string) error {
if !strings.Contains(path, "$") {
return nil
}

splitPaths := strings.SplitAfter(path, "$")
validEnv := regexp.MustCompile(`^(HOME|USER|UID)([^a-zA-Z]|$)`).MatchString
if len(splitPaths) > 1 {
for _, p := range splitPaths[1:] {
if !validEnv(p) {
return errors.Errorf("Unrecognized environment variable")
}
}
}
return nil
}
44 changes: 44 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package storage

import (
"testing"

"gotest.tools/assert"
)

func TestValidStoragePathFormat(t *testing.T) {
// Given
expectErr := "Unrecognized environment variable"
invalidPaths := []struct {
path string
expect string
}{
{"$", expectErr},
{"$HOMEDIR", expectErr},
{"$HOMEdir", expectErr},
{"/test/$HOMEDIR/$USERNAME/$UID", expectErr},
{"/test/$HOME/$USERNAME/$UID", expectErr},
{"/test/$HOME/$USER/$UIDNUM", expectErr},
}
validPaths := []string{
"$HOME",
"$HOME/",
"/test/path",
"/test/$HOME",
"/test/$HOME/path",
"/test/$HOME/$USER/$UID",
"/test/$HOME/$USER/$UID/path",
"$HOME/$USER/$UID/path",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about these valid paths?
/tmp/$HOME$USER$UID
/tmp/$HOME-$USER-$UID

}

// Then
for _, conf := range invalidPaths {
err := validRootlessStoragePathFormat(conf.path)
assert.Error(t, err, "Unrecognized environment variable")
}

for _, path := range validPaths {
err := validRootlessStoragePathFormat(path)
assert.NilError(t, err)
}
}