Skip to content

Commit 45d8a3a

Browse files
authored
Merge commit from fork
A root built from a placeholder such as {query.tenant} is expanded per request and never goes through a system call, so a %00 in the request value reached SCRIPT_FILENAME. PHP reads that path as a C string: the fixed part of the root after the NUL and the script name were dropped, and PHP ran or printed the file named before the NUL, such as an uploaded image. WithRequestDocumentRoot() now refuses a root containing a NUL byte, before caching it, and the request gets the same 400 as a NUL byte in the path. An option returning an ErrRejected now writes its response, as validate() does. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GXFUZ7EW.
1 parent f3e5908 commit 45d8a3a

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎context.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func newContextFromRequest(request *http.Request, responseWriter http.ResponseWr
9191

9292
for _, o := range opts {
9393
if err := o(fc); err != nil {
94+
if _, rejected := errors.AsType[ErrRejected](err); rejected {
95+
fc.reject(err)
96+
}
97+
9498
return nil, err
9599
}
96100
}

‎nuldocumentroot_test.go‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package frankenphp_test
2+
3+
import (
4+
"net/http"
5+
"net/http/httptest"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/dunglas/frankenphp"
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestRejectNULByteInDocumentRoot(t *testing.T) {
16+
dir := t.TempDir()
17+
require.NoError(t, os.WriteFile(filepath.Join(dir, "avatar.gif"), []byte("GIF89a<?php echo 'executed';"), 0o644))
18+
19+
require.NoError(t, frankenphp.Init())
20+
t.Cleanup(frankenphp.Shutdown)
21+
22+
req, err := frankenphp.NewRequestWithContext(
23+
httptest.NewRequest(http.MethodGet, "http://example.com/index.php", nil),
24+
frankenphp.WithRequestDocumentRoot(dir+"/avatar.gif\x00/public", false),
25+
)
26+
require.NoError(t, err)
27+
28+
w := httptest.NewRecorder()
29+
_ = frankenphp.ServeHTTP(w, req)
30+
31+
assert.Equal(t, http.StatusBadRequest, w.Code)
32+
assert.NotContains(t, w.Body.String(), "executed")
33+
}

‎requestoptions.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ var (
3535
// directive will set $_SERVER['DOCUMENT_ROOT'] to the real directory path.
3636
func WithRequestDocumentRoot(documentRoot string, resolveSymlink bool) RequestOption {
3737
return func(o *frankenPHPContext) (err error) {
38+
// PHP reads SCRIPT_FILENAME as a C string: a NUL byte from a placeholder root would cut off the script name
39+
if strings.IndexByte(documentRoot, 0) != -1 {
40+
return ErrInvalidRequestPath
41+
}
42+
3843
v, ok := documentRootCache.Load(documentRoot)
3944
if !ok {
4045
// make sure file root is absolute

0 commit comments

Comments
 (0)