Skip to content
Myr

fix: reset skipped-nodes stack on getValue entry to prevent slice overflow panic [#4818] (#4819)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix: reset skipped-nodes stack on getValue entry to prevent slice overflow panic [#4818] * test: add regression test for getValue skipped-nodes overflow panic [#4818] * test(gin): consolidate routing regression tests - Move skipped-node regression cases into the existing routing test suite without changing behavior. ---------

authored by water · committed by GitHub · parent d8f2d58 · Browse files · 2 files+105 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+105 added−0 removed

  • other:+105 added−0 removed

2 files+105 added−0 removed

modified:routes_test.go+97 added
842 unmodified lines
843844 w := PerformRequest(r, http.MethodGet, "/base/v1/user/groups")845 assert.Equal(t, http.StatusNotFound, w.Code)846}847848// TestIssue4818_skippedNodesOverflow_Panic reproduces the panic from issue #4818:849// with HandleMethodNotAllowed enabled, getValue is called once per method tree850// reusing the same c.skippedNodes stack without resetting it, so residue851// accumulates until the reslice exceeds the capacity (engine.maxSections).852// The request must not panic and must fall through to the normal 404 path853// (the path genuinely matches no route, so 405 is not applicable here).854func TestIssue4818_skippedNodesOverflow_Panic(t *testing.T) {855 router := New()856 router.HandleMethodNotAllowed = true857858 h := func(c *Context) {}859 router.OPTIONS("/:p0/:p1/a/:p2", h)860 router.GET("/:p0/:p1/a/:p2", h)861 router.PATCH("/b/:p0/:p1/c", h)862 router.DELETE("/b/:p0/:p1/d/:p3", h)863 router.GET("/b/:p0/:p1/e/f", h)864 router.POST("/b/:p0/:p1/g/:p4/h", h)865 router.OPTIONS("/b/:p0/:p1/g/:p4/h", h)866 router.DELETE("/b/cache", h)867 router.GET("/b/clients/:p1/g", h)868 router.POST("/b/clients/:p1/g", h)869 router.PATCH("/b/clients/:p1/g/:p4", h)870 router.OPTIONS("/b/clients/:p1/g/:p4", h)871872 req := httptest.NewRequest(http.MethodPost, "/b/clients/42", nil)873 w := httptest.NewRecorder()874 router.ServeHTTP(w, req)875876 // Must not panic; path matches no route, so 404 is the correct outcome.877 if w.Code != http.StatusNotFound {878 t.Fatalf("expected 404, got %d (a panic would have aborted the test)", w.Code)879 }880}881882// TestIssue4818_MethodNotAllowedStillWorks verifies the HandleMethodNotAllowed883// loop still produces a correct 405 with an Allow header after the fix, i.e.884// the reset does not break the loop across multiple method trees.885func TestIssue4818_MethodNotAllowedStillWorks(t *testing.T) {886 router := New()887 router.HandleMethodNotAllowed = true888889 h := func(c *Context) {}890 router.OPTIONS("/:p0/:p1/a/:p2", h)891 router.GET("/:p0/:p1/a/:p2", h)892 router.PATCH("/b/:p0/:p1/c", h)893 router.DELETE("/b/:p0/:p1/d/:p3", h)894 router.GET("/b/:p0/:p1/e/f", h)895 router.POST("/b/:p0/:p1/g/:p4/h", h)896 router.OPTIONS("/b/:p0/:p1/g/:p4/h", h)897 router.DELETE("/b/cache", h)898 router.GET("/b/clients/:p1/g", h)899 router.POST("/b/clients/:p1/g", h)900 router.PATCH("/b/clients/:p1/g/:p4", h)901 router.OPTIONS("/b/clients/:p1/g/:p4", h)902903 // PATCH /b/clients/42/g is registered under GET but not under PATCH904 // -> HandleMethodNotAllowed should yield 405 with an Allow header.905 req := httptest.NewRequest(http.MethodPatch, "/b/clients/42/g", nil)906 w := httptest.NewRecorder()907 router.ServeHTTP(w, req)908909 if w.Code != http.StatusMethodNotAllowed {910 t.Fatalf("expected 405, got %d", w.Code)911 }912 allow := w.Header().Get("Allow")913 if allow == "" {914 t.Fatal("expected Allow header to be set for 405")915 }916}917918// TestIssue4818_MethodNotAllowedManyTrees stresses the loop across many919// method trees to make sure the skipped-nodes accumulator can never overflow.920func TestIssue4818_MethodNotAllowedManyTrees(t *testing.T) {921 router := New()922 router.HandleMethodNotAllowed = true923924 h := func(c *Context) {}925 for _, m := range []string{926 http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete,927 http.MethodPatch, http.MethodOptions, http.MethodHead, http.MethodConnect,928 http.MethodTrace,929 } {930 router.Handle(m, "/:p0/:p1/a/:p2", h)931 router.Handle(m, "/s/:p0/:p1/:p2/:p3/:p4/:p5/d", h)932 }933 // GET route so that a POST to a GET-only path yields 405934 router.GET("/only-get", h)935936 req := httptest.NewRequest(http.MethodPost, "/only-get", nil)937 w := httptest.NewRecorder()938 router.ServeHTTP(w, req)939940 if w.Code != http.StatusMethodNotAllowed {941 t.Fatalf("expected 405, got %d", w.Code)942 }943}
modified:tree.go+8 added
416 unmodified lines
417// given path.418func (n *node) getValue(path string, params *Params, skippedNodes *[]skippedNode, unescape bool) (value nodeValue) {419 var globalParamsCount int16420421 // Reset the skipped-nodes stack on entry. getValue is called once per422 // method tree (e.g. in the HandleMethodNotAllowed loop) reusing the same423 // pooled Context stack, and the walk below grows it via a raw reslice that424 // cannot exceed engine.maxSections. Without this reset the residue from a425 // previous tree leaks into the next call and can panic with "slice bounds426 // out of range" once the accumulated length passes the capacity.427 *skippedNodes = (*skippedNodes)[:0]428429walk: // Outer loop for walking the tree430 for {431 prefix := n.path432 if len(path) > len(prefix) {
526 unmodified lines