From 765e7d48aa5fa947c5e48f2076c31e6ac6c6bbdd Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 12:09:23 +0000 Subject: [PATCH 1/3] Formatting: Keep `wp_parse_str()` working when the string contains a NUL byte. As of PHP 8.6, `parse_str()` throws a `ValueError` when its input contains a NUL byte. Earlier PHP versions stopped reading at the first NUL byte and returned what had been parsed up to that point. `wp_parse_str()` passes its input straight through, so `add_query_arg()`, `remove_query_arg()` and `wp_parse_args()` now throw on PHP 8.6 for input they previously accepted. The input is now cut at the first NUL byte before parsing, which keeps the earlier result on every supported PHP version. --- src/wp-includes/formatting.php | 13 ++++++++++++- tests/phpunit/tests/formatting/wpParseStr.php | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index ad12579fdafa1..e949f5786dab4 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -5299,7 +5299,18 @@ function map_deep( $value, $callback ) { * @param array $result Variables will be stored in this array. */ function wp_parse_str( $input_string, &$result ) { - parse_str( (string) $input_string, $result ); + $input_string = (string) $input_string; + + /* + * As of PHP 8.6, parse_str() throws a ValueError when the string contains a NUL byte. + * Earlier versions stopped reading at the first NUL byte, so the same result is kept here. + */ + $nul_position = strpos( $input_string, "\0" ); + if ( false !== $nul_position ) { + $input_string = substr( $input_string, 0, $nul_position ); + } + + parse_str( $input_string, $result ); /** * Filters the array of variables derived from a parsed string. diff --git a/tests/phpunit/tests/formatting/wpParseStr.php b/tests/phpunit/tests/formatting/wpParseStr.php index 09cd548fd0522..efa4fe45631ab 100644 --- a/tests/phpunit/tests/formatting/wpParseStr.php +++ b/tests/phpunit/tests/formatting/wpParseStr.php @@ -75,6 +75,23 @@ public static function data_wp_parse_str() { '_baba' => 'dudu', ), ), + 'NUL byte in value' => array( + 'input' => "x=5\0z&_baba=dudu", + 'expected' => array( + 'x' => '5', + ), + ), + 'NUL byte in name' => array( + 'input' => "x=5&_ba\0ba=dudu", + 'expected' => array( + 'x' => '5', + '_ba' => '', + ), + ), + 'leading NUL byte' => array( + 'input' => "\0x=5", + 'expected' => array(), + ), 'stringable object' => array( 'input' => new Fixture_Formatting_wpParseStr(), 'expected' => array( From 86d3eb64b4592fe2e182703b1baac9a45121b925 Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 13:03:56 +0000 Subject: [PATCH 2/3] Tests: Add the ticket reference for #66785. --- tests/phpunit/tests/formatting/wpParseStr.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/formatting/wpParseStr.php b/tests/phpunit/tests/formatting/wpParseStr.php index efa4fe45631ab..72041b31dfa5a 100644 --- a/tests/phpunit/tests/formatting/wpParseStr.php +++ b/tests/phpunit/tests/formatting/wpParseStr.php @@ -13,6 +13,8 @@ class Tests_Formatting_wpParseStr extends WP_UnitTestCase { * Note: While the function under test does not contain any significant logic, * these tests document the behavior and safeguard PHP cross-version compatibility. * + * @ticket 66785 + * * @dataProvider data_wp_parse_str * * @param mixed $input Value to parse. From a6f9273976896e253f8e68ce10a7eca4b4a83712 Mon Sep 17 00:00:00 2001 From: Shane Rounce Date: Mon, 23 Nov 2026 13:52:29 +0000 Subject: [PATCH 3/3] REST API: Keep parsing URL-encoded request bodies that contain a NUL byte. `WP_REST_Request::parse_body_params()` passes the raw request body to `parse_str()` for PUT, PATCH and DELETE requests. As of PHP 8.6 that throws a `ValueError` when the body contains a NUL byte, where earlier versions stopped reading at that byte. The body is now cut at the first NUL byte before parsing, matching the change to `wp_parse_str()`. --- .../rest-api/class-wp-rest-request.php | 13 ++++++++++++- tests/phpunit/tests/rest-api/rest-request.php | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php index 5efb16b28ff5d..6d1925749f455 100644 --- a/src/wp-includes/rest-api/class-wp-rest-request.php +++ b/src/wp-includes/rest-api/class-wp-rest-request.php @@ -820,7 +820,18 @@ protected function parse_body_params() { return; } - parse_str( $this->get_body(), $params ); + $body = (string) $this->get_body(); + + /* + * As of PHP 8.6, parse_str() throws a ValueError when the string contains a NUL byte. + * Earlier versions stopped reading at the first NUL byte, so the same result is kept here. + */ + $nul_position = strpos( $body, "\0" ); + if ( false !== $nul_position ) { + $body = substr( $body, 0, $nul_position ); + } + + parse_str( $body, $params ); /* * Add to the POST parameters stored internally. If a user has already diff --git a/tests/phpunit/tests/rest-api/rest-request.php b/tests/phpunit/tests/rest-api/rest-request.php index bdec2b3dd2daa..c43221563b729 100644 --- a/tests/phpunit/tests/rest-api/rest-request.php +++ b/tests/phpunit/tests/rest-api/rest-request.php @@ -341,6 +341,22 @@ public function test_non_post_body_parameters( $request_method ) { } } + /** + * Tests that a URL-encoded body containing a NUL byte is read up to that byte. + * + * @ticket 66785 + * + * @dataProvider data_non_post_body_parameters + */ + public function test_non_post_body_parameters_with_nul_byte( $request_method ) { + $this->request->set_method( $request_method ); + $this->request->set_body_params( array() ); + $this->request->set_body( "foo=bar\0baz&other=value" ); + + $this->assertSame( 'bar', $this->request->get_param( 'foo' ), 'The value should be read up to the NUL byte.' ); + $this->assertNull( $this->request->get_param( 'other' ), 'Nothing after the NUL byte should be read.' ); + } + public static function data_non_post_body_parameters() { return array( array( 'PUT' ),