rewrite: escape decoded original and component path placeholders
Decoded path placeholders (orig_uri.path, path components and file matcher values) were inserted raw into the rewritten URI, so an encoded '%' or '?' could be decoded a second time. Escape all decoded path placeholders in path context in a single replacement pass; escaped and whole-URI placeholders keep their URI syntax. Addresses scan report 9e3a082391f793d1. 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:
parent
fc6c202ea6
commit
3c65959560
2 changed files with 86 additions and 28 deletions
|
|
@ -68,6 +68,13 @@ type Rewrite struct {
|
|||
// key-value pairs you add to the query string will not overwrite
|
||||
// existing values (individual pairs are append-only).
|
||||
//
|
||||
// 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.
|
||||
// Placeholders ending in "_escaped" are inserted unchanged.
|
||||
//
|
||||
// To clear the query string, explicitly set an empty one: "?"
|
||||
URI string `json:"uri,omitempty"`
|
||||
|
||||
|
|
@ -212,8 +219,7 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
|
|||
var newPath, newQuery, newFrag string
|
||||
|
||||
if path != "" {
|
||||
path = escapePathPlaceholders(path, r, repl)
|
||||
newPath = repl.ReplaceAll(path, "")
|
||||
newPath = replacePathPlaceholders(path, r, repl)
|
||||
}
|
||||
|
||||
// a fragment may have snuck into the path component during
|
||||
|
|
@ -335,34 +341,25 @@ func (rewr Rewrite) Rewrite(r *http.Request, repl *caddy.Replacer) bool {
|
|||
return r.Method != oldMethod || r.RequestURI != oldURI
|
||||
}
|
||||
|
||||
func escapePathPlaceholders(path string, r *http.Request, repl *caddy.Replacer) string {
|
||||
// Replace path-valued placeholders in escaped form before the URI is parsed,
|
||||
// otherwise literal '?' and '%' bytes from the path can be interpreted as URI
|
||||
// delimiters or percent-escape sequences during the rewrite.
|
||||
pathPlaceholder := "{http.request.uri.path}"
|
||||
if strings.Contains(path, pathPlaceholder) {
|
||||
path = strings.ReplaceAll(path, pathPlaceholder, r.URL.EscapedPath())
|
||||
}
|
||||
|
||||
fileMatchRelativePlaceholder := "{http.matchers.file.relative}"
|
||||
if strings.Contains(path, fileMatchRelativePlaceholder) {
|
||||
if val, ok := repl.Get("http.matchers.file.relative"); ok {
|
||||
if relativePath, ok := val.(string); ok {
|
||||
path = strings.ReplaceAll(path, fileMatchRelativePlaceholder, escapePathPreservingSlashes(relativePath))
|
||||
}
|
||||
func replacePathPlaceholders(path string, r *http.Request, repl *caddy.Replacer) string {
|
||||
// Decoded path fields must remain path data, not URI delimiters or a second
|
||||
// layer of percent-encoding. Whole-URI and explicitly escaped placeholders
|
||||
// retain their URI syntax; query components are escaped by buildQueryString.
|
||||
result, _ := repl.ReplaceFunc(path, func(name string, val any) (any, error) {
|
||||
if name == "http.request.uri.path" {
|
||||
return r.URL.EscapedPath(), nil
|
||||
}
|
||||
}
|
||||
|
||||
fileMatchRemainderPlaceholder := "{http.matchers.file.remainder}"
|
||||
if strings.Contains(path, fileMatchRemainderPlaceholder) {
|
||||
if val, ok := repl.Get("http.matchers.file.remainder"); ok {
|
||||
if remainder, ok := val.(string); ok {
|
||||
path = strings.ReplaceAll(path, fileMatchRemainderPlaceholder, escapePathPreservingSlashes(remainder))
|
||||
}
|
||||
decodedPath := name == "http.request.orig_uri.path" ||
|
||||
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"
|
||||
if decodedPath && !strings.HasSuffix(name, "_escaped") {
|
||||
return escapePathPreservingSlashes(caddy.ToString(val)), nil
|
||||
}
|
||||
}
|
||||
|
||||
return path
|
||||
return val, nil
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
func escapePathPreservingSlashes(path string) string {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ package rewrite
|
|||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
|
@ -687,6 +688,66 @@ func TestRewriteEscapesFileMatcherRemainder(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRewriteEscapesDecodedPathPlaceholders(t *testing.T) {
|
||||
for _, name := range []string{
|
||||
"http.request.uri.path",
|
||||
"http.request.uri.path.dir",
|
||||
"http.request.uri.path.file",
|
||||
"http.request.uri.path.file.base",
|
||||
"http.request.uri.path.file.ext",
|
||||
"http.request.uri.path.1",
|
||||
"http.request.orig_uri.path",
|
||||
"http.request.orig_uri.path.dir",
|
||||
"http.request.orig_uri.path.file",
|
||||
"http.request.orig_uri.path.1",
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
r := newRequest(t, http.MethodGet, "/pub/%252e%252e/a%3Fb%23c.%25txt")
|
||||
repl := caddy.NewReplacer()
|
||||
r = caddyhttp.PrepareRequest(r, repl, nil, &caddyhttp.Server{})
|
||||
value, ok := repl.Get(name)
|
||||
if !ok {
|
||||
t.Fatalf("unknown placeholder: %s", name)
|
||||
}
|
||||
pathValue := caddy.ToString(value)
|
||||
Rewrite{URI: "/files{" + name + "}?q={" + name + "}"}.Rewrite(r, repl)
|
||||
if r.URL.Path != "/files"+pathValue {
|
||||
t.Errorf("Path = %q; want %q", r.URL.Path, "/files"+pathValue)
|
||||
}
|
||||
if r.URL.RawQuery != "q="+url.QueryEscape(pathValue) || r.URL.Fragment != "" {
|
||||
t.Errorf("incorrect query or injected fragment: %s", r.URL)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, target, uri, want string
|
||||
}{
|
||||
{"original path traversal", "/pub/%252e%252e/priv/secret", "/files{http.request.orig_uri.path}", "/files/pub/%252e%252e/priv/secret"},
|
||||
{"original after rewrite", "/pub/%252fsecret", "/files{http.request.orig_uri.path}", "/files/pub/%252fsecret"},
|
||||
{"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"},
|
||||
{"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"},
|
||||
{"Unicode path", "/pub/%C3%84pfel", "/files{http.request.orig_uri.path}", "/files/pub/%C3%84pfel"},
|
||||
} {
|
||||
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{})
|
||||
if tc.name == "original after rewrite" {
|
||||
Rewrite{URI: "/changed"}.Rewrite(r, repl)
|
||||
}
|
||||
Rewrite{URI: tc.uri}.Rewrite(r, repl)
|
||||
if r.RequestURI != tc.want {
|
||||
t.Errorf("RequestURI = %q; want %q", r.RequestURI, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryOpsRenameNoOpCases(t *testing.T) {
|
||||
repl := caddy.NewReplacer()
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue