Opened 8 days ago
Last modified 4 days ago
#66686 new defect (bug)
Secrets API: Network-scope secrets are lost when a single site is converted to multisite
| Reported by: | desrosj | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 7.3 |
| Component: | Security | Version: | trunk |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | multisite |
Description
Follow-up to [64337] (#66187).
On a single site, wp_set_network_secret() stores its record through update_site_option(), which on a single site is update_option(). The record lands in wp_options as _wp_network_secret_{name}.
After the site is converted to multisite, WP_Secrets_Option_Store reads network-scope records from wp_sitemeta. Nothing moves the existing _wp_network_secret_* rows there, so every network secret stored before the conversion disappears:
wp_get_network_secret()returnsnull, the "does not exist" state, not aWP_Error. A plugin that seesnullmay ask for the credential again or generate a new one.wp_list_network_secrets()returns an empty array, so Site Health's undecryptable check cannot see them either.- The rows are still in the main site's
wp_options, unused.
The root key already handles this case. The feature plugin hit the same problem for the root key in its WP-CLI smoke test: every secret became unreadable after wp core multisite-convert. It fixed that in WP_Secrets_Key_Manager::get_wrapped_root_key(), which moves a pre-conversion root key into sitemeta (https://github.com/ericmann/secrets-api/pull/1). The feature plugin's network spec says the result is that "converting a single site into a network does not strand its secrets" (https://github.com/ericmann/secrets-api/blob/main/docs/spec/network.md).
That holds for site-scope secrets but not network-scope ones. The core tests (test_get_root_key_adopts_a_pre_conversion_root_key, test_secret_written_before_conversion_still_decrypts) and the plugin's smoke test (convert_to_multisite) all write only a site-scope secret before converting.
Steps to reproduce
- On a single site:
wp_set_secret( 'acme/site-key', 'site-value' ); wp_set_network_secret( 'acme/net-key', 'network-value' ); var_dump( wp_get_network_secret( 'acme/net-key' )->reveal() ); // 'network-value'
- Convert to multisite (Tools > Network Setup, then add the constants to wp-config.php).
- On the main site:
var_dump( wp_get_secret( 'acme/site-key' )->reveal() ); // 'site-value' var_dump( wp_get_network_secret( 'acme/net-key' ) ); // NULL var_dump( wp_list_network_secrets() ); // array()
SELECT option_name FROM wp_options WHERE option_name LIKE '\_wp\_%secret%'still lists_wp_network_secret_acme/net-key.
AI assistance: Claude Code for finding this, writing the reproduction and drafting this ticket. I reviewed the results and confirmed that it appears to be valid.
Change History (5)
#2
in reply to: ↑ 1
@
8 days ago
Replying to desrosj:
Confirmed, thanks @desrosj. WP_Secrets_Option_Store has nothing for this case, and the existing conversion tests only write a site-scope secret. Two-Factor stores TOTP secrets in network scope on single sites, so this will be hit in practice.
I'm wary of fixing it with a migration, in populate_network() or anywhere else:
populate_network()runs before multisite is active.is_multisite()stays false until the constants are added towp-config.php, and until then the site still reads and writes these records, and the root key, inwp_options. Moving them at that point breaks the site that is still running, and copying them leaves anything written in between stale.- Moving them later, on first use, turns a read into a write, as you point out.
- More generally, I'd like the API to avoid relocating secrets on its own. Several hosts want secret storage to be read-only from WordPress, and a store that moves records when nobody asked makes that harder to enforce.
The direction I'm leaning is to leave the records where they are: after conversion, network-scope reads fall back to the main site's options, and a record only moves to wp_sitemeta when something writes or deletes it anyway. I still need to work through listing and deletion.
Whichever shape it takes, I'll put up a patch with a test that stores a network secret before conversion and reads it back after.
This ticket was mentioned in PR #15977 on WordPress/wordpress-develop by @ericmann.
8 days ago
#3
- Keywords has-patch has-unit-tests added
This builds on #15974 (#66424), which changes the same class. Its commits come first in this branch, so please review only the last commit, 889f8c035a, until #15974 lands.
On a single site, network-scope secrets are stored as options of the site. Converting it to multisite leaves those rows in what becomes the main site's options table, but WP_Secrets_Option_Store reads network-scope records from wp_sitemeta. So every network secret stored before the conversion read as absent: wp_get_network_secret() returned null and wp_list_network_secrets() didn't list it.
As discussed on the ticket, this leaves the records where they are rather than migrating them:
- Reads: on the main network, a network-scope read that finds nothing in
wp_sitemetaalso checks the main site's options table. Nothing is moved on a read. Other networks were created after the conversion, so they never look there. - Writes: the record is stored in
wp_sitemetaas before, and the pre-conversion row is removed. The old value becomes the previous version as usual, because a network secret's AAD contains no site or network ID. - Deletes: both rows are removed, and the result is checked. An older row left behind would otherwise come back once the newer one is gone.
- Listing:
wp_sitemetaand the main site's options are combined, and each name appears once.
The main site's table is read directly, with failure taken from wpdb::query() as in #15974, so a failed read reports secret_store_unavailable instead of an absent secret.
Testing
New multisite tests (--group 66686). Each stores a network secret, then moves its row from wp_sitemeta into the main site's options, as a conversion leaves it:
- it reads after the conversion;
- reading it doesn't move it;
- it is listed once, alongside a secret stored after the conversion;
- writing it moves it to
wp_sitemeta, and the pre-conversion value becomes the previous version; - deleting it removes it;
- deleting a moved secret doesn't bring back a pre-conversion copy left behind;
- another network doesn't read it.
Without this commit, the first six fail. The last one passes either way: it checks the fallback's scope.
--group secrets: 448 tests on single site and 464 on multisite, no failures.--group site-health: 121 tests on each, no failures.--group option: no failures. Its one warning comes from an unrelatedwp_user_settingstest and also appears on trunk.- PHPCS and PHPStan are clean on the changed files.
Props desrosj.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: The patch, the tests and this description. I reviewed the changes and the results.
#4
@
8 days ago
PR 15977 follows comment:2: the records stay where the conversion left them.
- On the main network, a network-scope read that finds nothing in wp_sitemeta also checks the main site's options table. A read never moves anything. Other networks were created after the conversion, so they never look there.
- The next write stores the record in wp_sitemeta and removes the old row. The old value becomes the previous version as usual, since a network secret's AAD has no site or network ID. That answers the open question in comment:1.
- A delete removes both rows and checks that neither remains, so an older copy can't come back.
- wp_list_network_secrets() lists both, each name once.
It builds on PR 15974 (#66424), which changes the same class, so only the last commit is new.
I think there's a few possible ways to address this.
First is to move the
_wp_network_secret_*rows, and ideally the root key too, from the main site's options into sitemeta once, while the network is created (populate_network()). That is better than moving them on first access the wayget_wrapped_root_key()does for the root key. Moving on read turns a read into a write, and trusts whatever happens to be in the main site's options at that moment. Doing it once, at conversion, avoids both.get_wrapped_root_key()'s fallback could stay for networks converted before the fix.A moved record should decrypt unchanged: the network master key comes from the same root key, and the AAD (
network|0|{name}|{slot}) contains no site or network ID. I have not tested that part.Either way, a test should store a network secret before conversion and read it back afterwards.