diff --git a/commit.go b/commit.go index 139355517..9f12e5d14 100644 --- a/commit.go +++ b/commit.go @@ -18,7 +18,6 @@ import ( "github.com/containers/image/v5/signature" is "github.com/containers/image/v5/storage" "github.com/containers/image/v5/transports" - "github.com/containers/image/v5/transports/alltransports" "github.com/containers/image/v5/types" encconfig "github.com/containers/ocicrypt/config" "github.com/containers/storage" @@ -190,13 +189,11 @@ func (b *Builder) addManifest(ctx context.Context, manifestName string, imageSpe return "", errors.Wrapf(err, "error encountered while expanding image name %q", manifestName) } - ref, err := alltransports.ParseImageName(imageSpec) + ref, err := util.VerifyTagName(imageSpec) if err != nil { - if ref, err = alltransports.ParseImageName(util.DefaultTransport + imageSpec); err != nil { - // check if the local image exists - if ref, _, err = util.FindImage(b.store, "", systemContext, imageSpec); err != nil { - return "", err - } + // check if the local image exists + if ref, _, err = util.FindImage(b.store, "", systemContext, imageSpec); err != nil { + return "", err } } diff --git a/imagebuildah/build.go b/imagebuildah/build.go index 62e656271..d2282e268 100644 --- a/imagebuildah/build.go +++ b/imagebuildah/build.go @@ -13,6 +13,7 @@ import ( "strings" "github.com/containers/buildah/define" + "github.com/containers/buildah/util" "github.com/containers/common/pkg/config" "github.com/containers/image/v5/docker/reference" "github.com/containers/storage" @@ -56,6 +57,14 @@ func BuildDockerfiles(ctx context.Context, store storage.Store, options define.B } }(dockerfiles...) + for _, tag := range append([]string{options.Output}, options.AdditionalTags...) { + if tag == "" { + continue + } + if _, err := util.VerifyTagName(tag); err != nil { + return "", nil, errors.Wrapf(err, "tag %s", tag) + } + } for _, dfile := range paths { var data io.ReadCloser diff --git a/tests/bud.bats b/tests/bud.bats index 01c630a9e..18a99bae7 100644 --- a/tests/bud.bats +++ b/tests/bud.bats @@ -406,6 +406,15 @@ symlink(subdir)" expect_output --substring "Successfully tagged localhost/test2:latest" } +@test "bud with bad --tag " { + target=scratch-image + run_buildah 125 bud --quiet=false --tag TEST1 --signature-policy ${TESTSDIR}/policy.json -t ${target} ${TESTSDIR}/bud/from-scratch + expect_output --substring "tag TEST1: invalid reference format: repository name must be lowercase" + + run_buildah 125 bud --quiet=false --tag test1 --tag TEST2 --signature-policy ${TESTSDIR}/policy.json -t ${target} ${TESTSDIR}/bud/from-scratch + expect_output --substring "tag TEST2: invalid reference format: repository name must be lowercase" +} + @test "bud-from-scratch-iid" { target=scratch-image run_buildah bud --iidfile ${TESTDIR}/output.iid --signature-policy ${TESTSDIR}/policy.json -t ${target} ${TESTSDIR}/bud/from-scratch diff --git a/util/util.go b/util/util.go index 3b22a3943..5e52a4f65 100644 --- a/util/util.go +++ b/util/util.go @@ -482,3 +482,13 @@ func SortMounts(m []specs.Mount) []specs.Mount { sort.Sort(byDestination(m)) return m } + +func VerifyTagName(imageSpec string) (types.ImageReference, error) { + ref, err := alltransports.ParseImageName(imageSpec) + if err != nil { + if ref, err = alltransports.ParseImageName(DefaultTransport + imageSpec); err != nil { + return nil, err + } + } + return ref, nil +}