Repository navigation
Security: Hash wp_signups.activation_key (Trac #38474, CVE-2017-14990) #12235
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
Open
bor0
wants to merge
13
commits into
WordPress:trunk
Choose a base branch
from
bor0:fix/38474-hash-signup-activation-keys
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+422
−18
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
54c4bb5
Security: Hash activation keys in wp_signups (Trac #38474)
bor0 b4af97a
Tests: Fix PHPCS coding standards issues
bor0 6af49cb
Security: Switch to HMAC-SHA256 for activation key storage (#38474)
bor0 9804333
Merge branch 'trunk' into fix/38474-hash-signup-activation-keys
bor0 8f5140c
Security: Address PR review feedback for #38474
bor0 03b9ede
Merge branch 'trunk' into fix/38474-hash-signup-activation-keys
bor0 6c93bdd
Merge branch 'trunk' into fix/38474-hash-signup-activation-keys
bor0 ed83556
Security: Address second round of PR review feedback for #38474
bor0 29e6e6f
Security: Address third round of PR review feedback for #38474
bor0 0f9c97c
Security: Address fourth round of PR review feedback for #38474
bor0 14da63f
Update src/wp-includes/ms-functions.php
bor0 c997902
Merge branch 'trunk' into fix/38474-hash-signup-activation-keys
bor0 c88d132
Merge branch 'trunk' into fix/38474-hash-signup-activation-keys
johnbillion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
| * @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. | ||
| * | ||
|
|
@@ -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. | ||
|
|
@@ -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 ), | ||
| ) | ||
| ); | ||
|
|
@@ -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 ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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. | ||
|
|
@@ -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 ), | ||
| ) | ||
| ); | ||
|
|
@@ -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 ); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -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 ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 ); | ||
|
|
@@ -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 ) ) { | ||
|
|
@@ -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; | ||
|
|
@@ -1306,7 +1459,7 @@ function wpmu_activate_signup( | |
| 'active' => 1, | ||
| 'activated' => $now, | ||
| ), | ||
| array( 'activation_key' => $key ) | ||
| array( 'signup_id' => $signup->signup_id ) | ||
| ); | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
keywhile we’re at it, it wouldn’t hurt to return something like
'type' => 'hmac_sha256'especially for debugging purposesThere was a problem hiding this comment.
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.