fix: panic on Mount() when a router is mounted onto itself (#1159)
* fix: panic on Mount() when a router is mounted onto itself
With() and Group() return a new inline *Mux that shares the same
underlying tree pointer as the original router rather than copying it.
Mounting such a derived mux back onto its origin (e.g.
r.Mount("/", r.Group(fn))) wires a mount whose target handler routes
through the exact same tree the mount itself lives in.
Once a routing context already exists on a request, ServeHTTP calls
mx.handler.ServeHTTP directly, and nextRoutePath() recomputes RoutePath
as "/" + the wildcard capture. For a mount at "/", that capture is the
entire remaining path, so RoutePath ends up identical to before.
Combined with the shared tree, any request that doesn't match a
defined route falls through to the same mount node, calls the same
mountHandler, recomputes the same RoutePath, and re-enters routing
with zero state change - an infinite loop that pins one goroutine per
bad request forever.
Mount() now panics immediately at registration time when the handler
being mounted shares the mounting router's tree, matching the existing
runtime-safety checks already in Mount() for other invalid usage.
Fixes #843.
* Trim comments in Mount() to just the why
Addresses review feedback on #1159 asking for a more concise comment,
matching the existing 1-2 line style used by the other runtime-safety
panics in Mount().
This commit is contained in:
parent
735ae2b87f
commit
aba86186a2
2 changed files with 45 additions and 0 deletions
8
mux.go
8
mux.go
|
|
@ -305,6 +305,14 @@ func (mx *Mux) Mount(pattern string, handler http.Handler) {
|
|||
|
||||
// Assign sub-Router's with the parent not found & method not allowed handler if not specified.
|
||||
subr, ok := handler.(*Mux)
|
||||
|
||||
// Provide runtime safety for ensuring a router isn't mounted onto itself,
|
||||
// which includes an inline mux derived from it via With()/Group() since
|
||||
// that shares the same underlying tree. See issue #843.
|
||||
if ok && subr.tree == mx.tree {
|
||||
panic(fmt.Sprintf("chi: attempting to Mount() a router onto itself on '%s'", pattern))
|
||||
}
|
||||
|
||||
if ok && subr.notFoundHandler == nil && mx.notFoundHandler != nil {
|
||||
subr.NotFound(mx.notFoundHandler)
|
||||
}
|
||||
|
|
|
|||
37
mux_test.go
37
mux_test.go
|
|
@ -1513,6 +1513,43 @@ func TestMountingSimilarPattern(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestMountingSelf ensures Mount() panics instead of wiring a router (or an
|
||||
// inline mux derived from it via With()/Group(), which shares the same
|
||||
// underlying tree) back onto itself. Without this guard, any request that
|
||||
// doesn't match a defined route recurses through the same mount forever,
|
||||
// spinning a goroutine at 100% CPU instead of falling through to a 404.
|
||||
// See https://github.com/go-chi/chi/issues/843.
|
||||
func TestMountingSelf(t *testing.T) {
|
||||
t.Run("direct self-mount", func(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Error("expected panic()")
|
||||
}
|
||||
}()
|
||||
|
||||
r := NewRouter()
|
||||
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("pong"))
|
||||
})
|
||||
r.Mount("/", r)
|
||||
})
|
||||
|
||||
t.Run("mount of inline mux sharing the same tree", func(t *testing.T) {
|
||||
defer func() {
|
||||
if recover() == nil {
|
||||
t.Error("expected panic()")
|
||||
}
|
||||
}()
|
||||
|
||||
r := NewRouter().With(func(next http.Handler) http.Handler { return next })
|
||||
r.Mount("/", r.Group(func(r Router) {
|
||||
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("pong"))
|
||||
})
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
func TestMuxEmptyParams(t *testing.T) {
|
||||
r := NewRouter()
|
||||
r.Get(`/users/{x}/{y}/{z}`, func(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue