Skip to content

Commit eadbef2

Browse files
authored
fix(router): keep escaped colon and parameter routes reachable (#3113)
Distinguish parameter markers without reserving a path byte; cover both route orders through ServeHTTP. Refs #3111; v4 backport in #3132.
1 parent b59b347 commit eadbef2

2 files changed

Lines changed: 127 additions & 14 deletions

File tree

‎router.go‎

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,9 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
536536
}
537537

538538
paramNames := make([]string, 0)
539+
// Positions of parameter markers after names are removed. Literal colons
540+
// remain ordinary path bytes, so no sentinel byte is reserved.
541+
paramMarkers := make([]int, 0)
539542
originalPath := path
540543
wasAdded := false
541544
var ri RouteInfo
@@ -549,11 +552,12 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
549552
}
550553
j := i + 1
551554

552-
r.insert(staticKind, path[:i], method, routeMethod{RouteInfo: &RouteInfo{Method: method}})
555+
r.insert(staticKind, path[:i], method, routeMethod{RouteInfo: &RouteInfo{Method: method}}, paramMarkers)
553556
for ; i < lcpIndex && path[i] != '/'; i++ {
554557
}
555558

556559
paramNames = append(paramNames, path[j:i])
560+
paramMarkers = append(paramMarkers, j-1)
557561
path = path[:j] + path[i:]
558562
i, lcpIndex = j, len(path)
559563

@@ -566,14 +570,14 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
566570
orgRouteInfo: ri,
567571
wrappedHeadHandler: headH,
568572
}
569-
r.insert(paramKind, path[:i], method, rm)
573+
r.insert(paramKind, path[:i], method, rm, paramMarkers)
570574
wasAdded = true
571575
break
572576
} else {
573-
r.insert(paramKind, path[:i], method, routeMethod{RouteInfo: &RouteInfo{Method: method}})
577+
r.insert(paramKind, path[:i], method, routeMethod{RouteInfo: &RouteInfo{Method: method}}, paramMarkers)
574578
}
575579
} else if path[i] == anyLabel {
576-
r.insert(staticKind, path[:i], method, routeMethod{RouteInfo: &RouteInfo{Method: method}})
580+
r.insert(staticKind, path[:i], method, routeMethod{RouteInfo: &RouteInfo{Method: method}}, paramMarkers)
577581
paramNames = append(paramNames, "*")
578582
ri = route.ToRouteInfo(paramNames)
579583
rm := routeMethod{
@@ -582,7 +586,7 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
582586
orgRouteInfo: ri,
583587
wrappedHeadHandler: headH,
584588
}
585-
r.insert(anyKind, path[:i+1], method, rm)
589+
r.insert(anyKind, path[:i+1], method, rm, paramMarkers)
586590
wasAdded = true
587591
break
588592
}
@@ -596,7 +600,7 @@ func (r *DefaultRouter) Add(route Route) (RouteInfo, error) {
596600
orgRouteInfo: ri,
597601
wrappedHeadHandler: headH,
598602
}
599-
r.insert(staticKind, path, method, rm)
603+
r.insert(staticKind, path, method, rm, paramMarkers)
600604
}
601605

602606
r.storeRouteInfo(ri)
@@ -623,12 +627,13 @@ func (r *DefaultRouter) storeRouteInfo(ri RouteInfo) {
623627
r.routes = append(r.routes, ri)
624628
}
625629

626-
func (r *DefaultRouter) insert(t kind, path string, method string, ri routeMethod) {
630+
func (r *DefaultRouter) insert(t kind, path string, method string, ri routeMethod, paramMarkers []int) {
627631
if len(ri.Parameters) > r.maxPathParamsLength {
628632
r.maxPathParamsLength = len(ri.Parameters)
629633
}
630634
currentNode := r.tree // Current node as root
631635
search := path
636+
searchOffset := 0
632637

633638
for {
634639
searchLen := len(search)
@@ -717,8 +722,16 @@ func (r *DefaultRouter) insert(t kind, path string, method string, ri routeMetho
717722
}
718723
currentNode.refreshLeaf()
719724
} else if lcpLen < searchLen {
725+
searchOffset += lcpLen
720726
search = search[lcpLen:]
721-
c := currentNode.findChildWithLabel(search[0])
727+
isParamMarker := false
728+
for _, marker := range paramMarkers {
729+
if marker == searchOffset {
730+
isParamMarker = true
731+
break
732+
}
733+
}
734+
c := currentNode.findChildWithLabel(search[0], isParamMarker)
722735
if c != nil {
723736
// Go deeper
724737
currentNode = c
@@ -810,13 +823,13 @@ func (n *node) findStaticChild(l byte) *node {
810823
return nil
811824
}
812825

813-
func (n *node) findChildWithLabel(l byte) *node {
826+
func (n *node) findChildWithLabel(l byte, isParamMarker bool) *node {
827+
if isParamMarker {
828+
return n.paramChild
829+
}
814830
if c := n.findStaticChild(l); c != nil {
815831
return c
816832
}
817-
if l == paramLabel {
818-
return n.paramChild
819-
}
820833
if l == anyLabel {
821834
return n.anyChild
822835
}
@@ -937,8 +950,8 @@ func (r *DefaultRouter) Route(c *Context) HandlerFunc {
937950
searchIndex -= len(previous.prefix)
938951
} else {
939952
paramIndex--
940-
// for param/any node.prefix value is always `:` so we can not deduce searchIndex from that and must use pValue
941-
// for that index as it would also contain part of path we cut off before moving into node we are backtracking from
953+
// param/any node prefixes are a single marker byte, so restore searchIndex
954+
// from the value stored for that param instead
942955
searchIndex -= len(pathValues[paramIndex].Value)
943956
pathValues[paramIndex].Value = ""
944957
}

‎router_test.go‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,106 @@ func TestRouterParamStaticConflict(t *testing.T) {
13201320
}
13211321
}
13221322

1323+
// Issue #3111
1324+
func TestRouterParam_escapeColonAndParamConflict(t *testing.T) {
1325+
var testCases = []struct {
1326+
name string
1327+
routes []string
1328+
whenURL string
1329+
expectRoute string
1330+
expectParam map[string]string
1331+
}{
1332+
{
1333+
name: "escaped colon route first, request escaped colon route",
1334+
routes: []string{`/name\:verb/x`, `/name:id`},
1335+
whenURL: "/name:verb/x",
1336+
expectRoute: `/name\:verb/x`,
1337+
expectParam: map[string]string{},
1338+
},
1339+
{
1340+
name: "escaped colon route first, request param route",
1341+
routes: []string{`/name\:verb/x`, `/name:id`},
1342+
whenURL: "/name1",
1343+
expectRoute: "/name:id",
1344+
expectParam: map[string]string{"id": "1"},
1345+
},
1346+
{
1347+
name: "param route first, request escaped colon route",
1348+
routes: []string{`/name:id`, `/name\:verb/x`},
1349+
whenURL: "/name:verb/x",
1350+
expectRoute: `/name\:verb/x`,
1351+
expectParam: map[string]string{},
1352+
},
1353+
{
1354+
name: "param route first, request param route",
1355+
routes: []string{`/name:id`, `/name\:verb/x`},
1356+
whenURL: "/name1",
1357+
expectRoute: "/name:id",
1358+
expectParam: map[string]string{"id": "1"},
1359+
},
1360+
}
1361+
for _, tc := range testCases {
1362+
t.Run(tc.name, func(t *testing.T) {
1363+
e := New()
1364+
for _, route := range tc.routes {
1365+
e.GET(route, handlerFunc)
1366+
}
1367+
1368+
c := e.NewContext(httptest.NewRequest(http.MethodGet, tc.whenURL, nil), nil)
1369+
1370+
handler := e.router.Route(c)
1371+
1372+
assert.NoError(t, handler(c))
1373+
assert.Equal(t, tc.expectRoute, c.Path())
1374+
for param, expectedValue := range tc.expectParam {
1375+
assert.Equal(t, expectedValue, c.pathValues.GetOr(param, "---none---"))
1376+
}
1377+
checkUnusedParamValues(t, c, tc.expectParam)
1378+
})
1379+
}
1380+
}
1381+
1382+
func TestRouterParamLiteralByteConflictServeHTTP(t *testing.T) {
1383+
tests := []struct {
1384+
name, literalRoute, literalRequest string
1385+
}{
1386+
{"escaped colon", `/name\:verb/x`, "/name:verb/x"},
1387+
{"encoded NUL", "/name\x00verb/x", "/name%00verb/x"},
1388+
}
1389+
for _, tc := range tests {
1390+
for _, literalFirst := range []bool{true, false} {
1391+
name := tc.name + "/parameter-first"
1392+
routes := []string{"/name:id", tc.literalRoute}
1393+
if literalFirst {
1394+
name = tc.name + "/literal-first"
1395+
routes[0], routes[1] = routes[1], routes[0]
1396+
}
1397+
t.Run(name, func(t *testing.T) {
1398+
e := New()
1399+
for _, route := range routes {
1400+
e.GET(route, func(c *Context) error {
1401+
return c.String(http.StatusOK, c.RouteInfo().Path)
1402+
})
1403+
}
1404+
for _, request := range []struct{ path, want string }{
1405+
{tc.literalRequest, tc.literalRoute},
1406+
{"/name1", "/name:id"},
1407+
} {
1408+
t.Run(request.path, func(t *testing.T) {
1409+
rec := httptest.NewRecorder()
1410+
req := httptest.NewRequest(http.MethodGet, request.path, nil)
1411+
if !assert.NotPanics(t, func() { e.ServeHTTP(rec, req) }) {
1412+
return
1413+
}
1414+
assert.Equal(t, http.StatusOK, rec.Code)
1415+
assert.Equal(t, request.want, rec.Body.String())
1416+
})
1417+
}
1418+
})
1419+
}
1420+
}
1421+
}
1422+
13231423
func TestRouterParam_escapeColon(t *testing.T) {
13241424
// to allow Google cloud API like route paths with colon in them
13251425
// i.e. https://service.name/v1/some/resource/name:customVerb <- that `:customVerb` is not path param. It is just a string

0 commit comments

Comments
 (0)