Skip to content

Secrets API: Keep network secrets stored before a conversion to multisite - #15977

Open
ericmann wants to merge 7 commits into
WordPress:trunkfrom
ericmann:secrets-network-secrets-after-conversion
Open

ericmann wants to merge 7 commits into
WordPress:trunkfrom
ericmann:secrets-network-secrets-after-conversion

Conversation

@ericmann

Copy link
Copy Markdown

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_sitemeta also 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_sitemeta as 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_sitemeta and 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 unrelated wp_user_settings test and also appears on trunk.
  • PHPCS and PHPStan are clean on the changed files.

Trac ticket: https://core.trac.wordpress.org/ticket/66686

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.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

MicahelE and others added 7 commits October 31, 2026 16:37
`get_option()` and `get_site_option()` return the default both when an option does not exist and when their query fails, and cache the option as missing either way. `WP_Secrets_Option_Store::get()` read that as an absent secret, so during a failed read `wp_get_secret()` returned `null` for a stored secret and `wp_set_secret()` treated it as new, writing a record with no previous version.

When the Options API returns the default, the store now checks the database directly. If the row exists, it returns the record and clears the cached "missing" entry. If that read fails too, it returns a `secret_store_unavailable` error, which the write path already treats as fatal.

`WP_Secrets_Option_Store::delete()` now uses the same check, so it no longer reports success when it cannot tell whether the record still exists.

See #66424.
Adds tests that read a network secret back after the cache is flushed, which catches a second `wp_sitemeta` row left by writing through a stale "missing" cache entry.

A row with an empty or `NULL` value now reports `secret_record_malformed` rather than reading as an absent secret.

See #66424.
…re-failed-read

# Conflicts:
#	src/wp-includes/secrets/class-wp-secrets-option-store.php
wpdb::get_col() returns an empty array when its query fails, so the
is_array() check in WP_Secrets_Option_Store::list_names() could never
fire, and a failed listing read as no secrets. Failure is now taken from
wpdb::query(), as get() does.
…ntions.

Adds a summary, @Covers annotations and a unique failure message to each
test, as r65272 did for the existing Secrets API tests. Asserts the result
of every setup write, and uses the multisite group and
skipWithoutMultisite() for the multisite-only test.

Also adds a test for a failed listing in WP_Secrets_Option_Store::list_names().
…s result.

wpdb::get_var() and wpdb::get_col() are called without a query, to read
the result of the prepared query that wpdb::query() just ran. PHPCS reads
that as an unprepared query.
On a single site, network-scope secrets are options of the site. Converting it
to multisite leaves them in what becomes the main site's options table, while
WP_Secrets_Option_Store reads network-scope records from wp_sitemeta, so every
network secret stored before the conversion read as absent.

On the main network, the store now also reads and lists them where they are.
A read never moves them. The next write stores the record in wp_sitemeta and
removes the old row, and a delete removes both.

Follow-up to r64337.

See #66686.
@github-actions

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 micahele, ericmann.

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.

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.

2 participants