2015-02-15 03:48:15 -05:00
|
|
|
/*
|
2020-03-22 01:10:13 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2015-2020 MinIO, Inc.
|
2015-02-15 03:48:15 -05:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2015-02-11 06:23:15 -05:00
|
|
|
|
|
|
|
import (
|
2020-10-30 15:20:28 -04:00
|
|
|
"context"
|
2015-02-11 06:23:15 -05:00
|
|
|
"net/http"
|
2015-10-07 23:36:36 -04:00
|
|
|
"strings"
|
2015-04-27 06:54:49 -04:00
|
|
|
"time"
|
2015-02-11 06:23:15 -05:00
|
|
|
|
2020-07-14 12:38:05 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/set"
|
2018-05-11 15:02:30 -04:00
|
|
|
|
2016-11-22 21:18:22 -05:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2020-09-10 17:19:32 -04:00
|
|
|
"github.com/minio/minio/cmd/config/dns"
|
2018-10-16 22:22:09 -04:00
|
|
|
"github.com/minio/minio/cmd/crypto"
|
2019-07-03 01:34:32 -04:00
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
2020-04-30 01:17:36 -04:00
|
|
|
"github.com/minio/minio/cmd/http/stats"
|
2018-04-19 20:24:43 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-02-02 21:18:52 -05:00
|
|
|
"github.com/minio/minio/pkg/handlers"
|
2015-02-11 06:23:15 -05:00
|
|
|
)
|
|
|
|
|
2016-07-28 15:02:22 -04:00
|
|
|
// Adds limiting body size middleware
|
|
|
|
|
2016-11-22 22:58:51 -05:00
|
|
|
// Maximum allowed form data field values. 64MiB is a guessed practical value
|
|
|
|
// which is more than enough to accommodate any form data fields and headers.
|
|
|
|
const requestFormDataSize = 64 * humanize.MiByte
|
|
|
|
|
2017-03-03 13:14:17 -05:00
|
|
|
// For any HTTP request, request body should be not more than 16GiB + requestFormDataSize
|
|
|
|
// where, 16GiB is the maximum allowed object size for object upload.
|
|
|
|
const requestMaxBodySize = globalMaxObjectSize + requestFormDataSize
|
2016-07-28 15:02:22 -04:00
|
|
|
|
|
|
|
func setRequestSizeLimitHandler(h http.Handler) http.Handler {
|
2021-01-04 12:54:22 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Restricting read data to a given maximum length
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, requestMaxBodySize)
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2016-07-28 15:02:22 -04:00
|
|
|
}
|
|
|
|
|
2017-08-22 19:53:35 -04:00
|
|
|
const (
|
|
|
|
// Maximum size for http headers - See: https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
|
|
|
|
maxHeaderSize = 8 * 1024
|
|
|
|
// Maximum size for user-defined metadata - See: https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
|
|
|
|
maxUserDataSize = 2 * 1024
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServeHTTP restricts the size of the http header to 8 KB and the size
|
|
|
|
// of the user-defined metadata to 2 KB.
|
2021-01-04 12:54:22 -05:00
|
|
|
func setRequestHeaderSizeLimitHandler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if isHTTPHeaderSizeTooLarge(r.Header) {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrMetadataTooLarge), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2017-08-22 19:53:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// isHTTPHeaderSizeTooLarge returns true if the provided
|
|
|
|
// header is larger than 8 KB or the user-defined metadata
|
|
|
|
// is larger than 2 KB.
|
|
|
|
func isHTTPHeaderSizeTooLarge(header http.Header) bool {
|
|
|
|
var size, usersize int
|
|
|
|
for key := range header {
|
|
|
|
length := len(key) + len(header.Get(key))
|
|
|
|
size += length
|
|
|
|
for _, prefix := range userMetadataKeyPrefixes {
|
2020-06-12 23:04:01 -04:00
|
|
|
if strings.HasPrefix(strings.ToLower(key), prefix) {
|
2017-08-22 19:53:35 -04:00
|
|
|
usersize += length
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if usersize > maxUserDataSize || size > maxHeaderSize {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:18:59 -05:00
|
|
|
// ReservedMetadataPrefix is the prefix of a metadata key which
|
|
|
|
// is reserved and for internal use only.
|
2020-05-28 17:36:38 -04:00
|
|
|
const (
|
|
|
|
ReservedMetadataPrefix = "X-Minio-Internal-"
|
|
|
|
ReservedMetadataPrefixLower = "x-minio-internal-"
|
|
|
|
)
|
2017-11-07 18:18:59 -05:00
|
|
|
|
|
|
|
// ServeHTTP fails if the request contains at least one reserved header which
|
|
|
|
// would be treated as metadata.
|
2021-01-04 12:54:22 -05:00
|
|
|
func filterReservedMetadata(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if containsReservedMetadata(r.Header) {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrUnsupportedMetadata), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2017-11-07 18:18:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// containsReservedMetadata returns true if the http.Header contains
|
|
|
|
// keys which are treated as metadata but are reserved for internal use
|
|
|
|
// and must not set by clients
|
|
|
|
func containsReservedMetadata(header http.Header) bool {
|
|
|
|
for key := range header {
|
2020-05-28 17:36:38 -04:00
|
|
|
if strings.HasPrefix(strings.ToLower(key), ReservedMetadataPrefixLower) {
|
2017-11-07 18:18:59 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-03-27 15:37:21 -04:00
|
|
|
// Reserved bucket.
|
2016-03-12 19:08:15 -05:00
|
|
|
const (
|
2017-02-16 17:52:14 -05:00
|
|
|
minioReservedBucket = "minio"
|
2019-08-06 15:08:58 -04:00
|
|
|
minioReservedBucketPath = SlashSeparator + minioReservedBucket
|
2019-11-21 20:45:15 -05:00
|
|
|
loginPathPrefix = SlashSeparator + "login"
|
2016-03-12 19:08:15 -05:00
|
|
|
)
|
|
|
|
|
2020-10-30 15:20:28 -04:00
|
|
|
func setRedirectHandler(h http.Handler) http.Handler {
|
2021-01-04 12:54:22 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if !shouldProxy() || guessIsRPCReq(r) || guessIsBrowserReq(r) ||
|
|
|
|
guessIsHealthCheckReq(r) || guessIsMetricsReq(r) || isAdminReq(r) {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if this server is still initializing, proxy the request
|
|
|
|
// to any other online servers to avoid 503 for any incoming
|
|
|
|
// API calls.
|
|
|
|
if idx := getOnlineProxyEndpointIdx(); idx >= 0 {
|
|
|
|
proxyRequest(context.TODO(), w, r, globalProxyEndpoints[idx])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2020-10-30 15:20:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func setBrowserRedirectHandler(h http.Handler) http.Handler {
|
2021-01-04 12:54:22 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Re-direction is handled specifically for browser requests.
|
|
|
|
if globalBrowserEnabled && guessIsBrowserReq(r) {
|
|
|
|
// Fetch the redirect location if any.
|
|
|
|
redirectLocation := getRedirectLocation(r.URL.Path)
|
|
|
|
if redirectLocation != "" {
|
|
|
|
// Employ a temporary re-direct.
|
|
|
|
http.Redirect(w, r, redirectLocation, http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2020-10-30 15:20:28 -04:00
|
|
|
}
|
|
|
|
|
2020-11-08 00:03:06 -05:00
|
|
|
func shouldProxy() bool {
|
|
|
|
if newObjectLayerFn() == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return !globalIAMSys.Initialized()
|
|
|
|
}
|
|
|
|
|
2016-12-10 03:42:22 -05:00
|
|
|
// Fetch redirect location if urlPath satisfies certain
|
|
|
|
// criteria. Some special names are considered to be
|
|
|
|
// redirectable, this is purely internal function and
|
|
|
|
// serves only limited purpose on redirect-handler for
|
|
|
|
// browser requests.
|
|
|
|
func getRedirectLocation(urlPath string) (rLocation string) {
|
2017-02-16 17:52:14 -05:00
|
|
|
if urlPath == minioReservedBucketPath {
|
2019-08-06 15:08:58 -04:00
|
|
|
rLocation = minioReservedBucketPath + SlashSeparator
|
2016-12-10 03:42:22 -05:00
|
|
|
}
|
|
|
|
if contains([]string{
|
2019-08-06 15:08:58 -04:00
|
|
|
SlashSeparator,
|
2016-12-10 03:42:22 -05:00
|
|
|
"/webrpc",
|
|
|
|
"/login",
|
2019-08-12 01:17:02 -04:00
|
|
|
"/favicon-16x16.png",
|
|
|
|
"/favicon-32x32.png",
|
|
|
|
"/favicon-96x96.png",
|
2016-12-10 03:42:22 -05:00
|
|
|
}, urlPath) {
|
2017-02-16 17:52:14 -05:00
|
|
|
rLocation = minioReservedBucketPath + urlPath
|
2016-12-10 03:42:22 -05:00
|
|
|
}
|
|
|
|
return rLocation
|
|
|
|
}
|
|
|
|
|
|
|
|
// guessIsBrowserReq - returns true if the request is browser.
|
|
|
|
// This implementation just validates user-agent and
|
|
|
|
// looks for "Mozilla" string. This is no way certifiable
|
|
|
|
// way to know if the request really came from a browser
|
|
|
|
// since User-Agent's can be arbitrary. But this is just
|
|
|
|
// a best effort function.
|
|
|
|
func guessIsBrowserReq(req *http.Request) bool {
|
|
|
|
if req == nil {
|
|
|
|
return false
|
|
|
|
}
|
2019-04-04 02:10:37 -04:00
|
|
|
aType := getRequestAuthType(req)
|
2019-10-23 01:59:13 -04:00
|
|
|
return strings.Contains(req.Header.Get("User-Agent"), "Mozilla") && globalBrowserEnabled &&
|
2019-04-04 02:10:37 -04:00
|
|
|
(aType == authTypeJWT || aType == authTypeAnonymous)
|
2016-02-16 21:50:36 -05:00
|
|
|
}
|
|
|
|
|
2018-03-12 02:16:53 -04:00
|
|
|
// guessIsHealthCheckReq - returns true if incoming request looks
|
|
|
|
// like healthcheck request
|
|
|
|
func guessIsHealthCheckReq(req *http.Request) bool {
|
|
|
|
if req == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
aType := getRequestAuthType(req)
|
2018-03-15 00:25:02 -04:00
|
|
|
return aType == authTypeAnonymous && (req.Method == http.MethodGet || req.Method == http.MethodHead) &&
|
2018-03-12 02:16:53 -04:00
|
|
|
(req.URL.Path == healthCheckPathPrefix+healthCheckLivenessPath ||
|
2020-07-02 19:17:27 -04:00
|
|
|
req.URL.Path == healthCheckPathPrefix+healthCheckReadinessPath ||
|
|
|
|
req.URL.Path == healthCheckPathPrefix+healthCheckClusterPath)
|
2018-03-12 02:16:53 -04:00
|
|
|
}
|
|
|
|
|
2018-04-18 19:01:42 -04:00
|
|
|
// guessIsMetricsReq - returns true if incoming request looks
|
|
|
|
// like metrics request
|
|
|
|
func guessIsMetricsReq(req *http.Request) bool {
|
|
|
|
if req == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
aType := getRequestAuthType(req)
|
2019-09-22 10:57:12 -04:00
|
|
|
return (aType == authTypeAnonymous || aType == authTypeJWT) &&
|
2018-04-18 19:01:42 -04:00
|
|
|
req.URL.Path == minioReservedBucketPath+prometheusMetricsPath
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:16:54 -04:00
|
|
|
// guessIsRPCReq - returns true if the request is for an RPC endpoint.
|
|
|
|
func guessIsRPCReq(req *http.Request) bool {
|
|
|
|
if req == nil {
|
|
|
|
return false
|
|
|
|
}
|
2018-12-05 17:28:48 -05:00
|
|
|
return req.Method == http.MethodPost &&
|
2019-08-06 15:08:58 -04:00
|
|
|
strings.HasPrefix(req.URL.Path, minioReservedBucketPath+SlashSeparator)
|
2017-09-01 15:16:54 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 16:57:56 -05:00
|
|
|
// Adds Cache-Control header
|
2016-02-16 21:50:36 -05:00
|
|
|
func setBrowserCacheControlHandler(h http.Handler) http.Handler {
|
2021-01-04 12:54:22 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if globalBrowserEnabled && r.Method == http.MethodGet && guessIsBrowserReq(r) {
|
|
|
|
// For all browser requests set appropriate Cache-Control policies
|
|
|
|
if HasPrefix(r.URL.Path, minioReservedBucketPath+SlashSeparator) {
|
|
|
|
if HasSuffix(r.URL.Path, ".js") || r.URL.Path == minioReservedBucketPath+"/favicon.ico" {
|
|
|
|
// For assets set cache expiry of one year. For each release, the name
|
|
|
|
// of the asset name will change and hence it can not be served from cache.
|
|
|
|
w.Header().Set(xhttp.CacheControl, "max-age=31536000")
|
|
|
|
} else {
|
|
|
|
// For non asset requests we serve index.html which will never be cached.
|
|
|
|
w.Header().Set(xhttp.CacheControl, "no-store")
|
|
|
|
}
|
2016-12-17 14:00:16 -05:00
|
|
|
}
|
2016-02-26 08:19:35 -05:00
|
|
|
}
|
2016-12-17 14:00:16 -05:00
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2016-02-16 21:50:36 -05:00
|
|
|
}
|
|
|
|
|
2018-01-22 17:54:55 -05:00
|
|
|
// Check to allow access to the reserved "bucket" `/minio` for Admin
|
|
|
|
// API requests.
|
|
|
|
func isAdminReq(r *http.Request) bool {
|
2019-11-04 12:30:59 -05:00
|
|
|
return strings.HasPrefix(r.URL.Path, adminPathPrefix)
|
2018-01-22 17:54:55 -05:00
|
|
|
}
|
|
|
|
|
2019-11-21 20:45:15 -05:00
|
|
|
// guessIsLoginSTSReq - returns true if incoming request is Login STS user
|
|
|
|
func guessIsLoginSTSReq(req *http.Request) bool {
|
|
|
|
if req == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return strings.HasPrefix(req.URL.Path, loginPathPrefix) ||
|
|
|
|
(req.Method == http.MethodPost && req.URL.Path == SlashSeparator &&
|
|
|
|
getRequestAuthType(req) == authTypeSTS)
|
|
|
|
}
|
|
|
|
|
2016-02-16 21:50:36 -05:00
|
|
|
// Adds verification for incoming paths.
|
2017-09-01 15:16:54 -04:00
|
|
|
func setReservedBucketHandler(h http.Handler) http.Handler {
|
2021-01-04 12:54:22 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// For all other requests reject access to reserved buckets
|
|
|
|
bucketName, _ := request2BucketObjectName(r)
|
|
|
|
if isMinioReservedBucket(bucketName) || isMinioMetaBucket(bucketName) {
|
|
|
|
if !guessIsRPCReq(r) && !guessIsBrowserReq(r) && !guessIsHealthCheckReq(r) && !guessIsMetricsReq(r) && !isAdminReq(r) {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrAllAccessDisabled), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
2017-09-01 15:16:54 -04:00
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2016-12-10 03:42:22 -05:00
|
|
|
}
|
|
|
|
|
2016-03-30 23:04:51 -04:00
|
|
|
// Supported Amz date formats.
|
|
|
|
var amzDateFormats = []string{
|
2016-03-07 12:52:20 -05:00
|
|
|
time.RFC1123,
|
|
|
|
time.RFC1123Z,
|
|
|
|
iso8601Format,
|
2016-03-30 23:04:51 -04:00
|
|
|
// Add new AMZ date formats here.
|
2016-03-07 12:52:20 -05:00
|
|
|
}
|
|
|
|
|
2016-12-10 03:42:22 -05:00
|
|
|
// Supported Amz date headers.
|
|
|
|
var amzDateHeaders = []string{
|
|
|
|
"x-amz-date",
|
|
|
|
"date",
|
|
|
|
}
|
|
|
|
|
2016-03-30 23:04:51 -04:00
|
|
|
// parseAmzDate - parses date string into supported amz date formats.
|
|
|
|
func parseAmzDate(amzDateStr string) (amzDate time.Time, apiErr APIErrorCode) {
|
|
|
|
for _, dateFormat := range amzDateFormats {
|
2016-12-10 03:42:22 -05:00
|
|
|
amzDate, err := time.Parse(dateFormat, amzDateStr)
|
|
|
|
if err == nil {
|
2016-03-30 23:04:51 -04:00
|
|
|
return amzDate, ErrNone
|
2016-03-07 12:52:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-30 23:04:51 -04:00
|
|
|
return time.Time{}, ErrMalformedDate
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAmzDateHeader - parses supported amz date headers, in
|
|
|
|
// supported amz date formats.
|
|
|
|
func parseAmzDateHeader(req *http.Request) (time.Time, APIErrorCode) {
|
|
|
|
for _, amzDateHeader := range amzDateHeaders {
|
2020-03-18 19:19:29 -04:00
|
|
|
amzDateStr := req.Header.Get(amzDateHeader)
|
2016-03-30 23:04:51 -04:00
|
|
|
if amzDateStr != "" {
|
|
|
|
return parseAmzDate(amzDateStr)
|
2016-03-07 12:52:20 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-30 23:04:51 -04:00
|
|
|
// Date header missing.
|
|
|
|
return time.Time{}, ErrMissingDateHeader
|
2016-03-07 12:52:20 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
// setTimeValidityHandler to validate parsable time over http header
|
|
|
|
func setTimeValidityHandler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
aType := getRequestAuthType(r)
|
|
|
|
if aType == authTypeSigned || aType == authTypeSignedV2 || aType == authTypeStreamingSigned {
|
|
|
|
// Verify if date headers are set, if not reject the request
|
|
|
|
amzDate, errCode := parseAmzDateHeader(r)
|
|
|
|
if errCode != ErrNone {
|
|
|
|
// All our internal APIs are sensitive towards Date
|
|
|
|
// header, for all requests where Date header is not
|
|
|
|
// present we will reject such clients.
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(errCode), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Verify if the request date header is shifted by less than globalMaxSkewTime parameter in the past
|
|
|
|
// or in the future, reject request otherwise.
|
|
|
|
curTime := UTCNow()
|
|
|
|
if curTime.Sub(amzDate) > globalMaxSkewTime || amzDate.Sub(curTime) > globalMaxSkewTime {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrRequestTimeTooSkewed), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
2015-04-29 21:30:17 -04:00
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2015-02-18 15:15:33 -05:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
var supportedDummyBucketAPIs = map[string][]string{
|
|
|
|
"acl": {http.MethodPut, http.MethodGet},
|
|
|
|
"cors": {http.MethodGet},
|
|
|
|
"website": {http.MethodGet, http.MethodDelete},
|
|
|
|
"logging": {http.MethodGet},
|
|
|
|
"accelerate": {http.MethodGet},
|
|
|
|
"requestPayment": {http.MethodGet},
|
|
|
|
}
|
|
|
|
|
|
|
|
// List of not implemented bucket queries
|
|
|
|
var notImplementedBucketResourceNames = map[string]struct{}{
|
|
|
|
"cors": {},
|
|
|
|
"metrics": {},
|
|
|
|
"website": {},
|
|
|
|
"logging": {},
|
|
|
|
"inventory": {},
|
|
|
|
"accelerate": {},
|
|
|
|
"requestPayment": {},
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:28:04 -04:00
|
|
|
// Checks requests for not implemented Bucket resources
|
|
|
|
func ignoreNotImplementedBucketResources(req *http.Request) bool {
|
2016-02-19 19:04:29 -05:00
|
|
|
for name := range req.URL.Query() {
|
2020-06-12 23:04:01 -04:00
|
|
|
methods, ok := supportedDummyBucketAPIs[name]
|
|
|
|
if ok {
|
|
|
|
for _, method := range methods {
|
|
|
|
if method == req.Method {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 00:02:26 -04:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
if _, ok := notImplementedBucketResourceNames[name]; ok {
|
2015-02-11 06:23:15 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
var supportedDummyObjectAPIs = map[string][]string{
|
|
|
|
"acl": {http.MethodGet, http.MethodPut},
|
|
|
|
}
|
|
|
|
|
|
|
|
// List of not implemented object APIs
|
|
|
|
var notImplementedObjectResourceNames = map[string]struct{}{
|
|
|
|
"torrent": {},
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:28:04 -04:00
|
|
|
// Checks requests for not implemented Object resources
|
|
|
|
func ignoreNotImplementedObjectResources(req *http.Request) bool {
|
2016-02-19 19:04:29 -05:00
|
|
|
for name := range req.URL.Query() {
|
2020-06-12 23:04:01 -04:00
|
|
|
methods, ok := supportedDummyObjectAPIs[name]
|
|
|
|
if ok {
|
|
|
|
for _, method := range methods {
|
|
|
|
if method == req.Method {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2018-05-10 00:02:26 -04:00
|
|
|
}
|
2020-06-12 23:04:01 -04:00
|
|
|
if _, ok := notImplementedObjectResourceNames[name]; ok {
|
2015-02-11 06:23:15 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-02-19 19:04:29 -05:00
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
// setIgnoreResourcesHandler -
|
|
|
|
// Ignore resources handler is wrapper handler used for API request resource validation
|
|
|
|
// Since we do not support all the S3 queries, it is necessary for us to throw back a
|
|
|
|
// valid error message indicating that requested feature is not implemented.
|
|
|
|
func setIgnoreResourcesHandler(h http.Handler) http.Handler {
|
|
|
|
// Resource handler ServeHTTP() wrapper
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
bucketName, objectName := request2BucketObjectName(r)
|
|
|
|
|
|
|
|
// If bucketName is present and not objectName check for bucket level resource queries.
|
|
|
|
if bucketName != "" && objectName == "" {
|
|
|
|
if ignoreNotImplementedBucketResources(r) {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
2016-12-10 03:42:22 -05:00
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
// If bucketName and objectName are present check for its resource queries.
|
|
|
|
if bucketName != "" && objectName != "" {
|
|
|
|
if ignoreNotImplementedObjectResources(r) {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
2016-12-10 03:42:22 -05:00
|
|
|
}
|
2017-02-06 12:29:53 -05:00
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
// Serve HTTP.
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2017-02-06 12:29:53 -05:00
|
|
|
}
|
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
// setHttpStatsHandler sets a http Stats handler to gather HTTP statistics
|
2017-02-06 12:29:53 -05:00
|
|
|
func setHTTPStatsHandler(h http.Handler) http.Handler {
|
2021-01-04 12:54:22 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Meters s3 connection stats.
|
|
|
|
meteredRequest := &stats.IncomingTrafficMeter{ReadCloser: r.Body}
|
|
|
|
meteredResponse := &stats.OutgoingTrafficMeter{ResponseWriter: w}
|
|
|
|
|
|
|
|
// Execute the request
|
|
|
|
r.Body = meteredRequest
|
|
|
|
h.ServeHTTP(meteredResponse, r)
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, minioReservedBucketPath) {
|
|
|
|
globalConnStats.incInputBytes(meteredRequest.BytesCount())
|
|
|
|
globalConnStats.incOutputBytes(meteredResponse.BytesCount())
|
|
|
|
} else {
|
|
|
|
globalConnStats.incS3InputBytes(meteredRequest.BytesCount())
|
|
|
|
globalConnStats.incS3OutputBytes(meteredResponse.BytesCount())
|
|
|
|
}
|
|
|
|
})
|
2017-04-24 21:13:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Bad path components to be rejected by the path validity handler.
|
|
|
|
const (
|
|
|
|
dotdotComponent = ".."
|
|
|
|
dotComponent = "."
|
|
|
|
)
|
|
|
|
|
|
|
|
// Check if the incoming path has bad path components,
|
|
|
|
// such as ".." and "."
|
|
|
|
func hasBadPathComponent(path string) bool {
|
|
|
|
path = strings.TrimSpace(path)
|
2019-08-06 15:08:58 -04:00
|
|
|
for _, p := range strings.Split(path, SlashSeparator) {
|
2017-04-24 21:13:46 -04:00
|
|
|
switch strings.TrimSpace(p) {
|
|
|
|
case dotdotComponent:
|
|
|
|
return true
|
|
|
|
case dotComponent:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-20 00:02:41 -05:00
|
|
|
// Check if client is sending a malicious request.
|
|
|
|
func hasMultipleAuth(r *http.Request) bool {
|
|
|
|
authTypeCount := 0
|
|
|
|
for _, hasValidAuth := range []func(*http.Request) bool{isRequestSignatureV2, isRequestPresignedSignatureV2, isRequestSignatureV4, isRequestPresignedSignatureV4, isRequestJWT, isRequestPostPolicySignatureV4} {
|
|
|
|
if hasValidAuth(r) {
|
|
|
|
authTypeCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return authTypeCount > 1
|
|
|
|
}
|
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
// requestValidityHandler validates all the incoming paths for
|
|
|
|
// any malicious requests.
|
|
|
|
func setRequestValidityHandler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Check for bad components in URL path.
|
|
|
|
if hasBadPathComponent(r.URL.Path) {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInvalidResourceName), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Check for bad components in URL query values.
|
|
|
|
for _, vv := range r.URL.Query() {
|
|
|
|
for _, v := range vv {
|
|
|
|
if hasBadPathComponent(v) {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInvalidResourceName), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
2017-04-24 21:13:46 -04:00
|
|
|
}
|
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
if hasMultipleAuth(r) {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2017-04-24 21:13:46 -04:00
|
|
|
}
|
2017-12-20 03:00:14 -05:00
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
var fwd = handlers.NewForwarder(&handlers.Forwarder{
|
|
|
|
PassHost: true,
|
|
|
|
RoundTripper: newGatewayHTTPTransport(1 * time.Hour),
|
|
|
|
Logger: func(err error) {
|
|
|
|
logger.LogIf(GlobalContext, err)
|
|
|
|
},
|
|
|
|
})
|
2018-02-02 21:18:52 -05:00
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
// setBucketForwardingHandler middleware forwards the path style requests
|
|
|
|
// on a bucket to the right bucket location, bucket to IP configuration
|
|
|
|
// is obtained from centralized etcd configuration service.
|
|
|
|
func setBucketForwardingHandler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if globalDNSConfig == nil || len(globalDomainNames) == 0 || !globalBucketFederation ||
|
|
|
|
guessIsHealthCheckReq(r) || guessIsMetricsReq(r) ||
|
|
|
|
guessIsRPCReq(r) || guessIsLoginSTSReq(r) || isAdminReq(r) {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2018-12-19 08:13:47 -05:00
|
|
|
|
2021-01-04 12:54:22 -05:00
|
|
|
// For browser requests, when federation is setup we need to
|
|
|
|
// specifically handle download and upload for browser requests.
|
|
|
|
if guessIsBrowserReq(r) {
|
|
|
|
var bucket, _ string
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPut:
|
|
|
|
if getRequestAuthType(r) == authTypeJWT {
|
|
|
|
bucket, _ = path2BucketObjectWithBasePath(minioReservedBucketPath+"/upload", r.URL.Path)
|
|
|
|
}
|
|
|
|
case http.MethodGet:
|
|
|
|
if t := r.URL.Query().Get("token"); t != "" {
|
|
|
|
bucket, _ = path2BucketObjectWithBasePath(minioReservedBucketPath+"/download", r.URL.Path)
|
|
|
|
} else if getRequestAuthType(r) != authTypeJWT && !strings.HasPrefix(r.URL.Path, minioReservedBucketPath) {
|
|
|
|
bucket, _ = request2BucketObjectName(r)
|
|
|
|
}
|
2018-12-19 08:13:47 -05:00
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
if bucket == "" {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sr, err := globalDNSConfig.Get(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if err == dns.ErrNoEntriesFound {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrNoSuchBucket),
|
|
|
|
r.URL, guessIsBrowserReq(r))
|
|
|
|
} else {
|
|
|
|
writeErrorResponse(r.Context(), w, toAPIError(r.Context(), err),
|
|
|
|
r.URL, guessIsBrowserReq(r))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if globalDomainIPs.Intersection(set.CreateStringSet(getHostsSlice(sr)...)).IsEmpty() {
|
|
|
|
r.URL.Scheme = "http"
|
|
|
|
if globalIsTLS {
|
|
|
|
r.URL.Scheme = "https"
|
|
|
|
}
|
|
|
|
r.URL.Host = getHostFromSrv(sr)
|
|
|
|
fwd.ServeHTTP(w, r)
|
|
|
|
return
|
2018-12-19 08:13:47 -05:00
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
2018-12-19 08:13:47 -05:00
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
|
|
|
|
bucket, object := request2BucketObjectName(r)
|
|
|
|
|
|
|
|
// Requests in federated setups for STS type calls which are
|
|
|
|
// performed at '/' resource should be routed by the muxer,
|
|
|
|
// the assumption is simply such that requests without a bucket
|
|
|
|
// in a federated setup cannot be proxied, so serve them at
|
|
|
|
// current server.
|
2019-08-22 04:02:39 -04:00
|
|
|
if bucket == "" {
|
2021-01-04 12:54:22 -05:00
|
|
|
h.ServeHTTP(w, r)
|
2019-08-22 04:02:39 -04:00
|
|
|
return
|
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
|
|
|
|
// MakeBucket requests should be handled at current endpoint
|
|
|
|
if r.Method == http.MethodPut && bucket != "" && object == "" && r.URL.RawQuery == "" {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CopyObject requests should be handled at current endpoint as path style
|
|
|
|
// requests have target bucket and object in URI and source details are in
|
|
|
|
// header fields
|
|
|
|
if r.Method == http.MethodPut && r.Header.Get(xhttp.AmzCopySource) != "" {
|
|
|
|
bucket, object = path2BucketObject(r.Header.Get(xhttp.AmzCopySource))
|
|
|
|
if bucket == "" || object == "" {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 04:02:39 -04:00
|
|
|
sr, err := globalDNSConfig.Get(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if err == dns.ErrNoEntriesFound {
|
2021-01-04 12:54:22 -05:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrNoSuchBucket), r.URL, guessIsBrowserReq(r))
|
2019-08-22 04:02:39 -04:00
|
|
|
} else {
|
2021-01-04 12:54:22 -05:00
|
|
|
writeErrorResponse(r.Context(), w, toAPIError(r.Context(), err), r.URL, guessIsBrowserReq(r))
|
2018-12-19 08:13:47 -05:00
|
|
|
}
|
2019-08-22 04:02:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if globalDomainIPs.Intersection(set.CreateStringSet(getHostsSlice(sr)...)).IsEmpty() {
|
|
|
|
r.URL.Scheme = "http"
|
2020-12-22 00:42:38 -05:00
|
|
|
if globalIsTLS {
|
2019-08-22 04:02:39 -04:00
|
|
|
r.URL.Scheme = "https"
|
2018-12-19 08:13:47 -05:00
|
|
|
}
|
2019-08-22 04:02:39 -04:00
|
|
|
r.URL.Host = getHostFromSrv(sr)
|
2021-01-04 12:54:22 -05:00
|
|
|
fwd.ServeHTTP(w, r)
|
2019-08-22 04:02:39 -04:00
|
|
|
return
|
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
h.ServeHTTP(w, r)
|
2018-02-02 21:18:52 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-01 15:22:01 -04:00
|
|
|
// customHeaderHandler sets x-amz-request-id header.
|
2018-11-19 17:47:03 -05:00
|
|
|
// Previously, this value was set right before a response was sent to
|
|
|
|
// the client. So, logger and Error response XML were not using this
|
|
|
|
// value. This is set here so that this header can be logged as
|
|
|
|
// part of the log entry, Error response XML and auditing.
|
|
|
|
func addCustomHeaders(h http.Handler) http.Handler {
|
2021-01-04 12:54:22 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Set custom headers such as x-amz-request-id for each request.
|
|
|
|
w.Header().Set(xhttp.AmzRequestID, mustGetRequestID(UTCNow()))
|
|
|
|
h.ServeHTTP(logger.NewResponseWriter(w), r)
|
|
|
|
})
|
2018-04-12 18:57:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func addSecurityHeaders(h http.Handler) http.Handler {
|
2021-01-04 12:54:22 -05:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
header := w.Header()
|
|
|
|
header.Set("X-XSS-Protection", "1; mode=block") // Prevents against XSS attacks
|
|
|
|
header.Set("Content-Security-Policy", "block-all-mixed-content") // prevent mixed (HTTP / HTTPS content)
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2018-04-12 18:57:41 -04:00
|
|
|
}
|
2018-06-25 16:51:49 -04:00
|
|
|
|
|
|
|
// criticalErrorHandler handles critical server failures caused by
|
|
|
|
// `panic(logger.ErrCritical)` as done by `logger.CriticalIf`.
|
|
|
|
//
|
|
|
|
// It should be always the first / highest HTTP handler.
|
|
|
|
type criticalErrorHandler struct{ handler http.Handler }
|
|
|
|
|
|
|
|
func (h criticalErrorHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err == logger.ErrCritical { // handle
|
2020-03-22 15:16:36 -04:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInternalError), r.URL, guessIsBrowserReq(r))
|
2020-06-11 11:19:55 -04:00
|
|
|
return
|
2018-06-25 16:51:49 -04:00
|
|
|
} else if err != nil {
|
|
|
|
panic(err) // forward other panic calls
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
2018-10-16 22:22:09 -04:00
|
|
|
|
|
|
|
// sseTLSHandler enforces certain rules for SSE requests which are made / must be made over TLS.
|
2021-01-04 12:54:22 -05:00
|
|
|
func setSSETLSHandler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Deny SSE-C requests if not made over TLS
|
|
|
|
if !globalIsTLS && (crypto.SSEC.IsRequested(r.Header) || crypto.SSECopy.IsRequested(r.Header)) {
|
|
|
|
if r.Method == http.MethodHead {
|
|
|
|
writeErrorResponseHeadersOnly(w, errorCodes.ToAPIErr(ErrInsecureSSECustomerRequest))
|
|
|
|
} else {
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInsecureSSECustomerRequest), r.URL, guessIsBrowserReq(r))
|
|
|
|
}
|
|
|
|
return
|
2018-10-26 21:03:17 -04:00
|
|
|
}
|
2021-01-04 12:54:22 -05:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
2018-10-16 22:22:09 -04:00
|
|
|
}
|