Skip to content

rewrite: escape file matcher remainder before rewriting - #8325

Merged
francislavoie merged 1 commit into
caddyserver:masterfrom
dunglas:rewrite-escape-file-remainder
Nov 1, 2026
Merged

francislavoie merged 1 commit into
caddyserver:masterfrom
dunglas:rewrite-escape-file-remainder

Conversation

@dunglas

@dunglas dunglas commented Nov 1, 2026 •

Copy link
Copy Markdown
Collaborator

{http.matchers.file.remainder} holds decoded path text. escapePathPlaceholders() already escapes {http.request.uri.path} and {http.matchers.file.relative} before the rewritten URI is parsed, but not the remainder. The rewrite handler inserts it raw, cuts the result at # and ?, and percent-decodes it a second time. This happens after the matchers that ran on the original path, so a path-based deny rule can be bypassed, and %3F or %23 in the path info changes the query string or truncates the path.

try_files with split_path and a rewrite to {http.matchers.file.relative}{http.matchers.file.remainder} is the usual setup for PHP-style path info (FrankenPHP's php_server generated it).

Details Repro (`public/index.php` and `public/private/secret.txt` exist):
:8080 {
	root * ./public
	route {
		@denied path /private/*
		error @denied 403
		@php file {
			try_files {path} index.php
			split_path .php
		}
		rewrite @php {http.matchers.file.relative}{http.matchers.file.remainder}
		@phpfiles path *.php /index.php/*
		respond @phpfiles "PHP route: {uri}" 200
		file_server
	}
}
$ curl 'http://localhost:8080/index.php/..%252fprivate%252fsecret.txt'
SECRET
$ curl 'http://localhost:8080/index.php/x%23y?a=b'
PHP route: /index.php/x?a=b
$ curl 'http://localhost:8080/index.php/x%253Fa=evil?a=b'
PHP route: /index.php/x%3Fa=evil?a=b

The first request is a single opaque segment for @denied. After the rewrite it becomes /index.php/../private/secret.txt, misses the PHP route and file_server serves the protected file.

With this patch:

$ curl 'http://localhost:8080/index.php/..%252fprivate%252fsecret.txt'
PHP route: /index.php/..%252fprivate%252fsecret.txt
$ curl 'http://localhost:8080/index.php/x%23y?a=b'
PHP route: /index.php/x%23y?a=b
$ curl 'http://localhost:8080/index.php/x%253Fa=evil?a=b'
PHP route: /index.php/x%253Fa=evil?a=b

The fix escapes the remainder with escapePathPreservingSlashes(), the same way as {http.matchers.file.relative}. A remainder without reserved characters, such as /foo/bar, is rewritten as before.

Assistance Disclosure

Generated by Claude Code (Opus 5.5), reviewed by me.

{http.matchers.file.remainder} holds decoded path text. The rewrite
handler inserted it raw into the new URI, then split at '#' and '?' and
percent-decoded the path again. A request to
/index.php/..%252fprivate%252fsecret.txt became
/index.php/../private/secret.txt after matchers had already run, and a
%3F in the path info replaced the query string.

Escape the remainder like {http.matchers.file.relative}.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-32RAL55Q.
@francislavoie francislavoie added the bug 🐞 Something isn't working label Nov 1, 2026
@francislavoie francislavoie added this to the v2.12.0 milestone Nov 1, 2026
@francislavoie
francislavoie merged commit 094172d into caddyserver:master Nov 1, 2026
26 checks passed
This was referenced Nov 3, 2026
NickCao pushed a commit to NickCao/caddy that referenced this pull request Nov 4, 2026
@francislavoie francislavoie modified the milestones: v2.12.0, v2.11.8 Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants