From c3fab6576f0ef5f5bd53de589c8b0e2a8d622f8a Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 10 Nov 2026 13:12:13 -0700 Subject: [PATCH] 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> --- modules/caddyhttp/matchers.go | 7 +++--- modules/caddyhttp/matchers_test.go | 35 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) 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