Skip to content

Commit aaca75e

Browse files
committed
Exclude domain from name length check
Signed-off-by: Ozair <ozair.asim@docker.com>
1 parent 8507c7f commit aaca75e

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

reference.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ import (
3535
)
3636

3737
const (
38+
// RepositoryNameTotalLengthMax is the maximum total number of characters in a repository name.
39+
RepositoryNameTotalLengthMax = 255
40+
3841
// NameTotalLengthMax is the maximum total number of characters in a repository name.
39-
NameTotalLengthMax = 255
42+
//
43+
// Deprecated: use [RepositoryNameTotalLengthMax] instead.
44+
NameTotalLengthMax = RepositoryNameTotalLengthMax
4045
)
4146

4247
var (
@@ -55,8 +60,8 @@ var (
5560
// ErrNameEmpty is returned for empty, invalid repository names.
5661
ErrNameEmpty = errors.New("repository name must have at least one component")
5762

58-
// ErrNameTooLong is returned when a repository name is longer than NameTotalLengthMax.
59-
ErrNameTooLong = fmt.Errorf("repository name must not be more than %v characters", NameTotalLengthMax)
63+
// ErrNameTooLong is returned when a repository name is longer than RepositoryNameTotalLengthMax.
64+
ErrNameTooLong = fmt.Errorf("repository name must not be more than %v characters", RepositoryNameTotalLengthMax)
6065

6166
// ErrNameNotCanonical is returned when a name is not canonical.
6267
ErrNameNotCanonical = errors.New("repository name must be canonical")
@@ -190,10 +195,6 @@ func Parse(s string) (Reference, error) {
190195
return nil, ErrReferenceInvalidFormat
191196
}
192197

193-
if len(matches[1]) > NameTotalLengthMax {
194-
return nil, ErrNameTooLong
195-
}
196-
197198
var repo repository
198199

199200
nameMatch := anchoredNameRegexp.FindStringSubmatch(matches[1])
@@ -205,6 +206,10 @@ func Parse(s string) (Reference, error) {
205206
repo.path = matches[1]
206207
}
207208

209+
if len(repo.path) > RepositoryNameTotalLengthMax {
210+
return nil, ErrNameTooLong
211+
}
212+
208213
ref := reference{
209214
namedRepository: repo,
210215
tag: matches[2],
@@ -243,14 +248,15 @@ func ParseNamed(s string) (Named, error) {
243248
// WithName returns a named object representing the given string. If the input
244249
// is invalid ErrReferenceInvalidFormat will be returned.
245250
func WithName(name string) (Named, error) {
246-
if len(name) > NameTotalLengthMax {
247-
return nil, ErrNameTooLong
248-
}
249-
250251
match := anchoredNameRegexp.FindStringSubmatch(name)
251252
if match == nil || len(match) != 3 {
252253
return nil, ErrReferenceInvalidFormat
253254
}
255+
256+
if len(match[2]) > RepositoryNameTotalLengthMax {
257+
return nil, ErrNameTooLong
258+
}
259+
254260
return repository{
255261
domain: match[1],
256262
path: match[2],

reference_test.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
_ "crypto/sha256"
55
_ "crypto/sha512"
66
"encoding/json"
7+
"errors"
78
"strings"
89
"testing"
910

@@ -117,7 +118,7 @@ func TestReferenceParse(t *testing.T) {
117118
tag: "Uppercase",
118119
},
119120
{
120-
input: strings.Repeat("a/", 128) + "a:tag",
121+
input: "domain/" + strings.Repeat("a", 256) + ":tag",
121122
err: ErrNameTooLong,
122123
},
123124
{
@@ -266,6 +267,12 @@ func TestReferenceParse(t *testing.T) {
266267
input: "[fe80::1%@invalidzone]:5000/repo",
267268
err: ErrReferenceInvalidFormat,
268269
},
270+
{
271+
input: "example.com/" + strings.Repeat("a", 255) + ":tag",
272+
domain: "example.com",
273+
repository: "example.com/" + strings.Repeat("a", 255),
274+
tag: "tag",
275+
},
269276
}
270277
for _, tc := range tests {
271278
tc := tc
@@ -337,7 +344,7 @@ func TestWithNameFailure(t *testing.T) {
337344
}{
338345
{
339346
input: "",
340-
err: ErrNameEmpty,
347+
err: ErrReferenceInvalidFormat,
341348
},
342349
{
343350
input: ":justtag",
@@ -352,7 +359,11 @@ func TestWithNameFailure(t *testing.T) {
352359
err: ErrReferenceInvalidFormat,
353360
},
354361
{
355-
input: strings.Repeat("a/", 128) + "a:tag",
362+
input: "example.com/repo:tag",
363+
err: ErrReferenceInvalidFormat,
364+
},
365+
{
366+
input: "example.com/" + strings.Repeat("a", 256),
356367
err: ErrNameTooLong,
357368
},
358369
{
@@ -365,8 +376,8 @@ func TestWithNameFailure(t *testing.T) {
365376
t.Run(tc.input, func(t *testing.T) {
366377
t.Parallel()
367378
_, err := WithName(tc.input)
368-
if err == nil {
369-
t.Errorf("no error parsing name. expected: %s", tc.err)
379+
if !errors.Is(err, tc.err) {
380+
t.Errorf("unexpected error parsing name. expected: %s, got: %s", tc.err, err)
370381
}
371382
})
372383
}

0 commit comments

Comments
 (0)