Skip to content

Secrets API: Report a failed read of a secret's record, or of the list, as an error - #15974

Open
ericmann wants to merge 7 commits into
WordPress:trunkfrom
ericmann:secrets-option-store-failed-read
Open

ericmann wants to merge 7 commits into
WordPress:trunkfrom
ericmann:secrets-option-store-failed-read

Conversation

@ericmann

Copy link
Copy Markdown

Takes over #15329 by @MicahelE, which conflicts with trunk and hasn't been updated since the review on 6 November. MicahelE's two commits are unchanged. Trunk is merged in, and three commits of mine on top make the changes requested on the ticket and in review.

What changes

From #15329 (MicahelE):

  1. A failed read of a secret's record no longer reads as a missing secret. When get_option() or get_site_option() returns the default, WP_Secrets_Option_Store::get() reads the row directly:
    • If the row exists, it returns the record and clears the cached "missing" entry.
    • If there is no row, it returns null.
    • If that read fails too, it returns secret_store_unavailable, which the write path already treats as fatal.
  2. Clearing the cached entry matters on multisite. Without it, the next update_site_option() goes through add_network_option(), which inserts a second wp_sitemeta row, and later requests read the old one.
  3. delete() uses the same check, so it no longer reports success when it can't tell whether the record still exists.
  4. A row with an empty or NULL value is a malformed record, not an absent one. wp_sitemeta.meta_value is nullable. This case came from @dhrupo on Secrets: Tell a failed record read apart from a missing secret. #15301.

Failure is taken from the return value of wpdb::query(), because wpdb::$last_error is not set on every failure path. When the connection is lost, flush() clears it.

Mine:

  • list_names() reports a failed query (requested in comment:4). wpdb::get_col() returns an empty array when its query fails, so the is_array() check could never fire. A failed listing made wp_list_secrets() show no secrets, and the Site Health check that keeps undecryptable records visible saw nothing. It now takes failure from wpdb::query(), as get() does.
  • The tests follow r65272's conventions: a summary, @covers and a unique message on every assertion. Every setup write is asserted, and the multisite-only test uses the multisite group and skipWithoutMultisite(). There's also a new test for the failed listing.
  • PHPCS ignores for get_var() and get_col() called without a query, which read the result of the prepared query before them.

Not changed:

  • The cache helper isn't shared with the root key's. r65373 added WP_Secrets_Key_Manager::forget_missing_root_key(), which does the same for the root key. Both are private, and sharing one would mean a new internal function. A committer can merge them on commit, as the ticket discussion suggested.
  • The direct read skips the Options API filters. It only runs after the Options API has returned the default. A host that keeps options elsewhere should replace the store through secrets.php.
  • Replica lag is out of scope. That read succeeds with no rows.

Testing

  • --group 66424: 14 tests on single site and 15 on multisite. With trunk's class-wp-secrets-option-store.php, every one fails.
  • --group secrets: 448 tests on single site and 457 on multisite, no failures (PHP 8.5, trunk at r65404).
  • --group option: 446 tests, no failures.
  • PHPCS is clean on both changed files. PHPStan passes with core's configuration.

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

Props MicahelE, dhrupo.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: The original commits were written with Claude Code, as noted on #15329. For this takeover: the trunk merge, the list_names() change, the test updates 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 6 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.
@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