Browse Source

Merge pull request #3197 from ajhager/3138-names

Validate container names on creation. Fixes #3138
Guillaume J. Charmes 11 năm trước cách đây
mục cha
commit
a6928e70ac
1 tập tin đã thay đổi với 10 bổ sung1 xóa
  1. 10 1
      runtime.go

+ 10 - 1
runtime.go

@@ -18,6 +18,7 @@ import (
 	"os"
 	"os"
 	"os/exec"
 	"os/exec"
 	"path"
 	"path"
+	"regexp"
 	"sort"
 	"sort"
 	"strings"
 	"strings"
 	"sync"
 	"sync"
@@ -29,7 +30,10 @@ import (
 // For more information see: http://sourceforge.net/p/aufs/aufs3-standalone/ci/aufs3.12/tree/config.mk
 // For more information see: http://sourceforge.net/p/aufs/aufs3-standalone/ci/aufs3.12/tree/config.mk
 const MaxImageDepth = 127
 const MaxImageDepth = 127
 
 
-var defaultDns = []string{"8.8.8.8", "8.8.4.4"}
+var (
+	defaultDns         = []string{"8.8.8.8", "8.8.4.4"}
+	validContainerName = regexp.MustCompile(`^/?[a-zA-Z0-9_-]+$`)
+)
 
 
 type Capabilities struct {
 type Capabilities struct {
 	MemoryLimit            bool
 	MemoryLimit            bool
@@ -420,7 +424,12 @@ func (runtime *Runtime) Create(config *Config, name string) (*Container, []strin
 		if err != nil {
 		if err != nil {
 			name = utils.TruncateID(id)
 			name = utils.TruncateID(id)
 		}
 		}
+	} else {
+		if !validContainerName.MatchString(name) {
+			return nil, nil, fmt.Errorf("Invalid container name (%s), only [a-zA-Z0-9_-] are allowed", name)
+		}
 	}
 	}
+
 	if name[0] != '/' {
 	if name[0] != '/' {
 		name = "/" + name
 		name = "/" + name
 	}
 	}