Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't strip trailing slash if it's escaped in StripSlashes middlware #790

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions middleware/strip.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"fmt"
"net/http"
"strings"

"github.com/go-chi/chi/v5"
)
Expand All @@ -12,20 +13,19 @@ import (
// matches, then it will serve the handler.
func StripSlashes(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
var path string
rctx := chi.RouteContext(r.Context())
if rctx != nil && rctx.RoutePath != "" {
path = rctx.RoutePath
} else {
path = r.URL.Path
}
if len(path) > 1 && path[len(path)-1] == '/' {
newPath := path[:len(path)-1]
if rctx == nil {
r.URL.Path = newPath
} else {
rctx.RoutePath = newPath

if rctx != nil && len(rctx.RoutePath) > 1 {
rctx.RoutePath = strings.TrimSuffix(rctx.RoutePath, "/")
} else if len(r.URL.RawPath) > 1 {
if r.URL.RawPath[len(r.URL.RawPath)-1] == '/' {
// Always update RawPath and Path in sync to make sure
// there are no unexpected mismatches
r.URL.RawPath = strings.TrimSuffix(r.URL.RawPath, "/")
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
}
} else if len(r.URL.Path) > 1 {
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
}
next.ServeHTTP(w, r)
}
Expand Down
12 changes: 12 additions & 0 deletions middleware/strip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ func TestStripSlashes(t *testing.T) {
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" {
t.Fatalf(resp)
}
if _, resp := testRequest(t, ts, "GET", "/accounts/escaped%2F", nil); resp != "escaped%2F" {
t.Fatalf(resp)
}
if _, resp := testRequest(t, ts, "GET", "/accounts/escaped%2F/", nil); resp != "escaped%2F" {
t.Fatalf(resp)
}
if _, resp := testRequest(t, ts, "GET", "/nothing-here", nil); resp != "nothing here" {
t.Fatalf(resp)
}
if _, resp := testRequest(t, ts, "GET", "/%2F", nil); resp != "nothing here" {
t.Fatalf(resp)
}
}

func TestStripSlashesInRoute(t *testing.T) {
Expand Down Expand Up @@ -97,6 +106,9 @@ func TestStripSlashesInRoute(t *testing.T) {
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query/", nil); resp != "admin" {
t.Fatalf(resp)
}
if _, resp := testRequest(t, ts, "GET", "/accounts/admin/query%2F", nil); resp != "nothing here" {
t.Fatalf(resp)
}
}

func TestRedirectSlashes(t *testing.T) {
Expand Down