match S3 within URL in full appropriately (#3163)

Fixes #3140
This commit is contained in:
BigUstad
2020-04-16 18:25:40 -07:00
committed by GitHub
parent 5c4add2000
commit ab2500e2ef
2 changed files with 21 additions and 0 deletions
+3
View File
@@ -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")
}
+18
View File
@@ -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
}