Skip to content

Commit

Permalink
Fix IPv6 url without 80/443 port (#1636)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vadmeste committed Apr 13, 2022
1 parent dc2a6a8 commit 37bb066
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
15 changes: 3 additions & 12 deletions utils.go
Expand Up @@ -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'.")
}
Expand Down
6 changes: 6 additions & 0 deletions utils_test.go
Expand Up @@ -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},
Expand Down

0 comments on commit 37bb066

Please sign in to comment.