Skip to content

Commit b46f044

Browse files
committed
update volume name regex
Disallow creating a volume starting with a /. Signed-off-by: Jessica Frazelle <[email protected]>
1 parent 723be0a commit b46f044

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

utils/names.go

+3
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
77

88
// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters.
99
var RestrictedNamePattern = regexp.MustCompile(`^/?` + RestrictedNameChars + `+$`)
10+
11+
// RestrictedVolumeNamePattern is a regular expression to validate volume names against the collection of restricted characters.
12+
var RestrictedVolumeNamePattern = regexp.MustCompile(`^` + RestrictedNameChars + `+$`)

volume/local/local.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
// volumeNameRegex ensures the name assigned for the volume is valid.
3232
// This name is used to create the bind directory, so we need to avoid characters that
3333
// would make the path to escape the root directory.
34-
volumeNameRegex = utils.RestrictedNamePattern
34+
volumeNameRegex = utils.RestrictedVolumeNamePattern
3535
)
3636

3737
// New instantiates a new Root instance with the provided scope. Scope

volume/local/local_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,24 @@ func TestCreate(t *testing.T) {
124124
}
125125
}
126126
}
127+
128+
func TestValidateName(t *testing.T) {
129+
r := &Root{}
130+
names := map[string]bool{
131+
"/testvol": false,
132+
"thing.d": true,
133+
"hello-world": true,
134+
"./hello": false,
135+
".hello": false,
136+
}
137+
138+
for vol, expected := range names {
139+
err := r.validateName(vol)
140+
if expected && err != nil {
141+
t.Fatalf("expected %s to be valid got %v", vol, err)
142+
}
143+
if !expected && err == nil {
144+
t.Fatalf("expected %s to be invalid", vol)
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)