From ab2500e2efc5f63a6873beda4d0d5968de3fc979 Mon Sep 17 00:00:00 2001 From: BigUstad Date: Thu, 16 Apr 2020 18:25:40 -0700 Subject: [PATCH] match S3 within URL in full appropriately (#3163) Fixes #3140 --- cmd/client-s3.go | 3 +++ cmd/utils.go | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/cmd/client-s3.go b/cmd/client-s3.go index ed945b51..6e6ac395 100644 --- a/cmd/client-s3.go +++ b/cmd/client-s3.go @@ -1480,6 +1480,9 @@ func (c *S3Client) url2BucketAndObject() (bucketName, objectName string) { if c.virtualStyle { var bucket string hostIndex := strings.Index(c.targetURL.Host, "s3") + if hostIndex != -1 && !matchS3InHost(c.targetURL.Host) { + hostIndex = -1 + } if hostIndex == -1 { hostIndex = strings.Index(c.targetURL.Host, "s3-accelerate") } diff --git a/cmd/utils.go b/cmd/utils.go index 313d274c..6025eb6e 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -21,6 +21,7 @@ import ( "errors" "io" "math/rand" + "net" "os" "path/filepath" "runtime" @@ -349,3 +350,20 @@ func parseAttribute(attrs string) (map[string]string, error) { return attribute, nil } + +// Returns true if "s3" is entirely in sub-domain and false otherwise. +// true for s3.amazonaws.com, false for ams3.digitaloceanspaces.com, 192.168.1.12 +func matchS3InHost(urlHost string) bool { + if strings.Contains(urlHost, ":") { + if host, _, err := net.SplitHostPort(urlHost); err == nil { + urlHost = host + } + } + fqdnParts := strings.Split(urlHost, ".") + for _, fqdn := range fqdnParts { + if fqdn == "s3" { + return true + } + } + return false +}