Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion src/wp-includes/rest-api/class-wp-rest-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit/tests/formatting/wpParseStr.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/tests/rest-api/rest-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
Expand Down
Loading