diff --git a/pkg/registry/reference.go b/pkg/registry/reference.go index cf88d8590..e0e4b7ab8 100644 --- a/pkg/registry/reference.go +++ b/pkg/registry/reference.go @@ -26,6 +26,7 @@ import ( var ( validPortRegEx = regexp.MustCompile("^([1-9]\\d{0,3}|0|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$") // adapted from https://stackoverflow.com/a/12968117 + emptyRepoError = errors.New("parsed repo was empty") tooManyColonsError = errors.New("ref may only contain a single colon character (:) unless specifying a port number") ) @@ -58,7 +59,7 @@ func ParseReference(s string) (*Reference, error) { return &ref, nil } -// setExtraFields adds the Rpeo and Tag fields to a Reference +// setExtraFields adds the Repo and Tag fields to a Reference func (ref *Reference) setExtraFields() { ref.Tag = ref.Object ref.Repo = ref.Locator @@ -92,22 +93,34 @@ func (ref *Reference) fixNoRepo() { // validate makes sure the ref meets our criteria func (ref *Reference) validate() error { - return ref.validateColons() + err := ref.validateRepo() + if err != nil { + return err + } + return ref.validateNumColons() +} + +// validateRepo checks that the Repo field is non-empty +func (ref *Reference) validateRepo() error { + if ref.Repo == "" { + return emptyRepoError + } + return nil } -// validateColons verifies the ref only contains one colon max -// (or two, there might be a port number specified i.e. :5000) -func (ref *Reference) validateColons() error { +// validateNumColon ensures the ref only contains a single colon character (:) +// (or potentially two, there might be a port number specified i.e. :5000) +func (ref *Reference) validateNumColons() error { if strings.Contains(ref.Tag, ":") { return tooManyColonsError } - locParts := strings.Split(ref.Repo, ":") - locLastIndex := len(locParts) - 1 - if 1 < locLastIndex { + parts := strings.Split(ref.Repo, ":") + lastIndex := len(parts) - 1 + if 1 < lastIndex { return tooManyColonsError } - if 0 < locLastIndex { - port := strings.Split(locParts[locLastIndex], "/")[0] + if 0 < lastIndex { + port := strings.Split(parts[lastIndex], "/")[0] if !isValidPort(port) { return tooManyColonsError }