Browse Source

Ensure container names start with a-zA-Z0-9

Closes #8012.

Docker-DCO-1.1-Signed-off-by: Jessica Frazelle <jess@docker.com> (github: jfrazelle)
Jessica Frazelle 10 years ago
parent
commit
aa2af817be
2 changed files with 18 additions and 1 deletions
  1. 17 0
      daemon/container_unit_test.go
  2. 1 1
      daemon/daemon.go

+ 17 - 0
daemon/container_unit_test.go

@@ -178,3 +178,20 @@ func TestGetFullName(t *testing.T) {
 		t.Fatal("Error should not be nil")
 	}
 }
+
+func TestValidContainerNames(t *testing.T) {
+	invalidNames := []string{"-rm", "&sdfsfd", "safd%sd"}
+	validNames := []string{"word-word", "word_word", "1weoid"}
+
+	for _, name := range invalidNames {
+		if validContainerNamePattern.MatchString(name) {
+			t.Fatalf("%q is not a valid container name and was returned as valid.", name)
+		}
+	}
+
+	for _, name := range validNames {
+		if !validContainerNamePattern.MatchString(name) {
+			t.Fatalf("%q is a valid container name and was returned as invalid.", name)
+		}
+	}
+}

+ 1 - 1
daemon/daemon.go

@@ -42,7 +42,7 @@ import (
 
 var (
 	DefaultDns                = []string{"8.8.8.8", "8.8.4.4"}
-	validContainerNameChars   = `[a-zA-Z0-9_.-]`
+	validContainerNameChars   = `[a-zA-Z0-9][a-zA-Z0-9_.-]`
 	validContainerNamePattern = regexp.MustCompile(`^/?` + validContainerNameChars + `+$`)
 )