Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Improve editable_roles validation in multisite environments #7999

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/wp-admin/includes/ms.php
Original file line number Diff line number Diff line change
Expand Up @@ -1172,3 +1172,17 @@ function get_site_screen_help_sidebar_content() {
'<p>' . __( '<a href="https://developer.wordpress.org/advanced-administration/multisite/admin/#network-admin-sites-screen">Documentation on Site Management</a>' ) . '</p>' .
'<p>' . __( '<a href="https://wordpress.org/support/forum/multisite/">Support forums</a>' ) . '</p>';
}

/**
* Stop execution if the role can not be assigned by the current user.
*
* @since 6.8.0
*
* @param string $role Role the user is attempting to assign.
*/
function wp_ensure_editable_role( $role ) {
$roles = get_editable_roles();
if ( ! isset( $roles[ $role ] ) ) {
wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );
}
}
5 changes: 5 additions & 0 deletions src/wp-admin/user-new.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
$redirect = add_query_arg( array( 'update' => 'addexisting' ), 'user-new.php' );
} else {
if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {

wp_ensure_editable_role( $_REQUEST['role'] );

$result = add_existing_user_to_blog(
array(
'user_id' => $user_id,
Expand Down Expand Up @@ -225,6 +228,8 @@
add_filter( 'wpmu_welcome_user_notification', '__return_false' ); // Disable welcome email.
}

wp_ensure_editable_role( $_REQUEST['role'] );

wpmu_signup_user(
$new_user_login,
$new_user_email,
Expand Down
48 changes: 48 additions & 0 deletions tests/phpunit/tests/multisite/wpmuValidateUserSignup.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,54 @@ public function test_signup_nonce_check_invalid() {

$this->assertContains( 'invalid_nonce', $valid['errors']->get_error_codes() );
}

/**
* Ensure that wp_ensure_editable_role does not throw an exception when the role is editable.
*
* @ticket 43251
*
* @covers ::wp_ensure_editable_role
*/
public function test_wp_ensure_editable_role_allows_editable_roles() {
$role = get_role( 'editor' );
$this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
$this->assertNull( wp_ensure_editable_role( 'editor' ), 'The editor role should be editable.' );
}

/**
* Ensure that wp_ensure_editable_role throws an exception for non-existent roles.
*
* @ticket 43251
*
* @covers ::wp_ensure_editable_role
*/
public function test_wp_ensure_editable_role_does_not_allow_non_existent_role() {
$this->expectException( 'WPDieException' );
$role = get_role( 'non-existent-role' );
$this->assertNotInstanceOf( 'WP_Role', $role, 'The non-existent-role role should not exist.' );
wp_ensure_editable_role( 'non-existent-role' );
}

/**
* Ensure that wp_ensure_editable_role throws an exception for roles that are not editable.
*
* @ticket 43251
*
* @covers ::wp_ensure_editable_role
*/
public function test_wp_ensure_editable_role_does_not_allow_uneditable_roles() {
add_filter(
'editable_roles',
function ( $roles ) {
unset( $roles['editor'] );
return $roles;
}
);
$this->expectException( 'WPDieException' );
$role = get_role( 'editor' );
$this->assertInstanceOf( 'WP_Role', $role, 'The editor role should exist.' );
wp_ensure_editable_role( 'editor' );
}
}

endif;
Loading