diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index a53c5bdb5..a7c431b5c 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -566,6 +566,7 @@ func (MatchPath) matchPatternWithEscapeSequence(escapedPath, matchPath string) b // get the next character from the request path pathCh := string(escapedPath[iPath]) + pathChLen := 1 var escapedPathCh string // normalize (decode) escape sequences @@ -584,7 +585,7 @@ func (MatchPath) matchPatternWithEscapeSequence(escapedPath, matchPath string) b // should be impossible unless EscapedPath() is giving us an invalid sequence! return false } - iPath += 2 // escape sequence is 2 bytes longer than normal char + pathChLen = 3 } // now get the next character from the pattern @@ -597,7 +598,7 @@ func (MatchPath) matchPatternWithEscapeSequence(escapedPath, matchPath string) b // if not a wildcard ("%*"), compare literally; consume next two bytes of pattern if len(matchPath) >= iPattern+3 && matchPath[iPattern+1] != '*' { sb.WriteString(escapedPathCh) - iPath++ + iPath += pathChLen iPattern += 2 break } @@ -635,7 +636,7 @@ func (MatchPath) matchPatternWithEscapeSequence(escapedPath, matchPath string) b iPath += until default: sb.WriteString(pathCh) - iPath++ + iPath += pathChLen } iPattern++ diff --git a/modules/caddyhttp/matchers_test.go b/modules/caddyhttp/matchers_test.go index c0932b549..28c83e214 100644 --- a/modules/caddyhttp/matchers_test.go +++ b/modules/caddyhttp/matchers_test.go @@ -518,6 +518,41 @@ func TestPathMatcher(t *testing.T) { } } +func TestEscapedPathWildcardCursor(t *testing.T) { + for _, tc := range []struct { + pattern, target string + want bool + }{ + {"/foo*bar%2f", "/fooXbar%2f", true}, + {"/foo*bar%2f", "/foobar%2f", true}, + {"/foo*bar%2f", "/foo%41bar%2f", true}, + {"/foo*bar%2f", "/foo%252fbar%2f", true}, + {"/foo*bar%2f", "/foo/bar/", false}, + {"/foo*bar%2f", "/foo/bar%2f", false}, + {"/foo*bar%2f", "/foo%2fbar%2f", false}, + {"/foo*bar%2f", "/foo%2Fbar%2F", false}, + {"/foo%2f*bar%2f", "/foo%2fXbar%2f", true}, + {"/foo%2f*bar%2f", "/foo%2f%2fbar%2f", false}, + {"/foo%2f*", "/foo%2f%41", true}, + {"/foo%2f*", "/foo%2f%2f", false}, + {"/foo%*bar%2f", "/foo%2fbar%2f", true}, + {"/foo%*bar%2f", "/foo/bar%2f", false}, + } { + t.Run(tc.pattern+" "+tc.target, func(t *testing.T) { + m := MatchPath{tc.pattern} + if err := m.Provision(caddy.Context{}); err != nil { + t.Fatal(err) + } + r := httptest.NewRequest(http.MethodGet, tc.target, nil) + NewTestReplacer(r) + got, err := m.MatchWithError(r) + if err != nil || got != tc.want { + t.Errorf("match = %t, %v; want %t", got, err, tc.want) + } + }) + } +} + func TestPathMatcherWindows(t *testing.T) { // only Windows has this bug where it will ignore // trailing dots and spaces in a filename