Skip to content

Commit b4b0bb9

Browse files
committed
Components: Restrict updates to site admins (0.7 branch).
Ensure that the current user has the `manage_options` capability before allowing them to change BuddyPress component status. Special thanks to kasthelord (Lukas Collishaw) who first reported this issue responsibly. Props emaralive, jjj, renato, kasthelord (Lukas Collishaw), 1353594865qq, jeromewincek (Jerome Wincek), vvh1te3zz. Special thanks to kasthelord (Lukas Collishaw) who first reported this issue responsibly. Props emaralive, jjj, renato, kasthelord (Lukas Collishaw), 1353594865qq, jeromewincek (Jerome Wincek), vvh1te3zz.
1 parent 8432518 commit b4b0bb9

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

includes/bp-components/classes/class-bp-rest-components-endpoint.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,18 @@ public function update_item( $request ) {
271271
* @return true|WP_Error
272272
*/
273273
public function update_item_permissions_check( $request ) {
274-
$retval = $this->get_items_permissions_check( $request );
274+
$retval = new WP_Error(
275+
'bp_rest_authorization_required',
276+
__( 'Sorry, you are not allowed to perform this action.', 'buddypress' ),
277+
array(
278+
'status' => rest_authorization_required_code(),
279+
)
280+
);
281+
282+
// Unlike `get_items`, toggling a component is an admin-only operation.
283+
if ( bp_current_user_can( 'manage_options' ) ) {
284+
$retval = true;
285+
}
275286

276287
/**
277288
* Filter the components `update_item` permissions check.

tests/testcases/components/test-controller.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,34 @@ public function test_update_item_without_permission() {
341341
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
342342
}
343343

344+
/**
345+
* @group update_item
346+
*/
347+
public function test_update_item_site_admin_only() {
348+
$u = static::factory()->user->create(
349+
array(
350+
'role' => 'author',
351+
)
352+
);
353+
354+
$this->bp::set_current_user( $u );
355+
356+
// Snapshot so we can prove no component was toggled.
357+
$before = bp_get_option( 'bp-active-components' );
358+
359+
$request = new WP_REST_Request( 'PUT', $this->endpoint_url );
360+
$request->set_query_params( array(
361+
'name' => 'friends',
362+
'action' => 'deactivate',
363+
) );
364+
$response = $this->server->dispatch( $request );
365+
366+
$this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
367+
368+
// The component toggle must not have executed.
369+
$this->assertSame( $before, bp_get_option( 'bp-active-components' ) );
370+
}
371+
344372
/**
345373
* @group delete_item
346374
*/

0 commit comments

Comments
 (0)