Skip to content

Commit 854468a

Browse files
authored
Return parseable error on S3 requests (#1120)
Return S3 compatible error when an S3 API request is made to the console port. Before: ``` λ mc ls local9090 mc: <ERROR> Unable to list folder. XML syntax error on line 1: invalid character entity &display (no semicolon) ``` After: ``` λ mc ls local9090 mc: <ERROR> Unable to list folder. S3 API Request made to Console port. S3 Requests should be sent to MinIO API port. ```
1 parent 1d69024 commit 854468a

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

restapi/configure_console.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,28 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler {
186186
IsDevelopment: false,
187187
}
188188
secureMiddleware := secure.New(secureOptions)
189-
return secureMiddleware.Handler(next)
189+
return RejectS3Middleware(secureMiddleware.Handler(next))
190+
}
191+
192+
// RejectS3Middleware will reject requests that have AWS S3 specific headers.
193+
func RejectS3Middleware(next http.Handler) http.Handler {
194+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
195+
if len(r.Header.Get("X-Amz-Content-Sha256")) > 0 ||
196+
len(r.Header.Get("X-Amz-Date")) > 0 ||
197+
strings.HasPrefix(r.Header.Get("Authorization"), "AWS4-HMAC-SHA256") ||
198+
r.URL.Query().Get("AWSAccessKeyId") != "" {
199+
w.WriteHeader(http.StatusForbidden)
200+
w.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
201+
<Error>
202+
<Code>AccessDenied</Code>
203+
<Message>S3 API Request made to Console port. S3 Requests should be sent to API port.</Message>
204+
<RequestId>0</RequestId>
205+
</Error>
206+
`))
207+
return
208+
}
209+
next.ServeHTTP(w, r)
210+
})
190211
}
191212

192213
func AuthenticationMiddleware(next http.Handler) http.Handler {

0 commit comments

Comments
 (0)