Decoded path placeholders can inject URI structure
Issue Details
Placeholders that return the decoded request path (req.URL.Path, e.g. {http.request.uri.path}, {http.request.orig_uri.path} and their .file/.dir/N variants) are expanded verbatim into URI fields — Location headers via redir/header/respond, and rewrite targets. Percent-encoded octets in the request path arrive decoded, so a single %2f, %5c, %3f, or %23 materializes as a real /, \, ?, or # at the sink. This produces open redirects and upstream path/query injection.
1. Location-emitting handlers have no open-redirect guard. StaticResponse.ServeHTTP expands header values with repl.ReplaceKnown and emits them verbatim. Meanwhile file_server's own redirect() (modules/caddyhttp/fileserver/staticfiles.go:865) explicitly strips leading // "to prevent path-based open redirects" — the codebase guards exactly one sink.
2. escapePathPlaceholders protects only 3 of the decoded-path placeholders (modules/caddyhttp/rewrite/rewrite.go:337): {http.request.uri.path}, {http.matchers.file.relative}, {http.matchers.file.remainder}. All other decoded-path placeholders — {http.request.orig_uri.path} and its .file/.dir variants, {http.request.uri.path.file}, .dir, .file.base, .file.ext, {http.request.uri.path.N}, {http.regexp.*}, {http.vars.*}, {http.request.header.*}, {http.request.cookie.*}, {http.url_pattern.*}, {http.request.body}, {http.reverse_proxy.*} — reintroduce the same structure-injection class (cf. GHSA-j8px-rmrx-76h9) into rewrite targets. Because orig_uri.path returns the already-decoded path, a single-encoded %3f/%2f suffices — no double encoding required.
Minimal reproduction
{
admin off
auto_https off
}
:9501 { redir {http.request.uri.path} }
:9502 { header Location {http.request.orig_uri.path}
respond "x" }
:9200 { rewrite /backend{http.request.orig_uri.path}
respond "p={http.request.uri.path} q={http.request.uri.query}" }
:9300 { rewrite /backend{http.request.orig_uri.path}
reverse_proxy localhost:9400 }
:9400 { respond "UPSTREAM_SAW uri={http.request.uri}" }
$ curl -D- --path-as-is "http://localhost:9501/%2f%2fevil.com%2fx"
HTTP/1.1 302 Found
Location: ///evil.com/x
$ curl -D- --path-as-is "http://localhost:9501/%5c%5cevil.com%2fx"
HTTP/1.1 302 Found
Location: /\\evil.com/x # WHATWG treats \ as / -> //evil.com
$ curl -D- --path-as-is "http://localhost:9502/%2f%2fevil.com%2fx"
HTTP/1.1 200 OK
Location: ///evil.com/x
$ curl --path-as-is "http://localhost:9200/x%3finjected%3d1%26z%3d2"
p=/backend/x q=injected=1&z=2 # %3f -> '?' splits path, injects query keys
$ curl --path-as-is "http://localhost:9300/x%252f..%252fadmin"
UPSTREAM_SAW uri=/backend/x/../admin # '..'/' /' forwarded verbatim upstream
Tested on master f53b832c41a42dbfc5968da07babf80ec0831126.
Impact
- Open redirect on any config that redirects with a decoded-path placeholder — including the common "preserve the path" idiom (
redir {http.request.orig_uri.path},redir {http.request.uri.path}).%2f/%5cproduceLocation: ///host//\\host, both followed cross-origin by WHATWG parsers. - Query-parameter injection into rewritten requests via
%3f(attacker-chosen keys/values reach upstream apps andquerymatchers). - Path confusion into upstreams: injected
..//segments reach the upstream request line verbatim (directRequestforwardsreq.URL), so a front layer matching on the escaped URI and a normalizing upstream disagree about the resolved path.
This requires an operator-authored config using a decoded-path placeholder in a URI field — but that is the documented "preserve the path" pattern, and precedent treats the class as fixable in code: GHSA-j8px-rmrx-76h9 fixed ?-injection through placeholders; escapePathPlaceholders covered 3 placeholders; commit 32bba15 (#8285) just fixed this same open-redirect primitive inside php_fastcgi's generated config by switching to {http.request.orig_uri.path.file_escaped}. The still-open family is every other decoded-path placeholder plus the unguarded Location sinks.
Suggested remediation
- Sanitize
Location-class headers at emission: strip or reject leading///\\when the value came from placeholder expansion — the same guardfile_server'sredirect()applies to//, extended to\. - Generalize
escapePathPlaceholdersto all decoded-path placeholder families (or invert the convention so placeholders return escaped text with*_unescapedopt-ins), and add the missing{http.request.orig_uri.path_escaped}to match{http.request.orig_uri.path.file_escaped}.
Assistance Disclosure
AI used. This issue was discovered, dynamically validated, and written by an AI agent (Devin/Cognition) under human direction: static analysis identified the primitive class; each PoC above was executed live against a locally built binary at the revision stated.
Gahhh so over-explained The rewrite case is valid but this combines it with sinks that intentionally permit external targets. redir and a configured Location header cannot be forced same-origin globally while headers, variables, cookies and captures are not inherently path-valued. Narrow this to decoded request-path placeholders in rewrite targets.
I’d like to work on a focused fix for decoded request-path placeholders being expanded into rewrite targets. The change would ensure these values are safely escaped while preserving intended path separators. I’ll keep redir and explicitly configured Location headers out of scope and add regression tests covering encoded delimiters and path-segment cases.
Does this approach sound reasonable?
Apologies. That rewrite-only scope is right, however, I just realised we already have a patch covering part of this; we will extend it to cover original-path and numbered-segment placeholders rather than open a separate PR.
Um what?
lol?