Skip to content

Commit

Permalink
Preserve path encoded subjects in middleware (#1526)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pkwarren authored Dec 3, 2024
1 parent e61ea17 commit 50d7797
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions backend/pkg/api/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down

0 comments on commit 50d7797

Please sign in to comment.