fix(web): strip trailing slash so /post/foo/ matches /post/foo
Go ServeMux does exact-match routing; a trailing slash caused dynamic
routes like /post/{slug} to fall through to the 404 catch-all. Add
withStripTrailingSlash middleware as the outermost handler wrapper so
all downstream middleware (RBAC, tenant, mux) see the normalized path.
Tested: static routes, dynamic routes, root path, query-string preservation.
This commit is contained in:
1 parent
3ae8f2c843
commit
9ede4bc63d
2 files changed
+78
No files matched your search
@@ -247,6 +247,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
handler := withMiddleware(mux, mw)
|
||||
handler = withRBAC(handler, authCfg.gates, authCfg.loginPath)
|
||||
handler = withTenant(handler, st.tenant)
|
||||
handler = withStripTrailingSlash(handler)
|
||||
return handler, nil
|
||||
}
|
||||
|
||||
@@ -641,6 +642,21 @@ func withTenant(next http.Handler, cfg tenant.Config) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
// withStripTrailingSlash rewrites request URLs so that paths like /post/foo/
|
||||
// are routed as /post/foo. The root path "/" is left unchanged.
|
||||
func withStripTrailingSlash(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
path := r.URL.Path
|
||||
if len(path) > 1 && path[len(path)-1] == '/' {
|
||||
r.URL.Path = strings.TrimRight(path, "/")
|
||||
if r.URL.Path == "" {
|
||||
r.URL.Path = "/"
|
||||
}
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func withMiddleware(next http.Handler, mw middleware.Config) http.Handler {
|
||||
h := next
|
||||
if mw.BodyLimit != nil && *mw.BodyLimit > 0 {
|
||||
|
||||
@@ -152,3 +152,65 @@ func TestNewHandlerIconsRedirectSitemap404(t *testing.T) {
|
||||
t.Fatalf("home missing icon link: %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrailingSlashNormalization(t *testing.T) {
|
||||
page := map[string]any{"title": "Home", "intro": "<p>home</p>"}
|
||||
a := app.New(map[string]any{"page": page})
|
||||
about := map[string]any{"title": "About", "intro": "<h1>About</h1>", "_route": "/about"}
|
||||
var err error
|
||||
a, err = app.Route(a, "/about", about)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Dynamic route: /post/{slug}
|
||||
post := map[string]any{"title": "Post", "intro": "<h1>Post</h1>", "_route": "/post/{slug}"}
|
||||
a, err = app.Route(a, "/post/{slug}", post)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
h, err := httpx.NewHandler(a, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Static route: /about should work with and without trailing slash.
|
||||
rec := httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
|
||||
t.Fatalf("about (no slash) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about/", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
|
||||
t.Fatalf("about (trailing slash) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Dynamic route: /post/hello should work with and without trailing slash.
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/post/hello", nil))
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("post/hello (no slash) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/post/hello/", nil))
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("post/hello (trailing slash) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Root path "/" should still work.
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "Home") {
|
||||
t.Fatalf("root code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
// Query string should be preserved after trailing slash strip.
|
||||
rec = httptest.NewRecorder()
|
||||
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/about/?foo=bar", nil))
|
||||
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "About") {
|
||||
t.Fatalf("about (trailing slash + query) code=%d body=%q", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user