Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/username-changer/class-username-changer.php
+++ b/username-changer/class-username-changer.php
@@ -5,7 +5,7 @@
* Description: Change usernames easily
* Author: DigitalME
* Author URI: https://www.digitalme.cc
- * Version: 3.3.1
+ * Version: 3.3.2
* Text Domain: username-changer
* Domain Path: languages
* Tested up to: 7.1
@@ -118,7 +118,7 @@
private function setup_constants() {
// Plugin version.
if ( ! defined( 'USERNAME_CHANGER_VER' ) ) {
- define( 'USERNAME_CHANGER_VER', '3.3.1' );
+ define( 'USERNAME_CHANGER_VER', '3.3.2' );
}
// Plugin path.
--- a/username-changer/includes/admin/actions.php
+++ b/username-changer/includes/admin/actions.php
@@ -86,8 +86,8 @@
// Send emails as necessary.
if ( username_changer()->settings->get_option( 'enable_notifications', false ) ) {
- $changed_user = get_user_by( 'login', $old_username );
- $mail_to = $changed_user->user_email;
+ $changed_user = get_user_by( 'login', $new_username );
+ $mail_to = $changed_user ? $changed_user->user_email : '';
$subject = username_changer_do_tags( username_changer()->settings->get_option( 'email_subject', __( 'Username change notification - {sitename}', 'username-changer' ) ), $old_username_tag, $new_username_tag );
$message = username_changer_do_tags( username_changer()->settings->get_option( 'email_message', __( 'Howdy! We're just writing to let you know that your username for {siteurl} has been changed to {new_username}.', 'username-changer' ) . "nn" . __( 'Login now at {loginurl}', 'username-changer' ) ), $old_username_tag, $new_username_tag );
@@ -100,7 +100,9 @@
$headers = 'From: ' . stripslashes_deep( html_entity_decode( $from_name, ENT_COMPAT, 'UTF-8' ) ) . " <$from_email>rn";
$headers .= 'Reply-To: ' . $from_email . "rn";
- wp_mail( $mail_to, $subject, $message, $headers ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
+ if ( ! empty( $mail_to ) ) {
+ wp_mail( $mail_to, $subject, $message, $headers ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
+ }
}
}
} else {
--- a/username-changer/includes/misc-functions.php
+++ b/username-changer/includes/misc-functions.php
@@ -60,6 +60,140 @@
/**
+ * Check whether a login is reserved by the network's Super Admin list
+ *
+ * On multisite, WordPress decides who is a Super Admin by matching the
+ * user_login of the current user against the "site_admins" network option.
+ * A login listed there must never be handed to anyone else, otherwise
+ * whoever ends up owning that login inherits network-wide privileges.
+ *
+ * @since 3.3.2
+ * @param string $login The login to check.
+ * @param string $own_login The current login of the user being renamed (allowed to match).
+ * @return bool Whether the login is a reserved Super Admin login belonging to someone else
+ */
+function username_changer_is_reserved_super_admin_login( $login, $own_login = '' ) {
+ if ( ! is_multisite() ) {
+ return false;
+ }
+
+ foreach ( (array) get_super_admins() as $super_admin ) {
+ if ( ! is_string( $super_admin ) ) {
+ continue;
+ }
+
+ if ( 0 === strcasecmp( $super_admin, $login ) && 0 !== strcasecmp( $super_admin, $own_login ) ) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+
+/**
+ * Keep the network's Super Admin list in sync after a rename
+ *
+ * Replaces the old login with the new one in the "site_admins" network
+ * option, so the renamed account stays a Super Admin and the old login is
+ * not left behind as an unowned entry that could be registered by someone
+ * else. This edits the option directly because core's revoke_super_admin()
+ * refuses to act on the account matching the network admin email.
+ *
+ * @since 3.3.2
+ * @param string $old_login The login before the rename.
+ * @param string $new_login The login after the rename.
+ * @return bool Whether the Super Admin list was changed
+ */
+function username_changer_sync_super_admin( $old_login, $new_login ) {
+ // A list defined in wp-config.php overrides the option; nothing to sync.
+ if ( ! is_multisite() || isset( $GLOBALS['super_admins'] ) ) {
+ return false;
+ }
+
+ $site_admins = get_site_option( 'site_admins', array( 'admin' ) );
+
+ if ( ! is_array( $site_admins ) ) {
+ return false;
+ }
+
+ $was_super_admin = false;
+ $updated = array();
+
+ foreach ( $site_admins as $super_admin ) {
+ if ( is_string( $super_admin ) && 0 === strcasecmp( $super_admin, $old_login ) ) {
+ $was_super_admin = true;
+ continue;
+ }
+
+ $updated[] = $super_admin;
+ }
+
+ if ( ! $was_super_admin ) {
+ return false;
+ }
+
+ if ( ! in_array( $new_login, $updated, true ) ) {
+ $updated[] = $new_login;
+ }
+
+ update_site_option( 'site_admins', array_values( $updated ) );
+
+ return true;
+}
+
+
+/**
+ * One-time cleanup of Super Admin entries that no user owns
+ *
+ * Earlier versions left the old login in "site_admins" when a Super Admin
+ * was renamed. Networks that already did this are exposed until the stale
+ * entry is removed, so remove any entry without an owning account once.
+ *
+ * @since 3.3.2
+ * @return void
+ */
+function username_changer_cleanup_orphaned_super_admins() {
+ if ( ! is_multisite() || isset( $GLOBALS['super_admins'] ) || ! is_super_admin() ) {
+ return;
+ }
+
+ if ( get_site_option( 'username_changer_orphans_cleaned' ) ) {
+ return;
+ }
+
+ if ( ! apply_filters( 'username_changer_cleanup_orphaned_super_admins', true ) ) {
+ return;
+ }
+
+ $site_admins = get_site_option( 'site_admins', array( 'admin' ) );
+
+ if ( is_array( $site_admins ) ) {
+ $kept = array();
+ $removed = array();
+
+ foreach ( $site_admins as $super_admin ) {
+ if ( is_string( $super_admin ) && ! username_exists( $super_admin ) ) {
+ $removed[] = $super_admin;
+ } else {
+ $kept[] = $super_admin;
+ }
+ }
+
+ if ( ! empty( $removed ) ) {
+ update_site_option( 'site_admins', array_values( $kept ) );
+
+ // Let devs know which unowned entries were removed.
+ do_action( 'username_changer_orphaned_super_admins_removed', $removed );
+ }
+ }
+
+ update_site_option( 'username_changer_orphans_cleaned', USERNAME_CHANGER_VER );
+}
+add_action( 'admin_init', 'username_changer_cleanup_orphaned_super_admins' );
+
+
+/**
* Process a username change
*
* @since 3.0.0
@@ -75,6 +209,22 @@
// One last sanity check to ensure the user exists.
$user_id = username_exists( $old_username );
if ( $user_id ) {
+ // Use the login exactly as stored, before anything changes.
+ $old_user = get_userdata( $user_id );
+ $stored_login = $old_user ? $old_user->user_login : $old_username;
+
+ if ( is_multisite() ) {
+ // Never hand a Super Admin's login to a different account.
+ if ( username_changer_is_reserved_super_admin_login( $new_username, $stored_login ) ) {
+ return false;
+ }
+
+ // A Super Admin list defined in wp-config.php cannot be updated from here.
+ if ( isset( $GLOBALS['super_admins'] ) && in_array( strtolower( $stored_login ), array_map( 'strtolower', array_filter( (array) $GLOBALS['super_admins'], 'is_string' ) ), true ) ) {
+ return false;
+ }
+ }
+
// Let devs hook into the process.
do_action( 'username_changer_before_process', $old_username, $new_username );
@@ -96,10 +246,11 @@
update_user_meta( $user_id, 'nickname', $new_username );
}
- // If the user is a Super Admin, update their permissions.
- if ( is_multisite() && is_super_admin( $user_id ) ) {
- grant_super_admin( $user_id );
- }
+ // The raw SQL update above bypasses WordPress, so clear the stale cached user.
+ clean_user_cache( $user_id );
+
+ // If the user is a Super Admin, move their entry to the new login.
+ username_changer_sync_super_admin( $stored_login, $new_username );
// Reassign Coauthor Attribution.
if ( username_changer_plugin_installed( 'co-authors-plus/co-authors-plus.php' ) ) {