Skip to content

Commit

Permalink
More fixes in ipv6 parsing in get bucket location
Browse files Browse the repository at this point in the history
Also fix the endpoint validation. The code was assuming that any address
has ':' in it means it has a port but this is not true.
  • Loading branch information
Anis Elleuch committed Apr 11, 2022
1 parent a585980 commit 1fb6aab
Show file tree
Hide file tree
Showing 3 changed files with 19 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
2 changes: 1 addition & 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,7 @@ 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},
}

for i, testCase := range testCases {
Expand Down

0 comments on commit 1fb6aab

Please sign in to comment.