Skip to content

Commit 6dbc2bc

Browse files
Abilities API: Check the post capability in the content update and delete permission callbacks.
The permission callbacks of `core/content-update` and `core/content-delete` now require `edit_post` or `delete_post` on the post that `where` names, the way the `core/content-create` callback already requires `create_posts`. Before, they only required a logged-in user and an exposed post type. A post that does not exist is let through, so that the call still ends as not found. The execute callbacks keep their own capability check. It applies when the `wp_ability_permission_result` filter grants the permission and when an execute callback is called directly. In the tests, a post the user cannot edit or delete is now refused by the permission callback. New tests cover the callback for input that was not prepared, for a post the user can edit or delete and for a missing post, and the capability check that remains after a filter grants the permission.
1 parent 3f6b7e9 commit 6dbc2bc

5 files changed

Lines changed: 233 additions & 48 deletions

File tree

‎src/wp-includes/abilities/class-wp-abilities-content.php‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ private function register_content_update( array $post_types, array $statuses ):
240240
return $this->execute_content_update( $input );
241241
},
242242
'permission_callback' => function ( $input = array() ): bool {
243-
return $this->check_post_permission( $input );
243+
return $this->check_post_permission( $input, 'edit_post' );
244244
},
245245
'meta' => array(
246246
'annotations' => array(
@@ -274,7 +274,7 @@ private function register_content_delete( array $post_types ): void {
274274
return $this->execute_content_delete( $input );
275275
},
276276
'permission_callback' => function ( $input = array() ): bool {
277-
return $this->check_post_permission( $input );
277+
return $this->check_post_permission( $input, 'delete_post' );
278278
},
279279
'meta' => array(
280280
'annotations' => array(
@@ -353,23 +353,31 @@ private function check_create_permission( $input = array() ): bool {
353353
/**
354354
* Permission callback for the `core/content-update` and `core/content-delete` abilities.
355355
*
356-
* A coarse gate: a logged-in user and a `where.type` that is exposed to abilities. The
357-
* post and the capabilities on it are checked by the execute callbacks.
356+
* Requires a logged-in user, a `where.type` that is exposed to abilities, and the given
357+
* capability on the post that `where` names. A post that does not exist is let through,
358+
* so that the execute callback can report it as not found. The execute callbacks check
359+
* the capability again and make every other decision.
358360
*
359361
* @since 7.3.0
360362
*
361-
* @param mixed $input Optional. The ability input. Default empty array.
363+
* @param mixed $input The ability input.
364+
* @param string $capability The capability required on the post: `edit_post` or `delete_post`.
362365
* @return bool True if the request may proceed, false otherwise.
363366
*/
364-
private function check_post_permission( $input = array() ): bool {
367+
private function check_post_permission( $input, string $capability ): bool {
365368
if ( ! is_user_logged_in() ) {
366369
return false;
367370
}
368371

369372
// The input is read defensively: WP_Ability::check_permissions() does not prepare it.
370373
$where = $this->get_input_object( rest_sanitize_object( $input ), 'where' ) ?? array();
374+
if ( ! $this->get_exposed_post_type( $where['type'] ?? null ) ) {
375+
return false;
376+
}
377+
378+
$post = $this->get_requested_post( array( 'where' => $where ) );
371379

372-
return null !== $this->get_exposed_post_type( $where['type'] ?? null );
380+
return ! $post || current_user_can( $capability, $post->ID );
373381
}
374382

375383
/**
@@ -920,6 +928,7 @@ private function execute_content_update( array $input ) {
920928
return $this->not_found_error();
921929
}
922930

931+
// Checked again after the permission callback, whose result a filter can change.
923932
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
924933
return new WP_Error(
925934
'content_cannot_edit',
@@ -968,6 +977,7 @@ private function execute_content_delete( array $input ) {
968977
return $this->not_found_error();
969978
}
970979

980+
// Checked again after the permission callback, whose result a filter can change.
971981
if ( ! current_user_can( 'delete_post', $post->ID ) ) {
972982
return new WP_Error(
973983
'content_cannot_delete',

‎tests/phpunit/tests/abilities-api/testcase-content-write-ability.php‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,19 @@ protected function assert_error( $result, string $code, int $status, string $con
258258
$this->assertSame( $status, $data['status'] ?? null, "{$context}: the error status should be {$status}." );
259259
}
260260

261+
/**
262+
* Asserts that a result is the error of a call that the permission callback refused.
263+
*
264+
* @since 7.3.0
265+
*
266+
* @param mixed $result The ability result.
267+
* @param string $context What the call was, for the assertion messages.
268+
*/
269+
protected function assert_refused( $result, string $context ): void {
270+
$this->assertWPError( $result, "{$context}: the call should fail." );
271+
$this->assertSame( 'ability_invalid_permissions', $result->get_error_code(), "{$context}: the permission callback should refuse the call." );
272+
}
273+
261274
/**
262275
* Asserts that a result is the invalid attribute error for the given attribute.
263276
*
@@ -307,6 +320,43 @@ protected function strip_descriptions( array $schema ): array {
307320
return $schema;
308321
}
309322

323+
/**
324+
* Returns the forms a `where` can take in input that was not prepared.
325+
*
326+
* @return array<string, array{id_type: string, as_object: bool}> The type of the ID, and
327+
* whether `where` is an object.
328+
*/
329+
public static function data_unprepared_where_forms(): array {
330+
return array(
331+
'integer ID' => array( 'integer', false ),
332+
'integer string ID' => array( 'string', false ),
333+
'float ID' => array( 'float', false ),
334+
'where that is an object' => array( 'integer', true ),
335+
);
336+
}
337+
338+
/**
339+
* Builds a `where` for a post in one of the forms of unprepared input.
340+
*
341+
* @since 7.3.0
342+
*
343+
* @param int $post_id The post ID.
344+
* @param string $id_type The type to send the ID as.
345+
* @param bool $as_object Whether to send `where` as an object.
346+
* @return array<string, mixed>|stdClass The `where`.
347+
*/
348+
protected function build_unprepared_where( int $post_id, string $id_type, bool $as_object ) {
349+
$id = $post_id;
350+
settype( $id, $id_type );
351+
352+
$where = array(
353+
'type' => get_post_type( $post_id ),
354+
'id' => $id,
355+
);
356+
357+
return $as_object ? (object) $where : $where;
358+
}
359+
310360
/**
311361
* Returns posts that cannot be reached as a `post`.
312362
*

‎tests/phpunit/tests/abilities-api/wpRegisterCoreContentDeleteAbility.php‎

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -425,18 +425,85 @@ public function test_permission_callback_returns_false_for_malformed_input( $inp
425425
}
426426

427427
/**
428-
* The permission callback is only a coarse gate: it lets a logged-in subscriber through, and
429-
* the execute callback decides on the post.
428+
* The permission callback returns false for a post the user cannot delete, in every form the
429+
* post can be named in input that was not prepared.
430+
*
431+
* @ticket 66706
432+
* @dataProvider data_unprepared_where_forms
433+
* @since 7.3.0
434+
*
435+
* @param string $id_type The type to send the ID as.
436+
* @param bool $as_object Whether to send `where` as an object.
437+
*/
438+
public function test_permission_callback_returns_false_for_a_post_the_user_cannot_delete( string $id_type, bool $as_object ): void {
439+
$this->login_as( 'contributor' );
440+
441+
$permission = $this->get_ability_callbacks()['permission_callback'];
442+
$where = $this->build_unprepared_where( self::$post_ids['admin_post'], $id_type, $as_object );
443+
444+
$this->assertFalse( $permission( array( 'where' => $where ) ), 'The permission callback should return false.' );
445+
}
446+
447+
/**
448+
* The permission callback returns true for a post the user can delete.
430449
*
431450
* @ticket 66706
432451
* @since 7.3.0
433452
*/
434-
public function test_permission_callback_returns_true_for_a_logged_in_user_and_an_exposed_type(): void {
453+
public function test_permission_callback_returns_true_for_a_post_the_user_can_delete(): void {
454+
$this->login_as( 'author' );
455+
456+
$permission = $this->get_ability_callbacks()['permission_callback'];
457+
458+
$this->assertTrue( $permission( $this->delete_input( self::$post_ids['author:draft'] ) ), 'The permission callback should return true.' );
459+
}
460+
461+
/**
462+
* The permission callback lets a post that does not exist through, also for a user who may
463+
* delete nothing.
464+
*
465+
* @ticket 66706
466+
* @since 7.3.0
467+
*/
468+
public function test_permission_callback_lets_a_missing_post_through(): void {
435469
$this->login_as( 'subscriber' );
436470

437471
$permission = $this->get_ability_callbacks()['permission_callback'];
438472

439-
$this->assertTrue( $permission( $this->delete_input( self::$post_ids['admin_post'] ) ), 'The permission callback should return true.' );
473+
$this->assertTrue(
474+
$permission(
475+
array(
476+
'where' => array(
477+
'type' => 'post',
478+
'id' => self::MISSING_ID,
479+
),
480+
)
481+
),
482+
'The permission callback should return true.'
483+
);
484+
}
485+
486+
/**
487+
* A permission result that a filter turned into a grant does not skip the capability check
488+
* on the post.
489+
*
490+
* @ticket 66706
491+
* @since 7.3.0
492+
*/
493+
public function test_granted_permission_result_does_not_skip_the_capability_check(): void {
494+
$post_id = self::$post_ids['admin_post'];
495+
496+
$this->login_as( 'contributor' );
497+
$this->register_abilities();
498+
add_filter( 'wp_ability_permission_result', '__return_true' );
499+
500+
$before = $this->snapshot( $post_id );
501+
$trash = $this->run_ability( $this->delete_input( $post_id ) );
502+
$permanent = $this->run_ability( $this->delete_input( $post_id, true ) );
503+
504+
$this->assert_cannot_delete( $trash, 'A trash granted by a filter' );
505+
$this->assert_cannot_delete( $permanent, 'A permanent delete granted by a filter' );
506+
$this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' );
440507
}
441508

442509
/**
@@ -603,7 +670,7 @@ public function test_direct_call_with_a_malformed_where_is_not_found( array $inp
603670

604671
/**
605672
* The lookup comes before the capability: a user who may delete nothing gets the not-found
606-
* error for a missing post, and the 403 error for an existing post.
673+
* error for a missing post, and is refused for an existing post.
607674
*
608675
* @ticket 66706
609676
* @since 7.3.0
@@ -624,7 +691,7 @@ public function test_lookup_comes_before_the_capability(): void {
624691
$existing = $this->run_ability( $this->delete_input( self::$post_ids['admin_post'] ) );
625692

626693
$this->assert_not_found( $missing, 'A missing post' );
627-
$this->assert_cannot_delete( $existing, 'An existing post the user may not delete' );
694+
$this->assert_refused( $existing, 'An existing post the user may not delete' );
628695
$this->assertSame( $before, $this->snapshot( self::$post_ids['admin_post'] ), 'The refused post should still exist, unchanged.' );
629696
}
630697

@@ -710,7 +777,7 @@ public static function data_delete_matrix(): array {
710777

711778
/**
712779
* Each role may trash the posts it may delete, gets the 410 error for those already in the
713-
* Trash, and the 403 error for the others, whatever their status.
780+
* Trash, and is refused for the others, whatever their status.
714781
*
715782
* @ticket 66706
716783
* @dataProvider data_delete_matrix
@@ -738,14 +805,14 @@ public function test_trash_matrix( string $role, string $owner, string $status,
738805
if ( $may_delete ) {
739806
$this->assert_error( $result, 'content_already_trashed', 410, 'Trashing a trashed post the role may delete' );
740807
} else {
741-
$this->assert_cannot_delete( $result, 'Trashing a post the role may not delete' );
808+
$this->assert_refused( $result, 'Trashing a post the role may not delete' );
742809
}
743810
$this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' );
744811
}
745812

746813
/**
747814
* Each role may delete permanently the posts it may delete, including posts in the Trash, and
748-
* gets the 403 error for the others, whatever their status.
815+
* is refused for the others, whatever their status.
749816
*
750817
* @ticket 66706
751818
* @dataProvider data_delete_matrix
@@ -770,7 +837,7 @@ public function test_permanent_delete_matrix( string $role, string $owner, strin
770837
return;
771838
}
772839

773-
$this->assert_cannot_delete( $result, 'Deleting a post the role may not delete' );
840+
$this->assert_refused( $result, 'Deleting a post the role may not delete' );
774841
$this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' );
775842
}
776843

@@ -863,8 +930,8 @@ public function test_editor_cannot_delete_a_special_page( string $page ): void {
863930
$trash = $this->run_ability( $this->delete_input( $post_id ) );
864931
$permanent = $this->run_ability( $this->delete_input( $post_id, true ) );
865932

866-
$this->assert_cannot_delete( $trash, 'An editor trashing a special page' );
867-
$this->assert_cannot_delete( $permanent, 'An editor deleting a special page' );
933+
$this->assert_refused( $trash, 'An editor trashing a special page' );
934+
$this->assert_refused( $permanent, 'An editor deleting a special page' );
868935
$this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' );
869936
}
870937

@@ -955,7 +1022,7 @@ public function test_site_administrator_deletes_special_pages_on_multisite( stri
9551022
return;
9561023
}
9571024

958-
$this->assert_cannot_delete( $result, 'A site administrator trashing the privacy policy page' );
1025+
$this->assert_refused( $result, 'A site administrator trashing the privacy policy page' );
9591026
$this->assertSame( $before, $this->snapshot( $post_id ), 'Nothing should be written.' );
9601027
}
9611028

0 commit comments

Comments
 (0)