From e00cfbb6f2b953301bfcd7270e9bdfde1a2d17c7 Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Wed, 13 May 2015 14:23:13 +0800 Subject: [PATCH] Fix invalid tag name Signed-off-by: Lei Jitang --- integration-cli/docker_cli_tag_test.go | 24 ++++++++++++++++++++++++ registry/config.go | 6 ++++++ registry/registry_test.go | 3 +++ 3 files changed, 33 insertions(+) diff --git a/integration-cli/docker_cli_tag_test.go b/integration-cli/docker_cli_tag_test.go index 35225f9c1e..694bab95c1 100644 --- a/integration-cli/docker_cli_tag_test.go +++ b/integration-cli/docker_cli_tag_test.go @@ -116,6 +116,30 @@ func (s *DockerSuite) TestTagExistedNameWithForce(c *check.C) { } } +func (s *DockerSuite) TestTagWithSuffixHyphen(c *check.C) { + if err := pullImageIfNotExist("busybox:latest"); err != nil { + c.Fatal("couldn't find the busybox:latest image locally and failed to pull it") + } + // test repository name begin with '-' + tagCmd := exec.Command(dockerBinary, "tag", "busybox:latest", "-busybox:test") + out, _, err := runCommandWithOutput(tagCmd) + if err == nil || !strings.Contains(out, "Invalid repository name (-busybox). Cannot begin or end with a hyphen") { + c.Fatal("tag a name begin with '-' should failed") + } + // test namespace name begin with '-' + tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-test/busybox:test") + out, _, err = runCommandWithOutput(tagCmd) + if err == nil || !strings.Contains(out, "Invalid namespace name (-test). Cannot begin or end with a hyphen") { + c.Fatal("tag a name begin with '-' should failed") + } + // test index name begin wiht '-' + tagCmd = exec.Command(dockerBinary, "tag", "busybox:latest", "-index:5000/busybox:test") + out, _, err = runCommandWithOutput(tagCmd) + if err == nil || !strings.Contains(out, "Invalid index name (-index:5000). Cannot begin or end with a hyphen") { + c.Fatal("tag a name begin with '-' should failed") + } +} + // ensure tagging using official names works // ensure all tags result in the same name func (s *DockerSuite) TestTagOfficialNames(c *check.C) { diff --git a/registry/config.go b/registry/config.go index a0a978cc72..568756f4e5 100644 --- a/registry/config.go +++ b/registry/config.go @@ -198,6 +198,9 @@ func ValidateIndexName(val string) (string, error) { if val == "index."+IndexServerName() { val = IndexServerName() } + if strings.HasPrefix(val, "-") || strings.HasSuffix(val, "-") { + return "", fmt.Errorf("Invalid index name (%s). Cannot begin or end with a hyphen.", val) + } // *TODO: Check if valid hostname[:port]/ip[:port]? return val, nil } @@ -235,6 +238,9 @@ func validateRemoteName(remoteName string) error { if !validRepo.MatchString(name) { return fmt.Errorf("Invalid repository name (%s), only [a-z0-9-_.] are allowed", name) } + if strings.HasPrefix(name, "-") || strings.HasSuffix(name, "-") { + return fmt.Errorf("Invalid repository name (%s). Cannot begin or end with a hyphen.", name) + } return nil } diff --git a/registry/registry_test.go b/registry/registry_test.go index 3f63eb6e25..799d080ed7 100644 --- a/registry/registry_test.go +++ b/registry/registry_test.go @@ -299,6 +299,9 @@ func TestValidateRepositoryName(t *testing.T) { invalidRepoNames := []string{ "https://github.com/docker/docker", "docker/Docker", + "-docker", + "-docker/docker", + "-docker.io/docker/docker", "docker///docker", "docker.io/docker/Docker", "docker.io/docker///docker",