Skip to content

Commit ee264c6

Browse files
committed
feat: enhance Docker image name validation and update dependencies
- Added validation for Docker image names using the reference package from github.com/distribution/reference. - Improved test cases for Docker image name validation to cover more scenarios.
1 parent c548437 commit ee264c6

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

cmd/cli/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cli
22

33
import (
4+
"fmt"
45
"os"
56

67
"github.com/spf13/cobra"
@@ -18,6 +19,7 @@ var rootCmd = &cobra.Command{
1819
func Execute() {
1920
err := rootCmd.Execute()
2021
if err != nil {
22+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
2123
os.Exit(1)
2224
}
2325
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ require (
1515
require (
1616
github.com/containerd/stargz-snapshotter/estargz v0.16.3 // indirect
1717
github.com/davecgh/go-spew v1.1.1 // indirect
18+
github.com/distribution/reference v0.6.0 // indirect
1819
github.com/docker/cli v28.1.1+incompatible // indirect
1920
github.com/docker/distribution v2.8.3+incompatible // indirect
2021
github.com/docker/docker-credential-helpers v0.9.3 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6N
66
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
77
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
88
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9+
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
10+
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
911
github.com/docker/cli v28.1.1+incompatible h1:eyUemzeI45DY7eDPuwUcmDyDj1pM98oD5MdSpiItp8k=
1012
github.com/docker/cli v28.1.1+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
1113
github.com/docker/distribution v2.8.3+incompatible h1:AtKxIZ36LoNK51+Z6RpzLpddBirtxJnzDrHLEKxTAYk=

internal/docker/fucntion_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,28 @@ func TestValidateDockerImageName(t *testing.T) {
1212
image string
1313
expected bool
1414
}{
15-
// Valid image names
15+
// Valid image names (following Docker and OCI standards)
1616
{"Valid image name without registry or tag", "ubuntu", true},
1717
{"Valid image name with namespace", "library/ubuntu", true},
1818
{"Valid image name with registry", "docker.io/library/ubuntu", true},
1919
{"Valid image name with custom registry and port", "localhost:5000/myproject/ubuntu", true},
2020
{"Valid image name with tag", "myregistry/myproject/ubuntu:latest", true},
21-
{"Valid image name with digest", "myregistry/myproject/ubuntu@sha256:abc123", true},
21+
{"Valid image name with version tag", "nginx:1.21.0", true},
22+
{"Valid image name with custom tag", "myapp:v1.0.0-beta", true},
23+
{"Valid image name with underscore", "my_project/app:latest", true},
24+
{"Valid image name with multiple slashes", "org/team/project/app:latest", true},
25+
{"Valid image name with IP address registry", "192.168.1.100:5000/app:latest", true},
2226

2327
// Invalid image names
2428
{"Invalid image name with uppercase letters in repository", "MyRegistry/MyProject/Ubuntu", false},
2529
{"Invalid image name with invalid characters", "invalid!@#/image/name", false},
26-
{"Invalid image name with empty repository", "", false},
30+
{"Invalid image name with empty string", "", false},
2731
{"Invalid image name with only slashes", "///", false},
2832
{"Invalid image name with multiple @ symbols", "invalid@image@name", false},
33+
{"Invalid image name with invalid tag characters", "app:tag:with:colons", false},
34+
{"Invalid image name with leading slash", "/app:latest", false},
35+
{"Invalid image name with trailing slash", "app/:latest", false},
36+
{"Invalid image name with consecutive slashes", "app//name:latest", false},
2937
}
3038

3139
for _, tt := range tests {

internal/docker/function.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,30 @@ import (
88
"net/http"
99
"os"
1010
"path/filepath"
11-
"regexp"
1211
"strconv"
1312
"strings"
1413
"sync/atomic"
1514
"time"
1615

1716
"403unlocker-cli/internal/common"
1817

18+
"github.com/distribution/reference"
1919
"github.com/google/go-containerregistry/pkg/authn"
2020
"github.com/google/go-containerregistry/pkg/name"
2121
"github.com/google/go-containerregistry/pkg/v1/remote"
2222
"github.com/google/go-containerregistry/pkg/v1/tarball"
2323
)
2424

25-
// DockerImageValidator validates a Docker image name using a regular expression.
25+
// DockerImageValidator validates a Docker image name using the same method as kubectl.
26+
// This uses reference.ReferenceRegexp from github.com/distribution/reference package
27+
// which provides comprehensive validation for Docker image references.
2628
func DockerImageValidator(imageName string) bool {
27-
pattern := `^(?:[a-zA-Z0-9\-._]+(?::[0-9]+)?/)?` +
28-
`(?:[a-z0-9\-._]+/)?` +
29-
`[a-z0-9\-._]+` +
30-
`(?::[a-zA-Z0-9\-._]+)?` +
31-
`(?:@[a-zA-Z0-9\-._:]+)?$`
32-
regex := regexp.MustCompile(pattern)
33-
return regex.MatchString(imageName) && !strings.Contains(imageName, "@@")
29+
if imageName == "" {
30+
return false
31+
}
32+
33+
// Use the same validation method as kubectl
34+
return reference.ReferenceRegexp.MatchString(imageName)
3435
}
3536

3637
// customTransport tracks the number of bytes transferred during HTTP requests.

0 commit comments

Comments
 (0)