diff --git a/api/client/commands.go b/api/client/commands.go index 5c8e31b9f5..a6e7defc79 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -26,6 +26,7 @@ import ( "github.com/docker/docker/archive" "github.com/docker/docker/dockerversion" "github.com/docker/docker/engine" + "github.com/docker/docker/graph" "github.com/docker/docker/nat" "github.com/docker/docker/opts" "github.com/docker/docker/pkg/log" @@ -174,10 +175,15 @@ func (cli *DockerCli) CmdBuild(args ...string) error { //Check if the given image name can be resolved if *tag != "" { - repository, _ := parsers.ParseRepositoryTag(*tag) + repository, tag := parsers.ParseRepositoryTag(*tag) if _, _, err := registry.ResolveRepositoryName(repository); err != nil { return err } + if len(tag) > 0 { + if err := graph.ValidateTagName(tag); err != nil { + return err + } + } } v.Set("t", *tag) diff --git a/graph/tags.go b/graph/tags.go index 1c2dd36a20..6643ebceac 100644 --- a/graph/tags.go +++ b/graph/tags.go @@ -209,7 +209,7 @@ func (store *TagStore) Set(repoName, tag, imageName string, force bool) error { if err := validateRepoName(repoName); err != nil { return err } - if err := validateTagName(tag); err != nil { + if err := ValidateTagName(tag); err != nil { return err } if err := store.reload(); err != nil { @@ -285,7 +285,7 @@ func validateRepoName(name string) error { } // Validate the name of a tag -func validateTagName(name string) error { +func ValidateTagName(name string) error { if name == "" { return fmt.Errorf("Tag name can't be empty") } diff --git a/graph/tags_unit_test.go b/graph/tags_unit_test.go index 918880c755..d3111cfd65 100644 --- a/graph/tags_unit_test.go +++ b/graph/tags_unit_test.go @@ -118,7 +118,7 @@ func TestLookupImage(t *testing.T) { func TestValidTagName(t *testing.T) { validTags := []string{"9", "foo", "foo-test", "bar.baz.boo"} for _, tag := range validTags { - if err := validateTagName(tag); err != nil { + if err := ValidateTagName(tag); err != nil { t.Errorf("'%s' should've been a valid tag", tag) } } @@ -127,7 +127,7 @@ func TestValidTagName(t *testing.T) { func TestInvalidTagName(t *testing.T) { validTags := []string{"-9", ".foo", "-test", ".", "-"} for _, tag := range validTags { - if err := validateTagName(tag); err == nil { + if err := ValidateTagName(tag); err == nil { t.Errorf("'%s' shouldn't have been a valid tag", tag) } } diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index 92846eeb99..d33a13752f 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -2237,3 +2237,16 @@ func TestBuildOnBuildOutput(t *testing.T) { logDone("build - onbuild output") } + +func TestBuildInvalidTag(t *testing.T) { + name := "abcd:A0123456789B0123456789C0123456789" + defer deleteImages(name) + _, out, err := buildImageWithOut(name, "FROM scratch\nMAINTAINER quux\n", true) + // if the error doesnt check for illegal tag name, or the image is built + // then this should fail + if !strings.Contains(err.Error(), "Illegal tag name") || + strings.Contains(out, "Sending build context to Docker daemon") { + t.Fatalf("failed to stop before building. Error: %s, Output: %s", err, out) + } + logDone("build - invalid tag") +}