2015-02-15 09:48:15 +01:00
|
|
|
/*
|
2020-03-22 06:10:13 +01:00
|
|
|
* MinIO Cloud Storage, (C) 2015-2020 MinIO, Inc.
|
2015-02-15 09:48:15 +01: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-19 01:23:42 +02:00
|
|
|
package cmd
|
2015-02-11 12:23:15 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2015-10-08 05:36:36 +02:00
|
|
|
"strings"
|
2015-04-27 12:54:49 +02:00
|
|
|
"time"
|
2015-02-11 12:23:15 +01:00
|
|
|
|
2019-05-30 01:35:12 +02:00
|
|
|
"github.com/minio/minio-go/v6/pkg/set"
|
2018-05-11 21:02:30 +02:00
|
|
|
|
2016-11-23 03:18:22 +01:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2019-11-26 01:33:34 +01:00
|
|
|
"github.com/minio/minio/cmd/config/etcd/dns"
|
2018-10-17 04:22:09 +02:00
|
|
|
"github.com/minio/minio/cmd/crypto"
|
2019-07-03 07:34:32 +02:00
|
|
|
xhttp "github.com/minio/minio/cmd/http"
|
2020-04-30 07:17:36 +02:00
|
|
|
"github.com/minio/minio/cmd/http/stats"
|
2018-04-20 02:24:43 +02:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-02-03 03:18:52 +01:00
|
|
|
"github.com/minio/minio/pkg/handlers"
|
2015-08-31 23:40:12 +02:00
|
|
|
"github.com/rs/cors"
|
2015-02-11 12:23:15 +01:00
|
|
|
)
|
|
|
|
|
2016-02-04 23:57:20 +01:00
|
|
|
// HandlerFunc - useful to chain different middleware http.Handler
|
|
|
|
type HandlerFunc func(http.Handler) http.Handler
|
2015-09-16 01:59:43 +02:00
|
|
|
|
2017-08-17 22:49:57 +02:00
|
|
|
func registerHandlers(h http.Handler, handlerFns ...HandlerFunc) http.Handler {
|
2016-02-04 23:57:20 +01:00
|
|
|
for _, hFn := range handlerFns {
|
2017-08-17 22:49:57 +02:00
|
|
|
h = hFn(h)
|
2015-10-09 04:56:41 +02:00
|
|
|
}
|
2017-08-17 22:49:57 +02:00
|
|
|
return h
|
2015-04-27 12:54:49 +02:00
|
|
|
}
|
|
|
|
|
2016-07-28 21:02:22 +02:00
|
|
|
// Adds limiting body size middleware
|
|
|
|
|
2016-11-23 04:58:51 +01: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 19:14:17 +01: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 21:02:22 +02:00
|
|
|
|
|
|
|
type requestSizeLimitHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
maxBodySize int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func setRequestSizeLimitHandler(h http.Handler) http.Handler {
|
|
|
|
return requestSizeLimitHandler{handler: h, maxBodySize: requestMaxBodySize}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h requestSizeLimitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Restricting read data to a given maximum length
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, h.maxBodySize)
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2017-08-23 01:53:35 +02: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
|
|
|
|
)
|
|
|
|
|
|
|
|
type requestHeaderSizeLimitHandler struct {
|
|
|
|
http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func setRequestHeaderSizeLimitHandler(h http.Handler) http.Handler {
|
|
|
|
return requestHeaderSizeLimitHandler{h}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP restricts the size of the http header to 8 KB and the size
|
|
|
|
// of the user-defined metadata to 2 KB.
|
|
|
|
func (h requestHeaderSizeLimitHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if isHTTPHeaderSizeTooLarge(r.Header) {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrMetadataTooLarge), r.URL, guessIsBrowserReq(r))
|
2017-08-23 01:53:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
h.Handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-12-06 08:16:06 +01:00
|
|
|
if HasPrefix(key, prefix) {
|
2017-08-23 01:53:35 +02:00
|
|
|
usersize += length
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if usersize > maxUserDataSize || size > maxHeaderSize {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-08 00:18:59 +01:00
|
|
|
// ReservedMetadataPrefix is the prefix of a metadata key which
|
|
|
|
// is reserved and for internal use only.
|
|
|
|
const ReservedMetadataPrefix = "X-Minio-Internal-"
|
|
|
|
|
|
|
|
type reservedMetadataHandler struct {
|
|
|
|
http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterReservedMetadata(h http.Handler) http.Handler {
|
|
|
|
return reservedMetadataHandler{h}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP fails if the request contains at least one reserved header which
|
|
|
|
// would be treated as metadata.
|
|
|
|
func (h reservedMetadataHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if containsReservedMetadata(r.Header) {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrUnsupportedMetadata), r.URL, guessIsBrowserReq(r))
|
2017-11-08 00:18:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
h.Handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-12-06 08:16:06 +01:00
|
|
|
if HasPrefix(key, ReservedMetadataPrefix) {
|
2017-11-08 00:18:59 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-03-27 21:37:21 +02:00
|
|
|
// Reserved bucket.
|
2016-03-13 01:08:15 +01:00
|
|
|
const (
|
2017-02-16 23:52:14 +01:00
|
|
|
minioReservedBucket = "minio"
|
2019-08-06 21:08:58 +02:00
|
|
|
minioReservedBucketPath = SlashSeparator + minioReservedBucket
|
2019-11-22 02:45:15 +01:00
|
|
|
loginPathPrefix = SlashSeparator + "login"
|
2016-03-13 01:08:15 +01:00
|
|
|
)
|
|
|
|
|
2016-12-10 09:42:22 +01:00
|
|
|
// Adds redirect rules for incoming requests.
|
|
|
|
type redirectHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2016-02-17 03:50:36 +01:00
|
|
|
func setBrowserRedirectHandler(h http.Handler) http.Handler {
|
2016-12-10 09:42:22 +01:00
|
|
|
return redirectHandler{handler: h}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 23:52:14 +01:00
|
|
|
if urlPath == minioReservedBucketPath {
|
2019-08-06 21:08:58 +02:00
|
|
|
rLocation = minioReservedBucketPath + SlashSeparator
|
2016-12-10 09:42:22 +01:00
|
|
|
}
|
|
|
|
if contains([]string{
|
2019-08-06 21:08:58 +02:00
|
|
|
SlashSeparator,
|
2016-12-10 09:42:22 +01:00
|
|
|
"/webrpc",
|
|
|
|
"/login",
|
2019-08-12 07:17:02 +02:00
|
|
|
"/favicon-16x16.png",
|
|
|
|
"/favicon-32x32.png",
|
|
|
|
"/favicon-96x96.png",
|
2016-12-10 09:42:22 +01:00
|
|
|
}, urlPath) {
|
2017-02-16 23:52:14 +01:00
|
|
|
rLocation = minioReservedBucketPath + urlPath
|
2016-12-10 09:42:22 +01: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 08:10:37 +02:00
|
|
|
aType := getRequestAuthType(req)
|
2019-10-23 07:59:13 +02:00
|
|
|
return strings.Contains(req.Header.Get("User-Agent"), "Mozilla") && globalBrowserEnabled &&
|
2019-04-04 08:10:37 +02:00
|
|
|
(aType == authTypeJWT || aType == authTypeAnonymous)
|
2016-02-17 03:50:36 +01:00
|
|
|
}
|
|
|
|
|
2018-03-12 07:16:53 +01: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 05:25:02 +01:00
|
|
|
return aType == authTypeAnonymous && (req.Method == http.MethodGet || req.Method == http.MethodHead) &&
|
2018-03-12 07:16:53 +01:00
|
|
|
(req.URL.Path == healthCheckPathPrefix+healthCheckLivenessPath ||
|
|
|
|
req.URL.Path == healthCheckPathPrefix+healthCheckReadinessPath)
|
|
|
|
}
|
|
|
|
|
2018-04-19 01:01:42 +02: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 16:57:12 +02:00
|
|
|
return (aType == authTypeAnonymous || aType == authTypeJWT) &&
|
2018-04-19 01:01:42 +02:00
|
|
|
req.URL.Path == minioReservedBucketPath+prometheusMetricsPath
|
|
|
|
}
|
|
|
|
|
2017-09-01 21:16:54 +02: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 23:28:48 +01:00
|
|
|
return req.Method == http.MethodPost &&
|
2019-08-06 21:08:58 +02:00
|
|
|
strings.HasPrefix(req.URL.Path, minioReservedBucketPath+SlashSeparator)
|
2017-09-01 21:16:54 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 03:50:36 +01:00
|
|
|
func (h redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-04-04 08:10:37 +02:00
|
|
|
// Re-direction is handled specifically for browser requests.
|
|
|
|
if 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
|
2015-05-01 01:29:03 +02:00
|
|
|
}
|
2015-04-27 12:54:49 +02:00
|
|
|
}
|
2016-02-17 03:50:36 +01:00
|
|
|
h.handler.ServeHTTP(w, r)
|
2015-04-27 12:54:49 +02:00
|
|
|
}
|
|
|
|
|
2016-02-04 22:57:56 +01:00
|
|
|
// Adds Cache-Control header
|
|
|
|
type cacheControlHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2016-02-17 03:50:36 +01:00
|
|
|
func setBrowserCacheControlHandler(h http.Handler) http.Handler {
|
2016-02-04 22:57:56 +01:00
|
|
|
return cacheControlHandler{h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h cacheControlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-04-04 08:10:37 +02:00
|
|
|
if r.Method == http.MethodGet && guessIsBrowserReq(r) {
|
2016-02-26 14:19:35 +01:00
|
|
|
// For all browser requests set appropriate Cache-Control policies
|
2019-12-06 08:16:06 +01:00
|
|
|
if HasPrefix(r.URL.Path, minioReservedBucketPath+SlashSeparator) {
|
|
|
|
if HasSuffix(r.URL.Path, ".js") || r.URL.Path == minioReservedBucketPath+"/favicon.ico" {
|
2016-12-17 20:00:16 +01:00
|
|
|
// 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.
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.CacheControl, "max-age=31536000")
|
2016-12-17 20:00:16 +01:00
|
|
|
} else {
|
|
|
|
// For non asset requests we serve index.html which will never be cached.
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.CacheControl, "no-store")
|
2016-12-17 20:00:16 +01:00
|
|
|
}
|
2016-02-26 14:19:35 +01:00
|
|
|
}
|
2016-02-17 03:50:36 +01:00
|
|
|
}
|
2016-12-17 20:00:16 +01:00
|
|
|
|
2016-02-17 03:50:36 +01:00
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:54:55 +01:00
|
|
|
// Check to allow access to the reserved "bucket" `/minio` for Admin
|
|
|
|
// API requests.
|
|
|
|
func isAdminReq(r *http.Request) bool {
|
2019-11-04 18:30:59 +01:00
|
|
|
return strings.HasPrefix(r.URL.Path, adminPathPrefix)
|
2018-01-22 23:54:55 +01:00
|
|
|
}
|
|
|
|
|
2019-11-22 02:45:15 +01: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-17 03:50:36 +01:00
|
|
|
// Adds verification for incoming paths.
|
2017-09-01 21:16:54 +02:00
|
|
|
type minioReservedBucketHandler struct {
|
2017-03-03 12:01:42 +01:00
|
|
|
handler http.Handler
|
2016-02-17 03:50:36 +01:00
|
|
|
}
|
|
|
|
|
2017-09-01 21:16:54 +02:00
|
|
|
func setReservedBucketHandler(h http.Handler) http.Handler {
|
|
|
|
return minioReservedBucketHandler{h}
|
2016-02-17 03:50:36 +01:00
|
|
|
}
|
|
|
|
|
2017-09-01 21:16:54 +02:00
|
|
|
func (h minioReservedBucketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2018-01-22 23:54:55 +01:00
|
|
|
switch {
|
2018-04-19 01:01:42 +02:00
|
|
|
case guessIsRPCReq(r), guessIsBrowserReq(r), guessIsHealthCheckReq(r), guessIsMetricsReq(r), isAdminReq(r):
|
2018-01-22 23:54:55 +01:00
|
|
|
// Allow access to reserved buckets
|
|
|
|
default:
|
2019-11-04 18:30:59 +01:00
|
|
|
// For all other requests reject access to reserved buckets
|
2019-06-27 03:21:54 +02:00
|
|
|
bucketName, _ := request2BucketObjectName(r)
|
2017-09-01 21:16:54 +02:00
|
|
|
if isMinioReservedBucket(bucketName) || isMinioMetaBucket(bucketName) {
|
2019-11-04 18:30:59 +01:00
|
|
|
browser := guessIsBrowserReq(r)
|
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrAllAccessDisabled), r.URL, browser)
|
2017-09-01 21:16:54 +02:00
|
|
|
return
|
|
|
|
}
|
2016-02-04 22:57:56 +01:00
|
|
|
}
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2016-12-10 09:42:22 +01:00
|
|
|
type timeValidityHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// setTimeValidityHandler to validate parsable time over http header
|
|
|
|
func setTimeValidityHandler(h http.Handler) http.Handler {
|
|
|
|
return timeValidityHandler{h}
|
|
|
|
}
|
|
|
|
|
2016-03-31 05:04:51 +02:00
|
|
|
// Supported Amz date formats.
|
|
|
|
var amzDateFormats = []string{
|
2016-03-07 18:52:20 +01:00
|
|
|
time.RFC1123,
|
|
|
|
time.RFC1123Z,
|
|
|
|
iso8601Format,
|
2016-03-31 05:04:51 +02:00
|
|
|
// Add new AMZ date formats here.
|
2016-03-07 18:52:20 +01:00
|
|
|
}
|
|
|
|
|
2016-12-10 09:42:22 +01:00
|
|
|
// Supported Amz date headers.
|
|
|
|
var amzDateHeaders = []string{
|
|
|
|
"x-amz-date",
|
|
|
|
"date",
|
|
|
|
}
|
|
|
|
|
2016-03-31 05:04:51 +02: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 09:42:22 +01:00
|
|
|
amzDate, err := time.Parse(dateFormat, amzDateStr)
|
|
|
|
if err == nil {
|
2016-03-31 05:04:51 +02:00
|
|
|
return amzDate, ErrNone
|
2016-03-07 18:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-31 05:04:51 +02: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-19 00:19:29 +01:00
|
|
|
amzDateStr := req.Header.Get(amzDateHeader)
|
2016-03-31 05:04:51 +02:00
|
|
|
if amzDateStr != "" {
|
|
|
|
return parseAmzDate(amzDateStr)
|
2016-03-07 18:52:20 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-31 05:04:51 +02:00
|
|
|
// Date header missing.
|
|
|
|
return time.Time{}, ErrMissingDateHeader
|
2016-03-07 18:52:20 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 21:58:36 +02:00
|
|
|
func (h timeValidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2016-11-12 01:36:07 +01:00
|
|
|
aType := getRequestAuthType(r)
|
2016-11-13 21:08:24 +01:00
|
|
|
if aType == authTypeSigned || aType == authTypeSignedV2 || aType == authTypeStreamingSigned {
|
2016-11-12 01:36:07 +01:00
|
|
|
// Verify if date headers are set, if not reject the request
|
2019-02-12 10:25:52 +01:00
|
|
|
amzDate, errCode := parseAmzDateHeader(r)
|
|
|
|
if errCode != ErrNone {
|
2016-02-17 03:50:36 +01:00
|
|
|
// All our internal APIs are sensitive towards Date
|
|
|
|
// header, for all requests where Date header is not
|
|
|
|
// present we will reject such clients.
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(errCode), r.URL, guessIsBrowserReq(r))
|
2015-04-30 03:30:17 +02:00
|
|
|
return
|
|
|
|
}
|
2016-10-07 20:15:55 +02:00
|
|
|
// Verify if the request date header is shifted by less than globalMaxSkewTime parameter in the past
|
2016-09-10 00:33:35 +02:00
|
|
|
// or in the future, reject request otherwise.
|
2017-03-18 19:28:41 +01:00
|
|
|
curTime := UTCNow()
|
2016-10-07 20:15:55 +02:00
|
|
|
if curTime.Sub(amzDate) > globalMaxSkewTime || amzDate.Sub(curTime) > globalMaxSkewTime {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrRequestTimeTooSkewed), r.URL, guessIsBrowserReq(r))
|
2015-04-30 03:30:17 +02:00
|
|
|
return
|
|
|
|
}
|
2015-04-27 12:54:49 +02:00
|
|
|
}
|
2016-11-12 01:36:07 +01:00
|
|
|
h.handler.ServeHTTP(w, r)
|
2015-04-27 12:54:49 +02:00
|
|
|
}
|
|
|
|
|
2016-02-17 03:50:36 +01:00
|
|
|
type resourceHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:57:20 +01:00
|
|
|
// setCorsHandler handler for CORS (Cross Origin Resource Sharing)
|
|
|
|
func setCorsHandler(h http.Handler) http.Handler {
|
2019-05-31 18:23:55 +02:00
|
|
|
commonS3Headers := []string{
|
2019-07-03 07:34:32 +02:00
|
|
|
xhttp.Date,
|
|
|
|
xhttp.ETag,
|
|
|
|
xhttp.ServerInfo,
|
|
|
|
xhttp.Connection,
|
|
|
|
xhttp.AcceptRanges,
|
|
|
|
xhttp.ContentRange,
|
|
|
|
xhttp.ContentEncoding,
|
|
|
|
xhttp.ContentLength,
|
|
|
|
xhttp.ContentType,
|
2019-05-31 18:23:55 +02:00
|
|
|
"X-Amz*",
|
|
|
|
"x-amz*",
|
|
|
|
"*",
|
|
|
|
}
|
2019-03-06 20:58:53 +01:00
|
|
|
|
|
|
|
c := cors.New(cors.Options{
|
|
|
|
AllowedOrigins: []string{"*"},
|
|
|
|
AllowedMethods: []string{
|
|
|
|
http.MethodGet,
|
|
|
|
http.MethodPut,
|
|
|
|
http.MethodHead,
|
|
|
|
http.MethodPost,
|
|
|
|
http.MethodDelete,
|
|
|
|
http.MethodOptions,
|
|
|
|
http.MethodPatch,
|
|
|
|
},
|
2019-05-31 18:23:55 +02:00
|
|
|
AllowedHeaders: commonS3Headers,
|
|
|
|
ExposedHeaders: commonS3Headers,
|
2019-03-06 20:58:53 +01:00
|
|
|
AllowCredentials: true,
|
|
|
|
})
|
|
|
|
|
2015-10-30 19:49:08 +01:00
|
|
|
return c.Handler(h)
|
2015-08-31 23:40:12 +02:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:57:20 +01:00
|
|
|
// setIgnoreResourcesHandler -
|
2015-02-24 01:46:48 +01:00
|
|
|
// 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
|
2016-02-17 03:50:36 +01:00
|
|
|
// valid error message indicating that requested feature is not implemented.
|
2016-02-04 23:57:20 +01:00
|
|
|
func setIgnoreResourcesHandler(h http.Handler) http.Handler {
|
2015-04-27 12:54:49 +02:00
|
|
|
return resourceHandler{h}
|
2015-02-18 21:15:33 +01:00
|
|
|
}
|
|
|
|
|
2015-04-30 00:28:04 +02:00
|
|
|
// Checks requests for not implemented Bucket resources
|
|
|
|
func ignoreNotImplementedBucketResources(req *http.Request) bool {
|
2016-02-20 01:04:29 +01:00
|
|
|
for name := range req.URL.Query() {
|
2020-02-16 07:07:52 +01:00
|
|
|
// Enable PutBucketACL, GetBucketACL, GetBucketCors,
|
|
|
|
// GetBucketWebsite, GetBucketAcccelerate,
|
|
|
|
// GetBucketRequestPayment, GetBucketLogging,
|
|
|
|
// GetBucketLifecycle, GetBucketReplication,
|
|
|
|
// GetBucketTagging, GetBucketVersioning,
|
|
|
|
// DeleteBucketTagging, and DeleteBucketWebsite
|
|
|
|
// dummy calls specifically.
|
|
|
|
if name == "acl" && req.Method == http.MethodPut {
|
|
|
|
return false
|
|
|
|
}
|
2019-02-09 01:18:13 +01:00
|
|
|
if ((name == "acl" ||
|
|
|
|
name == "cors" ||
|
|
|
|
name == "website" ||
|
|
|
|
name == "accelerate" ||
|
|
|
|
name == "requestPayment" ||
|
|
|
|
name == "logging" ||
|
|
|
|
name == "lifecycle" ||
|
|
|
|
name == "replication" ||
|
|
|
|
name == "tagging" ||
|
|
|
|
name == "versioning") && req.Method == http.MethodGet) ||
|
|
|
|
((name == "tagging" ||
|
|
|
|
name == "website") && req.Method == http.MethodDelete) {
|
2018-05-10 06:02:26 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-16 07:07:52 +01:00
|
|
|
if notImplementedBucketResourceNames[name] {
|
2015-02-11 12:23:15 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-04-30 00:28:04 +02:00
|
|
|
// Checks requests for not implemented Object resources
|
|
|
|
func ignoreNotImplementedObjectResources(req *http.Request) bool {
|
2016-02-20 01:04:29 +01:00
|
|
|
for name := range req.URL.Query() {
|
2020-02-16 07:07:52 +01:00
|
|
|
// Enable Get/PutObjectACL dummy call specifically.
|
|
|
|
if name == "acl" && (req.Method == http.MethodGet || req.Method == http.MethodPut) {
|
2018-05-10 06:02:26 +02:00
|
|
|
return false
|
|
|
|
}
|
2020-02-16 07:07:52 +01:00
|
|
|
if notImplementedObjectResourceNames[name] {
|
2015-02-11 12:23:15 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2016-02-20 01:04:29 +01:00
|
|
|
|
|
|
|
// List of not implemented bucket queries
|
2020-02-16 07:07:52 +01:00
|
|
|
var notImplementedBucketResourceNames = map[string]bool{
|
2019-02-09 01:18:13 +01:00
|
|
|
"accelerate": true,
|
2016-02-20 01:04:29 +01:00
|
|
|
"cors": true,
|
2019-02-09 01:18:13 +01:00
|
|
|
"inventory": true,
|
2016-02-20 01:04:29 +01:00
|
|
|
"logging": true,
|
2019-02-09 01:18:13 +01:00
|
|
|
"metrics": true,
|
2016-02-20 01:04:29 +01:00
|
|
|
"replication": true,
|
|
|
|
"requestPayment": true,
|
2019-02-09 01:18:13 +01:00
|
|
|
"tagging": true,
|
2016-02-20 01:04:29 +01:00
|
|
|
"versioning": true,
|
|
|
|
"website": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// List of not implemented object queries
|
2020-02-16 07:07:52 +01:00
|
|
|
var notImplementedObjectResourceNames = map[string]bool{
|
2018-05-08 18:15:10 +02:00
|
|
|
"restore": true,
|
2019-02-09 01:18:13 +01:00
|
|
|
"torrent": true,
|
2016-02-20 01:04:29 +01:00
|
|
|
}
|
2016-12-10 09:42:22 +01:00
|
|
|
|
|
|
|
// Resource handler ServeHTTP() wrapper
|
|
|
|
func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-06-27 03:21:54 +02:00
|
|
|
bucketName, objectName := request2BucketObjectName(r)
|
2016-12-10 09:42:22 +01:00
|
|
|
|
|
|
|
// If bucketName is present and not objectName check for bucket level resource queries.
|
|
|
|
if bucketName != "" && objectName == "" {
|
|
|
|
if ignoreNotImplementedBucketResources(r) {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL, guessIsBrowserReq(r))
|
2016-12-10 09:42:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If bucketName and objectName are present check for its resource queries.
|
|
|
|
if bucketName != "" && objectName != "" {
|
|
|
|
if ignoreNotImplementedObjectResources(r) {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL, guessIsBrowserReq(r))
|
2016-12-10 09:42:22 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve HTTP.
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
2017-02-06 18:29:53 +01:00
|
|
|
|
|
|
|
// httpStatsHandler definition: gather HTTP statistics
|
|
|
|
type httpStatsHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// setHttpStatsHandler sets a http Stats Handler
|
|
|
|
func setHTTPStatsHandler(h http.Handler) http.Handler {
|
|
|
|
return httpStatsHandler{handler: h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h httpStatsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2020-04-30 07:17:36 +02:00
|
|
|
// Meters s3 connection stats.
|
|
|
|
meteredRequest := &stats.IncomingTrafficMeter{ReadCloser: r.Body}
|
|
|
|
meteredResponse := &stats.OutgoingTrafficMeter{ResponseWriter: w}
|
|
|
|
|
2017-02-06 18:29:53 +01:00
|
|
|
// Execute the request
|
2020-04-30 07:17:36 +02:00
|
|
|
r.Body = meteredRequest
|
|
|
|
h.handler.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-02-06 18:29:53 +01:00
|
|
|
}
|
2017-04-25 03:13:46 +02:00
|
|
|
|
2019-02-20 06:02:41 +01:00
|
|
|
// requestValidityHandler validates all the incoming paths for
|
|
|
|
// any malicious requests.
|
|
|
|
type requestValidityHandler struct {
|
2017-04-25 03:13:46 +02:00
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2019-02-20 06:02:41 +01:00
|
|
|
func setRequestValidityHandler(h http.Handler) http.Handler {
|
|
|
|
return requestValidityHandler{handler: h}
|
2017-04-25 03:13:46 +02: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 21:08:58 +02:00
|
|
|
for _, p := range strings.Split(path, SlashSeparator) {
|
2017-04-25 03:13:46 +02:00
|
|
|
switch strings.TrimSpace(p) {
|
|
|
|
case dotdotComponent:
|
|
|
|
return true
|
|
|
|
case dotComponent:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-20 06:02:41 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h requestValidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-04-25 03:13:46 +02:00
|
|
|
// Check for bad components in URL path.
|
|
|
|
if hasBadPathComponent(r.URL.Path) {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInvalidResourceName), r.URL, guessIsBrowserReq(r))
|
2017-04-25 03:13:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Check for bad components in URL query values.
|
|
|
|
for _, vv := range r.URL.Query() {
|
|
|
|
for _, v := range vv {
|
|
|
|
if hasBadPathComponent(v) {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInvalidResourceName), r.URL, guessIsBrowserReq(r))
|
2017-04-25 03:13:46 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-20 06:02:41 +01:00
|
|
|
if hasMultipleAuth(r) {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL, guessIsBrowserReq(r))
|
2019-02-20 06:02:41 +01:00
|
|
|
return
|
|
|
|
}
|
2017-04-25 03:13:46 +02:00
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
2017-12-20 09:00:14 +01:00
|
|
|
|
2018-02-03 03:18:52 +01:00
|
|
|
// To forward the path style requests on a bucket to the right
|
|
|
|
// configured server, bucket to IP configuration is obtained
|
|
|
|
// from centralized etcd configuration service.
|
|
|
|
type bucketForwardingHandler struct {
|
|
|
|
fwd *handlers.Forwarder
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f bucketForwardingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-02-23 04:18:01 +01:00
|
|
|
if globalDNSConfig == nil || len(globalDomainNames) == 0 ||
|
2018-12-19 14:13:47 +01:00
|
|
|
guessIsHealthCheckReq(r) || guessIsMetricsReq(r) ||
|
2019-12-29 17:56:45 +01:00
|
|
|
guessIsRPCReq(r) || guessIsLoginSTSReq(r) || isAdminReq(r) ||
|
|
|
|
!globalBucketFederation {
|
2018-02-03 03:18:52 +01:00
|
|
|
f.handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2018-12-19 14:13:47 +01:00
|
|
|
|
|
|
|
// For browser requests, when federation is setup we need to
|
|
|
|
// specifically handle download and upload for browser requests.
|
2019-02-23 04:18:01 +01:00
|
|
|
if guessIsBrowserReq(r) && globalDNSConfig != nil && len(globalDomainNames) > 0 {
|
2018-12-19 14:13:47 +01:00
|
|
|
var bucket, _ string
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodPut:
|
|
|
|
if getRequestAuthType(r) == authTypeJWT {
|
2020-01-21 23:07:49 +01:00
|
|
|
bucket, _ = path2BucketObjectWithBasePath(minioReservedBucketPath+"/upload", r.URL.Path)
|
2018-12-19 14:13:47 +01:00
|
|
|
}
|
|
|
|
case http.MethodGet:
|
|
|
|
if t := r.URL.Query().Get("token"); t != "" {
|
2020-01-21 23:07:49 +01:00
|
|
|
bucket, _ = path2BucketObjectWithBasePath(minioReservedBucketPath+"/download", r.URL.Path)
|
2018-12-19 14:13:47 +01:00
|
|
|
} else if getRequestAuthType(r) != authTypeJWT && !strings.HasPrefix(r.URL.Path, minioReservedBucketPath) {
|
2019-12-18 09:36:03 +01:00
|
|
|
bucket, _ = request2BucketObjectName(r)
|
2018-12-19 14:13:47 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-22 10:02:39 +02:00
|
|
|
if bucket == "" {
|
|
|
|
f.handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sr, err := globalDNSConfig.Get(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if err == dns.ErrNoEntriesFound {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrNoSuchBucket),
|
2019-08-22 10:02:39 +02:00
|
|
|
r.URL, guessIsBrowserReq(r))
|
|
|
|
} else {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, toAPIError(r.Context(), err),
|
2019-08-22 10:02:39 +02:00
|
|
|
r.URL, guessIsBrowserReq(r))
|
2018-12-19 14:13:47 +01:00
|
|
|
}
|
2019-08-22 10:02:39 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if globalDomainIPs.Intersection(set.CreateStringSet(getHostsSlice(sr)...)).IsEmpty() {
|
|
|
|
r.URL.Scheme = "http"
|
|
|
|
if globalIsSSL {
|
|
|
|
r.URL.Scheme = "https"
|
2018-12-19 14:13:47 +01:00
|
|
|
}
|
2019-08-22 10:02:39 +02:00
|
|
|
r.URL.Host = getHostFromSrv(sr)
|
|
|
|
f.fwd.ServeHTTP(w, r)
|
|
|
|
return
|
2018-12-19 14:13:47 +01:00
|
|
|
}
|
2019-09-04 19:44:02 +02:00
|
|
|
f.handler.ServeHTTP(w, r)
|
2018-12-19 14:13:47 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-06-27 03:21:54 +02:00
|
|
|
bucket, object := request2BucketObjectName(r)
|
|
|
|
|
2018-05-11 21:02:30 +02:00
|
|
|
// ListBucket requests should be handled at current endpoint as
|
|
|
|
// all buckets data can be fetched from here.
|
2018-04-05 17:18:42 +02:00
|
|
|
if r.Method == http.MethodGet && bucket == "" && object == "" {
|
|
|
|
f.handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2018-05-16 03:20:22 +02:00
|
|
|
|
|
|
|
// MakeBucket requests should be handled at current endpoint
|
|
|
|
if r.Method == http.MethodPut && bucket != "" && object == "" {
|
|
|
|
f.handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-11 21:02:30 +02:00
|
|
|
// 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
|
2019-08-22 10:02:39 +02:00
|
|
|
if r.Method == http.MethodPut && r.Header.Get(xhttp.AmzCopySource) != "" {
|
2020-01-21 23:07:49 +01:00
|
|
|
bucket, object = path2BucketObject(r.Header.Get(xhttp.AmzCopySource))
|
2019-08-22 10:02:39 +02:00
|
|
|
if bucket == "" || object == "" {
|
|
|
|
f.handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
2018-05-11 21:02:30 +02:00
|
|
|
}
|
2018-02-03 03:18:52 +01:00
|
|
|
sr, err := globalDNSConfig.Get(bucket)
|
|
|
|
if err != nil {
|
2018-07-12 23:12:40 +02:00
|
|
|
if err == dns.ErrNoEntriesFound {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrNoSuchBucket), r.URL, guessIsBrowserReq(r))
|
2018-02-03 03:18:52 +01:00
|
|
|
} else {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, toAPIError(r.Context(), err), r.URL, guessIsBrowserReq(r))
|
2018-02-03 03:18:52 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2018-05-11 21:02:30 +02:00
|
|
|
if globalDomainIPs.Intersection(set.CreateStringSet(getHostsSlice(sr)...)).IsEmpty() {
|
2018-05-16 03:20:22 +02:00
|
|
|
r.URL.Scheme = "http"
|
2018-02-03 03:18:52 +01:00
|
|
|
if globalIsSSL {
|
2018-05-16 03:20:22 +02:00
|
|
|
r.URL.Scheme = "https"
|
2018-02-03 03:18:52 +01:00
|
|
|
}
|
2018-12-19 14:13:47 +01:00
|
|
|
r.URL.Host = getHostFromSrv(sr)
|
2018-02-03 03:18:52 +01:00
|
|
|
f.fwd.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
fwd := handlers.NewForwarder(&handlers.Forwarder{
|
|
|
|
PassHost: true,
|
2020-03-22 06:10:13 +01:00
|
|
|
RoundTripper: NewGatewayHTTPTransport(),
|
2019-07-31 20:08:10 +02:00
|
|
|
Logger: func(err error) {
|
2020-04-09 18:30:02 +02:00
|
|
|
logger.LogIf(GlobalContext, err)
|
2019-07-31 20:08:10 +02:00
|
|
|
},
|
2018-02-03 03:18:52 +01:00
|
|
|
})
|
|
|
|
return bucketForwardingHandler{fwd, h}
|
|
|
|
}
|
|
|
|
|
2019-07-01 21:22:01 +02:00
|
|
|
// customHeaderHandler sets x-amz-request-id header.
|
2018-11-19 23:47:03 +01: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.
|
|
|
|
type customHeaderHandler struct {
|
2018-07-21 03:46:32 +02:00
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
2018-11-19 23:47:03 +01:00
|
|
|
func addCustomHeaders(h http.Handler) http.Handler {
|
|
|
|
return customHeaderHandler{handler: h}
|
2018-07-21 03:46:32 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 23:47:03 +01:00
|
|
|
func (s customHeaderHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2019-07-01 21:22:01 +02:00
|
|
|
// Set custom headers such as x-amz-request-id for each request.
|
2019-07-03 07:34:32 +02:00
|
|
|
w.Header().Set(xhttp.AmzRequestID, mustGetRequestID(UTCNow()))
|
2018-11-19 23:47:03 +01:00
|
|
|
s.handler.ServeHTTP(logger.NewResponseWriter(w), r)
|
2018-07-21 03:46:32 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 00:57:41 +02:00
|
|
|
type securityHeaderHandler struct {
|
|
|
|
handler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func addSecurityHeaders(h http.Handler) http.Handler {
|
|
|
|
return securityHeaderHandler{handler: h}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s securityHeaderHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
header := w.Header()
|
2018-05-30 11:29:27 +02:00
|
|
|
header.Set("X-XSS-Protection", "1; mode=block") // Prevents against XSS attacks
|
2018-04-13 00:57:41 +02:00
|
|
|
header.Set("Content-Security-Policy", "block-all-mixed-content") // prevent mixed (HTTP / HTTPS content)
|
|
|
|
s.handler.ServeHTTP(w, r)
|
|
|
|
}
|
2018-06-25 22:51:49 +02: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 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInternalError), r.URL, guessIsBrowserReq(r))
|
2018-06-25 22:51:49 +02:00
|
|
|
} else if err != nil {
|
|
|
|
panic(err) // forward other panic calls
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|
2018-10-17 04:22:09 +02:00
|
|
|
|
|
|
|
func setSSETLSHandler(h http.Handler) http.Handler { return sseTLSHandler{h} }
|
|
|
|
|
|
|
|
// sseTLSHandler enforces certain rules for SSE requests which are made / must be made over TLS.
|
|
|
|
type sseTLSHandler struct{ handler http.Handler }
|
|
|
|
|
|
|
|
func (h sseTLSHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Deny SSE-C requests if not made over TLS
|
|
|
|
if !globalIsSSL && (crypto.SSEC.IsRequested(r.Header) || crypto.SSECopy.IsRequested(r.Header)) {
|
2018-10-27 03:03:17 +02:00
|
|
|
if r.Method == http.MethodHead {
|
2019-02-12 10:25:52 +01:00
|
|
|
writeErrorResponseHeadersOnly(w, errorCodes.ToAPIErr(ErrInsecureSSECustomerRequest))
|
2018-10-27 03:03:17 +02:00
|
|
|
} else {
|
2020-03-22 20:16:36 +01:00
|
|
|
writeErrorResponse(r.Context(), w, errorCodes.ToAPIErr(ErrInsecureSSECustomerRequest), r.URL, guessIsBrowserReq(r))
|
2018-10-27 03:03:17 +02:00
|
|
|
}
|
2018-10-17 04:22:09 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
h.handler.ServeHTTP(w, r)
|
|
|
|
}
|