validate the name of named volume

Signed-off-by: xlgao-zju <xlgao@zju.edu.cn>
Upstream-commit: 609961ddcc835422e51ef4f3fd6088b96180f72f
Component: engine
This commit is contained in:
xlgao-zju
2015-10-20 12:36:04 -04:00
committed by David Calavera
parent db6db88cda
commit 7cf93e47cd
3 changed files with 19 additions and 0 deletions
@@ -24,6 +24,10 @@ func TestParseBindMount(t *testing.T) {
{"name:/tmp:ro", "local", "/tmp", "", "name", "local", false, false},
{"local/name:/tmp:rw", "", "/tmp", "", "local/name", "local", true, false},
{"/tmp:tmp", "", "", "", "", "", true, true},
{"./name:/tmp", "", "", "", "", "", true, true},
{"../name:/tmp", "", "", "", "", "", true, true},
{"./:/tmp", "", "", "", "", "", true, true},
{"../:/tmp", "", "", "", "", "", true, true},
}
for _, c := range cases {
+7
View File
@@ -6,6 +6,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
@@ -102,6 +103,12 @@ func parseBindMount(spec, volumeDriver string) (*mountPoint, error) {
}
if len(source) == 0 {
//validate the name of named volume
nameRegex := regexp.MustCompile(`(^.+[^0-9A-Za-z_]+$)|(/)`)
if nameRegex.MatchString(name) {
return nil, derr.ErrorCodeVolumeName.WithArgs(name)
}
bind.Driver = volumeDriver
if len(bind.Driver) == 0 {
bind.Driver = volume.DefaultDriverName
+8
View File
@@ -385,6 +385,14 @@ var (
HTTPStatusCode: http.StatusInternalServerError,
})
// ErrorCodeVolumeName is generated when the name of named volume isn't valid.
ErrorCodeVolumeName = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "VOLUMENAME",
Message: "%s looks like a relative path, but it's taken as a name. And it is not a valid name.",
Description: "The name of named volume is invalid",
HTTPStatusCode: http.StatusInternalServerError,
})
// ErrorCodeVolumeFromBlank is generated when path to a volume is blank.
ErrorCodeVolumeFromBlank = errcode.Register(errGroup, errcode.ErrorDescriptor{
Value: "VOLUMEFROMBLANK",