From 958661cbb53af9b45844e20891e4308b267ca84e Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 9 Sep 2020 09:57:37 -0700 Subject: [PATCH] skip subdomain from bucket DNS which start with `minio.domain` (#10390) extend host matcher to reject the host match --- cmd/api-router.go | 40 +++++++++++++++++++++++++++++++++++++--- cmd/handler-utils.go | 3 +++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/cmd/api-router.go b/cmd/api-router.go index 409532598..00abcf4d0 100644 --- a/cmd/api-router.go +++ b/cmd/api-router.go @@ -17,6 +17,7 @@ package cmd import ( + "net" "net/http" "github.com/gorilla/mux" @@ -66,6 +67,16 @@ type objectAPIHandlers struct { AllowSSEKMS func() bool } +// getHost tries its best to return the request host. +// According to section 14.23 of RFC 2616 the Host header +// can include the port number if the default value of 80 is not used. +func getHost(r *http.Request) string { + if r.URL.IsAbs() { + return r.URL.Host + } + return r.Host +} + // registerAPIRouter - registers S3 compatible APIs. func registerAPIRouter(router *mux.Router, encryptionEnabled, allowSSEKMS bool) { // Initialize API. @@ -82,9 +93,28 @@ func registerAPIRouter(router *mux.Router, encryptionEnabled, allowSSEKMS bool) // API Router apiRouter := router.PathPrefix(SlashSeparator).Subrouter() + var routers []*mux.Router for _, domainName := range globalDomainNames { - routers = append(routers, apiRouter.Host("{bucket:.+}."+domainName).Subrouter()) + if IsKubernetes() { + routers = append(routers, apiRouter.MatcherFunc(func(r *http.Request, match *mux.RouteMatch) bool { + host, _, _ := net.SplitHostPort(getHost(r)) + // Make sure to skip matching minio.` this is + // specifically meant for operator/k8s deployment + // The reason we need to skip this is for a special + // usecase where we need to make sure that + // minio..svc. is ignored + // by the bucketDNS style to ensure that path style + // is available and honored at this domain. + // + // All other `..svc.` + // makes sure that buckets are routed through this matcher + // to match for `` + return host != minioReservedBucket+"."+domainName + }).Host("{bucket:.+}."+domainName).Subrouter()) + } else { + routers = append(routers, apiRouter.Host("{bucket:.+}."+domainName).Subrouter()) + } } routers = append(routers, apiRouter.PathPrefix("/{bucket}").Subrouter()) @@ -94,7 +124,10 @@ func registerAPIRouter(router *mux.Router, encryptionEnabled, allowSSEKMS bool) bucket.Methods(http.MethodHead).Path("/{object:.+}").HandlerFunc( maxClients(collectAPIStats("headobject", httpTraceAll(api.HeadObjectHandler)))) // CopyObjectPart - bucket.Methods(http.MethodPut).Path("/{object:.+}").HeadersRegexp(xhttp.AmzCopySource, ".*?(\\/|%2F).*?").HandlerFunc(maxClients(collectAPIStats("copyobjectpart", httpTraceAll(api.CopyObjectPartHandler)))).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}") + bucket.Methods(http.MethodPut).Path("/{object:.+}"). + HeadersRegexp(xhttp.AmzCopySource, ".*?(\\/|%2F).*?"). + HandlerFunc(maxClients(collectAPIStats("copyobjectpart", httpTraceAll(api.CopyObjectPartHandler)))). + Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}") // PutObjectPart bucket.Methods(http.MethodPut).Path("/{object:.+}").HandlerFunc( maxClients(collectAPIStats("putobjectpart", httpTraceHdrs(api.PutObjectPartHandler)))).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}") @@ -138,7 +171,8 @@ func registerAPIRouter(router *mux.Router, encryptionEnabled, allowSSEKMS bool) bucket.Methods(http.MethodGet).Path("/{object:.+}").HandlerFunc( maxClients(collectAPIStats("getobject", httpTraceHdrs(api.GetObjectHandler)))) // CopyObject - bucket.Methods(http.MethodPut).Path("/{object:.+}").HeadersRegexp(xhttp.AmzCopySource, ".*?(\\/|%2F).*?").HandlerFunc(maxClients(collectAPIStats("copyobject", httpTraceAll(api.CopyObjectHandler)))) + bucket.Methods(http.MethodPut).Path("/{object:.+}").HeadersRegexp(xhttp.AmzCopySource, ".*?(\\/|%2F).*?"). + HandlerFunc(maxClients(collectAPIStats("copyobject", httpTraceAll(api.CopyObjectHandler)))) // PutObjectRetention bucket.Methods(http.MethodPut).Path("/{object:.+}").HandlerFunc( maxClients(collectAPIStats("putobjectretention", httpTraceAll(api.PutObjectRetentionHandler)))).Queries("retention", "") diff --git a/cmd/handler-utils.go b/cmd/handler-utils.go index d27f48b71..b4e85f312 100644 --- a/cmd/handler-utils.go +++ b/cmd/handler-utils.go @@ -404,6 +404,9 @@ func getResource(path string, host string, domains []string) (string, error) { } } for _, domain := range domains { + if host == minioReservedBucket+"."+domain { + continue + } if !strings.HasSuffix(host, "."+domain) { continue }