Skip to content

Commit

Permalink
fix ipv6 parsing in get bucket location (#1635)
Browse files Browse the repository at this point in the history
Co-authored-by: Anis Elleuch <anis@min.io>
  • Loading branch information
vadmeste and Anis Elleuch committed Apr 12, 2022
1 parent 70804b7 commit dc2a6a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
3 changes: 3 additions & 0 deletions bucket-cache.go
Expand Up @@ -181,6 +181,9 @@ func (c *Client) getBucketLocationRequest(ctx context.Context, bucketName string
if h, p, err := net.SplitHostPort(targetURL.Host); err == nil {
if targetURL.Scheme == "http" && p == "80" || targetURL.Scheme == "https" && p == "443" {
targetURL.Host = h
if ip := net.ParseIP(h); ip != nil && ip.To16() != nil {
targetURL.Host = "[" + h + "]"
}
}
}

Expand Down
30 changes: 15 additions & 15 deletions utils.go
Expand Up @@ -105,21 +105,6 @@ func sumMD5Base64(data []byte) string {

// getEndpointURL - construct a new endpoint.
func getEndpointURL(endpoint string, secure bool) (*url.URL, error) {
if strings.Contains(endpoint, ":") {
host, _, err := net.SplitHostPort(endpoint)
if err != nil {
return nil, err
}
if !s3utils.IsValidIP(host) && !s3utils.IsValidDomain(host) {
msg := "Endpoint: " + endpoint + " does not follow ip address or domain name standards."
return nil, errInvalidArgument(msg)
}
} else {
if !s3utils.IsValidIP(endpoint) && !s3utils.IsValidDomain(endpoint) {
msg := "Endpoint: " + endpoint + " does not follow ip address or domain name standards."
return nil, errInvalidArgument(msg)
}
}
// If secure is false, use 'http' scheme.
scheme := "https"
if !secure {
Expand Down Expand Up @@ -176,6 +161,21 @@ 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
}
}
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 !s3utils.IsAmazonEndpoint(endpointURL) {
return errInvalidArgument("Amazon S3 endpoint should be 's3.amazonaws.com'.")
Expand Down
4 changes: 3 additions & 1 deletion utils_test.go
Expand Up @@ -153,7 +153,6 @@ func TestIsValidEndpointURL(t *testing.T) {
shouldPass bool
}{
{"", errInvalidArgument("Endpoint url cannot be empty."), false},
{"/", nil, true},
{"https://s3.amazonaws.com", nil, true},
{"https://s3.cn-north-1.amazonaws.com.cn", nil, true},
{"https://s3-us-gov-west-1.amazonaws.com", nil, true},
Expand All @@ -167,6 +166,9 @@ func TestIsValidEndpointURL(t *testing.T) {
{"https://amazon.googleapis.com/", errInvalidArgument("Google Cloud Storage endpoint should be 'storage.googleapis.com'."), false},
{"https://storage.googleapis.com/bucket/", errInvalidArgument("Endpoint url cannot have fully qualified paths."), false},
{"https://s3.amazonaws.com/bucket/object", errInvalidArgument("Endpoint url cannot have fully qualified paths."), false},
{"https://.s3server.example.com/", errInvalidArgument("Endpoint: .s3server.example.com does not follow ip address or domain name standards."), false},
{"https://s3server.example_/", errInvalidArgument("Endpoint: s3server.example_ does not follow ip address or domain name standards."), false},
{"https://_s3server.example.com/", errInvalidArgument("Endpoint: _s3server.example.com does not follow ip address or domain name standards."), false},
}

for i, testCase := range testCases {
Expand Down

0 comments on commit dc2a6a8

Please sign in to comment.