caddyhttp: keep escaped path cursor at wildcard span start

When an escape sequence was followed by a wildcard in the pattern, the
path cursor had already skipped to the last hex digit, so an encoded
slash could be swallowed into the wildcard span. Advance the cursor by
the escape width only when the character is consumed.

Addresses scan report 0e63a0fe7fea5ef7.

Code written by GitHub Copilot (GPT-6.1 Sol, Claude Opus 5.5); reviewed by mholt.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Matthew Holt 2026-11-10 13:12:13 -07:00
parent 3c65959560
commit c3fab6576f
2 changed files with 39 additions and 3 deletions

View file

@ -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++

View file

@ -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