Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mux: filter for invalid root path pattern #498

Merged
merged 4 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions mux/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,19 @@ func pathMatch(pattern Route, path string) bool {
return pattern.regexMatcher.regexp.MatchString(path)
}

// FilterPath checks the unfiltered input path or pattern against a blacklist and transforms them into valid paths
// of the same semantic meaning
func FilterPath(unfiltered string) string {
if unfiltered == "" {
return "/"
}
return unfiltered
}

// Find a handler on a handler map given a path string
// Most-specific (longest) pattern wins
func (r *Router) Match(path string, routeParams *RouteParams) (matchedRoute *Route, matchedPattern string) {
path = FilterPath(path)
r.m.RLock()
n := 0
for pattern, route := range r.z {
Expand Down Expand Up @@ -127,10 +137,7 @@ func (r *Router) Match(path string, routeParams *RouteParams) (matchedRoute *Rou

// Handle adds a handler to the Router for pattern.
func (r *Router) Handle(pattern string, handler Handler) error {
switch pattern {
case "", "/":
pattern = "/"
}
pattern = FilterPath(pattern)

if handler == nil {
return errors.New("nil handler")
Expand Down Expand Up @@ -170,10 +177,7 @@ func (r *Router) DefaultHandleFunc(handler func(w ResponseWriter, r *Message)) {

// HandleRemove deregistrars the handler specific for pattern from the Router.
func (r *Router) HandleRemove(pattern string) error {
switch pattern {
case "", "/":
pattern = "/"
}
pattern = FilterPath(pattern)
r.m.Lock()
defer r.m.Unlock()
if _, ok := r.z[pattern]; ok {
Expand All @@ -185,6 +189,7 @@ func (r *Router) HandleRemove(pattern string) error {

// GetRoute obtains route from the pattern it has been assigned
func (r *Router) GetRoute(pattern string) *Route {
pattern = FilterPath(pattern)
r.m.RLock()
defer r.m.RUnlock()
if route, ok := r.z[pattern]; ok {
Expand Down
52 changes: 34 additions & 18 deletions mux/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (

"github.com/plgd-dev/go-coap/v3/mux"
"github.com/stretchr/testify/require"

// TODO: replace with standard maps package as soon as Go dependency hits 1.20
"golang.org/x/exp/maps"
jkralik marked this conversation as resolved.
Show resolved Hide resolved
)

type routeTest struct {
Expand Down Expand Up @@ -78,6 +81,34 @@ func TestMux(t *testing.T) {
pathTemplate: `/{category:a|b/c}`,
shouldMatch: true,
},
{
title: "root path without slash",
vars: map[string]string{},
path: "",
pathTemplate: "",
shouldMatch: true,
},
{
title: "root path with slash",
vars: map[string]string{},
path: "/",
pathTemplate: "/",
shouldMatch: true,
},
{
title: "root path without slash",
vars: map[string]string{},
path: "",
pathTemplate: "/",
shouldMatch: true,
},
{
title: "root path with slash",
vars: map[string]string{},
path: "/",
pathTemplate: "",
shouldMatch: true,
},
}

for _, test := range tests {
Expand Down Expand Up @@ -116,29 +147,14 @@ func testRoute(t *testing.T, router *mux.Router, test routeTest) {
t.Errorf("(%v) %v:\nPath: %#v\nPathTemplate: %#v\nVars: %v\n", test.title, msg, test.path, test.pathTemplate, test.vars)
}
if test.shouldMatch {
if vars != nil && !stringMapEqual(vars, routeParams.Vars) {
if vars != nil && !maps.Equal(vars, routeParams.Vars) {
t.Errorf("(%v) Vars not equal: expected %v, got %v", test.title, vars, routeParams.Vars)
return
}

if routeParams.PathTemplate != test.pathTemplate {
t.Errorf("(%v) PathTemplate not equal: expected %v, got %v", test.title, test.pathTemplate, test.pathTemplate)
if routeParams.PathTemplate != mux.FilterPath(test.pathTemplate) {
t.Errorf("(%v) PathTemplate not equal: expected %v, got %v", test.title, test.pathTemplate, routeParams.PathTemplate)
return
}
}
}

// stringMapEqual checks the equality of two string maps
func stringMapEqual(m1, m2 map[string]string) bool {
nil1 := m1 == nil
nil2 := m2 == nil
if nil1 != nil2 || len(m1) != len(m2) {
return false
}
for k, v := range m1 {
if v != m2[k] {
return false
}
}
return true
}
Loading