Skip to content

Commit

Permalink
HTTP API: Ensure cookie names are cast to strings.
Browse files Browse the repository at this point in the history
Props nosilver4u, darssen, kraftbj, engahmeds3ed, barry.hughes, schlessera.
Fixes #58566.

git-svn-id: https://develop.svn.wordpress.org/trunk@57501 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
swissspidy committed Jan 31, 2024
1 parent 1338984 commit 7076ebb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/wp-includes/class-wp-http.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,9 @@ static function ( $attr ) {
return null !== $attr;
}
);
$cookie_jar[ $value->name ] = new WpOrg\Requests\Cookie( $value->name, $value->value, $attributes, array( 'host-only' => $value->host_only ) );
$cookie_jar[ $value->name ] = new WpOrg\Requests\Cookie( (string) $value->name, $value->value, $attributes, array( 'host-only' => $value->host_only ) );
} elseif ( is_scalar( $value ) ) {
$cookie_jar[ $name ] = new WpOrg\Requests\Cookie( $name, (string) $value );
$cookie_jar[ $name ] = new WpOrg\Requests\Cookie( (string) $name, (string) $value );
}
}

Expand Down
52 changes: 52 additions & 0 deletions tests/phpunit/tests/http/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -661,4 +661,56 @@ function ( $response, $parsed_args, $url ) use ( &$pre_http_request_filter_has_r
$this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' );
$this->assertTrue( $pre_http_request_filter_has_run, 'The pre_http_request filter is expected to run.' );
}

/**
* Test that WP_Http::normalize_cookies method correctly casts integer keys to string.
* @ticket 58566
*
* @covers WP_Http::normalize_cookies
*/
public function test_normalize_cookies_casts_integer_keys_to_string() {
$http = _wp_http_get_object();

$cookies = array(
'1' => 'foo',
2 => 'bar',
'qux' => 7,
);

$cookie_jar = $http->normalize_cookies( $cookies );

$this->assertInstanceOf( 'WpOrg\Requests\Cookie\Jar', $cookie_jar );

foreach ( array_keys( $cookies ) as $cookie ) {
if ( is_string( $cookie ) ) {
$this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar[ $cookie ] );
} else {
$this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar[ (string) $cookie ] );
}
}
}

/**
* Test that WP_Http::normalize_cookies method correctly casts integer cookie names to strings.
* @ticket 58566
*
* @covers WP_Http::normalize_cookies
*/
public function test_normalize_cookies_casts_cookie_name_integer_to_string() {
$http = _wp_http_get_object();

$cookies = array(
'foo' => new WP_Http_Cookie(
array(
'name' => 1,
'value' => 'foo',
)
),
);

$cookie_jar = $http->normalize_cookies( $cookies );

$this->assertInstanceOf( 'WpOrg\Requests\Cookie\Jar', $cookie_jar );
$this->assertInstanceOf( 'WpOrg\Requests\Cookie', $cookie_jar['1'] );
}
}

0 comments on commit 7076ebb

Please sign in to comment.