rewrite: canonicalize paths changed by URI rewrites
A rewrite's path can be relative or contain dot segments, for example when it comes from a placeholder. Path matchers and file_server then interpret the same value differently, so a path guard placed after the rewrite can be bypassed. uri replace, path_regexp, and prefix/suffix stripping already canonicalize changed paths; direct URI rewrites now do the same. A changed path is anchored and its dot segments are resolved, while repeated slashes are preserved. Unchanged paths and an explicitly configured OPTIONS * target are left as-is. Based on a patch by Zen Dodd. Reviewed by mholt. Code written by GitHub Copilot (Claude Opus 5.5), adapting the contributor's patch. Co-authored-by: Zen Dodd <mail@steadytao.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
7a58576d34
commit
f4c634f0ed
2 changed files with 53 additions and 2 deletions
|
|
@ -75,6 +75,10 @@ type Rewrite struct {
|
|||
// path stay part of the new path instead of starting a query string.
|
||||
// Placeholders ending in "_escaped" are inserted unchanged.
|
||||
//
|
||||
// If the path changes, the result is made absolute and its dot
|
||||
// segments are resolved (repeated slashes are preserved), so
|
||||
// "foo.html" becomes "/foo.html".
|
||||
//
|
||||
// To clear the query string, explicitly set an empty one: "?"
|
||||
URI string `json:"uri,omitempty"`
|
||||
|
||||
|
|
@ -272,12 +276,22 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
|
|||
// update the URI with the new components
|
||||
// only after building them
|
||||
if pathStart >= 0 {
|
||||
oldPath := r.URL.Path
|
||||
if path, err := url.PathUnescape(newPath); err != nil {
|
||||
r.URL.Path = newPath
|
||||
} else {
|
||||
r.URL.Path = path
|
||||
}
|
||||
r.URL.RawPath = "" // force recomputing when EscapedPath() is called
|
||||
|
||||
// a rewritten path can be relative or contain dot segments (often
|
||||
// from a placeholder), which downstream matchers and handlers would
|
||||
// interpret differently; so anchor and clean it, except for an
|
||||
// explicitly configured OPTIONS * request target
|
||||
optionsStar := r.Method == http.MethodOptions && path == "*" && newPath == "*"
|
||||
if r.URL.Path != oldPath && !optionsStar {
|
||||
canonicalizePath(r)
|
||||
}
|
||||
}
|
||||
if qsStart >= 0 || queryInjected {
|
||||
r.URL.RawQuery = newQuery
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ func TestRewrite(t *testing.T) {
|
|||
{
|
||||
rule: Rewrite{URI: "foo"},
|
||||
input: newRequest(t, "GET", "/"),
|
||||
expect: newRequest(t, "GET", "foo"),
|
||||
expect: newRequest(t, "GET", "/foo"),
|
||||
},
|
||||
{
|
||||
rule: Rewrite{URI: "{http.request.uri}"},
|
||||
|
|
@ -727,7 +727,7 @@ func TestRewriteEscapesDecodedPathPlaceholders(t *testing.T) {
|
|||
{"escaped file", "/pub/a%2Fb%3Fc%23d%25", "/files/{http.request.orig_uri.path.file_escaped}", "/files/a/b%3Fc%23d%25"},
|
||||
{"whole URI", "/pub/a%3Fb?x=y", "{http.request.orig_uri}", "/pub/a%3Fb?x=y"},
|
||||
{"escaped current path", "/pub/%252e%252e", "{http.request.uri.path_escaped}", "/pub/%252e%252e"},
|
||||
{"literal escapes", "/pub", "/files/%2e%2e/public?x=y", "/files/../public?x=y"},
|
||||
{"literal escapes", "/pub", "/files/%2e%2e/public?x=y", "/public?x=y"},
|
||||
{"encoded extension", "/pub/file.%252f", "/files/{http.request.uri.path.file.ext}", "/files/.%252f"},
|
||||
{"literal placeholder text", "/pub/%7Benv.SCAN_PATH_TEST%7D", "/files{http.request.orig_uri.path}", "/files/pub/%7Benv.SCAN_PATH_TEST%7D"},
|
||||
{"non-UTF-8 path", "/pub/%ff", "/files{http.request.orig_uri.path}", "/files/pub/%FF"},
|
||||
|
|
@ -970,3 +970,40 @@ func reqEqual(r1, r2 *http.Request) bool {
|
|||
r1.URL.RawQuery == r2.URL.RawQuery &&
|
||||
r1.URL.Fragment == r2.URL.Fragment
|
||||
}
|
||||
|
||||
func TestRewriteURICanonicalizesChangedPath(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
method string
|
||||
target string
|
||||
header string
|
||||
uri string
|
||||
want string
|
||||
}{
|
||||
{name: "relative placeholder", target: "/x", header: "../secret", uri: "{http.request.header.X-Path}", want: "/secret"},
|
||||
{name: "relative literal", target: "/x", uri: "secret", want: "/secret"},
|
||||
{name: "dot segments from placeholder", target: "/x", header: "/public/../secret", uri: "{http.request.header.X-Path}", want: "/secret"},
|
||||
{name: "query kept", target: "/x?a=b", header: "../secret", uri: "{http.request.header.X-Path}?{http.request.uri.query}", want: "/secret?a=b"},
|
||||
{name: "repeated slashes preserved", target: "/x", uri: "/a//b/", want: "/a//b/"},
|
||||
{name: "unchanged path left alone", target: "/a/../b", uri: "{http.request.uri.path}", want: "/a/../b"},
|
||||
{name: "OPTIONS star preserved", method: http.MethodOptions, target: "/x", uri: "*", want: "*"},
|
||||
{name: "star from placeholder is a path", target: "/x", header: "*", uri: "{http.request.header.X-Path}", want: "/%2A"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
method := tc.method
|
||||
if method == "" {
|
||||
method = http.MethodGet
|
||||
}
|
||||
r := newRequest(t, method, tc.target)
|
||||
if tc.header != "" {
|
||||
r.Header.Set("X-Path", tc.header)
|
||||
}
|
||||
repl := caddy.NewReplacer()
|
||||
r = caddyhttp.PrepareRequest(r, repl, nil, &caddyhttp.Server{})
|
||||
Rewrite{URI: tc.uri}.Rewrite(r, repl)
|
||||
if r.RequestURI != tc.want {
|
||||
t.Errorf("RequestURI = %q; want %q", r.RequestURI, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue