Skip to content
Myr

fix(tree): panic in findCaseInsensitivePathRec with RedirectFixedPath (#4535)

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

* fix(tree): panic in findCaseInsensitivePathRec with RedirectFixedPath enabled When RedirectFixedPath is enabled and a request doesn't match any route, findCaseInsensitivePathRec panics with "invalid node type". This happens because it accesses children[0] to find the wildcard child, but addChild() keeps the wildcard child at the end of the array (not the beginning). When a node has both static and wildcard children, children[0] is a static node, so the switch on nType falls through to the default panic case. The fix mirrors what getValue() already does correctly (line 483): use children[len(children)-1] to access the wildcard child. Fixes #2959 * Address review feedback: improve test assertions and prefer static routes in case-insensitive lookup - Assert found=false for non-matching paths instead of only checking for panics - Fix expected casing for case-insensitive static route match (/PREFIX/XXX -> /prefix/xxx) - Update findCaseInsensitivePathRec to try static children before falling back to wildcard child, ensuring static routes win over param routes in case-insensitive matching ---------

authored by Varun Chawla · committed by GitHub · parent fb25834 · Browse files · 2 files+159 added−1 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+159 added−1 removed

  • other:+159 added−1 removed

2 files+159 added−1 removed

modified:tree_test.go+93 added
1016 unmodified lines
1017 t.Fatalf(`"Expected panic "%s" for route '%s', got "%v"`, panicMsgPrefix, route, recv)1018 }1019 }1020}10211022func TestTreeFindCaseInsensitivePathWithMultipleChildrenAndWildcard(t *testing.T) {1023 tree := &node{}10241025 // Setup routes that create a node with both static children and a wildcard child.1026 // This configuration previously caused a panic ("invalid node type") in1027 // findCaseInsensitivePathRec because it accessed children[0] instead of the1028 // wildcard child (which is always at the end of the children array).1029 // See: https://github.com/gin-gonic/gin/issues/29591030 routes := [...]string{1031 "/aa/aa",1032 "/:bb/aa",1033 }10341035 for _, route := range routes {1036 recv := catchPanic(func() {1037 tree.addRoute(route, fakeHandler(route))1038 })1039 if recv != nil {1040 t.Fatalf("panic inserting route '%s': %v", route, recv)1041 }1042 }10431044 // These lookups previously panicked with "invalid node type" because1045 // findCaseInsensitivePathRec picked children[0] (a static node) instead1046 // of the wildcard child at the end of the array.1047 out, found := tree.findCaseInsensitivePath("/aa", true)1048 if found {1049 t.Errorf("Expected no match for '/aa', but got: %s", string(out))1050 }10511052 out, found = tree.findCaseInsensitivePath("/aa/aa/aa/aa", true)1053 if found {1054 t.Errorf("Expected no match for '/aa/aa/aa/aa', but got: %s", string(out))1055 }10561057 // Case-insensitive lookup should match the static route /aa/aa1058 out, found = tree.findCaseInsensitivePath("/AA/AA", true)1059 if !found {1060 t.Error("Route '/AA/AA' not found via case-insensitive lookup")1061 } else if string(out) != "/aa/aa" {1062 t.Errorf("Wrong result for '/AA/AA': expected '/aa/aa', got: %s", string(out))1063 }1064}10651066func TestTreeFindCaseInsensitivePathWildcardParamAndStaticChild(t *testing.T) {1067 tree := &node{}10681069 // Another variant: param route + static route under same prefix1070 routes := [...]string{1071 "/prefix/:id",1072 "/prefix/xxx",1073 }10741075 for _, route := range routes {1076 recv := catchPanic(func() {1077 tree.addRoute(route, fakeHandler(route))1078 })1079 if recv != nil {1080 t.Fatalf("panic inserting route '%s': %v", route, recv)1081 }1082 }10831084 // Should NOT panic even for paths that don't match any route1085 out, found := tree.findCaseInsensitivePath("/prefix/a/b/c", true)1086 if found {1087 t.Errorf("Expected no match for '/prefix/a/b/c', but got: %s", string(out))1088 }10891090 // Exact match should still work1091 out, found = tree.findCaseInsensitivePath("/prefix/xxx", true)1092 if !found {1093 t.Error("Route '/prefix/xxx' not found")1094 } else if string(out) != "/prefix/xxx" {1095 t.Errorf("Wrong result for '/prefix/xxx': %s", string(out))1096 }10971098 // Case-insensitive match should work1099 out, found = tree.findCaseInsensitivePath("/PREFIX/XXX", true)1100 if !found {1101 t.Error("Route '/PREFIX/XXX' not found via case-insensitive lookup")1102 } else if string(out) != "/prefix/xxx" {1103 t.Errorf("Wrong result for '/PREFIX/XXX': expected '/prefix/xxx', got: %s", string(out))1104 }11051106 // Param route should still match1107 out, found = tree.findCaseInsensitivePath("/prefix/something", true)1108 if !found {1109 t.Error("Route '/prefix/something' not found via param match")1110 } else if string(out) != "/prefix/something" {1111 t.Errorf("Wrong result for '/prefix/something': %s", string(out))1112 }1113}
modified:tree.go+66 added-1 removed
816 unmodified lines
817 }818 return nil819 }820821 n = n.children[0]821 // When wildChild is true, try static children first (via indices)822 // before falling back to the wildcard child. This ensures that823 // case-insensitive lookups prefer static routes over param routes824 // (e.g., /PREFIX/XXX should resolve to /prefix/xxx, not match :id).825 if len(n.indices) > 0 {826 rb = shiftNRuneBytes(rb, npLen)827828 if rb[0] != 0 {829 idxc := rb[0]830 for i, c := range []byte(n.indices) {831 if c == idxc {832 if out := n.children[i].findCaseInsensitivePathRec(833 path, ciPath, rb, fixTrailingSlash,834 ); out != nil {835 return out836 }837 break838 }839 }840 } else {841 var rv rune842 var off int843 for max_ := min(npLen, 3); off < max_; off++ {844 if i := npLen - off; utf8.RuneStart(oldPath[i]) {845 rv, _ = utf8.DecodeRuneInString(oldPath[i:])846 break847 }848 }849850 lo := unicode.ToLower(rv)851 utf8.EncodeRune(rb[:], lo)852 rb = shiftNRuneBytes(rb, off)853854 idxc := rb[0]855 for i, c := range []byte(n.indices) {856 if c == idxc {857 if out := n.children[i].findCaseInsensitivePathRec(858 path, ciPath, rb, fixTrailingSlash,859 ); out != nil {860 return out861 }862 break863 }864 }865866 if up := unicode.ToUpper(rv); up != lo {867 utf8.EncodeRune(rb[:], up)868 rb = shiftNRuneBytes(rb, off)869870 idxc := rb[0]871 for i, c := range []byte(n.indices) {872 if c == idxc {873 if out := n.children[i].findCaseInsensitivePathRec(874 path, ciPath, rb, fixTrailingSlash,875 ); out != nil {876 return out877 }878 break879 }880 }881 }882 }883 }884885 // Fall back to wildcard child, which is always at the end of the array886 n = n.children[len(n.children)-1]887 switch n.nType {888 case param:889 // Find param end (either '/' or path end)890 end := 0
60 unmodified lines