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/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/formatting/wpParseStr.php b/tests/phpunit/tests/formatting/wpParseStr.php index 09cd548fd0522..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. @@ -75,6 +77,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( 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' ),