From 342735e979b354655e26b55ade0eb334676c3e8e Mon Sep 17 00:00:00 2001 From: Alliballibaba Date: Wed, 2 Dec 2026 14:15:43 +0100 Subject: [PATCH 1/4] fix: skip CGI path parsing on worker match The match option forwards a request straight to a worker script, but the request path was still split as if it named a PHP file, so SCRIPT_NAME, PATH_INFO, PHP_SELF and DOCUMENT_URI depended on whatever .php segment the path contained. A matched worker now skips the split. --- cgi_test.go | 1 - context.go | 28 +++++++++++++++------------- docs/config.md | 14 ++++++-------- server_test.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 22 deletions(-) diff --git a/cgi_test.go b/cgi_test.go index 03b3c1020f..d0dff8aef4 100644 --- a/cgi_test.go +++ b/cgi_test.go @@ -760,4 +760,3 @@ func FuzzSplitRawHeader(f *testing.F) { } }) } - diff --git a/context.go b/context.go index 07f58ef599..21c87a46d7 100644 --- a/context.go +++ b/context.go @@ -95,14 +95,11 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr } } - // assign a worker directly if it has a request matcher - if fc.worker == nil { - for _, w := range s.workersWithRequestMatcher { - if w.matchRequest(request) { - fc.worker = w - break - } - } + // if no originalRequest was passed, use the URI from the actual request + // when using Caddy's http module, the original unchanged uri will be used here + // request.URL is often already rewritten to match a PHP script path + if fc.requestURI == "" { + fc.requestURI = fc.request.URL.RequestURI() } if fc.documentRoot == "" { @@ -116,11 +113,16 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr } } - // if no originalRequest was passed, use the URI from the actual request - // when using Caddy's http module, the original unchanged uri will be used here - // request.URL is often already rewritten to match a PHP script path - if fc.requestURI == "" { - fc.requestURI = fc.request.URL.RequestURI() + // assign a worker directly if it has a request matcher + if fc.worker == nil { + for _, w := range s.workersWithRequestMatcher { + if w.matchRequest(request) { + fc.worker = w + fc.scriptFilename = w.fileName + + return fc, nil + } + } } if err := splitCgiPath(fc); err != nil { diff --git a/docs/config.md b/docs/config.md index 49b2683212..171bd14ae9 100644 --- a/docs/config.md +++ b/docs/config.md @@ -265,18 +265,16 @@ The following example will always serve a file in the public directory if presen and otherwise forward the request to the worker matching the path pattern. ```caddyfile -{ - frankenphp { - php_server { - worker { - file /path/to/worker.php # file can be outside of public path - match /api/* # all requests starting with /api/ will be handled by this worker - } - } +php_server { + worker { + file /path/to/worker.php # file can be outside of public path + match /api/* # all requests starting with /api/ will be handled by this worker } } ``` +The following CGI superglobals will always be empty if a request is routed to a woker through `match`: `SCRIPT_NAME`, `PATH_INFO`, `PHP_SELF`, `DOCUMENT_URI`. + ## Restarting threads after a number of requests (experimental) FrankenPHP can automatically restart PHP threads after they have handled a given number of requests. diff --git a/server_test.go b/server_test.go index 89fcfbd575..f4469165ed 100644 --- a/server_test.go +++ b/server_test.go @@ -144,6 +144,34 @@ func TestServer(t *testing.T) { assert.ErrorIs(t, err6, frankenphp.ErrInvalidScriptPath, "the matcher worker is scoped to server 2, so server 1 has no script for this path") }) + t.Run("request_matcher_clears_script_vars", func(t *testing.T) { + server, _ := frankenphp.NewServer(testDataDir) + initServers( + t, + frankenphp.WithServer(server), + frankenphp.WithWorkers("match", testDataDir+"server-globals.php", 1, + frankenphp.WithWorkerServerScope(server), + frankenphp.WithWorkerMatcher(func(r *http.Request) bool { + return strings.HasPrefix(r.URL.Path, "/match/") + }), + ), + ) + + // without the matcher, "/server-globals.php/extra" is split into SCRIPT_NAME and PATH_INFO (control) + unmatched := serverGet(t, server, "http://example.com/server-globals.php/extra") + assert.Contains(t, unmatched, "SCRIPT_NAME: /server-globals.php\n") + assert.Contains(t, unmatched, "PATH_INFO: /extra\n") + + // the matched worker bypasses path splitting, so the script name variables must be empty + matched := serverGet(t, server, "http://example.com/match/server-globals.php/extra") + assert.Contains(t, matched, "SCRIPT_NAME:\n") + assert.Contains(t, matched, "PHP_SELF:\n") + assert.Contains(t, matched, "PATH_INFO:\n") + assert.Contains(t, matched, "DOCUMENT_URI:\n") + assert.Contains(t, matched, "SCRIPT_FILENAME: "+filepath.Join(testDataDir, "server-globals.php")+"\n") + assert.Contains(t, matched, "REQUEST_URI: /match/server-globals.php/extra\n") + }) + t.Run("worker_env_inheritance", func(t *testing.T) { server, _ := frankenphp.NewServer(testDataDir, frankenphp.WithServerEnv(map[string]string{ "FROM_SERVER_ENV": "original", From 810eb1f348c4520d2641b568b309bb10877b8b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 2 Dec 2026 14:15:47 +0100 Subject: [PATCH 2/4] fix: keep SCRIPT_NAME for matched workers in root Frameworks such as Symfony and Laravel derive their base URL from SCRIPT_NAME. With it empty, an application served under a path prefix, for instance behind match /public/*, routed every request to a 404. A matched worker inside the document root now gets its own path as SCRIPT_NAME and PHP_SELF. PATH_INFO and DOCUMENT_URI stay empty, and a worker outside the document root keeps all four empty. --- context.go | 5 +++++ docs/config.md | 2 +- server_test.go | 6 +++--- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index 21c87a46d7..31151eb108 100644 --- a/context.go +++ b/context.go @@ -120,6 +120,11 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr fc.worker = w fc.scriptFilename = w.fileName + // frameworks derive their base URL from SCRIPT_NAME, so keep it for a worker inside the document root + if rest, ok := strings.CutPrefix(w.fileName, fc.documentRoot); ok && rest != "" && rest[0] == filepath.Separator { + fc.scriptName = filepath.ToSlash(rest) + } + return fc, nil } } diff --git a/docs/config.md b/docs/config.md index 171bd14ae9..2190e90812 100644 --- a/docs/config.md +++ b/docs/config.md @@ -273,7 +273,7 @@ php_server { } ``` -The following CGI superglobals will always be empty if a request is routed to a woker through `match`: `SCRIPT_NAME`, `PATH_INFO`, `PHP_SELF`, `DOCUMENT_URI`. +When `match` routes a request to a worker, `PATH_INFO` and `DOCUMENT_URI` are always empty. `SCRIPT_NAME` and `PHP_SELF` name the worker script when it is inside the document root, and are empty otherwise. ## Restarting threads after a number of requests (experimental) diff --git a/server_test.go b/server_test.go index f4469165ed..52a3472bed 100644 --- a/server_test.go +++ b/server_test.go @@ -162,10 +162,10 @@ func TestServer(t *testing.T) { assert.Contains(t, unmatched, "SCRIPT_NAME: /server-globals.php\n") assert.Contains(t, unmatched, "PATH_INFO: /extra\n") - // the matched worker bypasses path splitting, so the script name variables must be empty + // the matched worker bypasses path splitting: SCRIPT_NAME names the worker, PATH_INFO stays empty matched := serverGet(t, server, "http://example.com/match/server-globals.php/extra") - assert.Contains(t, matched, "SCRIPT_NAME:\n") - assert.Contains(t, matched, "PHP_SELF:\n") + assert.Contains(t, matched, "SCRIPT_NAME: /server-globals.php\n") + assert.Contains(t, matched, "PHP_SELF: /server-globals.php\n") assert.Contains(t, matched, "PATH_INFO:\n") assert.Contains(t, matched, "DOCUMENT_URI:\n") assert.Contains(t, matched, "SCRIPT_FILENAME: "+filepath.Join(testDataDir, "server-globals.php")+"\n") From dda91a83da941b46b4b69dc4788e018577e27348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 2 Dec 2026 14:24:55 +0100 Subject: [PATCH 3/4] fix: capture REQUEST_URI after worker matchers A Go matcher passed to WithWorkerMatcher may rewrite the request URL. The fallback REQUEST_URI was taken before the matchers ran, while QUERY_STRING is read from the rewritten URL, so the two could disagree. Matchers now run first, as before the matched worker skipped the CGI path split. --- context.go | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/context.go b/context.go index 31151eb108..2ea2c3f216 100644 --- a/context.go +++ b/context.go @@ -95,6 +95,19 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr } } + // assign a worker directly if it has a request matcher + matched := false + if fc.worker == nil { + for _, w := range s.workersWithRequestMatcher { + if w.matchRequest(request) { + fc.worker = w + matched = true + + break + } + } + } + // if no originalRequest was passed, use the URI from the actual request // when using Caddy's http module, the original unchanged uri will be used here // request.URL is often already rewritten to match a PHP script path @@ -113,21 +126,15 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr } } - // assign a worker directly if it has a request matcher - if fc.worker == nil { - for _, w := range s.workersWithRequestMatcher { - if w.matchRequest(request) { - fc.worker = w - fc.scriptFilename = w.fileName - - // frameworks derive their base URL from SCRIPT_NAME, so keep it for a worker inside the document root - if rest, ok := strings.CutPrefix(w.fileName, fc.documentRoot); ok && rest != "" && rest[0] == filepath.Separator { - fc.scriptName = filepath.ToSlash(rest) - } + if matched { + fc.scriptFilename = fc.worker.fileName - return fc, nil - } + // frameworks derive their base URL from SCRIPT_NAME, so keep it for a worker inside the document root + if rest, ok := strings.CutPrefix(fc.worker.fileName, fc.documentRoot); ok && rest != "" && rest[0] == filepath.Separator { + fc.scriptName = filepath.ToSlash(rest) } + + return fc, nil } if err := splitCgiPath(fc); err != nil { From 0a1bf30cdba30427926418231cef8591449372c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 2 Dec 2026 14:24:55 +0100 Subject: [PATCH 4/4] test: resolve the root in the match worker test Worker filenames are resolved, so with a symlink in the checkout path the worker no longer sat under the server root and SCRIPT_NAME came out empty. The test now resolves the fixture directory first. --- server_test.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/server_test.go b/server_test.go index 52a3472bed..88e7c00979 100644 --- a/server_test.go +++ b/server_test.go @@ -145,11 +145,15 @@ func TestServer(t *testing.T) { }) t.Run("request_matcher_clears_script_vars", func(t *testing.T) { - server, _ := frankenphp.NewServer(testDataDir) + // worker filenames are resolved, so a symlinked checkout path would leave SCRIPT_NAME empty + root, err := filepath.EvalSymlinks(testDataDir) + require.NoError(t, err) + + server, _ := frankenphp.NewServer(root) initServers( t, frankenphp.WithServer(server), - frankenphp.WithWorkers("match", testDataDir+"server-globals.php", 1, + frankenphp.WithWorkers("match", filepath.Join(root, "server-globals.php"), 1, frankenphp.WithWorkerServerScope(server), frankenphp.WithWorkerMatcher(func(r *http.Request) bool { return strings.HasPrefix(r.URL.Path, "/match/") @@ -168,7 +172,7 @@ func TestServer(t *testing.T) { assert.Contains(t, matched, "PHP_SELF: /server-globals.php\n") assert.Contains(t, matched, "PATH_INFO:\n") assert.Contains(t, matched, "DOCUMENT_URI:\n") - assert.Contains(t, matched, "SCRIPT_FILENAME: "+filepath.Join(testDataDir, "server-globals.php")+"\n") + assert.Contains(t, matched, "SCRIPT_FILENAME: "+filepath.Join(root, "server-globals.php")+"\n") assert.Contains(t, matched, "REQUEST_URI: /match/server-globals.php/extra\n") })