[#174] Fix object keys with slashes in chi

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
Denis Kirillov 2023-07-19 15:25:11 +03:00
parent 57add29643
commit 6e3595e35b
2 changed files with 13 additions and 19 deletions

View file

@ -57,7 +57,6 @@ const HdrAmzRequestID = "x-amz-request-id"
const ( const (
BucketURLPrm = "bucket" BucketURLPrm = "bucket"
ObjectURLPrm = "object"
) )
var deploymentID = uuid.Must(uuid.NewRandom()) var deploymentID = uuid.Must(uuid.NewRandom())
@ -238,22 +237,18 @@ func AddBucketName(l *zap.Logger) Func {
func AddObjectName(l *zap.Logger) Func { func AddObjectName(l *zap.Logger) Func {
return func(h http.Handler) http.Handler { return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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) object, err := url.PathUnescape(obj)
if err != nil { if err != nil {
object = obj 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 reqInfo.ObjectName = object
reqLogger := reqLogOrDefault(ctx, l) reqLogger := reqLogOrDefault(ctx, l)

View file

@ -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) { func AttachChi(api *chi.Mux, domains []string, throttle middleware.ThrottleOpts, h Handler, center auth.Center, log *zap.Logger, appMetrics *metrics.AppMetrics) {
api.Use( api.Use(
middleware.CleanPath,
s3middleware.Request(log), s3middleware.Request(log),
middleware.ThrottleWithOpts(throttle), middleware.ThrottleWithOpts(throttle),
middleware.Recoverer, middleware.Recoverer,
@ -164,7 +163,7 @@ func bucketRouter(h Handler, log *zap.Logger) chi.Router {
s3middleware.WrapHandler(h.AppendCORSHeaders), s3middleware.WrapHandler(h.AppendCORSHeaders),
) )
bktRouter.Mount(fmt.Sprintf("/{%s}", s3middleware.ObjectURLPrm), objectRouter(h, log)) bktRouter.Mount("/", objectRouter(h, log))
bktRouter.Options("/", h.Preflight) bktRouter.Options("/", h.Preflight)
@ -310,11 +309,11 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router {
objRouter := chi.NewRouter() objRouter := chi.NewRouter()
objRouter.Use(s3middleware.AddObjectName(l)) objRouter.Use(s3middleware.AddObjectName(l))
objRouter.Head("/", named("HeadObject", h.HeadObjectHandler)) objRouter.Head("/*", named("HeadObject", h.HeadObjectHandler))
// GET method handlers // GET method handlers
objRouter.Group(func(r chi.Router) { objRouter.Group(func(r chi.Router) {
r.Method(http.MethodGet, "/", NewHandlerFilter(). r.Method(http.MethodGet, "/*", NewHandlerFilter().
Add(NewFilter(). Add(NewFilter().
Queries("uploadId"). Queries("uploadId").
Handler(named("ListParts", h.ListPartsHandler))). Handler(named("ListParts", h.ListPartsHandler))).
@ -338,7 +337,7 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router {
// PUT method handlers // PUT method handlers
objRouter.Group(func(r chi.Router) { objRouter.Group(func(r chi.Router) {
r.Method(http.MethodPut, "/", NewHandlerFilter(). r.Method(http.MethodPut, "/*", NewHandlerFilter().
Add(NewFilter(). Add(NewFilter().
Headers(AmzCopySource). Headers(AmzCopySource).
Queries("partNumber", "uploadId"). Queries("partNumber", "uploadId").
@ -366,7 +365,7 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router {
// POST method handlers // POST method handlers
objRouter.Group(func(r chi.Router) { objRouter.Group(func(r chi.Router) {
r.Method(http.MethodPost, "/", NewHandlerFilter(). r.Method(http.MethodPost, "/*", NewHandlerFilter().
Add(NewFilter(). Add(NewFilter().
Queries("uploadId"). Queries("uploadId").
Handler(named("CompleteMultipartUpload", h.CompleteMultipartUploadHandler))). Handler(named("CompleteMultipartUpload", h.CompleteMultipartUploadHandler))).
@ -378,7 +377,7 @@ func objectRouter(h Handler, l *zap.Logger) chi.Router {
// DELETE method handlers // DELETE method handlers
objRouter.Group(func(r chi.Router) { objRouter.Group(func(r chi.Router) {
r.Method(http.MethodDelete, "/", NewHandlerFilter(). r.Method(http.MethodDelete, "/*", NewHandlerFilter().
Add(NewFilter(). Add(NewFilter().
Queries("uploadId"). Queries("uploadId").
Handler(named("AbortMultipartUpload", h.AbortMultipartUploadHandler))). Handler(named("AbortMultipartUpload", h.AbortMultipartUploadHandler))).