From 660c4b1de627bec60c796043da2cfddaf9eaf582 Mon Sep 17 00:00:00 2001 From: Giteabot Date: Sat, 14 Nov 2026 12:18:43 +0000 Subject: [PATCH] fix(packages): accept OCI 1.1 container image names (#40363) (#40369) Backport #40363 by @lazerg The container registry rejected image names with `__` or runs of `-` (like `a__b` or `a--b`) with `NAME_INVALID`, because the name check still used the distribution-spec v1.0 grammar. This updates the pattern to the v1.1 grammar, which docker and podman already accept. Fixes: https://github.com/go-gitea/gitea/issues/40360 Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: wxiaoguang Co-authored-by: Lazizbek Ergashev Co-authored-by: wxiaoguang --- routers/api/packages/container/container.go | 3 ++- .../api/packages/container/container_test.go | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 routers/api/packages/container/container_test.go diff --git a/routers/api/packages/container/container.go b/routers/api/packages/container/container.go index ea527a9f12f..35817e6ccee 100644 --- a/routers/api/packages/container/container.go +++ b/routers/api/packages/container/container.go @@ -45,7 +45,8 @@ var globalVars = sync.OnceValue(func() (ret struct { imageNamePattern, referencePattern *regexp.Regexp }, ) { - ret.imageNamePattern = regexp.MustCompile(`\A[a-z0-9]+([._-][a-z0-9]+)*(/[a-z0-9]+([._-][a-z0-9]+)*)*\z`) + // ref: https://github.com/opencontainers/distribution-spec/blob/v1.1.1/spec.md#pulling-manifests + ret.imageNamePattern = regexp.MustCompile(`\A[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*(/[a-z0-9]+((\.|_|__|-+)[a-z0-9]+)*)*\z`) ret.referencePattern = regexp.MustCompile(`\A[a-zA-Z0-9_][a-zA-Z0-9._-]{0,127}\z`) return ret }) diff --git a/routers/api/packages/container/container_test.go b/routers/api/packages/container/container_test.go new file mode 100644 index 00000000000..cbf3b504cf8 --- /dev/null +++ b/routers/api/packages/container/container_test.go @@ -0,0 +1,19 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package container + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestImageNamePattern(t *testing.T) { + for _, name := range []string{"a", "a.b", "a_b", "a__b", "a-b", "a--b", "a---b", "sub/a__b/c--d"} { + assert.True(t, globalVars().imageNamePattern.MatchString(name), name) + } + for _, name := range []string{"", "A", "a___b", "a._b", "-a", "a-", "a//b", "/a", "a/"} { + assert.False(t, globalVars().imageNamePattern.MatchString(name), name) + } +}