Skip to content

Commit

Permalink
handler: Process Options before auth
Browse files Browse the repository at this point in the history
Closes #899.

Signed-off-by: Evgenii Baidakov <[email protected]>
  • Loading branch information
smallhive committed Jun 17, 2024
1 parent 0c5bdbe commit aefdf42
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/handler/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func (h *handler) AppendCORSHeaders(w http.ResponseWriter, r *http.Request) {
}

func (h *handler) Preflight(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodOptions {
return
}

reqInfo := api.GetReqInfo(r.Context())
bktInfo, err := h.obj.GetBucketInfo(r.Context(), reqInfo.BucketName)
if err != nil {
Expand All @@ -146,6 +150,7 @@ func (h *handler) Preflight(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get(api.Origin)
if origin == "" {
h.logAndSendError(w, "origin request header needed", reqInfo, s3errors.GetAPIError(s3errors.ErrBadRequest))
return
}

method := r.Header.Get(api.AccessControlRequestMethod)
Expand Down
17 changes: 17 additions & 0 deletions api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ func appendCORS(handler Handler) mux.MiddlewareFunc {
}
}

func appendPreflight(handler Handler) mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler.Preflight(w, r)
if www, ok := w.(*logResponseWriter); ok {
if www.statusCode != 0 {
return
}
}

h.ServeHTTP(w, r)
})
}
}

func logErrorResponse(l *zap.Logger) mux.MiddlewareFunc {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -199,6 +214,8 @@ func Attach(r *mux.Router, domains []string, m MaxClients, h Handler, center aut

// -- logging error requests
logErrorResponse(log),

appendPreflight(h),
)

// Attach user authentication for all S3 routes.
Expand Down

0 comments on commit aefdf42

Please sign in to comment.