| 
									
										
										
										
											2015-02-15 16:48:15 +08:00
										 |  |  | /* | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  |  * Minio Cloud Storage, (C) 2015, 2016, 2017 Minio, Inc. | 
					
						
							| 
									
										
										
										
											2015-02-15 16:48:15 +08: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 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2015-02-11 19:23:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2017-02-07 15:46:33 +08:00
										 |  |  | 	"bufio" | 
					
						
							|  |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2015-02-11 19:23:15 +08:00
										 |  |  | 	"net/http" | 
					
						
							| 
									
										
										
										
											2015-10-08 11:36:36 +08:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2015-04-27 18:54:49 +08:00
										 |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2015-02-11 19:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 	humanize "github.com/dustin/go-humanize" | 
					
						
							| 
									
										
										
										
											2015-09-01 05:40:12 +08:00
										 |  |  | 	"github.com/rs/cors" | 
					
						
							| 
									
										
										
										
											2015-02-11 19:23:15 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 06:57:20 +08:00
										 |  |  | // HandlerFunc - useful to chain different middleware http.Handler
 | 
					
						
							|  |  |  | type HandlerFunc func(http.Handler) http.Handler | 
					
						
							| 
									
										
										
										
											2015-09-16 07:59:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-18 04:49:57 +08:00
										 |  |  | func registerHandlers(h http.Handler, handlerFns ...HandlerFunc) http.Handler { | 
					
						
							| 
									
										
										
										
											2016-02-05 06:57:20 +08:00
										 |  |  | 	for _, hFn := range handlerFns { | 
					
						
							| 
									
										
										
										
											2017-08-18 04:49:57 +08:00
										 |  |  | 		h = hFn(h) | 
					
						
							| 
									
										
										
										
											2015-10-09 10:56:41 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-08-18 04:49:57 +08:00
										 |  |  | 	return h | 
					
						
							| 
									
										
										
										
											2015-04-27 18:54:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-29 03:02:22 +08:00
										 |  |  | // Adds limiting body size middleware
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 11:58:51 +08: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-04 02:14:17 +08: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-29 03:02:22 +08: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 07:53:35 +08: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) { | 
					
						
							|  |  |  | 		writeErrorResponse(w, ErrMetadataTooLarge, r.URL) | 
					
						
							|  |  |  | 		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 { | 
					
						
							|  |  |  | 			if strings.HasPrefix(key, prefix) { | 
					
						
							|  |  |  | 				usersize += length | 
					
						
							|  |  |  | 				break | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if usersize > maxUserDataSize || size > maxHeaderSize { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-28 03:37:21 +08:00
										 |  |  | // Reserved bucket.
 | 
					
						
							| 
									
										
										
										
											2016-03-13 08:08:15 +08:00
										 |  |  | const ( | 
					
						
							| 
									
										
										
										
											2017-02-17 06:52:14 +08:00
										 |  |  | 	minioReservedBucket     = "minio" | 
					
						
							|  |  |  | 	minioReservedBucketPath = "/" + minioReservedBucket | 
					
						
							| 
									
										
										
										
											2016-03-13 08:08:15 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | // Adds redirect rules for incoming requests.
 | 
					
						
							|  |  |  | type redirectHandler struct { | 
					
						
							|  |  |  | 	handler http.Handler | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | func setBrowserRedirectHandler(h http.Handler) http.Handler { | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08: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-17 06:52:14 +08:00
										 |  |  | 	if urlPath == minioReservedBucketPath { | 
					
						
							|  |  |  | 		rLocation = minioReservedBucketPath + "/" | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if contains([]string{ | 
					
						
							|  |  |  | 		"/", | 
					
						
							|  |  |  | 		"/webrpc", | 
					
						
							|  |  |  | 		"/login", | 
					
						
							|  |  |  | 		"/favicon.ico", | 
					
						
							|  |  |  | 	}, urlPath) { | 
					
						
							| 
									
										
										
										
											2017-02-17 06:52:14 +08:00
										 |  |  | 		rLocation = minioReservedBucketPath + urlPath | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08: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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return strings.Contains(req.Header.Get("User-Agent"), "Mozilla") | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-02 03:16:54 +08:00
										 |  |  | // guessIsRPCReq - returns true if the request is for an RPC endpoint.
 | 
					
						
							|  |  |  | func guessIsRPCReq(req *http.Request) bool { | 
					
						
							|  |  |  | 	if req == nil { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return req.Method == http.MethodConnect && req.Proto == "HTTP/1.0" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | func (h redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2016-11-28 08:30:46 +08:00
										 |  |  | 	aType := getRequestAuthType(r) | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | 	// Re-direct only for JWT and anonymous requests from browser.
 | 
					
						
							| 
									
										
										
										
											2016-11-28 08:30:46 +08:00
										 |  |  | 	if aType == authTypeJWT || aType == authTypeAnonymous { | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | 		// Re-direction is handled specifically for browser requests.
 | 
					
						
							|  |  |  | 		if guessIsBrowserReq(r) && globalIsBrowserEnabled { | 
					
						
							|  |  |  | 			// 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) | 
					
						
							| 
									
										
										
										
											2016-11-28 08:30:46 +08:00
										 |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-05-01 07:29:03 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-27 18:54:49 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | 	h.handler.ServeHTTP(w, r) | 
					
						
							| 
									
										
										
										
											2015-04-27 18:54:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 05:57:56 +08:00
										 |  |  | // Adds Cache-Control header
 | 
					
						
							|  |  |  | type cacheControlHandler struct { | 
					
						
							|  |  |  | 	handler http.Handler | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | func setBrowserCacheControlHandler(h http.Handler) http.Handler { | 
					
						
							| 
									
										
										
										
											2016-02-05 05:57:56 +08:00
										 |  |  | 	return cacheControlHandler{h} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (h cacheControlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | 	if r.Method == httpGET && guessIsBrowserReq(r) && globalIsBrowserEnabled { | 
					
						
							| 
									
										
										
										
											2016-02-26 21:19:35 +08:00
										 |  |  | 		// For all browser requests set appropriate Cache-Control policies
 | 
					
						
							| 
									
										
										
										
											2017-02-17 06:52:14 +08:00
										 |  |  | 		if hasPrefix(r.URL.Path, minioReservedBucketPath+"/") { | 
					
						
							|  |  |  | 			if hasSuffix(r.URL.Path, ".js") || r.URL.Path == minioReservedBucketPath+"/favicon.ico" { | 
					
						
							| 
									
										
										
										
											2016-12-18 03:00:16 +08: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.
 | 
					
						
							|  |  |  | 				w.Header().Set("Cache-Control", "max-age=31536000") | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// For non asset requests we serve index.html which will never be cached.
 | 
					
						
							|  |  |  | 				w.Header().Set("Cache-Control", "no-store") | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-02-26 21:19:35 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-12-18 03:00:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | 	h.handler.ServeHTTP(w, r) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Adds verification for incoming paths.
 | 
					
						
							| 
									
										
										
										
											2017-09-02 03:16:54 +08:00
										 |  |  | type minioReservedBucketHandler struct { | 
					
						
							| 
									
										
										
										
											2017-03-03 19:01:42 +08:00
										 |  |  | 	handler http.Handler | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-02 03:16:54 +08:00
										 |  |  | func setReservedBucketHandler(h http.Handler) http.Handler { | 
					
						
							|  |  |  | 	return minioReservedBucketHandler{h} | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-02 03:16:54 +08:00
										 |  |  | func (h minioReservedBucketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 	if !guessIsRPCReq(r) && !guessIsBrowserReq(r) { | 
					
						
							|  |  |  | 		// For all non browser, non RPC requests, reject access to 'minioReservedBucketPath'.
 | 
					
						
							|  |  |  | 		bucketName, _ := urlPath2BucketObjectName(r.URL) | 
					
						
							|  |  |  | 		if isMinioReservedBucket(bucketName) || isMinioMetaBucket(bucketName) { | 
					
						
							|  |  |  | 			writeErrorResponse(w, ErrAllAccessDisabled, r.URL) | 
					
						
							|  |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-02-05 05:57:56 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	h.handler.ServeHTTP(w, r) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08: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 11:04:51 +08:00
										 |  |  | // Supported Amz date formats.
 | 
					
						
							|  |  |  | var amzDateFormats = []string{ | 
					
						
							| 
									
										
										
										
											2016-03-08 01:52:20 +08:00
										 |  |  | 	time.RFC1123, | 
					
						
							|  |  |  | 	time.RFC1123Z, | 
					
						
							|  |  |  | 	iso8601Format, | 
					
						
							| 
									
										
										
										
											2016-03-31 11:04:51 +08:00
										 |  |  | 	// Add new AMZ date formats here.
 | 
					
						
							| 
									
										
										
										
											2016-03-08 01:52:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | // Supported Amz date headers.
 | 
					
						
							|  |  |  | var amzDateHeaders = []string{ | 
					
						
							|  |  |  | 	"x-amz-date", | 
					
						
							|  |  |  | 	"date", | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-31 11:04:51 +08: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 16:42:22 +08:00
										 |  |  | 		amzDate, err := time.Parse(dateFormat, amzDateStr) | 
					
						
							|  |  |  | 		if err == nil { | 
					
						
							| 
									
										
										
										
											2016-03-31 11:04:51 +08:00
										 |  |  | 			return amzDate, ErrNone | 
					
						
							| 
									
										
										
										
											2016-03-08 01:52:20 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-31 11:04:51 +08: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 { | 
					
						
							|  |  |  | 		amzDateStr := req.Header.Get(http.CanonicalHeaderKey(amzDateHeader)) | 
					
						
							|  |  |  | 		if amzDateStr != "" { | 
					
						
							|  |  |  | 			return parseAmzDate(amzDateStr) | 
					
						
							| 
									
										
										
										
											2016-03-08 01:52:20 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-31 11:04:51 +08:00
										 |  |  | 	// Date header missing.
 | 
					
						
							|  |  |  | 	return time.Time{}, ErrMissingDateHeader | 
					
						
							| 
									
										
										
										
											2016-03-08 01:52:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 03:58:36 +08:00
										 |  |  | func (h timeValidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | 	aType := getRequestAuthType(r) | 
					
						
							| 
									
										
										
										
											2016-11-14 04:08:24 +08:00
										 |  |  | 	if aType == authTypeSigned || aType == authTypeSignedV2 || aType == authTypeStreamingSigned { | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | 		// Verify if date headers are set, if not reject the request
 | 
					
						
							| 
									
										
										
										
											2016-03-31 11:04:51 +08:00
										 |  |  | 		amzDate, apiErr := parseAmzDateHeader(r) | 
					
						
							|  |  |  | 		if apiErr != ErrNone { | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | 			// All our internal APIs are sensitive towards Date
 | 
					
						
							|  |  |  | 			// header, for all requests where Date header is not
 | 
					
						
							|  |  |  | 			// present we will reject such clients.
 | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 			writeErrorResponse(w, apiErr, r.URL) | 
					
						
							| 
									
										
										
										
											2015-04-30 09:30:17 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-08 02:15:55 +08:00
										 |  |  | 		// Verify if the request date header is shifted by less than globalMaxSkewTime parameter in the past
 | 
					
						
							| 
									
										
										
										
											2016-09-10 06:33:35 +08:00
										 |  |  | 		// or in the future, reject request otherwise.
 | 
					
						
							| 
									
										
										
										
											2017-03-19 02:28:41 +08:00
										 |  |  | 		curTime := UTCNow() | 
					
						
							| 
									
										
										
										
											2016-10-08 02:15:55 +08:00
										 |  |  | 		if curTime.Sub(amzDate) > globalMaxSkewTime || amzDate.Sub(curTime) > globalMaxSkewTime { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 			writeErrorResponse(w, ErrRequestTimeTooSkewed, r.URL) | 
					
						
							| 
									
										
										
										
											2015-04-30 09:30:17 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-27 18:54:49 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-12 08:36:07 +08:00
										 |  |  | 	h.handler.ServeHTTP(w, r) | 
					
						
							| 
									
										
										
										
											2015-04-27 18:54:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 10:50:36 +08:00
										 |  |  | type resourceHandler struct { | 
					
						
							|  |  |  | 	handler http.Handler | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 04:24:34 +08:00
										 |  |  | // List of http methods.
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	httpGET     = "GET" | 
					
						
							|  |  |  | 	httpPUT     = "PUT" | 
					
						
							|  |  |  | 	httpHEAD    = "HEAD" | 
					
						
							|  |  |  | 	httpPOST    = "POST" | 
					
						
							|  |  |  | 	httpDELETE  = "DELETE" | 
					
						
							|  |  |  | 	httpOPTIONS = "OPTIONS" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // List of default allowable HTTP methods.
 | 
					
						
							|  |  |  | var defaultAllowableHTTPMethods = []string{ | 
					
						
							|  |  |  | 	httpGET, | 
					
						
							|  |  |  | 	httpPUT, | 
					
						
							|  |  |  | 	httpHEAD, | 
					
						
							|  |  |  | 	httpPOST, | 
					
						
							|  |  |  | 	httpDELETE, | 
					
						
							|  |  |  | 	httpOPTIONS, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 06:57:20 +08:00
										 |  |  | // setCorsHandler handler for CORS (Cross Origin Resource Sharing)
 | 
					
						
							|  |  |  | func setCorsHandler(h http.Handler) http.Handler { | 
					
						
							| 
									
										
										
										
											2017-09-11 23:15:51 +08:00
										 |  |  | 	commonS3Headers := []string{"Content-Length", "Content-Type", "Connection", | 
					
						
							|  |  |  | 		"Date", "ETag", "Server", "x-amz-delete-marker", "x-amz-id-2", | 
					
						
							|  |  |  | 		"x-amz-request-id", "x-amz-version-id"} | 
					
						
							| 
									
										
										
										
											2015-10-31 02:49:08 +08:00
										 |  |  | 	c := cors.New(cors.Options{ | 
					
						
							| 
									
										
										
										
											2017-04-28 03:40:22 +08:00
										 |  |  | 		AllowedOrigins:   []string{"*"}, | 
					
						
							|  |  |  | 		AllowedMethods:   defaultAllowableHTTPMethods, | 
					
						
							|  |  |  | 		AllowedHeaders:   []string{"*"}, | 
					
						
							| 
									
										
										
										
											2017-09-11 23:15:51 +08:00
										 |  |  | 		ExposedHeaders:   commonS3Headers, | 
					
						
							| 
									
										
										
										
											2017-04-28 03:40:22 +08:00
										 |  |  | 		AllowCredentials: true, | 
					
						
							| 
									
										
										
										
											2015-10-31 02:49:08 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 	return c.Handler(h) | 
					
						
							| 
									
										
										
										
											2015-09-01 05:40:12 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-05 06:57:20 +08:00
										 |  |  | // setIgnoreResourcesHandler -
 | 
					
						
							| 
									
										
										
										
											2015-02-24 08:46:48 +08: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 10:50:36 +08:00
										 |  |  | // valid error message indicating that requested feature is not implemented.
 | 
					
						
							| 
									
										
										
										
											2016-02-05 06:57:20 +08:00
										 |  |  | func setIgnoreResourcesHandler(h http.Handler) http.Handler { | 
					
						
							| 
									
										
										
										
											2015-04-27 18:54:49 +08:00
										 |  |  | 	return resourceHandler{h} | 
					
						
							| 
									
										
										
										
											2015-02-19 04:15:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-30 06:28:04 +08:00
										 |  |  | // Checks requests for not implemented Bucket resources
 | 
					
						
							|  |  |  | func ignoreNotImplementedBucketResources(req *http.Request) bool { | 
					
						
							| 
									
										
										
										
											2016-02-20 08:04:29 +08:00
										 |  |  | 	for name := range req.URL.Query() { | 
					
						
							| 
									
										
										
										
											2015-04-30 06:28:04 +08:00
										 |  |  | 		if notimplementedBucketResourceNames[name] { | 
					
						
							| 
									
										
										
										
											2015-02-11 19:23:15 +08:00
										 |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-30 06:28:04 +08:00
										 |  |  | // Checks requests for not implemented Object resources
 | 
					
						
							|  |  |  | func ignoreNotImplementedObjectResources(req *http.Request) bool { | 
					
						
							| 
									
										
										
										
											2016-02-20 08:04:29 +08:00
										 |  |  | 	for name := range req.URL.Query() { | 
					
						
							| 
									
										
										
										
											2015-04-30 06:28:04 +08:00
										 |  |  | 		if notimplementedObjectResourceNames[name] { | 
					
						
							| 
									
										
										
										
											2015-02-11 19:23:15 +08:00
										 |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-02-20 08:04:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // List of not implemented bucket queries
 | 
					
						
							|  |  |  | var notimplementedBucketResourceNames = map[string]bool{ | 
					
						
							| 
									
										
										
											
												accessPolicy: Implement Put, Get, Delete access policy.
This patch implements Get,Put,Delete bucket policies
Supporting - http://docs.aws.amazon.com/AmazonS3/latest/dev/access-policy-language-overview.html
Currently supports following actions.
   "*":                             true,
   "s3:*":                          true,
   "s3:GetObject":                  true,
   "s3:ListBucket":                 true,
   "s3:PutObject":                  true,
   "s3:CreateBucket":               true,
   "s3:GetBucketLocation":          true,
   "s3:DeleteBucket":               true,
   "s3:DeleteObject":               true,
   "s3:AbortMultipartUpload":       true,
   "s3:ListBucketMultipartUploads": true,
   "s3:ListMultipartUploadParts":   true,
following conditions for "StringEquals" and "StringNotEquals"
   "s3:prefix", "s3:max-keys"
											
										 
											2016-02-04 08:46:56 +08:00
										 |  |  | 	"acl":            true, | 
					
						
							| 
									
										
										
										
											2016-02-20 08:04:29 +08:00
										 |  |  | 	"cors":           true, | 
					
						
							|  |  |  | 	"lifecycle":      true, | 
					
						
							|  |  |  | 	"logging":        true, | 
					
						
							|  |  |  | 	"replication":    true, | 
					
						
							|  |  |  | 	"tagging":        true, | 
					
						
							|  |  |  | 	"versions":       true, | 
					
						
							|  |  |  | 	"requestPayment": true, | 
					
						
							|  |  |  | 	"versioning":     true, | 
					
						
							|  |  |  | 	"website":        true, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // List of not implemented object queries
 | 
					
						
							|  |  |  | var notimplementedObjectResourceNames = map[string]bool{ | 
					
						
							|  |  |  | 	"torrent": true, | 
					
						
							|  |  |  | 	"acl":     true, | 
					
						
							|  |  |  | 	"policy":  true, | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Resource handler ServeHTTP() wrapper
 | 
					
						
							|  |  |  | func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
					
						
							| 
									
										
										
										
											2017-01-11 03:01:23 +08:00
										 |  |  | 	bucketName, objectName := urlPath2BucketObjectName(r.URL) | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// If bucketName is present and not objectName check for bucket level resource queries.
 | 
					
						
							|  |  |  | 	if bucketName != "" && objectName == "" { | 
					
						
							|  |  |  | 		if ignoreNotImplementedBucketResources(r) { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 			writeErrorResponse(w, ErrNotImplemented, r.URL) | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// If bucketName and objectName are present check for its resource queries.
 | 
					
						
							|  |  |  | 	if bucketName != "" && objectName != "" { | 
					
						
							|  |  |  | 		if ignoreNotImplementedObjectResources(r) { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 			writeErrorResponse(w, ErrNotImplemented, r.URL) | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | 			return | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// A put method on path "/" doesn't make sense, ignore it.
 | 
					
						
							| 
									
										
										
										
											2017-02-28 03:40:27 +08:00
										 |  |  | 	if r.Method == httpPUT && r.URL.Path == "/" && r.Header.Get(minioAdminOpHeader) == "" { | 
					
						
							| 
									
										
										
										
											2017-01-06 16:37:00 +08:00
										 |  |  | 		writeErrorResponse(w, ErrNotImplemented, r.URL) | 
					
						
							| 
									
										
										
										
											2016-12-10 16:42:22 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Serve HTTP.
 | 
					
						
							|  |  |  | 	h.handler.ServeHTTP(w, r) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-07 01:29:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // httpResponseRecorder wraps http.ResponseWriter
 | 
					
						
							|  |  |  | // to record some useful http response data.
 | 
					
						
							|  |  |  | type httpResponseRecorder struct { | 
					
						
							|  |  |  | 	http.ResponseWriter | 
					
						
							|  |  |  | 	respStatusCode int | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wraps ResponseWriter's Write()
 | 
					
						
							|  |  |  | func (rww *httpResponseRecorder) Write(b []byte) (int, error) { | 
					
						
							|  |  |  | 	return rww.ResponseWriter.Write(b) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wraps ResponseWriter's Flush()
 | 
					
						
							|  |  |  | func (rww *httpResponseRecorder) Flush() { | 
					
						
							| 
									
										
										
										
											2017-02-07 15:46:33 +08:00
										 |  |  | 	rww.ResponseWriter.(http.Flusher).Flush() | 
					
						
							| 
									
										
										
										
											2017-02-07 01:29:53 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Wraps ResponseWriter's WriteHeader() and record
 | 
					
						
							|  |  |  | // the response status code
 | 
					
						
							|  |  |  | func (rww *httpResponseRecorder) WriteHeader(httpCode int) { | 
					
						
							|  |  |  | 	rww.respStatusCode = httpCode | 
					
						
							|  |  |  | 	rww.ResponseWriter.WriteHeader(httpCode) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 15:46:33 +08:00
										 |  |  | func (rww *httpResponseRecorder) Hijack() (net.Conn, *bufio.ReadWriter, error) { | 
					
						
							|  |  |  | 	return rww.ResponseWriter.(http.Hijacker).Hijack() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 01:29:53 +08: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) { | 
					
						
							|  |  |  | 	// Wraps w to record http response information
 | 
					
						
							|  |  |  | 	ww := &httpResponseRecorder{ResponseWriter: w} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 14:08:33 +08:00
										 |  |  | 	// Time start before the call is about to start.
 | 
					
						
							|  |  |  | 	tBefore := UTCNow() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 01:29:53 +08:00
										 |  |  | 	// Execute the request
 | 
					
						
							|  |  |  | 	h.handler.ServeHTTP(ww, r) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-07 14:08:33 +08:00
										 |  |  | 	// Time after call has completed.
 | 
					
						
							|  |  |  | 	tAfter := UTCNow() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Time duration in secs since the call started.
 | 
					
						
							|  |  |  | 	//
 | 
					
						
							|  |  |  | 	// We don't need to do nanosecond precision in this
 | 
					
						
							|  |  |  | 	// simply for the fact that it is not human readable.
 | 
					
						
							|  |  |  | 	durationSecs := tAfter.Sub(tBefore).Seconds() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 01:29:53 +08:00
										 |  |  | 	// Update http statistics
 | 
					
						
							| 
									
										
										
										
											2017-04-07 14:08:33 +08:00
										 |  |  | 	globalHTTPStats.updateStats(r, ww, durationSecs) | 
					
						
							| 
									
										
										
										
											2017-02-07 01:29:53 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-25 09:13:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // pathValidityHandler validates all the incoming paths for
 | 
					
						
							|  |  |  | // any bad components and rejects them.
 | 
					
						
							|  |  |  | type pathValidityHandler struct { | 
					
						
							|  |  |  | 	handler http.Handler | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setPathValidityHandler(h http.Handler) http.Handler { | 
					
						
							|  |  |  | 	return pathValidityHandler{handler: h} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 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) | 
					
						
							|  |  |  | 	for _, p := range strings.Split(path, slashSeparator) { | 
					
						
							|  |  |  | 		switch strings.TrimSpace(p) { | 
					
						
							|  |  |  | 		case dotdotComponent: | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		case dotComponent: | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (h pathValidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | 
					
						
							|  |  |  | 	// Check for bad components in URL path.
 | 
					
						
							|  |  |  | 	if hasBadPathComponent(r.URL.Path) { | 
					
						
							|  |  |  | 		writeErrorResponse(w, ErrInvalidResourceName, r.URL) | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Check for bad components in URL query values.
 | 
					
						
							|  |  |  | 	for _, vv := range r.URL.Query() { | 
					
						
							|  |  |  | 		for _, v := range vv { | 
					
						
							|  |  |  | 			if hasBadPathComponent(v) { | 
					
						
							|  |  |  | 				writeErrorResponse(w, ErrInvalidResourceName, r.URL) | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	h.handler.ServeHTTP(w, r) | 
					
						
							|  |  |  | } |