fastcgi: split script path at segment boundary (#8386)

The transport split the path at the first split extension anywhere, so "/admin.php.php" and "/admin.phpx/y.php" ran "/admin.php" with the rest as PATH_INFO. Route matchers judged a different file: "path /admin.php /admin.php/*" didn't match, and with php_fastcgi the file matcher rewrote "/a.phpx/b.php" to that file while PHP ran "/a.php". With "index off", or try_files not ending in a PHP file, nothing rewrites the request, so the protected script runs.

A match must now end the path or be followed by "/", as in the file matcher.
This commit is contained in:
Kévin Dunglas 2026-11-08 22:54:23 +01:00 committed by GitHub
parent af4d0c37d4
commit b30c019341
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 6 deletions

View file

@ -487,6 +487,10 @@ func (t Transport) buildEnv(r *http.Request) (envVars, error) {
// example: if splitPath is [".php"]
// "/path/to/script.php/some/path": ("/path/to/script.php", "/some/path")
//
// A match must end the path or be followed by "/", as in the file matcher, so
// "/admin.php.php" or "/admin.phpx/y.php" can't run "/admin.php" past route
// matchers that judged another file.
//
// Matching is strictly ASCII case-insensitive. Bytes >= utf8.RuneSelf in path
// never match any split entry: split strings are validated ASCII-only and
// lower-cased in Provision(), so any Unicode equivalence (e.g. fullwidth or
@ -533,8 +537,8 @@ func (t Transport) splitPos(path string) int {
}
}
if match {
return i + splitLen
if end := i + splitLen; match && (end == pathLen || path[end] == '/') {
return end
}
}
}

View file

@ -148,7 +148,7 @@ func TestSplitPos(t *testing.T) {
},
{
name: "unicode in filename with multiple php occurrences",
path: "/ȺȺȺȺshell.php.txt.php",
path: "/ȺȺȺȺshell.php/txt.php",
splitPath: []string{".php"},
wantPos: 18, // should match first .php, not be confused by byte offset shift
},
@ -195,7 +195,25 @@ func TestSplitPos(t *testing.T) {
name: "extension in middle of filename",
path: "/test.php.bak",
splitPath: []string{".php"},
wantPos: 9,
wantPos: -1,
},
{
name: "extension in middle of filename before a later script",
path: "/admin.php.php",
splitPath: []string{".php"},
wantPos: 14,
},
{
name: "extension in middle of a directory name",
path: "/admin.phpx/y.php",
splitPath: []string{".php"},
wantPos: 17,
},
{
name: "extension in middle of a directory name with path info",
path: "/a.phpx/b.php/info",
splitPath: []string{".php"},
wantPos: 13,
},
// Regression tests adapted from FrankenPHP advisories
// GHSA-3g8v-8r37-cgjm and GHSA-v4h7-cj44-8fc8: search.IgnoreCase
@ -290,7 +308,7 @@ func TestSplitPos(t *testing.T) {
// incorrect SCRIPT_NAME/PATH_INFO splitting
func TestSplitPosUnicodeSecurityRegression(t *testing.T) {
// U+023A: Ⱥ (UTF-8: C8 BA). Lowercase is ⱥ (UTF-8: E2 B1 A5), longer in bytes.
path := "/ȺȺȺȺshell.php.txt.php"
path := "/ȺȺȺȺshell.php/txt.php"
split := []string{".php"}
pos := Transport{SplitPath: split}.splitPos(path)
@ -306,7 +324,7 @@ func TestSplitPosUnicodeSecurityRegression(t *testing.T) {
pathInfo := path[pos:]
assert.Equal(t, "/ȺȺȺȺshell.php", scriptName, "script name should be the path up to first .php")
assert.Equal(t, ".txt.php", pathInfo, "path info should be the remainder after first .php")
assert.Equal(t, "/txt.php", pathInfo, "path info should be the remainder after first .php")
}
}