Skip to content

Security: Hash wp_signups.activation_key (Trac #38474, CVE-2017-14990) - #12235

Open
bor0 wants to merge 13 commits into
WordPress:trunkfrom
bor0:fix/38474-hash-signup-activation-keys
Open

bor0 wants to merge 13 commits into
WordPress:trunkfrom
bor0:fix/38474-hash-signup-activation-keys

Conversation

@bor0

@bor0 bor0 commented Jun 19, 2026 •

Copy link
Copy Markdown
Member

Summary

  • wp_signups.activation_key stored activation keys as plain text. This patches it to use HMAC-SHA256, following a suggestion from @dmsnell in the ticket thread.
  • On signup: a triplet email:timestamp:random_key is built; base64(HMAC-SHA256(triplet, AUTH_KEY+AUTH_SALT)) (44 chars) is stored in the DB; base64url(triplet) is sent in the activation email.
  • On activation: the triplet is decoded from the URL, the HMAC is recomputed, and the row is fetched via WHERE user_email = %s AND activation_key = %s — a direct indexed lookup, no extra URL parameter needed.
  • Legacy plain-text keys (pre-upgrade pending activations) continue to work via a fallback path — detected by matching the original [0-9a-f]{16} format — so no pending activations are broken by the upgrade. Legacy keys are also now subject to the same expiration check (via registered column) as new keys.
  • A new activate_signup_expiration filter (default: DAY_IN_SECONDS) controls key expiry, checked from the timestamp embedded in the triplet for new keys, and from $signup->registered for legacy keys.
  • Key generation is extracted into a private _wp_generate_signup_key() helper used by both signup functions, with random_bytes() where available. A companion _wp_resolve_signup_key() helper handles parsing and DB-lookup-key resolution for both new and legacy formats.
  • Note: activation links are invalidated when the site's authentication keys or salts change, consistent with how WordPress already invalidates login sessions on salt rotation.
  • 10 new PHPUnit tests (including a legacy-schema column-length check and an expired legacy key test); one existing test fixed to work with the new format.

Fixes #38474. See also: https://core.trac.wordpress.org/ticket/38474

Props bor0, tomdxw, jeremyfelt, SergeyBiryukov, SirLouen, dmsnell, peterwilsoncc.

Test plan

Automated (PHPUnit)

npm install
# edit .env: set LOCAL_MULTISITE=true
npm run env:start
npm run env:install

# New tests:
npm run test:php -- -c tests/phpunit/multisite.xml --filter Tests_Multisite_wpmuActivateSignup

# Fixed regression test:
npm run test:php -- -c tests/phpunit/multisite.xml --filter test_should_not_fail_for_data_used_by_a_deleted_user

All 10 tests should pass.

Manual

  1. Hashed key in DB — Register at /wp-signup.php as a logged-out user. Check wp_signups.activation_key: should be a ~44-char base64 string (e.g. 5c4hlLJIcvtSnse3cuKo93pVWx2KBNOPan4HHpN7mfs=), not a plain hex string.

  2. Activation link works — The confirmation email link contains just key=<base64url_triplet> (no signup_id). Clicking it shows "Your account is now active!"

  3. Wrong key rejected — Visit /wp-activate.php?key=WRONGKEY. Activation must fail.

  4. Legacy key BC — Insert a row into wp_signups with a 16-char hex activation_key and a recent registered timestamp. Visiting /wp-activate.php?key=<hex_key> should still succeed — existing pending activations must not break after upgrade.

  5. Expired legacy key rejected — Insert a row into wp_signups with a 16-char hex activation_key and a registered timestamp older than DAY_IN_SECONDS. Activation must fail with an expired-key error.

  6. Expiry (new format) — Add add_filter('activate_signup_expiration', fn() => -1) to an mu-plugin, sign up, try to activate. Must fail with an expired-key error.

🤖 Generated with Claude Code

`wp_signups.activation_key` stored activation keys as plain text
(e.g. `7259c714857ef009`), unlike `wp_users.user_activation_key`
which already stores a `timestamp:hash` pair. This was assigned
CVE-2017-14990.

This patch brings `wp_signups` into line with `wp_users`:

- `wpmu_signup_blog()` and `wpmu_signup_user()` now hash the key
  with phpass before storing it (`timestamp:phpass_hash` format),
  mirroring the approach used for password-reset keys in [25696].
- `wpmu_activate_signup()` verifies the submitted key against the
  stored hash and enforces a 24-hour expiry via the new
  `activate_signup_expiration` filter.
- Legacy plain-text keys (rows created before the upgrade) continue
  to work for backwards compatibility so no pending activations are
  broken by the upgrade.
- Activation URLs now include `&signup_id=N` so the correct row can
  be fetched for hash verification without a full-table scan.
- `wp-activate.php` gains a Signup ID field on the manual
  activation form.
- Unit tests cover: hashed storage, successful activation, wrong
  key rejection, wrong signup_id rejection, legacy key BC, expiry,
  and the `activate_signup_expiration` filter.

Props bor0, tomdxw, jeremyfelt, SergeyBiryukov, SirLouen, dmsnell.
Fixes #38474.

== Testing Instructions ==

=== Automated (PHPUnit) ===

Requires the Docker-based local environment:

  npm install
  # edit .env: set LOCAL_MULTISITE=true
  npm run env:start
  npm run env:install

  # New tests for this ticket:
  npm run test:php -- -c tests/phpunit/multisite.xml \
    --filter Tests_Multisite_wpmuActivateSignup

  # Regression: existing test that was updated:
  npm run test:php -- -c tests/phpunit/multisite.xml \
    --filter test_should_not_fail_for_data_used_by_a_deleted_user

All 9 tests should pass.

=== Manual (browser) ===

1. HASHED KEY IN DB
   - Go to /wp-signup.php as a logged-out user and register.
   - Check wp_signups: activation_key should look like
     "1700000000:$P$Bxxx..." not a plain 16-char hex string.

2. ACTIVATION LINK WORKS
   - The confirmation email link includes both key= and signup_id=.
   - Clicking it shows "Your account is now active!"

3. SIGNUP ID FIELD ON FORM
   - Visit /wp-activate.php with no params.
   - The manual entry form should show both "Activation Key"
     and "Signup ID" fields.

4. WRONG KEY REJECTED
   - Visit /wp-activate.php?key=WRONGKEY&signup_id=<valid_id>
   - Activation must fail (no "now active" message).

5. LEGACY KEY BACKWARDS COMPAT
   - Insert a row with a plain-text activation_key directly into
     wp_signups (simulating a pre-upgrade pending activation).
   - Visit /wp-activate.php?key=<plain_key>&signup_id=<id>
   - Activation should succeed -- pre-upgrade pending activations
     must not be broken by the upgrade.

6. EXPIRY
   - Add add_filter('activate_signup_expiration', fn() => -1)
     to an mu-plugin, sign up, try to activate.
   - Activation should fail with an expired-key error.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Hi there! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request to wordpress-develop. Here are a few things to be aware of that may help you out!

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook.

Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Thank you,
The WordPress Project

@github-actions

github-actions Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props bor0, dmsnell, johnbillion, peterwilsoncc.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

bor0 and others added 3 commits June 19, 2026 18:17
- Multi-item associative arrays: each value on its own line
- Space after function keyword in anonymous/arrow functions
- Inline closure expanded to multi-line (brace must be last content on line)

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Replaces the phpass approach with HMAC-SHA256 per feedback from @dmsnell
in comment:29.

Instead of storing timestamp:phpass_hash and requiring signup_id as a
second lookup parameter, we now:

- Build a triplet: user_email:timestamp:random_key
- Store base64(HMAC-SHA256(triplet, AUTH_KEY+AUTH_SALT)) in the DB (44
  chars, fits the existing varchar(50) column)
- Send base64url(triplet) as the activation URL parameter

On activation the triplet is decoded from the URL, the HMAC is
recomputed, and the row is found with a direct indexed lookup:
WHERE user_email = %s AND activation_key = %s

This eliminates signup_id from URLs entirely. Legacy plain-text keys
(pre-upgrade pending activations) continue to work via a fallback
WHERE activation_key = %s path.

Props bor0, dmsnell, tomdxw, jeremyfelt, SergeyBiryukov, SirLouen.
Fixes #38474.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Comment thread src/wp-includes/ms-functions.php Resolved
Comment thread src/wp-includes/ms-functions.php Resolved
Comment thread src/wp-includes/ms-functions.php Resolved
Comment thread src/wp-includes/ms-functions.php Resolved
bor0 and others added 2 commits June 23, 2026 23:55
- Extract `_wp_generate_signup_key()` helper to deduplicate the
  triplet/payload/hash logic shared between `wpmu_signup_blog()` and
  `wpmu_signup_user()`. Uses `random_bytes()` where available for
  better entropy, and a single `time()` call throughout.
- Tighten legacy key detection: `preg_match('~^[0-9a-f]{16}$~', $key)`
  explicitly matches the original MD5-substr format instead of falling
  through a broad else branch.
- Add test asserting the stored activation key is ≤ 60 chars for
  compatibility with legacy table schemas (e.g. DO_NOT_UPGRADE_GLOBAL_TABLES).
- Fix legacy key test fixtures to use valid 16-char hex keys.

Props peterwilsoncc, dmsnell.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@bor0

bor0 commented Jun 23, 2026

Copy link
Copy Markdown
Member Author

Thanks @dmsnell, @peterwilsoncc - addressed your comments, lmk how this looks! 🙇‍♂️

Comment thread src/wp-includes/ms-functions.php Resolved
Comment thread src/wp-includes/ms-functions.php Resolved
bor0 and others added 2 commits June 25, 2026 09:49
- Extract _wp_resolve_signup_key() to pair with _wp_generate_signup_key(),
  handling both new HMAC and legacy hex formats per @dmsnell's suggestion.
- Apply expiration check to legacy keys via $signup->registered, eliminating
  the asymmetric behaviour flagged by @dmsnell.
- Add docblock note to _wp_generate_signup_key() about salt rotation
  invalidating pending activations.
- Add PHPUnit test for expired legacy key rejection.

Props dmsnell.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>

@dmsnell dmsnell left a comment

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 looking for after_signup_user filters I saw that BuddyPress calls do_action() on it, sending an activation key.

We probably want to think about flows that call this rather than hook into it

cc: @JJJ for your thoughts on this change

Comment thread src/wp-includes/ms-functions.php Outdated
* @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 $payload The URL-safe base64-encoded activation payload sent via email.

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 warrants a @since tag on the filter noting the change, but it might require some other consideration. what are plugins using this $key value for. in one sense, the change is superficial and this should be an opaque token; on the other hand, previously it was a value stored in the database and now it’s something else.

we might want to continue to return the database-stored value and expose the payload some other way, if that’s what plugin depend on. I did not do a more in-depth analysis; might be a good task for an LLM

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I added a @SInCE tag.

I also ran some quick analysis with Claude (searched GitHub for code that hooks after_signup_user and after_signup_site, found ~8 distinct plugin usages).

Passing $payload seems correct for all actual listeners. The only BC gap is plugins that fire the hook themselves with $signup->activation_key (wp-graphql-buddypress and GRead), those will need to update to pass the payload instead.

Comment thread src/wp-includes/ms-functions.php Outdated
// New HMAC format: email:timestamp:random.
list( $email, $timestamp, $random ) = $parts;
return array(
'format' => 'new',

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.

let’s avoid terms like “new” and “legacy”, preferring semantically rich terms which can identify the format concretely

perhaps hmac:email:timestamp:random and md5 or v1 and v2 with a description in the docblock

v1 - md5 of random number blah blah (deprecated in 7.1.0)
v2 - hmac of email:timstamp:random

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks for this comment. I just want to highlight the value of such comments that is still rare to see in reviews nowadays. Data structures with proper devex are tricky to build :)

I didn't like the hmac:... because it's too long (I think it adds more burden than help), so went ahead with short ones like hmac_sha256 and md5. Would be great to get your thoughts on it

Comment thread src/wp-includes/ms-functions.php Outdated
if ( 'new' === $resolved['format'] ) {
$signup = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM $wpdb->signups WHERE user_email = %s AND activation_key = %s",

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.

with the use of the HMAC, we can remove the constraint on the email as well, right? perhaps this means we could plausibly expect more than one, but as with the signup_id, I think we want to eliminate the variability that we introduce when mixing a secret value (the hash) and a known value (the email address) since that provides an opportunity to expose information about the generation of the hash and gives someone the ability to try different payloads using the same email.

granted, I think given the payload and entropy already in the key this should be a rare problem, but I wonder if further-filtering by email has a benefit, even while it introduces this risk

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah, that's actually a good catch!

I wonder if further-filtering by email has a benefit

The only thing that comes to my mind is the entropy thing you already mentioned. Claude agreed with you that the extra clause is redundant:

Birthday bound on a 256-bit output. With ~10^9 total WordPress signups ever created (generous upper bound), the probability of any collision is roughly 2^30 / 2^256 ≈ 10^−68. Effectively zero.

It also brought bruteforcing for some reason 🤷 it was fun though

❯ why did you bring bruteforcing?

⏺ Fair point — brute force isn't relevant here. The question was about whether the email constraint in the WHERE clause adds security value when the HMAC already binds the email. The only meaningful number is the collision probability: ~10^−68. It's zero.

- Rename format identifiers 'new'/'legacy' to concrete 'hmac_sha256'/'md5'
- Remove email from hmac_sha256 DB lookup (HMAC already binds the email)
- Add @SInCE 6.9.0 BC note to after_signup_user/after_signup_site docblocks

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Comment thread src/wp-includes/ms-functions.php Outdated
* 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 6.9.0

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 will need to be 7.1.0

* @access private
*
* @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.

Comment thread src/wp-includes/ms-functions.php Outdated
*
* Handles two key formats:
*
* - `hmac_sha256` (introduced in 6.9.0): a URL-safe base64-encoded triplet

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.

6.9 released two versions ago. all of these references need updating to 7.1.0

Comment thread src/wp-includes/ms-functions.php Outdated
$signup = $wpdb->get_row(
$wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $resolved['hash'] )
);
}

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.

these are running the same code, are they not? so why separate them into a conditional branch?

// For md5 (legacy) keys, fall back to the registered column in the database.
$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.

Per feedback from @dmsnell:
- Bump all @SInCE tags from 6.9.0 to 7.1.0 in _wp_generate_signup_key(),
  _wp_resolve_signup_key(), wpmu_activate_signup(), and the after_signup_site/
  after_signup_user hook changelogs.
- Fix _wp_generate_signup_key() @return to document the missing `key` field;
  add `type => 'hmac_sha256'` to the return array and docblock for debuggability.
- Add @see cross-references between _wp_generate_signup_key() and
  _wp_resolve_signup_key().
- Collapse the duplicate if/else SQL query branches in wpmu_activate_signup()
  into a single query with a combined comment.
- Expand the timestamp ternary comment to explain why the split is load-bearing:
  the hmac_sha256 payload timestamp is cryptographically bound and cannot be
  extended by modifying the database.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Comment thread src/wp-includes/ms-functions.php Resolved

@dmsnell dmsnell left a comment

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.

GitHub no longer allows leaving review comments without text here, so here is some text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants