Skip to content

Commit

Permalink
fix: if reference exceeds max threshold return 400 and detail (#4168)
Browse files Browse the repository at this point in the history
  • Loading branch information
milosgajdos authored Nov 24, 2023
2 parents 96582fc + 35abc92 commit 97f8a6c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions registry/api/errcode/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,20 @@ func (errs Errors) MarshalJSON() ([]byte, error) {
msg = err.Code.Message()
}

tmpErrs.Errors = append(tmpErrs.Errors, Error{
tmpErr := Error{
Code: err.Code,
Message: msg,
Detail: err.Detail,
})
}

// if the detail contains error extract the error message
// otherwise json.Marshal will not serialize it at all
// https://github.com/golang/go/issues/10748
if detail, ok := tmpErr.Detail.(error); ok {
tmpErr.Detail = detail.Error()
}

tmpErrs.Errors = append(tmpErrs.Errors, tmpErr)
}

return json.Marshal(tmpErrs)
Expand Down
6 changes: 6 additions & 0 deletions registry/storage/cache/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func NewInMemoryBlobDescriptorCacheProvider(size int) cache.BlobDescriptorCacheP

func (imbdcp *inMemoryBlobDescriptorCacheProvider) RepositoryScoped(repo string) (distribution.BlobDescriptorService, error) {
if _, err := reference.ParseNormalizedNamed(repo); err != nil {
if err == reference.ErrNameTooLong {
return nil, distribution.ErrRepositoryNameInvalid{
Name: repo,
Reason: reference.ErrNameTooLong,
}
}
return nil, err
}

Expand Down
6 changes: 6 additions & 0 deletions registry/storage/cache/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func NewRedisBlobDescriptorCacheProvider(pool *redis.Client) cache.BlobDescripto
// RepositoryScoped returns the scoped cache.
func (rbds *redisBlobDescriptorService) RepositoryScoped(repo string) (distribution.BlobDescriptorService, error) {
if _, err := reference.ParseNormalizedNamed(repo); err != nil {
if err == reference.ErrNameTooLong {
return nil, distribution.ErrRepositoryNameInvalid{
Name: repo,
Reason: reference.ErrNameTooLong,
}
}
return nil, err
}

Expand Down

0 comments on commit 97f8a6c

Please sign in to comment.