From aa2af817bee123827f4a857555db8fea5e01e7d7 Mon Sep 17 00:00:00 2001 From: Jessica Frazelle Date: Fri, 12 Sep 2014 10:45:07 -0700 Subject: [PATCH] Ensure container names start with a-zA-Z0-9 Closes #8012. Docker-DCO-1.1-Signed-off-by: Jessica Frazelle (github: jfrazelle) --- daemon/container_unit_test.go | 17 +++++++++++++++++ daemon/daemon.go | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/daemon/container_unit_test.go b/daemon/container_unit_test.go index 1b1b934f42..9599675f79 100644 --- a/daemon/container_unit_test.go +++ b/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) + } + } +} diff --git a/daemon/daemon.go b/daemon/daemon.go index f24745901c..b9c652cb4e 100644 --- a/daemon/daemon.go +++ b/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 + `+$`) )