Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions pkg/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,23 @@ func ValidateRegistryName(rn string) bool {
return re.MatchString(rn)
}

// ValidateURL checks if the URL has valid format, non-empty host, and host is a valid IP or domain.
// Domain regex: labels must start/end with alphanumeric, can contain hyphens, max 63 chars, TLD min 2 letters.
func ValidateURL(rawURL string) error {
var domainNameRegex = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
var (
domainNameRegex = regexp.MustCompile(`^(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$`)
hostLabelRegex = regexp.MustCompile(`^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$`)
)

// ValidateURL checks if the URL has valid format, an HTTP(S) scheme, and a valid host.
// Hostnames may be IP addresses, localhost, FQDNs, or single RFC 1123 labels for internal networks.
func ValidateURL(rawURL string) error {
parsedURL, err := url.ParseRequestURI(rawURL)
if err != nil {
return fmt.Errorf("invalid URL format: %v", err)
}

if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
return fmt.Errorf("URL scheme must be http or https")
}

host := parsedURL.Hostname()
if host == "" {
return fmt.Errorf("URL must contain a valid host")
Expand All @@ -196,7 +203,7 @@ func ValidateURL(rawURL string) error {
return nil
}

if host == "localhost" {
if host == "localhost" || hostLabelRegex.MatchString(host) {
return nil
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/utils/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,20 @@ func TestValidateURL(t *testing.T) {
{"valid localhost https", "https://localhost", false},
{"valid localhost http with port", "http://localhost:8080", false},
{"valid localhost https with port", "https://localhost:8443", false},
{"valid single-label host", "http://harbor", false},
{"valid single-label host with hyphen", "http://harbor-core", false},
{"valid single-label host with port", "https://registry:8443", false},

// invalid URLs
{"empty string", "", true},
{"no host", "https://", true},
{"bare path", "/just/a/path", true},
{"unsupported ftp scheme", "ftp://example.com/webhook", true},
{"unsupported file scheme", "file://example.com/webhook", true},
{"invalid domain double dot", "https://invalid..domain", true},
{"domain starting with hyphen", "https://-invalid.com", true},
{"single-label host starting with hyphen", "https://-harbor", true},
{"single-label host ending with hyphen", "https://harbor-", true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand All @@ -165,6 +172,9 @@ func TestValidateURL(t *testing.T) {
}
})
}

err := utils.ValidateURL("ftp://example.com/webhook")
assert.EqualError(t, err, "URL scheme must be http or https")
}

func TestPrintFormat(t *testing.T) {
Expand Down