Make WordPress Core

Opened 12 days ago

Last modified 7 days ago

#66399 assigned defect (bug)

Secrets API: Generate `WP_SECRETS_KEY` during installation so secrets do not depend on the salts

Reported by: ericmann Owned by: ericmann
Priority: normal Milestone: 7.2
Component: Security Version: trunk
Severity: normal Keywords: has-patch needs-testing
Cc: Focuses:

Description

Follow-up to [64337] (#66187). See #66380.

When WP_SECRETS_KEY is not defined, the Secrets API derives its key from LOGGED_IN_KEY and LOGGED_IN_SALT. Rotating the salts is common advice (wp config shuffle-salts, security plugins), and before 7.2 it only logged users out. On a site using that fallback it now makes every stored secret unreadable, and blocks saving new ones, until the old values are restored. #66187 comment:23 raised this, and comment:24 confirmed it on 7.2-beta2. #66380 fixes what Site Health says about it. This ticket removes the dependency for new installs.

Proposed fix

setup-config.php already generates the eight keys and salts. It should also write a WP_SECRETS_KEY define after them: a base64-encoded random 32-byte value, which is the form the Secrets API uses as raw key material. New installs then never use the fallback, and rotating the salts leaves their secrets readable.

Notes

  • wp-config-sample.php stays unchanged on purpose. The Secrets API rejects the salt placeholder, but it accepts any WP_SECRETS_KEY string, so a placeholder in the sample would give every hand-edited install that left it alone the same public key. A site whose config was copied from the sample by hand keeps the salt fallback, and Site Health recommends a dedicated key.
  • Without a CSPRNG, setup-config.php leaves the define out rather than fall back to wp_generate_password() or the secret-key API, and the site uses the salt fallback.
  • Existing sites are not affected. Moving one off the fallback means re-wrapping the root key, which core has no public path for yet (planned for 7.3).
  • Installers that write wp-config.php themselves, such as WP-CLI's wp config create and hosts' one-click installers, are out of scope here, but should do the same.

AI assistance: Claude Code (Claude Opus 5.5) for drafting the patch and this ticket. I reviewed both.

Change History (6)

This ticket was mentioned in ​PR #15124 on ​WordPress/wordpress-develop by ​@ericmann.


12 days ago
#1

  • Keywords has-patch added; needs-patch removed

Without WP_SECRETS_KEY, the Secrets API derives its key from LOGGED_IN_KEY and LOGGED_IN_SALT, so rotating the salts makes every stored secret unreadable until the old values come back (#66187 comment:23, confirmed in comment:24). This makes new installs independent of the salts.

What changes

  • setup-config.php writes WP_SECRETS_KEY as a base64-encoded random 32-byte value, the form the Secrets API uses as raw key material.
  • It goes after the closing /**#@-*/ of the keys-and-salts group, with its own docblock. The group's comment says the salts can be changed at any time, which is exactly wrong for this key. If a custom sample has no closing tag, it goes right after NONCE_SALT.
  • No CSPRNG: the define is left out, rather than taken from the weaker sources the salts fall back to. The site uses the salt fallback.
  • A wp-config-sample.php that already defines WP_SECRETS_KEY is left alone, so there is never a second define.
  • wp-config-sample.php is unchanged. The Secrets API rejects the salt placeholder but accepts any WP_SECRETS_KEY string, so a placeholder there would become a public key on every hand-edited install that left it in place.

Existing sites are not affected. The Site Health side is #66380.

Testing

setup-config.php has no unit tests, so this was tested end to end on PHP 8.5 and MySQL 8: run setup-config.php?step=2 and install.php?step=2 against a fresh database, store a secret, replace all eight keys and salts as wp config shuffle-salts does, then read the secret again.

trunk this PR
WP_SECRETS_KEY in the generated wp-config.php none one, canonical base64 of 32 bytes
Site Health key source fallback key dedicated key
Read after shuffling the salts secret_key_unavailable the stored value

Also checked:

  • A sample that already defines WP_SECRETS_KEY gives exactly one define, the sample's own.
  • A sample without /**#@-*/ gets the key after NONCE_SALT.
  • The generated file passes php -l.
  • phpcs reports nothing new on the file, and phpstan analyse with core's configuration passes.

To test by hand: delete wp-config.php, run the installer, and check that WP_SECRETS_KEY follows the salts. Then store a secret, run wp config shuffle-salts, and read the secret back.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: Drafting the patch, running the install tests above, and this description. I reviewed the change and the results.

#2 @ericmann
12 days ago

  • Keywords needs-testing added
  • Owner set to ericmann
  • Status new → assigned

#3 @ericmann
10 days ago

  • Milestone Awaiting Review → 7.2

​PR 15124 is updated after @peterwilsoncc's review:

  • wp-config-sample.php defines WP_SECRETS_KEY with the placeholder, after the salts and outside their group. setup-config.php fills it in with the same loop as the salts. A custom or older localized sample without the line still gets it added after the salts.
  • Without a CSPRNG, the key comes from wp_generate_password(), as the salts do.

This now depends on ​PR 15258 (#66187), which makes WP_SECRETS_KEY required and rejects the placeholder. With the fallback gone this is more than hardening: without it, a new install has no Secrets API until someone edits wp-config.php. Adding to the 7.2 milestone unless a committer disagrees.

Tested end to end: install, store a secret, shuffle all eight salts, read it back. This was run with the new sample, with PR 15258 applied, and with the old sample. In every case there is one canonical key and the secret reads back. WP-CLI's wp config create needs the same change, and I'll open an issue there.

​@ericmann commented on ​PR #15124:


10 days ago
#4

This is working for a manual install but a fresh install via WP-CLI. It looks like an associated PR will need to be made to ​wp-cli/config-command, ​wp-cli/core-command or both.

Added some notes inline.

Agreed. wp config create writes its own file, so it needs the same change. I'll open an issue on wp-cli/config-command. wp config shuffle-salts already touches only the eight keys and salts, so it's fine as is.

#5 follow-up: @digitalpritam
9 days ago

Patch Testing Report

Patch tested: ​https://github.com/WordPress/wordpress-develop/pull/15124 (head 6c93419bd4), alone and together with ​https://github.com/WordPress/wordpress-develop/pull/15258 (head d7abced039), both applied to trunk r65147.

Environment

  • WordPress: 7.2-beta3-64914-src (trunk r65147)
  • PHP: 8.3.36
  • Server: nginx/1.31.8 (wordpress-develop Docker environment)
  • Database: MySQL 9.7.4
  • Browser: Chrome 156.0.8078.74
  • OS: macOS 27.0.1
  • Theme: Ipsum 1.0
  • MU Plugins:
    • fix-docker-loopback 1.0.0 (added by the Docker environment)
    • a local helper for another ticket that only filters dashboard_secondary_items (unrelated)
  • Plugins:
    • None activated

Steps taken

  1. Reproduced on trunk without the PRs: on an install whose wp-config.php has no WP_SECRETS_KEY, get_key_source() reports "derived from LOGGED_IN_KEY and LOGGED_IN_SALT". Stored a secret with wp_set_secret(), ran wp config shuffle-salts, then wp_get_secret() returned secret_key_unavailable, and saving a new secret also failed with secret_key_unavailable.
  2. Applied PR 15124, removed wp-config.php, emptied the database, and ran the browser installer (setup-config.php then install.php).
  3. The generated wp-config.php has exactly one WP_SECRETS_KEY define, after the closing /**#@-*/ of the keys and salts, with the new docblock. The value is strict base64 that decodes to 32 bytes, and is canonical. php -l reports no syntax errors.
  4. get_key_source() reports "WP_SECRETS_KEY (base64-encoded 32 bytes)".
  5. Stored a secret, ran wp config shuffle-salts (confirmed LOGGED_IN_SALT changed), then read the secret back: it returned the stored value. Saving and reading a new secret after the shuffle also worked.
  6. Older or custom sample without the WP_SECRETS_KEY line (trunk's wp-config-sample.php): the key is added once, right after /**#@-*/, valid 32-byte base64, file passes php -l.
  7. Custom sample without the closing /**#@-*/: the key is added once, right after NONCE_SALT, valid 32-byte base64, file passes php -l.
  8. Applied PR 15258 on top (applies cleanly) and repeated steps 2 to 5 on an empty database: same results, and the secret still reads back after shuffling the salts.
  9. With both PRs, replacing the generated key with the sample placeholder makes get_key_source() report "WP_SECRETS_KEY is defined but not usable", and saving fails with a clear message. Removing the define (as wp config create currently writes it) gives "WP_SECRETS_KEY is not defined." and saving fails.
  10. npm run test:php -- --group secrets with both PRs: OK, 392 tests, 983 assertions, 25 skipped.
  11. ✅ Patch is solving the problem: new installs made through the installer get their own WP_SECRETS_KEY, so rotating the salts no longer makes stored secrets unreadable.

Expected result

  • The installer writes one valid WP_SECRETS_KEY after the salts, for the bundled sample, older samples and custom samples, and secrets stay readable after the salts are rotated.

Additional Notes

  • With PR 15124 alone, a hand-edited wp-config.php that keeps the sample's put your unique phrase here value is accepted on a fresh database: get_key_source() reports "WP_SECRETS_KEY (legacy interpretation, hashed as an opaque string)" and secrets save and read with that public value. With PR 15258 applied, the same value is rejected (step 9). So PR 15124 looks safe only together with PR 15258, as noted in comment:3.
  • Installs created with WP-CLI (wp config create) still get no key, and with PR 15258 the Secrets API is then unavailable until the key is added. This matches comment:4 and the planned WP-CLI issue.
  • Not tested: the no-CSPRNG fallback to wp_generate_password(), and the screen shown when wp-config.php cannot be written.

Screenshots/Screencast with results

  • None; the results are command output, summarised above.

Support Content

  • Commands used (WP-CLI eval through npm run env:cli):
    1. Key source: ( new WP_Secrets_Config_Key_Provider() )->get_key_source()
    2. Store: wp_set_secret( 'test-66399/demo', 'hello-66399' )
    3. Read: wp_get_secret( 'test-66399/demo' )->reveal()
    4. Rotate: wp config shuffle-salts

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (claude-opus-5-5)
Used for: running the test steps in my local environment and drafting this report; I reviewed the results and the wording.

#6 in reply to: ↑ 5 @ericmann
7 days ago

Replying to digitalpritam:

Thanks @digitalpritam, that covers every sample the installer can meet. The two untested paths, no CSPRNG and an unwritable wp-config.php, change only where the key comes from and where it's shown. The second uses the same text area as the salts.

Note: See TracTickets for help on using tickets.