Skip to content

Commit 0059692

Browse files
authored
feat: allow localhost as a valid host in URL validation (#785)
1 parent f938cb4 commit 0059692

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

pkg/utils/helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ func ValidateURL(rawURL string) error {
196196
return nil
197197
}
198198

199+
if host == "localhost" {
200+
return nil
201+
}
202+
199203
if !domainNameRegex.MatchString(host) {
200204
return fmt.Errorf("invalid host: must be a valid IP address or domain name")
201205
}

pkg/utils/helper_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ func TestValidateURL(t *testing.T) {
143143
{"valid IPv4", "http://192.168.1.1", false},
144144
{"valid IPv4 with port", "http://192.168.1.1:8080", false},
145145
{"valid IPv6", "http://[::1]", false},
146+
{"valid localhost http", "http://localhost", false},
147+
{"valid localhost https", "https://localhost", false},
148+
{"valid localhost http with port", "http://localhost:8080", false},
149+
{"valid localhost https with port", "https://localhost:8443", false},
146150

147151
// invalid URLs
148152
{"empty string", "", true},

0 commit comments

Comments
 (0)