From f2567f598009e66464169b87efbc12858182fbdd Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Wed, 13 Apr 2022 11:28:28 +0100 Subject: [PATCH] Fix IPv6 url without 80/443 port This PR fixes another use case when no port is passed to a IPv6 with brackets hostname. Use url.URL.Hostname() since it strips the port and the brackets from the hostname as well. Add more unit tests to getEndpointURL() as well. --- utils.go | 15 +++------------ utils_test.go | 6 ++++++ 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/utils.go b/utils.go index efe974d0b..72c4cd5ce 100644 --- a/utils.go +++ b/utils.go @@ -161,27 +161,18 @@ func isValidEndpointURL(endpointURL url.URL) error { if endpointURL.Path != "/" && endpointURL.Path != "" { return errInvalidArgument("Endpoint url cannot have fully qualified paths.") } - - host, _, err := net.SplitHostPort(endpointURL.Host) - if err != nil { - if strings.Contains(err.Error(), "missing port in address") { - err = nil - host = endpointURL.Host - } else { - return err - } - } + host := endpointURL.Hostname() if !s3utils.IsValidIP(host) && !s3utils.IsValidDomain(host) { msg := "Endpoint: " + endpointURL.Host + " does not follow ip address or domain name standards." return errInvalidArgument(msg) } - if strings.Contains(endpointURL.Host, ".s3.amazonaws.com") { + if strings.Contains(host, ".s3.amazonaws.com") { if !s3utils.IsAmazonEndpoint(endpointURL) { return errInvalidArgument("Amazon S3 endpoint should be 's3.amazonaws.com'.") } } - if strings.Contains(endpointURL.Host, ".googleapis.com") { + if strings.Contains(host, ".googleapis.com") { if !s3utils.IsGoogleEndpoint(endpointURL) { return errInvalidArgument("Google Cloud Storage endpoint should be 'storage.googleapis.com'.") } diff --git a/utils_test.go b/utils_test.go index 39285e84d..ea786cf11 100644 --- a/utils_test.go +++ b/utils_test.go @@ -113,6 +113,12 @@ func TestGetEndpointURL(t *testing.T) { {"192.168.1.1:9000", false, "http://192.168.1.1:9000", nil, true}, {"192.168.1.1:9000", true, "https://192.168.1.1:9000", nil, true}, {"s3.amazonaws.com:443", true, "https://s3.amazonaws.com:443", nil, true}, + {"[::1]", false, "http://[::1]", nil, true}, + {"[::1]", true, "https://[::1]", nil, true}, + {"[::1]:80", false, "http://[::1]:80", nil, true}, + {"[::1]:443", true, "https://[::1]:443", nil, true}, + {"[::1]:9000", false, "http://[::1]:9000", nil, true}, + {"[::1]:9000", true, "https://[::1]:9000", nil, true}, {"13333.123123.-", true, "", errInvalidArgument(fmt.Sprintf("Endpoint: %s does not follow ip address or domain name standards.", "13333.123123.-")), false}, {"13333.123123.-", true, "", errInvalidArgument(fmt.Sprintf("Endpoint: %s does not follow ip address or domain name standards.", "13333.123123.-")), false}, {"storage.googleapis.com:4000", true, "", errInvalidArgument("Google Cloud Storage endpoint should be 'storage.googleapis.com'."), false},