Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b576f54

Browse files
xlgao-zjuTibor Vass
authored andcommitted
validate the name of named volume
Signed-off-by: xlgao-zju <[email protected]>
1 parent d2e25ed commit b576f54

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

daemon/volumes_linux_unit_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ func TestParseBindMount(t *testing.T) {
2424
{"name:/tmp:ro", "local", "/tmp", "", "name", "local", false, false},
2525
{"local/name:/tmp:rw", "", "/tmp", "", "local/name", "local", true, false},
2626
{"/tmp:tmp", "", "", "", "", "", true, true},
27+
{"./name:/tmp", "", "", "", "", "", true, true},
28+
{"../name:/tmp", "", "", "", "", "", true, true},
29+
{"./:/tmp", "", "", "", "", "", true, true},
30+
{"../:/tmp", "", "", "", "", "", true, true},
2731
}
2832

2933
for _, c := range cases {

daemon/volumes_unix.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"os"
88
"path/filepath"
9+
"regexp"
910
"sort"
1011
"strings"
1112

@@ -102,6 +103,12 @@ func parseBindMount(spec, volumeDriver string) (*mountPoint, error) {
102103
}
103104

104105
if len(source) == 0 {
106+
//validate the name of named volume
107+
nameRegex := regexp.MustCompile(`(^.+[^0-9A-Za-z_]+$)|(/)`)
108+
if nameRegex.MatchString(name) {
109+
return nil, derr.ErrorCodeVolumeName.WithArgs(name)
110+
}
111+
105112
bind.Driver = volumeDriver
106113
if len(bind.Driver) == 0 {
107114
bind.Driver = volume.DefaultDriverName

errors/daemon.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,14 @@ var (
385385
HTTPStatusCode: http.StatusInternalServerError,
386386
})
387387

388+
// ErrorCodeVolumeName is generated when the name of named volume isn't valid.
389+
ErrorCodeVolumeName = errcode.Register(errGroup, errcode.ErrorDescriptor{
390+
Value: "VOLUMENAME",
391+
Message: "%s looks like a relative path, but it's taken as a name. And it is not a valid name.",
392+
Description: "The name of named volume is invalid",
393+
HTTPStatusCode: http.StatusInternalServerError,
394+
})
395+
388396
// ErrorCodeVolumeFromBlank is generated when path to a volume is blank.
389397
ErrorCodeVolumeFromBlank = errcode.Register(errGroup, errcode.ErrorDescriptor{
390398
Value: "VOLUMEFROMBLANK",

0 commit comments

Comments
 (0)