Browse code

Validate container names on creation. Fixes #3138

Move valid container name regex to the top of the file

Added hyphen as a valid rune in container names.

Remove group in valid container name regex.

Joseph Hager authored on 2013/12/13 06:34:26
Showing 1 changed files
... ...
@@ -18,6 +18,7 @@ import (
18 18
 	"os"
19 19
 	"os/exec"
20 20
 	"path"
21
+	"regexp"
21 22
 	"sort"
22 23
 	"strings"
23 24
 	"sync"
... ...
@@ -27,7 +28,10 @@ import (
27 27
 // Set the max depth to the aufs restriction
28 28
 const MaxImageDepth = 42
29 29
 
30
-var defaultDns = []string{"8.8.8.8", "8.8.4.4"}
30
+var (
31
+	defaultDns         = []string{"8.8.8.8", "8.8.4.4"}
32
+	validContainerName = regexp.MustCompile(`^/?[a-zA-Z0-9_-]+$`)
33
+)
31 34
 
32 35
 type Capabilities struct {
33 36
 	MemoryLimit            bool
... ...
@@ -418,7 +422,12 @@ func (runtime *Runtime) Create(config *Config, name string) (*Container, []strin
418 418
 		if err != nil {
419 419
 			name = utils.TruncateID(id)
420 420
 		}
421
+	} else {
422
+		if !validContainerName.MatchString(name) {
423
+			return nil, nil, fmt.Errorf("Invalid container name (%s), only [a-zA-Z0-9_-] are allowed", name)
424
+		}
421 425
 	}
426
+
422 427
 	if name[0] != '/' {
423 428
 		name = "/" + name
424 429
 	}