From 32bba1545989e07368c0977a37652f33d80c8cd4 Mon Sep 17 00:00:00 2001 From: sam-kota Date: Wed, 28 Oct 2026 15:15:37 +0530 Subject: [PATCH] fastcgi: keep the php_fastcgi directory redirect on the same origin (#8285) * fastcgi: keep the php_fastcgi directory redirect on the same origin * fastcgi: build the directory redirect from the escaped original segment --- .../php_fastcgi_expanded_form.caddyfiletest | 4 +- .../php_fastcgi_handle_response.caddyfiletest | 2 +- .../php_fastcgi_matcher.caddyfiletest | 2 +- .../php_fastcgi_subdirectives.caddyfiletest | 2 +- ...p_fastcgi_try_files_override.caddyfiletest | 2 +- caddytest/integration/phpfastcgi_test.go | 78 +++++++++++++++++++ modules/caddyhttp/app.go | 1 + modules/caddyhttp/replacer.go | 7 ++ .../reverseproxy/fastcgi/caddyfile.go | 10 ++- 9 files changed, 101 insertions(+), 7 deletions(-) diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.caddyfiletest b/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.caddyfiletest index df2e24885..47f0eb520 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.caddyfiletest +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_expanded_form.caddyfiletest @@ -8,7 +8,7 @@ route { } not path */ } - redir @canonicalPath {orig_path}/{orig_?query} 308 + redir @canonicalPath ./{http.request.orig_uri.path.file_escaped}/{orig_?query} 308 # If the requested file does not exist, try index files @indexFiles { @@ -50,7 +50,7 @@ route { "handler": "static_response", "headers": { "Location": [ - "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}" + "./{http.request.orig_uri.path.file_escaped}/{http.request.orig_uri.prefixed_query}" ] }, "status_code": 308 diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.caddyfiletest b/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.caddyfiletest index 3a857654f..84f4aed1f 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.caddyfiletest +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_handle_response.caddyfiletest @@ -42,7 +42,7 @@ "handler": "static_response", "headers": { "Location": [ - "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}" + "./{http.request.orig_uri.path.file_escaped}/{http.request.orig_uri.prefixed_query}" ] }, "status_code": 308 diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.caddyfiletest b/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.caddyfiletest index 4d1298fcc..9d13b9884 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.caddyfiletest +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_matcher.caddyfiletest @@ -33,7 +33,7 @@ php_fastcgi @test localhost:9000 "handler": "static_response", "headers": { "Location": [ - "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}" + "./{http.request.orig_uri.path.file_escaped}/{http.request.orig_uri.prefixed_query}" ] }, "status_code": 308 diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.caddyfiletest b/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.caddyfiletest index 9a9ab5ab0..55c2c4b28 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.caddyfiletest +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_subdirectives.caddyfiletest @@ -43,7 +43,7 @@ php_fastcgi localhost:9000 { "handler": "static_response", "headers": { "Location": [ - "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}" + "./{http.request.orig_uri.path.file_escaped}/{http.request.orig_uri.prefixed_query}" ] }, "status_code": 308 diff --git a/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.caddyfiletest b/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.caddyfiletest index 75487a937..397a46782 100644 --- a/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.caddyfiletest +++ b/caddytest/integration/caddyfile_adapt/php_fastcgi_try_files_override.caddyfiletest @@ -46,7 +46,7 @@ php_fastcgi localhost:9000 { "handler": "static_response", "headers": { "Location": [ - "{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}" + "./{http.request.orig_uri.path.file_escaped}/{http.request.orig_uri.prefixed_query}" ] }, "status_code": 308 diff --git a/caddytest/integration/phpfastcgi_test.go b/caddytest/integration/phpfastcgi_test.go index 5ce3a22e1..79371d8e5 100644 --- a/caddytest/integration/phpfastcgi_test.go +++ b/caddytest/integration/phpfastcgi_test.go @@ -22,6 +22,7 @@ import ( "net/http/fcgi" "os" "path/filepath" + "runtime" "strings" "testing" @@ -149,3 +150,80 @@ func TestPHPFastCGIProtectedRouteIgnoresDotSegments(t *testing.T) { }) } } + +// The php_fastcgi directory redirect must stay on the requesting origin. Its +// matcher judges the cleaned request path, so /%2Fevil.com%2F..%2Fsub is matched +// as /sub and resolves to /sub/index.php, but the Location header used to +// be built from the raw original path and came out as //evil.com/../sub/, which +// browsers treat as a scheme-relative URL to another host. The relative +// reference is built from the escaped original segment, so names with reserved +// or non-ASCII characters must still round-trip to the same directory. +func TestPHPFastCGIDirectoryRedirectStaysOnOrigin(t *testing.T) { + dirs := []string{"sub", "a b", "a#b", "50%", "a;b", "café", "x/sub"} + if runtime.GOOS != "windows" { + dirs = append(dirs, "a?b", "a:b") + } + root := t.TempDir() + for _, dir := range dirs { + if err := os.MkdirAll(filepath.Join(root, filepath.FromSlash(dir)), 0o755); err != nil { + t.Fatalf("creating %s: %v", dir, err) + } + if err := os.WriteFile(filepath.Join(root, filepath.FromSlash(dir), "index.php"), []byte(" 0x7e { + t.Errorf("requesting %q: Location %q contains an unescaped byte %#x", tc.uri, loc, loc[i]) + break + } + } + } +} diff --git a/modules/caddyhttp/app.go b/modules/caddyhttp/app.go index a2c20d91e..2df24c176 100644 --- a/modules/caddyhttp/app.go +++ b/modules/caddyhttp/app.go @@ -76,6 +76,7 @@ func init() { // `{http.request.orig_uri.path.*}` | Parts of the original path, split by `/` (0-based from left) // `{http.request.orig_uri.path.dir}` | The request's original directory // `{http.request.orig_uri.path.file}` | The request's original filename +// `{http.request.orig_uri.path.file_escaped}` | The request's original filename, percent-encoded as in the request's escaped path // `{http.request.orig_uri.query}` | The request's original query string (without `?`) // `{http.request.orig_uri.prefixed_query}` | The request's original query string with a `?` prefix, if non-empty // `{http.request.port}` | The port part of the request's Host header diff --git a/modules/caddyhttp/replacer.go b/modules/caddyhttp/replacer.go index c3978bf5e..6fbe90778 100644 --- a/modules/caddyhttp/replacer.go +++ b/modules/caddyhttp/replacer.go @@ -263,6 +263,13 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request) _, file := path.Split(or.URL.Path) return file, true + case "http.request.orig_uri.path.file_escaped": + // split the escaped path rather than escaping the decoded + // file name, since an encoded slash (%2F) in the request + // is part of the last segment, not a separator + or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request) + _, file := path.Split(or.URL.EscapedPath()) + return file, true case "http.request.orig_uri.path.dir": or, _ := req.Context().Value(OriginalRequestCtxKey).(http.Request) dir, _ := path.Split(or.URL.Path) diff --git a/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go b/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go index 2325af9a7..909afc661 100644 --- a/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go +++ b/modules/caddyhttp/reverseproxy/fastcgi/caddyfile.go @@ -346,9 +346,17 @@ func parsePHPFastCGI(h httpcaddyfile.Helper) ([]httpcaddyfile.ConfigValue, error }, }), } + // The matcher above judges the cleaned request path, so the + // Location must not echo the raw original path: a request for + // /%2Fevil.com%2F..%2Fsub matches /sub/index.php but would + // redirect to //evil.com/../sub/, which browsers resolve to + // another host. A reference relative to the last path segment + // resolves against the URL the client actually requested, so it + // can only ever land on the same origin; the leading ./ keeps a + // segment containing ':' from being read as a scheme. redirHandler := caddyhttp.StaticResponse{ StatusCode: caddyhttp.WeakString(strconv.Itoa(http.StatusPermanentRedirect)), - Headers: http.Header{"Location": []string{"{http.request.orig_uri.path}/{http.request.orig_uri.prefixed_query}"}}, + Headers: http.Header{"Location": []string{"./{http.request.orig_uri.path.file_escaped}/{http.request.orig_uri.prefixed_query}"}}, } redirRoute := caddyhttp.Route{ MatcherSetsRaw: []caddy.ModuleMap{redirMatcherSet},