From 500be0b298db3eafa46d6f38c642fdbb6f2dac46 Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 12:45:24 +0000 Subject: [PATCH 1/2] Upload: Report incomplete writes in `wp_upload_bits()`. `wp_upload_bits()` ignores the results of `fwrite()` and `fclose()`. When only part of the data is written, the function still returns the file as a successful upload. The byte count is now compared with the length of the data, and the result of closing the stream is checked. As of PHP 8.6, `fclose()` returns `false` when flushing or closing the stream fails. In both cases the existing "Could not write file" error is returned. --- src/wp-includes/functions.php | 15 +- .../phpunit/tests/functions/wpUploadBits.php | 135 ++++++++++++++++++ 2 files changed, 148 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/functions/wpUploadBits.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 59d91815d122e..b02c6a787279d 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -3046,10 +3046,21 @@ function wp_upload_bits( $name, $deprecated, $bits, $time = null ) { ); } - fwrite( $ifp, $bits ); - fclose( $ifp ); + $bytes_written = fwrite( $ifp, $bits ); + $closed = fclose( $ifp ); clearstatcache(); + /* + * A short write means the file is incomplete. As of PHP 8.6, fclose() also + * returns false when flushing or closing the stream fails. + */ + if ( strlen( $bits ) !== $bytes_written || ! $closed ) { + return array( + /* translators: %s: File name. */ + 'error' => sprintf( __( 'Could not write file %s' ), $new_file ), + ); + } + // Set correct file permissions. $stat = @ stat( dirname( $new_file ) ); $perms = $stat['mode'] & 0007777; diff --git a/tests/phpunit/tests/functions/wpUploadBits.php b/tests/phpunit/tests/functions/wpUploadBits.php new file mode 100644 index 0000000000000..f7ee2a8c3c984 --- /dev/null +++ b/tests/phpunit/tests/functions/wpUploadBits.php @@ -0,0 +1,135 @@ +upload_bits_to_stream( 'WP_Test_Stream', 'File contents.' ); + + $this->assertFalse( $actual['error'], 'The upload should not report an error.' ); + $this->assertSame( self::PROTOCOL . '://uploads/test.txt', $actual['file'], 'The file path should be returned.' ); + $this->assertSame( 'File contents.', WP_Test_Stream::$data['uploads']['/test.txt'], 'The contents should be stored.' ); + } + + /** + * Tests that an incomplete write is reported as an error. + */ + public function test_should_return_an_error_when_not_all_bytes_are_written() { + $actual = $this->upload_bits_to_stream( 'WP_Test_Stream_Upload_Bits_Short_Write', 'File contents.' ); + + $this->assertIsString( $actual['error'], 'The upload should report an error.' ); + $this->assertArrayNotHasKey( 'file', $actual, 'No file should be returned.' ); + } + + /** + * Tests that a failure while flushing the stream is reported as an error. + * + * PHP 8.6 is the first version where fclose() reports flush and close failures. + * + * @requires PHP >= 8.6 + */ + public function test_should_return_an_error_when_the_stream_cannot_be_flushed() { + $actual = $this->upload_bits_to_stream( 'WP_Test_Stream_Upload_Bits_Flush_Failure', 'File contents.' ); + + $this->assertIsString( $actual['error'], 'The upload should report an error.' ); + $this->assertArrayNotHasKey( 'file', $actual, 'No file should be returned.' ); + } + + /** + * Uploads bits to an in-memory stream. + * + * @param string $wrapper_class The stream wrapper class. + * @param string $bits The file contents. + * @return array The result of wp_upload_bits(). + */ + private function upload_bits_to_stream( $wrapper_class, $bits ) { + stream_wrapper_register( self::PROTOCOL, $wrapper_class ); + WP_Test_Stream::$data = array( + 'uploads' => array( + '/' => 'DIRECTORY', + ), + ); + + add_filter( 'upload_dir', array( $this, 'filter_upload_dir' ) ); + + return wp_upload_bits( 'test.txt', null, $bits ); + } + + /** + * Points the upload directory at the in-memory stream. + * + * @param array $uploads Upload directory data. + * @return array Filtered upload directory data. + */ + public function filter_upload_dir( $uploads ) { + $uploads['path'] = self::PROTOCOL . '://uploads'; + $uploads['basedir'] = self::PROTOCOL . '://uploads'; + $uploads['subdir'] = ''; + $uploads['url'] = 'http://example.org/uploads'; + $uploads['baseurl'] = 'http://example.org/uploads'; + $uploads['error'] = false; + + return $uploads; + } +} + +/** + * A stream wrapper that accepts only part of the data. + */ +class WP_Test_Stream_Upload_Bits_Short_Write extends WP_Test_Stream { + + /** + * Whether the first chunk has been written. + * + * @var bool + */ + private $written = false; + + public function stream_write( $data ) { + if ( $this->written ) { + return 0; + } + + $this->written = true; + + return parent::stream_write( substr( $data, 0, 4 ) ); + } +} + +/** + * A stream wrapper that accepts data but cannot flush it. + */ +class WP_Test_Stream_Upload_Bits_Flush_Failure extends WP_Test_Stream { + public function stream_flush() { + return false; + } +} From 66f405f2d982d161f37a51628b7ccd7134088718 Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 13:03:59 +0000 Subject: [PATCH 2/2] Tests: Add the ticket reference for #66786. --- tests/phpunit/tests/functions/wpUploadBits.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/phpunit/tests/functions/wpUploadBits.php b/tests/phpunit/tests/functions/wpUploadBits.php index f7ee2a8c3c984..67c64b7ae4930 100644 --- a/tests/phpunit/tests/functions/wpUploadBits.php +++ b/tests/phpunit/tests/functions/wpUploadBits.php @@ -31,6 +31,8 @@ public function tear_down() { /** * Tests that a complete write is reported as a success. + * + * @ticket 66786 */ public function test_should_return_the_file_when_all_bytes_are_written() { $actual = $this->upload_bits_to_stream( 'WP_Test_Stream', 'File contents.' ); @@ -42,6 +44,8 @@ public function test_should_return_the_file_when_all_bytes_are_written() { /** * Tests that an incomplete write is reported as an error. + * + * @ticket 66786 */ public function test_should_return_an_error_when_not_all_bytes_are_written() { $actual = $this->upload_bits_to_stream( 'WP_Test_Stream_Upload_Bits_Short_Write', 'File contents.' ); @@ -55,6 +59,8 @@ public function test_should_return_an_error_when_not_all_bytes_are_written() { * * PHP 8.6 is the first version where fclose() reports flush and close failures. * + * @ticket 66786 + * * @requires PHP >= 8.6 */ public function test_should_return_an_error_when_the_stream_cannot_be_flushed() {