From ce960565b1b4503b2c18647a2785f92a396c5984 Mon Sep 17 00:00:00 2001 From: Krishna Srinivas <634494+krishnasrinivas@users.noreply.github.com> Date: Tue, 19 Feb 2019 21:02:41 -0800 Subject: [PATCH] Validate and reject unusual requests (#7258) --- cmd/generic-handlers.go | 27 +++++++++++++++++++++------ cmd/routers.go | 4 ++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/cmd/generic-handlers.go b/cmd/generic-handlers.go index d1e0cdf70..0a44c30eb 100644 --- a/cmd/generic-handlers.go +++ b/cmd/generic-handlers.go @@ -550,14 +550,14 @@ func (h httpStatsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { globalHTTPStats.updateStats(r, ww, durationSecs) } -// pathValidityHandler validates all the incoming paths for -// any bad components and rejects them. -type pathValidityHandler struct { +// requestValidityHandler validates all the incoming paths for +// any malicious requests. +type requestValidityHandler struct { handler http.Handler } -func setPathValidityHandler(h http.Handler) http.Handler { - return pathValidityHandler{handler: h} +func setRequestValidityHandler(h http.Handler) http.Handler { + return requestValidityHandler{handler: h} } // Bad path components to be rejected by the path validity handler. @@ -581,7 +581,18 @@ func hasBadPathComponent(path string) bool { return false } -func (h pathValidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +// 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) { // Check for bad components in URL path. if hasBadPathComponent(r.URL.Path) { writeErrorResponse(context.Background(), w, errorCodes.ToAPIErr(ErrInvalidResourceName), r.URL, guessIsBrowserReq(r)) @@ -596,6 +607,10 @@ func (h pathValidityHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } } + if hasMultipleAuth(r) { + writeErrorResponse(context.Background(), w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL, guessIsBrowserReq(r)) + return + } h.handler.ServeHTTP(w, r) } diff --git a/cmd/routers.go b/cmd/routers.go index 877272401..7952992ce 100644 --- a/cmd/routers.go +++ b/cmd/routers.go @@ -55,8 +55,8 @@ var globalHandlers = []HandlerFunc{ setBucketForwardingHandler, // Ratelimit the incoming requests using a token bucket algorithm setRateLimitHandler, - // Validate all the incoming paths. - setPathValidityHandler, + // Validate all the incoming requests. + setRequestValidityHandler, // Network statistics setHTTPStatsHandler, // Limits all requests size to a maximum fixed limit