diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php index 11bc499fc6720..8aec375bc8bb8 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php @@ -504,9 +504,9 @@ public function get_item_permissions_check( $request ) { ); } - if ( $post && ! empty( $request['password'] ) ) { + if ( $post && ! empty( $request->get_query_params()['password'] ) ) { // Check post password, and return error if invalid. - if ( ! hash_equals( $post->post_password, $request['password'] ) ) { + if ( ! hash_equals( $post->post_password, $request->get_query_params()['password'] ) ) { return new WP_Error( 'rest_post_incorrect_password', __( 'Incorrect post password.' ), diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php index 3085d066fc6ae..9b697fe2efd3d 100644 --- a/tests/phpunit/tests/rest-api/rest-posts-controller.php +++ b/tests/phpunit/tests/rest-api/rest-posts-controller.php @@ -2232,6 +2232,51 @@ public function test_get_post_with_password_without_permission() { $this->assertTrue( $data['excerpt']['protected'] ); } + /** + * @ticket 61837 + */ + public function test_get_item_permissions_check_while_updating_password() { + $endpoint = new WP_REST_Posts_Controller( 'post' ); + + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); + $request->set_url_params( array( 'id' => self::$post_id ) ); + $request->set_body_params( + $this->set_post_data( + array( + 'id' => self::$post_id, + 'password' => '123', + ) + ) + ); + $permission = $endpoint->get_item_permissions_check( $request ); + + // Password provided in POST data, should not be used as authentication. + $this->assertNotWPError( $permission, 'Password in post body should be ignored by permissions check.' ); + $this->assertTrue( $permission ); + } + + /** + * @ticket 61837 + */ + public function test_get_item_permissions_check_while_updating_password_with_invalid_type() { + $endpoint = new WP_REST_Posts_Controller( 'post' ); + + $request = new WP_REST_Request( 'POST', sprintf( '/wp/v2/posts/%d', self::$post_id ) ); + $request->set_url_params( array( 'id' => self::$post_id ) ); + $request->set_body_params( + $this->set_post_data( + array( + 'id' => self::$post_id, + 'password' => 123, + ) + ) + ); + $permission = $endpoint->get_item_permissions_check( $request ); + + $this->assertNotWPError( $permission, 'Password in post body should be ignored by permissions check even when it is an invalid type.' ); + $this->assertTrue( $permission ); + } + /** * The post response should not have `block_version` when in view context. *