From 6e3595e35bd6ff12529dc7b732f88bf670147884 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 19 Jul 2023 15:25:11 +0300 Subject: [PATCH] [#174] Fix object keys with slashes in chi Signed-off-by: Denis Kirillov --- api/middleware/reqinfo.go | 19 +++++++------------ api/router.go | 13 ++++++------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/api/middleware/reqinfo.go b/api/middleware/reqinfo.go index c541ad05..4e34b1cf 100644 --- a/api/middleware/reqinfo.go +++ b/api/middleware/reqinfo.go @@ -57,7 +57,6 @@ const HdrAmzRequestID = "x-amz-request-id" const ( BucketURLPrm = "bucket" - ObjectURLPrm = "object" ) var deploymentID = uuid.Must(uuid.NewRandom()) @@ -238,22 +237,18 @@ func AddBucketName(l *zap.Logger) Func { func AddObjectName(l *zap.Logger) Func { return func(h http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - obj := chi.URLParam(r, ObjectURLPrm) + ctx := r.Context() + reqInfo := GetReqInfo(ctx) + + rctx := chi.RouteContext(ctx) + // trim leading slash (always present) + obj := rctx.RoutePath[1:] + object, err := url.PathUnescape(obj) if err != nil { object = obj } - prefix, err := url.QueryUnescape(chi.URLParam(r, "prefix")) - if err != nil { - prefix = chi.URLParam(r, "prefix") - } - if prefix != "" { - object = prefix - } - ctx := r.Context() - - reqInfo := GetReqInfo(ctx) reqInfo.ObjectName = object reqLogger := reqLogOrDefault(ctx, l) diff --git a/api/router.go b/api/router.go index ac183f96..c974cd20 100644 --- a/api/router.go +++ b/api/router.go @@ -91,7 +91,6 @@ type ( func AttachChi(api *chi.Mux, domains []string, throttle middleware.ThrottleOpts, h Handler, center auth.Center, log *zap.Logger, appMetrics *metrics.AppMetrics) { api.Use( - middleware.CleanPath, s3middleware.Request(log), middleware.ThrottleWithOpts(throttle), middleware.Recoverer, @@ -164,7 +163,7 @@ func bucketRouter(h Handler, log *zap.Logger) chi.Router { s3middleware.WrapHandler(h.AppendCORSHeaders), ) - bktRouter.Mount(fmt.Sprintf("/{%s}", s3middleware.ObjectURLPrm), objectRouter(h, log)) + bktRouter.Mount("/", objectRouter(h, log)) bktRouter.Options("/", h.Preflight) @@ -310,11 +309,11 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router { objRouter := chi.NewRouter() objRouter.Use(s3middleware.AddObjectName(l)) - objRouter.Head("/", named("HeadObject", h.HeadObjectHandler)) + objRouter.Head("/*", named("HeadObject", h.HeadObjectHandler)) // GET method handlers objRouter.Group(func(r chi.Router) { - r.Method(http.MethodGet, "/", NewHandlerFilter(). + r.Method(http.MethodGet, "/*", NewHandlerFilter(). Add(NewFilter(). Queries("uploadId"). Handler(named("ListParts", h.ListPartsHandler))). @@ -338,7 +337,7 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router { // PUT method handlers objRouter.Group(func(r chi.Router) { - r.Method(http.MethodPut, "/", NewHandlerFilter(). + r.Method(http.MethodPut, "/*", NewHandlerFilter(). Add(NewFilter(). Headers(AmzCopySource). Queries("partNumber", "uploadId"). @@ -366,7 +365,7 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router { // POST method handlers objRouter.Group(func(r chi.Router) { - r.Method(http.MethodPost, "/", NewHandlerFilter(). + r.Method(http.MethodPost, "/*", NewHandlerFilter(). Add(NewFilter(). Queries("uploadId"). Handler(named("CompleteMultipartUpload", h.CompleteMultipartUploadHandler))). @@ -378,7 +377,7 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router { // DELETE method handlers objRouter.Group(func(r chi.Router) { - r.Method(http.MethodDelete, "/", NewHandlerFilter(). + r.Method(http.MethodDelete, "/*", NewHandlerFilter(). Add(NewFilter(). Queries("uploadId"). Handler(named("AbortMultipartUpload", h.AbortMultipartUploadHandler))).