Skip to:
Content

BuddyPress.org

Changeset 14214


Ignore:
Timestamp:
07/07/2026 08:02:23 PM (6 weeks ago)
Author:
dcavins
Message:

Components: Restrict updates to site admins.

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, johnjamesjacoby, espellcaste, kasthelord (Lukas Collishaw), 1353594865qq, jeromewincek (Jerome Wincek), vvh1te3zz.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/classes/class-bp-core-components-rest-controller.php

    r14042 r14214  
    267267         */
    268268        public function update_item_permissions_check( $request ) {
    269                 $retval = $this->get_items_permissions_check( $request );
     269                $retval = new WP_Error(
     270                        'bp_rest_authorization_required',
     271                        __( 'Sorry, you are not allowed to perform this action.', 'buddypress' ),
     272                        array(
     273                                'status' => rest_authorization_required_code(),
     274                        )
     275                );
     276
     277                // Unlike `get_items`, toggling a component is an admin-only operation.
     278                if ( bp_current_user_can( 'manage_options' ) ) {
     279                        $retval = true;
     280                }
    270281
    271282                /**
  • trunk/tests/phpunit/testcases/core/test-components-controller.php

    r14070 r14214  
    347347
    348348        /**
     349         * @group update_item
     350         */
     351        public function test_update_item_site_admin_only() {
     352                $u = static::factory()->user->create(
     353                        array(
     354                                'role' => 'author',
     355                        )
     356                );
     357
     358                $this->bp::set_current_user( $u );
     359
     360                // Snapshot so we can prove no component was toggled.
     361                $before = bp_get_option( 'bp-active-components' );
     362
     363                $request = new WP_REST_Request( 'PUT', $this->endpoint_url );
     364                $request->set_query_params( array(
     365                        'name'   => 'friends',
     366                        'action' => 'deactivate',
     367                ) );
     368                $response = $this->server->dispatch( $request );
     369
     370                $this->assertErrorResponse( 'bp_rest_authorization_required', $response, 403 );
     371
     372                // The component toggle must not have executed.
     373                $this->assertSame( $before, bp_get_option( 'bp-active-components' ) );
     374        }
     375
     376        /**
    349377         * @group delete_item
    350378         */
Note: See TracChangeset for help on using the changeset viewer.