Skip to content
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
12 changes: 10 additions & 2 deletions src/wp-admin/user-new.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@

wp_ensure_editable_role( $_REQUEST['role'] );

// Capture the activation payload from the action so we can activate immediately if noconfirmation is set.
$activation_payload = null;
$capture_payload = static function ( $u, $e, $payload ) use ( &$activation_payload ) {
$activation_payload = $payload;
};
add_action( 'after_signup_user', $capture_payload, 10, 3 );

wpmu_signup_user(
$new_user_login,
$new_user_email,
Expand All @@ -245,9 +252,10 @@
)
);

remove_action( 'after_signup_user', $capture_payload, 10 );

if ( isset( $_POST['noconfirmation'] ) && current_user_can( 'manage_network_users' ) ) {
$key = $wpdb->get_var( $wpdb->prepare( "SELECT activation_key FROM {$wpdb->signups} WHERE user_login = %s AND user_email = %s", $new_user_login, $new_user_email ) );
$new_user = wpmu_activate_signup( $key );
$new_user = wpmu_activate_signup( $activation_payload );
if ( is_wp_error( $new_user ) ) {
$redirect = add_query_arg( array( 'update' => 'addnoconfirmation' ), 'user-new.php' );
} elseif ( ! is_user_member_of_blog( $new_user['user_id'] ) ) {
Expand Down
177 changes: 165 additions & 12 deletions src/wp-includes/ms-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,108 @@ function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
return apply_filters( 'wpmu_validate_blog_signup', $result );
}

/**
* Generates an activation key pair for a multisite signup.
*
* Returns a URL-safe base64 payload to embed in the activation link, and a
* base64-encoded HMAC-SHA256 hash to store in the database. The payload encodes
* `email:timestamp:random` and the hash is computed over that same string using
* `AUTH_KEY` + `AUTH_SALT`, so the stored value can be reconstructed from the
* link without storing the random component separately.
*
* The payload uses URL-safe base64 (`-_`, no padding) for embedding in links.
* The hash uses standard base64 (`+/=`) since it is stored in the DB, not URLs.
* Both fit within the legacy `activation_key varchar(50)` column constraint.
*
* Note: Activation links are invalidated when the site's authentication keys or salts
* change, consistent with how WordPress invalidates login sessions on salt rotation.
*
* @since 7.1.0
* @access private
*
* @see _wp_resolve_signup_key()
*
* @param string $user_email The user's email address, embedded in the payload.
* @return array {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this disagrees with the returned value. we’re missing key

while we’re at it, it wouldn’t hurt to return something like 'type' => 'hmac_sha256' especially for debugging purposes

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while we’re add it, let’s add @see _wp_resolve_signup_key() in this docblock and @see _wp_generate_signup_key() in the other. I can do this before merge if you prefer.

* @type string $type Key type. Always 'hmac_sha256'.
* @type string $key The random component embedded in the payload.
* @type string $payload URL-safe base64-encoded activation payload for the email link.
* @type string $hash Base64-encoded HMAC-SHA256 hash to store in the database.
* }
*/
function _wp_generate_signup_key( $user_email ) {
$timestamp = time();
$random = function_exists( 'random_bytes' )
? bin2hex( random_bytes( 8 ) )
: substr( md5( $timestamp . wp_rand() . $user_email ), 0, 16 );
$triplet = $user_email . ':' . $timestamp . ':' . $random;
$payload = rtrim( strtr( base64_encode( $triplet ), '+/', '-_' ), '=' );
$hash = base64_encode( hash_hmac( 'sha256', $triplet, wp_salt( 'auth' ), true ) );

return array(
'type' => 'hmac_sha256',
'key' => $random,
'payload' => $payload,
'hash' => $hash,
);
}

/**
* Resolves an activation key from an email link into a database lookup value.
* Note! This does not indicate if a valid signup key exists in the database;
* it only returns the lookup key to use when querying for such a key.
* Handles two key formats:
*
* - `hmac_sha256` (introduced in 7.1.0): a URL-safe base64-encoded triplet
* `email:timestamp:random`, whose HMAC-SHA256 is stored in the database.
* - `md5` (deprecated in 7.1.0): a plain 16-character lowercase hex string
* generated by `substr( md5( … ), 0, 16 )` and stored directly in the database.
*
* @since 7.1.0
* @access private
*
* @see _wp_generate_signup_key()
*
* @param string $key The activation key from the email link.
* @return array|null {
* Parsed key data, or null if the key format is not recognized.
*
* @type string $format 'hmac_sha256' for keys introduced in 7.1.0, 'md5' for pre-7.1.0 keys.
* @type string $hash The value to look up in the `activation_key` column.
* @type string|null $email User email embedded in the key ('hmac_sha256' format only).
* @type int|null $timestamp Key issuance timestamp embedded in the key ('hmac_sha256' format only).
* }
*/
function _wp_resolve_signup_key( $key ) {
// Restore base64 padding before decoding (it was stripped when the payload was generated).
$padded = $key . str_repeat( '=', ( 4 - strlen( $key ) % 4 ) % 4 );
$decoded = base64_decode( strtr( $padded, '-_', '+/' ), true );
$parts = is_string( $decoded ) ? explode( ':', $decoded, 3 ) : array();

if ( 3 === count( $parts ) && is_email( $parts[0] ) && ctype_digit( $parts[1] ) ) {
// hmac_sha256 format (since 7.1.0): email:timestamp:random.
list( $email, $timestamp, $random ) = $parts;
return array(
'format' => 'hmac_sha256',
'hash' => base64_encode( hash_hmac( 'sha256', $decoded, wp_salt( 'auth' ), true ) ),
'email' => $email,
'timestamp' => (int) $timestamp,
);
}

if ( 1 === preg_match( '~^[0-9a-f]{16}$~', $key ) ) {
// md5 format (deprecated in 7.1.0): plain 16-char hex, stored directly in the DB.
return array(
'format' => 'md5',
'hash' => $key,
'email' => null,
'timestamp' => null,
);
}

return null;
}

/**
* Records site signup information for future activation.
*
Expand All @@ -802,7 +904,10 @@ function wpmu_validate_blog_signup( $blogname, $blog_title, $user = '' ) {
function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = array() ) {
global $wpdb;

$key = substr( md5( time() . wp_rand() . $domain ), 0, 16 );
$signup_key = _wp_generate_signup_key( $user_email );
$key = $signup_key['key'];
$payload = $signup_key['payload'];
$hash = $signup_key['hash'];

/**
* Filters the metadata for a site signup.
Expand Down Expand Up @@ -830,7 +935,7 @@ function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = a
'user_login' => $user,
'user_email' => $user_email,
'registered' => current_time( 'mysql', true ),
'activation_key' => $key,
'activation_key' => $hash,
'meta' => serialize( $meta ),
)
);
Expand All @@ -839,16 +944,19 @@ function wpmu_signup_blog( $domain, $path, $title, $user, $user_email, $meta = a
* Fires after site signup information has been written to the database.
*
* @since 4.4.0
* @since 7.1.0 The `$key` parameter format changed. Previously a 16-character MD5 hex
* string stored directly in the database; now a URL-safe base64-encoded
* HMAC payload (`email:timestamp:random`) to embed in activation links.
*
* @param string $domain The requested domain.
* @param string $path The requested path.
* @param string $title The requested site title.
* @param string $user The user's requested login name.
* @param string $user_email The user's email address.
* @param string $key The user's activation key.
* @param string $key The activation key to embed in the activation URL.
* @param array $meta Signup meta data. By default, contains the requested privacy setting and lang_id.
*/
do_action( 'after_signup_site', $domain, $path, $title, $user, $user_email, $key, $meta );
do_action( 'after_signup_site', $domain, $path, $title, $user, $user_email, $payload, $meta );
}

/**
Expand All @@ -871,7 +979,11 @@ function wpmu_signup_user( $user, $user_email, $meta = array() ) {
// Format data.
$user = preg_replace( '/\s+/', '', sanitize_user( $user, true ) );
$user_email = sanitize_email( $user_email );
$key = substr( md5( time() . wp_rand() . $user_email ), 0, 16 );

$signup_key = _wp_generate_signup_key( $user_email );
$key = $signup_key['key'];
$payload = $signup_key['payload'];
$hash = $signup_key['hash'];

/**
* Filters the metadata for a user signup.
Expand All @@ -896,7 +1008,7 @@ function wpmu_signup_user( $user, $user_email, $meta = array() ) {
'user_login' => $user,
'user_email' => $user_email,
'registered' => current_time( 'mysql', true ),
'activation_key' => $key,
'activation_key' => $hash,
'meta' => serialize( $meta ),
)
);
Expand All @@ -905,13 +1017,16 @@ function wpmu_signup_user( $user, $user_email, $meta = array() ) {
* Fires after a user's signup information has been written to the database.
*
* @since 4.4.0
* @since 7.1.0 The `$key` parameter format changed. Previously a 16-character MD5 hex
* string stored directly in the database; now a URL-safe base64-encoded
* HMAC payload (`email:timestamp:random`) to embed in activation links.
*
* @param string $user The user's requested login name.
* @param string $user_email The user's email address.
* @param string $key The user's activation key.
* @param string $key The activation key to embed in the activation URL.
* @param array $meta Signup meta data. Default empty array.
*/
do_action( 'after_signup_user', $user, $user_email, $key, $meta );
do_action( 'after_signup_user', $user, $user_email, $payload, $meta );
}

/**
Expand Down Expand Up @@ -1208,13 +1323,51 @@ function wpmu_activate_signup(
) {
global $wpdb;

$resolved = _wp_resolve_signup_key( $key );

if ( null === $resolved ) {
return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
}

/*
* Both hmac_sha256 and md5 formats are looked up by the resolved hash value.
* The HMAC cryptographically binds the email to the hash, so querying by hash alone
* is sufficient and avoids mixing a known public value (email) with the secret lookup.
*/
/** @var object{ signup_id: string, domain: string, path: string, title: string, user_login: string, user_email: string, registered: string, activated: string, active: string, activation_key: string, meta: string|null }|null $signup */
$signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key ) );
$signup = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM $wpdb->signups WHERE activation_key = %s",
$resolved['hash']
)
);

if ( empty( $signup ) ) {
return new WP_Error( 'invalid_key', __( 'Invalid activation key.' ) );
}

/**
* Filters the expiration time of signup activation keys.
*
* @since 7.1.0
*
* @param int $expiration_duration The expiration time in seconds.
*/
$expiration_duration = apply_filters( 'activate_signup_expiration', DAY_IN_SECONDS );

/*
* For hmac_sha256 keys, use the timestamp from the cryptographically bound payload —
* it cannot be extended by modifying the database. md5 keys have no embedded timestamp,
* so fall back to the `registered` column.
*/
$issued_at = 'hmac_sha256' === $resolved['format']
? $resolved['timestamp']
: (int) mysql2date( 'U', $signup->registered );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious if this is even worth splitting. theoretically the resolved timestamp ought to be identical to the registered value, no?

I don’t have any great insight here, just raising the question.


if ( time() > ( $issued_at + $expiration_duration ) ) {
return new WP_Error( 'expired_key', __( 'Invalid key' ) );
}

if ( $signup->active ) {
if ( empty( $signup->domain ) ) {
return new WP_Error( 'already_active', __( 'The user is already active.' ), $signup );
Expand Down Expand Up @@ -1252,7 +1405,7 @@ function wpmu_activate_signup(
'active' => 1,
'activated' => $now,
),
array( 'activation_key' => $key )
array( 'activation_key' => $signup->activation_key )
);

if ( isset( $user_already_exists ) ) {
Expand Down Expand Up @@ -1294,7 +1447,7 @@ function wpmu_activate_signup(
'active' => 1,
'activated' => $now,
),
array( 'activation_key' => $key )
array( 'activation_key' => $signup->activation_key )
);
}
return $blog_id;
Expand All @@ -1306,7 +1459,7 @@ function wpmu_activate_signup(
'active' => 1,
'activated' => $now,
),
array( 'activation_key' => $key )
array( 'signup_id' => $signup->signup_id )
);

/**
Expand Down
Loading
Loading