Skip to content

Fix uploaded files leak in request_parse_body() - #25387

Open
dunglas wants to merge 1 commit into
php:PHP-8.4from
dunglas:fix-rfc1867-uploaded-files-leak
Open

dunglas wants to merge 1 commit into
php:PHP-8.4from
dunglas:fix-rfc1867-uploaded-files-leak

Conversation

@dunglas

@dunglas dunglas commented Nov 11, 2026

Copy link
Copy Markdown
Member

While working on the "Request lifecycle hooks for SAPIs with a worker mode" RFC, I discovered a bug affecting existing SAPIs.

Calling request_parse_body() on a multipart request whose body was already parsed replaces SG(rfc1867_uploaded_files) with a new instance. This happens when the body was parsed at startup (POST with enable_post_data_reading=1), or when the userland request_parse_body() function is called several times.

The files from the previous parses are then lost. This patch reuses the existing table instead.

Related to #24802 and #24987, but neither the same bug nor the same fix.

AI: Claude Code used to find the issue and write the patch.

Calling request_parse_body() on a multipart request whose body was already parsed (at startup, or by a previous call) replaced SG(rfc1867_uploaded_files). The files from the first parse were no longer accepted by is_uploaded_file() and move_uploaded_file(), and were never deleted at the end of the request.
$tmp_name = $_FILES['file_name']['tmp_name'];
var_dump(is_uploaded_file($tmp_name));

request_parse_body();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/ext/standard/tests/http/request_parse_body/multipart_twice.phpt
+++ b/ext/standard/tests/http/request_parse_body/multipart_twice.phpt
@@ -17,11 +17,29 @@
 $tmp_name = $files['file_name']['tmp_name'];
 var_dump(is_uploaded_file($tmp_name));

-request_parse_body();
+var_dump(request_parse_body());

 var_dump(is_uploaded_file($tmp_name));

+$dst = __DIR__ . '/multipart_twice.txt';
+var_dump(move_uploaded_file($tmp_name, $dst));
+var_dump(file_get_contents($dst));
+
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/multipart_twice.txt');
 ?>
 --EXPECT--
 bool(true)
+array(2) {
+  [0]=>
+  array(0) {
+  }
+  [1]=>
+  array(0) {
+  }
+}
+bool(true)
 bool(true)
+string(9) "file data"

$tmp_name = $files['file_name']['tmp_name'];
var_dump(is_uploaded_file($tmp_name));

request_parse_body();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--- a/ext/standard/tests/http/request_parse_body/multipart_after_startup_parse.phpt
+++ b/ext/standard/tests/http/request_parse_body/multipart_after_startup_parse.phpt
@@ -14,11 +14,29 @@
 $tmp_name = $_FILES['file_name']['tmp_name'];
 var_dump(is_uploaded_file($tmp_name));

-request_parse_body();
+var_dump(request_parse_body());

 var_dump(is_uploaded_file($tmp_name));

+$dst = __DIR__ . '/multipart_after_startup_parse.txt';
+var_dump(move_uploaded_file($tmp_name, $dst));
+var_dump(file_get_contents($dst));
+
+?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . '/multipart_after_startup_parse.txt');
 ?>
 --EXPECT--
 bool(true)
+array(2) {
+  [0]=>
+  array(0) {
+  }
+  [1]=>
+  array(0) {
+  }
+}
+bool(true)
 bool(true)
+string(9) "file data"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants