rewrite: escape regexp captures in rewritten paths

The rewrite handler percent-decodes the path after expanding placeholders.
Regexp captures, usually taken from the already-decoded request path by
path_regexp, were inserted verbatim, so a double-encoded "../" or "?"
became a real separator or query string after path guards had run.

{http.regexp.*} values are now escaped like the other path-derived
placeholders (slashes preserved) when used in the path part of a rewrite.
Captures intentionally used to inject "?" into the path from a header or
other source must now set the query explicitly.

Based on a patch by Adel Assakaf.
Reviewed by mholt.

Code written by GitHub Copilot (Claude Opus 5.5), adapting the
contributor's patch.

Co-authored-by: Adel Assakaf <212399165+assakafpix@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Matthew Holt 2026-11-11 12:15:55 -07:00
parent f4c634f0ed
commit 7591cff1fb
No known key found for this signature in database
2 changed files with 40 additions and 4 deletions

View file

@ -70,9 +70,11 @@ type Rewrite struct {
//
// Decoded path placeholders, such as {http.request.orig_uri.path},
// {http.request.uri.path.*}, and the file matcher's relative and
// remainder paths, are percent-encoded (except for slashes) when
// used before the "?", so characters like "?" or "%" from a request
// path stay part of the new path instead of starting a query string.
// remainder paths, as well as regular expression captures
// ({http.regexp.*}, from any matcher), are percent-encoded (except
// for slashes) when used before the "?", so characters like "?" or
// "%" from a request stay part of the new path instead of starting a
// query string or forming an escape sequence.
// Placeholders ending in "_escaped" are inserted unchanged.
//
// If the path changes, the result is made absolute and its dot
@ -368,7 +370,10 @@ func replacePathPlaceholders(path string, r *http.Request, repl *caddy.Replacer)
strings.HasPrefix(name, "http.request.uri.path.") ||
strings.HasPrefix(name, "http.request.orig_uri.path.") ||
name == "http.matchers.file.relative" ||
name == "http.matchers.file.remainder"
name == "http.matchers.file.remainder" ||
// regexp captures usually come from the decoded path (path_regexp),
// and their names don't say which value they were captured from
strings.HasPrefix(name, "http.regexp.")
if decodedPath && !strings.HasSuffix(name, "_escaped") {
return escapePathPreservingSlashes(caddy.ToString(val)), nil
}

View file

@ -1007,3 +1007,34 @@ func TestRewriteURICanonicalizesChangedPath(t *testing.T) {
})
}
}
func TestRewriteEscapesRegexpCaptures(t *testing.T) {
for _, tc := range []struct {
name string
target string
uri string
want string
}{
{name: "double-encoded traversal stays data", target: "/pub/%252e%252e%252fsecret", uri: "/files/{http.regexp.pub.1}", want: "/files/%252e%252e%252fsecret"},
{name: "decoded question mark stays in path", target: "/pub/a%3Fx=1", uri: "/files/{http.regexp.pub.1}", want: "/files/a%3Fx=1"},
{name: "slashes preserved", target: "/pub/a/b", uri: "/files/{http.regexp.pub.1}", want: "/files/a/b"},
{name: "configured query unaffected", target: "/pub/a", uri: "/files?id={http.regexp.pub.1}", want: "/files?id=a"},
} {
t.Run(tc.name, func(t *testing.T) {
r := newRequest(t, http.MethodGet, tc.target)
repl := caddy.NewReplacer()
r = caddyhttp.PrepareRequest(r, repl, nil, &caddyhttp.Server{})
m := caddyhttp.MatchPathRE{MatchRegexp: caddyhttp.MatchRegexp{Name: "pub", Pattern: "^/pub/(.*)$"}}
if err := m.Provision(caddy.Context{}); err != nil {
t.Fatal(err)
}
if ok, err := m.MatchWithError(r); err != nil || !ok {
t.Fatalf("path_regexp did not match: %v", err)
}
Rewrite{URI: tc.uri}.Rewrite(r, repl)
if r.RequestURI != tc.want {
t.Errorf("RequestURI = %q; want %q", r.RequestURI, tc.want)
}
})
}
}