From 50d7797538342bf8365c7cbc3dc4356eecb19165 Mon Sep 17 00:00:00 2001 From: "Philip K. Warren" Date: Tue, 3 Dec 2024 09:24:49 -0600 Subject: [PATCH] Preserve path encoded subjects in middleware (#1526) * Preserve path encoded subjects in middleware Update basePathMiddleware to use `RawPath` (if present) over `Path` to ensure that URL path encoded subjects are preserved when routing requests. Update the middleware to also remove the prefix from `RawPath` for consistency. Fixes #1525. * Fix lint failure --- backend/pkg/api/middlewares.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/backend/pkg/api/middlewares.go b/backend/pkg/api/middlewares.go index 60a654255..3c2699f7f 100644 --- a/backend/pkg/api/middlewares.go +++ b/backend/pkg/api/middlewares.go @@ -75,9 +75,12 @@ func (b *basePathMiddleware) Wrap(next http.Handler) http.Handler { // Strip prefix from the request url var path string rctx := chi.RouteContext(r.Context()) - if rctx.RoutePath != "" { + switch { + case rctx.RoutePath != "": path = rctx.RoutePath - } else { + case r.URL.RawPath != "": + path = r.URL.RawPath + default: path = r.URL.Path } @@ -91,6 +94,11 @@ func (b *basePathMiddleware) Wrap(next http.Handler) http.Handler { r.URL.Path = "/" + strings.TrimPrefix(r.URL.Path, prefix) } + // URL.RawPath + if strings.HasPrefix(r.URL.RawPath, prefix) { + r.URL.RawPath = "/" + strings.TrimPrefix(r.URL.RawPath, prefix) + } + // requestURI if strings.HasPrefix(r.RequestURI, prefix) { r.RequestURI = "/" + strings.TrimPrefix(r.RequestURI, prefix)