Skip to content

Commit 15b7d2a

Browse files
committed
REST API: Only check password value in query parameters while checking post permissions.
The `password` property which gets sent as part of a request POST body while setting a post's password should not be checked when calculating post visibility permissions. That value in the request body is intended to update the post, not to authenticate, and may be malformed or an invalid non-string type which would cause a fatal when checking against the hashed post password value. Query parameter `?password=` values are the correct interface to check, and are also guaranteed to be strings. Props mlf20, devansh016, antonvlasenko, TimothyBlynJacobs, kadamwhite. Fixes #61837. git-svn-id: https://develop.svn.wordpress.org/trunk@59036 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d3d02c4 commit 15b7d2a

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,9 @@ public function get_item_permissions_check( $request ) {
504504
);
505505
}
506506

507-
if ( $post && ! empty( $request['password'] ) ) {
507+
if ( $post && ! empty( $request->get_query_params()['password'] ) ) {
508508
// Check post password, and return error if invalid.
509-
if ( ! hash_equals( $post->post_password, $request['password'] ) ) {
509+
if ( ! hash_equals( $post->post_password, $request->get_query_params()['password'] ) ) {
510510
return new WP_Error(
511511
'rest_post_incorrect_password',
512512
__( 'Incorrect post password.' ),

tests/phpunit/tests/rest-api/rest-posts-controller.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,6 +2232,51 @@ public function test_get_post_with_password_without_permission() {
22322232
$this->assertTrue( $data['excerpt']['protected'] );
22332233
}
22342234

2235+
/**
2236+
* @ticket 61837
2237+
*/
2238+
public function test_get_item_permissions_check_while_updating_password() {
2239+
$endpoint = new WP_REST_Posts_Controller( 'post' );
2240+
2241+
$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
2242+
$request->set_url_params( array( 'id' => self::$post_id ) );
2243+
$request->set_body_params(
2244+
$this->set_post_data(
2245+
array(
2246+
'id' => self::$post_id,
2247+
'password' => '123',
2248+
)
2249+
)
2250+
);
2251+
$permission = $endpoint->get_item_permissions_check( $request );
2252+
2253+
// Password provided in POST data, should not be used as authentication.
2254+
$this->assertNotWPError( $permission, 'Password in post body should be ignored by permissions check.' );
2255+
$this->assertTrue( $permission );
2256+
}
2257+
2258+
/**
2259+
* @ticket 61837
2260+
*/
2261+
public function test_get_item_permissions_check_while_updating_password_with_invalid_type() {
2262+
$endpoint = new WP_REST_Posts_Controller( 'post' );
2263+
2264+
$request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
2265+
$request->set_url_params( array( 'id' => self::$post_id ) );
2266+
$request->set_body_params(
2267+
$this->set_post_data(
2268+
array(
2269+
'id' => self::$post_id,
2270+
'password' => 123,
2271+
)
2272+
)
2273+
);
2274+
$permission = $endpoint->get_item_permissions_check( $request );
2275+
2276+
$this->assertNotWPError( $permission, 'Password in post body should be ignored by permissions check even when it is an invalid type.' );
2277+
$this->assertTrue( $permission );
2278+
}
2279+
22352280
/**
22362281
* The post response should not have `block_version` when in view context.
22372282
*

0 commit comments

Comments
 (0)