fix: prevent crash when worker terminates after a file upload

This commit is contained in:
Kévin Dunglas
2024-06-07 18:05:42 +02:00
parent acf6d0ffc4
commit 4537f27f67
3 changed files with 48 additions and 0 deletions

View File

@@ -137,6 +137,7 @@ static void frankenphp_worker_request_shutdown() {
zend_end_try();
zend_set_memory_limit(PG(memory_limit));
SG(rfc1867_uploaded_files) = NULL;
}
/* Adapted from php_request_startup() */

View File

@@ -5,10 +5,12 @@
package frankenphp_test
import (
"bytes"
"context"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"net/http/httptest"
@@ -600,6 +602,33 @@ func testRequestHeaders(t *testing.T, opts *testOptions) {
}, opts)
}
func TestFileUpload_module(t *testing.T) { testFileUpload(t, &testOptions{}) }
func TestFileUpload_worker(t *testing.T) {
testFileUpload(t, &testOptions{workerScript: "file-upload.php"})
}
func testFileUpload(t *testing.T, opts *testOptions) {
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
requestBody := &bytes.Buffer{}
writer := multipart.NewWriter(requestBody)
part, _ := writer.CreateFormFile("file", "foo.txt")
_, err := part.Write([]byte("bar"))
require.NoError(t, err)
writer.Close()
req := httptest.NewRequest("POST", "http://example.com/file-upload.php", requestBody)
req.Header.Add("Content-Type", writer.FormDataContentType())
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Contains(t, string(body), "Upload OK")
}, opts)
}
func TestExecuteScriptCLI(t *testing.T) {
if _, err := os.Stat("internal/testcli/testcli"); err != nil {
t.Skip("internal/testcli/testcli has not been compiled, run `cd internal/testcli/ && go build`")

18
testdata/file-upload.php vendored Normal file
View File

@@ -0,0 +1,18 @@
<?php
require_once __DIR__.'/_executor.php';
return function()
{
$uploaded = ($_FILES['file']['tmp_name'] ?? null) ? file_get_contents($_FILES['file']['tmp_name']) : null;
if ($uploaded) {
echo 'Upload OK';
return;
}
echo <<<'HTML'
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
HTML;
};