Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 79 additions & 4 deletions src/wp-includes/secrets/class-wp-secrets-option-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ final class WP_Secrets_Option_Store implements WP_Secrets_Store {
/**
* Reads a secret's stored record.
*
* get_option() and get_site_option() return the default both when the option does
* not exist and when their query failed, so a default is checked against the database
* before the secret is reported as absent.
*
* @since 7.2.0
*
* @param string $name The secret's namespaced name.
Expand All @@ -53,7 +57,11 @@ public function get( string $name, bool $network = false ) {
$value = $network ? get_site_option( $option_name, null ) : get_option( $option_name, null );

if ( null === $value ) {
return null;
$value = $this->read_stored_value( $option_name, $network );
}

if ( null === $value || is_wp_error( $value ) ) {
return $value;
}

if ( ! is_array( $value ) ) {
Expand Down Expand Up @@ -126,9 +134,7 @@ public function delete( string $name, bool $network = false ) {

// delete_*_option() also returns false when the option never existed.
// Deleting something already absent is not a failure.
$still_exists = null !== ( $network ? get_site_option( $option_name, null ) : get_option( $option_name, null ) );

if ( ! $still_exists ) {
if ( null === $this->get( $name, $network ) ) {
return true;
}

Expand Down Expand Up @@ -191,6 +197,75 @@ public function list_names( bool $network = false ) {
return $names;
}

/**
* Reads an option's value from the database, bypassing the options cache.
*
* A failed query in get_option() or get_site_option() also caches the option as
* missing. When the row exists, this clears that cache entry, so the failed read
* does not outlive this call.
*
* @since 7.2.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $option_name The option name.
* @param bool $network Whether this is a network-scope secret.
* @return mixed The stored value, null if there is no row, or WP_Error if the read failed.
*/
private function read_stored_value( string $option_name, bool $network ) {
global $wpdb;

if ( $network && is_multisite() ) {
$network_id = get_current_network_id();
$cache_group = 'site-options';
$cache_key = "$network_id:$option_name";
$notoptions_key = "$network_id:notoptions";
$result = $wpdb->query(
$wpdb->prepare(
"SELECT meta_value FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d LIMIT 1",
$option_name,
$network_id
)
);
} else {
$cache_group = 'options';
$cache_key = $option_name;
$notoptions_key = 'notoptions';
$result = $wpdb->query(
$wpdb->prepare(
"SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1",
$option_name
)
);
}

if ( false === $result ) {
return new WP_Error(
WP_SECRETS_ERROR_STORE_UNAVAILABLE,
__( 'The secret record could not be read.' )
);
}

if ( ! $wpdb->num_rows ) {
return null;
}

$stored = $wpdb->get_var();

// get_var() returns null for a row with no value. That row exists, so it must not read as absent.
$value = maybe_unserialize( $stored ?? '' );

wp_cache_delete( $cache_key, $cache_group );

$notoptions = wp_cache_get( $notoptions_key, $cache_group );

if ( is_array( $notoptions ) && isset( $notoptions[ $option_name ] ) ) {
unset( $notoptions[ $option_name ] );
wp_cache_set( $notoptions_key, $notoptions, $cache_group );
}

return $value;
}

/**
* Builds the option name a secret is stored under.
Expand Down
256 changes: 256 additions & 0 deletions tests/phpunit/tests/secrets/wpSecretsOptionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,260 @@ public function test_list_names_escapes_the_underscore_wildcard(): void {
$this->assertContains( 'myplugin/real', $names );
$this->assertNotContains( 'decoy/name', $names );
}

/**
* Data provider for tests that run in both scopes.
*
* @return array<string, array{0: bool}>
*/
public static function data_scopes(): array {
return array(
'site scope' => array( false ),
'network scope' => array( true ),
);
}

/**
* @ticket 66424
*
* @dataProvider data_scopes
*/
public function test_one_failed_read_does_not_hide_a_stored_secret( bool $network ): void {
$this->write_secret( 'test/key', 'one', $network );

$filter = $this->fail_record_selects( 'test/key', $network, 1 );
$value = $this->read_secret( 'test/key', $network );

remove_filter( 'query', $filter );

$this->assertSame( 'one', $value );
$this->assertSame( 'one', $this->read_secret( 'test/key', $network ) );

// The Options API must not keep the record cached as missing.
if ( $network ) {
$this->assertIsArray( get_site_option( WP_Secrets_Option_Store::NETWORK_PREFIX . 'test/key' ) );
} else {
$this->assertIsArray( get_option( WP_Secrets_Option_Store::SITE_PREFIX . 'test/key' ) );
}
}

/**
* A failed read must not make a rotation discard the value it depends on.
*
* @ticket 66424
*
* @dataProvider data_scopes
*/
public function test_one_failed_read_during_a_write_keeps_the_previous_version( bool $network ): void {
$this->write_secret( 'test/key', 'one', $network );
$this->write_secret( 'test/key', 'two', $network );

$filter = $this->fail_record_selects( 'test/key', $network, 1 );
$result = $this->write_secret( 'test/key', 'three', $network );

remove_filter( 'query', $filter );

$this->assertTrue( $result );
$this->assertSame( 'three', $this->read_secret( 'test/key', $network ) );
$this->assertSame( 'two', $this->read_secret( 'test/key', $network, WP_Secret_Version::PREVIOUS ) );

// Later requests read from the database, so the write must have updated the stored row.
wp_cache_flush();

$this->assertSame( 'three', $this->read_secret( 'test/key', $network ) );
$this->assertSame( 'two', $this->read_secret( 'test/key', $network, WP_Secret_Version::PREVIOUS ) );
}

/**
* A row that exists but holds no value is a malformed record, not an absent one.
*
* @ticket 66424
*
* @dataProvider data_scopes
*/
public function test_a_failed_read_of_an_empty_row_reports_a_malformed_record( bool $network ): void {
$store = new WP_Secrets_Option_Store();

if ( $network ) {
update_site_option( WP_Secrets_Option_Store::NETWORK_PREFIX . 'test/key', '' );
} else {
update_option( WP_Secrets_Option_Store::SITE_PREFIX . 'test/key', '', false );
}

$filter = $this->fail_record_selects( 'test/key', $network, 1 );
$result = $this->without_db_errors( array( $store, 'get' ), 'test/key', $network );

remove_filter( 'query', $filter );

$this->assertWPError( $result );
$this->assertSame( WP_SECRETS_ERROR_RECORD_MALFORMED, $result->get_error_code() );
}

/**
* Unlike wp_options.option_value, wp_sitemeta.meta_value is nullable.
*
* @ticket 66424
*
* @group ms-required
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
public function test_a_network_row_with_a_null_value_reports_a_malformed_record(): void {
global $wpdb;

$wpdb->insert(
$wpdb->sitemeta,
array(
'site_id' => get_current_network_id(),
'meta_key' => WP_Secrets_Option_Store::NETWORK_PREFIX . 'test/key',
'meta_value' => null,
)
);

$store = new WP_Secrets_Option_Store();
$result = $store->get( 'test/key', true );

$this->assertWPError( $result );
$this->assertSame( WP_SECRETS_ERROR_RECORD_MALFORMED, $result->get_error_code() );
}

/**
* @ticket 66424
*
* @dataProvider data_scopes
*/
public function test_an_unreadable_secret_is_reported_and_not_overwritten( bool $network ): void {
$this->write_secret( 'test/key', 'one', $network );
$this->write_secret( 'test/key', 'two', $network );

$filter = $this->fail_record_selects( 'test/key', $network, 10 );
$read = $this->read_secret( 'test/key', $network );
$write = $this->write_secret( 'test/key', 'three', $network );

remove_filter( 'query', $filter );

$this->assertWPError( $read );
$this->assertSame( WP_SECRETS_ERROR_STORE_UNAVAILABLE, $read->get_error_code() );
$this->assertWPError( $write );
$this->assertSame( WP_SECRETS_ERROR_STORE_UNAVAILABLE, $write->get_error_code() );

// Once the database recovers, the failed reads must not leave the secret looking missing.
$this->assertSame( 'two', $this->read_secret( 'test/key', $network ) );
$this->assertSame( 'one', $this->read_secret( 'test/key', $network, WP_Secret_Version::PREVIOUS ) );
}

/**
* @ticket 66424
*
* @dataProvider data_scopes
*/
public function test_get_does_not_report_a_secret_as_absent_when_its_read_fails( bool $network ): void {
$store = new WP_Secrets_Option_Store();

$filter = $this->fail_record_selects( 'test/absent', $network, 10 );
$result = $this->without_db_errors( array( $store, 'get' ), 'test/absent', $network );

remove_filter( 'query', $filter );

$this->assertWPError( $result );
$this->assertSame( WP_SECRETS_ERROR_STORE_UNAVAILABLE, $result->get_error_code() );
$this->assertNull( $store->get( 'test/absent', $network ) );
}

/**
* @ticket 66424
*
* @dataProvider data_scopes
*/
public function test_delete_does_not_report_success_when_it_cannot_read_the_record( bool $network ): void {
$store = new WP_Secrets_Option_Store();
$record = $this->sample_record();

$store->set( 'test/key', $record, $network );

$filter = $this->fail_record_selects( 'test/key', $network, 10 );
$result = $this->without_db_errors( array( $store, 'delete' ), 'test/key', $network );

remove_filter( 'query', $filter );

$this->assertWPError( $result );
$this->assertSame( WP_SECRETS_ERROR_STORE_UNAVAILABLE, $result->get_error_code() );
$this->assertSame( $record, $store->get( 'test/key', $network ) );
}

/**
* Writes a secret through the public API.
*
* @param string $name The secret's namespaced name.
* @param string $value The value to store.
* @param bool $network Whether this is a network-scope secret.
* @return true|WP_Error
*/
private function write_secret( string $name, string $value, bool $network ) {
return $this->without_db_errors( $network ? 'wp_set_network_secret' : 'wp_set_secret', $name, $value );
}

/**
* Reads a secret through the public API.
*
* @param string $name The secret's namespaced name.
* @param bool $network Whether this is a network-scope secret.
* @param string $version The version slot to read.
* @return string|null|WP_Error The revealed value, null, or the error.
*/
private function read_secret( string $name, bool $network, string $version = WP_Secret_Version::CURRENT ) {
$secret = $this->without_db_errors( $network ? 'wp_get_network_secret' : 'wp_get_secret', $name, $version );

return $secret instanceof WP_Secret ? $secret->reveal() : $secret;
}

/**
* Makes the next database reads of a record fail, as a killed query would, after
* removing the record from the object cache so that it is read again.
*
* @param string $name The secret's namespaced name.
* @param bool $network Whether this is a network-scope secret.
* @param int $count Number of reads to fail.
* @return callable The 'query' filter that was added.
*/
private function fail_record_selects( string $name, bool $network, int $count ): callable {
$option_name = ( $network ? WP_Secrets_Option_Store::NETWORK_PREFIX : WP_Secrets_Option_Store::SITE_PREFIX ) . $name;

if ( $network && is_multisite() ) {
wp_cache_delete( get_current_network_id() . ':' . $option_name, 'site-options' );
} else {
wp_cache_delete( $option_name, 'options' );
}

$filter = static function ( $query ) use ( &$count, $option_name ) {
if ( $count > 0 && str_starts_with( ltrim( $query ), 'SELECT' ) && str_contains( $query, "'$option_name'" ) ) {
--$count;

return 'SELECT * FROM wptests_table_that_does_not_exist';
}

return $query;
};

add_filter( 'query', $filter );

return $filter;
}

/**
* Calls a function with database errors suppressed.
*
* @param callable $callback Function to call.
* @param mixed ...$args Arguments to pass.
* @return mixed The function's return value.
*/
private function without_db_errors( callable $callback, ...$args ) {
global $wpdb;

$suppress = $wpdb->suppress_errors( true );
$result = $callback( ...$args );
$wpdb->suppress_errors( $suppress );

return $result;
}
}
Loading