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
65 changes: 43 additions & 22 deletions src/wp-admin/includes/class-wp-site-health.php
Original file line number Diff line number Diff line change
Expand Up @@ -2953,28 +2953,42 @@ public function get_test_secrets_api_key_source(): array {

if ( WP_Secrets_Config_Key_Provider::KEY_STATUS_UNDEFINED === $key_status || WP_Secrets_Config_Key_Provider::KEY_STATUS_UNUSABLE === $key_status ) {
$is_defined = WP_Secrets_Config_Key_Provider::KEY_STATUS_UNUSABLE === $key_status;
$example = sprintf( "define( 'WP_SECRETS_KEY', '%s' );", base64_encode( random_bytes( 32 ) ) );

return $this->get_secrets_api_test_result(
'secrets_api_key_source',
__( 'Secrets API has no encryption key' ),
'recommended',
sprintf(
'<p>%s</p><p>%s</p><p><code>%s</code></p><p>%s</p>',
$is_defined
? sprintf(
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php, 3: wp-config-sample.php */
__( '%1$s is defined in %2$s, but its value cannot be used: it is empty, still the placeholder from %3$s, or the same as one of the authentication keys and salts. Plugins cannot store API keys and other credentials with the Secrets API until it is fixed.' ),
'<code>WP_SECRETS_KEY</code>',
'<code>wp-config.php</code>',
'<code>wp-config-sample.php</code>'
)
: sprintf(
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php */
__( '%1$s is not defined in %2$s. Plugins cannot store API keys and other credentials with the Secrets API until it is.' ),
'<code>WP_SECRETS_KEY</code>',
'<code>wp-config.php</code>'
),
// This runs on every load of the screen and in a scheduled event, so a failure to generate a key must not be fatal.
try {
$example = sprintf( "define( 'WP_SECRETS_KEY', '%s' );", base64_encode( random_bytes( 32 ) ) );
} catch ( Exception $e ) {
$example = '';
}

$problem = $is_defined
? sprintf(
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php, 3: wp-config-sample.php */
__( '%1$s is defined in %2$s, but its value cannot be used: it is empty, still the placeholder from %3$s, or the same as one of the authentication keys and salts. Plugins cannot store API keys and other credentials with the Secrets API until it is fixed.' ),
'<code>WP_SECRETS_KEY</code>',
'<code>wp-config.php</code>',
'<code>wp-config-sample.php</code>'
)
: sprintf(
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php */
__( '%1$s is not defined in %2$s. Plugins cannot store API keys and other credentials with the Secrets API until it is.' ),
'<code>WP_SECRETS_KEY</code>',
'<code>wp-config.php</code>'
);

if ( '' === $example ) {
$instructions = sprintf(
'<p>%s</p>',
sprintf(
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php */
__( 'A key could not be generated on this server. Set %1$s in %2$s to a base64-encoded random 32-byte value, and keep a copy of it with your backups: secrets stored with it cannot be read without it.' ),
'<code>WP_SECRETS_KEY</code>',
'<code>wp-config.php</code>'
)
);
} else {
$instructions = sprintf(
'<p>%s</p><p><code>%s</code></p><p>%s</p>',
$is_defined
? sprintf(
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php */
Expand All @@ -2990,7 +3004,14 @@ public function get_test_secrets_api_key_source(): array {
),
esc_html( $example ),
esc_html__( 'A new value is generated each time this page loads. Keep a copy of the one you use with your backups: secrets stored with it cannot be read without it.' )
)
);
}

return $this->get_secrets_api_test_result(
'secrets_api_key_source',
__( 'Secrets API has no encryption key' ),
'recommended',
sprintf( '<p>%s</p>%s', $problem, $instructions )
);
}

Expand Down
26 changes: 16 additions & 10 deletions src/wp-includes/secrets.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@
*
* @param string $value The value to clear, by reference.
*/
function wp_secrets_memzero( &$value ): void {
function wp_secrets_memzero(
#[\SensitiveParameter]
&$value
): void {
if ( ! is_string( $value ) ) {
return;
}
Expand Down Expand Up @@ -700,14 +703,20 @@ function wp_secrets_provider_is_writable(): bool {
* Whether the Secrets API can store and read secrets on this site.
*
* A plugin can check this before it moves a credential into the Secrets API, and
* keep its existing storage while it returns false:
*
* if ( wp_is_secrets_api_available() ) {
* // Move the credential, then use wp_set_secret() and wp_get_secret().
* } else {
* // Keep using the existing storage.
* keep its existing storage while it returns false. An available provider can still
* refuse writes, so the plugin also checks wp_secrets_provider_is_writable(), and
* removes its own copy only after wp_set_secret() has returned true:
*
* if ( wp_is_secrets_api_available() && wp_secrets_provider_is_writable() ) {
* if ( true === wp_set_secret( 'my-plugin/api-key', $api_key ) ) {
* // Stored. Remove the copy in the existing storage.
* }
* }
*
* When the API is available but the provider is not writable, its credentials are
* managed outside WordPress. Read them with wp_get_secret(), and do not fall back
* to storing a credential elsewhere because a write was refused.
*
* Returns false when the secrets.php drop-in failed to load, when the default
* keyring has no usable WP_SECRETS_KEY, or when WP_SECRETS_KEY cannot unwrap the
* stored root key. The last happens when a site that already stored secrets adds
Expand All @@ -717,9 +726,6 @@ function wp_secrets_provider_is_writable(): bool {
* reported as available. Its backend can still be unreachable on a given request,
* which each call reports as a WP_Error.
*
* Whether the provider accepts writes is a separate question. See
* wp_secrets_provider_is_writable().
*
* @since 7.2.0
*
* @global WP_Secrets_Provider $wp_secrets_provider Optional. Provider override set by the drop-in.
Expand Down
7 changes: 5 additions & 2 deletions src/wp-includes/secrets/class-wp-secrets-cipher.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* This class does not know where a master key comes from (that is
* WP_Secrets_Key_Manager's job), does not know about the 'v'/current/previous record
* envelope (that is assembled by the functions in secrets.php), and does not set
* envelope (that is assembled by WP_Secrets_Libsodium_Provider), and does not set
* 'created' or 'needs_rotation' (those are facts about a write, not about the
* cryptography). It knows exactly one thing: given a 32-byte master key and a single
* slot's worth of material, encrypt or decrypt it correctly.
Expand Down Expand Up @@ -375,7 +375,10 @@ private function invalid_argument( string $message ): WP_Error {
* @param string $master_key Candidate master key.
* @return bool
*/
private function is_valid_master_key( string $master_key ): bool {
private function is_valid_master_key(
#[\SensitiveParameter]
string $master_key
): bool {
return SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES === strlen( $master_key );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,12 @@ private function get_site_key() {
*
* @phpstan-assert-if-true non-empty-string $value
*/
private function is_usable_key_value( $value, array $auth_values ): bool {
private function is_usable_key_value(
#[\SensitiveParameter]
$value,
#[\SensitiveParameter]
array $auth_values
): bool {
return is_string( $value )
&& '' !== trim( $value )
&& self::KNOWN_PLACEHOLDER !== $value
Expand Down
23 changes: 16 additions & 7 deletions src/wp-includes/secrets/class-wp-secrets-key-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ public function __construct( ?WP_Secrets_Keyring $keyring = null ) {
/**
* Returns the keyring in use.
*
* Used by Site Health and by `wp secret dropin` and `wp secret health`, which all
* need to describe the active keyring without duplicating the logic that resolves
* it.
* Used by Site Health and wp_is_secrets_api_available(), which need to describe
* the active keyring without duplicating the logic that resolves it.
*
* @since 7.2.0
*
Expand Down Expand Up @@ -486,33 +485,43 @@ private function read_stored_root_key() {
global $wpdb;

if ( is_multisite() ) {
$value = $wpdb->get_var(
$result = $wpdb->query(
$wpdb->prepare(
"SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d ORDER BY meta_id ASC LIMIT 1",
self::ROOT_KEY_OPTION,
get_current_network_id()
)
);
} else {
$value = $wpdb->get_var(
$result = $wpdb->query(
$wpdb->prepare(
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
self::ROOT_KEY_OPTION
)
);
}

if ( '' !== $wpdb->last_error ) {
/*
* Failure is taken from wpdb::query(), because wpdb::$last_error is not set on
* every failure path, such as when the connection is not ready.
*/
if ( false === $result ) {
return new WP_Error(
WP_SECRETS_ERROR_STORE_UNAVAILABLE,
__( 'Could not read the root key.' )
);
}

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

// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Reads the result of the prepared query above.
$value = $wpdb->get_var();

// A row with no value exists, so it is reported as malformed below, not as absent.
$value = $value ?? '';

$this->forget_missing_root_key();

$value = maybe_unserialize( $value );
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/secrets/class-wp-secrets-keyring.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* and cannot turn encryption off. There is no method here that
* accepts a plaintext secret value at all.
*
* An implementation should run WP_Secrets_Keyring_Conformance against itself before
* shipping.
* An implementation should run WP_Secrets_Keyring_Conformance, from core's PHPUnit
* test suite, against itself before shipping.
*
* Implementations extend this class. It is abstract rather than an interface so
* that a later release can add a method with a default implementation, and every
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public function get_protection_boundary(): string {
public function is_writable(): bool {
return true;
}

/**
* Validates the shape of a record read back from a store.
*
Expand Down Expand Up @@ -476,7 +477,6 @@ public function set(
* @phpstan-param WP_Secret_Version::CURRENT|WP_Secret_Version::PREVIOUS $version
*/
public function get( string $name, string $version, bool $network = false ) {

$name_check = wp_secrets_validate_name( $name );

if ( is_wp_error( $name_check ) ) {
Expand Down Expand Up @@ -689,7 +689,6 @@ public function retire_previous( string $name, bool $network = false ) {
* @phpstan-return list<array{name: string, fingerprint: string, created: int, has_previous: bool, needs_rotation: bool}>|WP_Error
*/
public function list_secrets( string $name_prefix = '', bool $network = false ) {

$store = $this->store;

$names = $store->list_names( $network );
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/secrets/class-wp-secrets-store.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Where a secret's encrypted record lives.
*
* An implementation is never handed a plaintext secret, only the record array
* produced by WP_Secrets_Cipher and assembled by the functions in secrets.php, and
* produced by WP_Secrets_Cipher and assembled by WP_Secrets_Libsodium_Provider, and
* cannot turn encryption off; there is no method here that accepts or returns
* anything but ciphertext-bearing structures.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/phpunit/tests/secrets/wpSecretsKeyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,52 @@ public function test_an_unreadable_root_key_is_reported_and_not_replaced(): void
$this->assertSame( $stored, $this->get_stored_root_key(), 'An unreadable root key should not be replaced.' );
}

/**
* Ensure that a root key read that fails without a database error message is reported as an unavailable store.
*
* wpdb::query() returns false without setting wpdb::$last_error on some failure
* paths, such as when the connection is not ready.
*
* @ticket 66187
*
* @covers WP_Secrets_Key_Manager::get_root_key
* @covers WP_Secrets_Key_Manager::read_stored_root_key
*/
public function test_a_root_key_read_that_fails_without_an_error_message_is_reported_and_not_replaced(): void {
global $wpdb;

$this->assertTrue( wp_set_secret( 'test/key', 'value-one' ), 'Storing the first secret should succeed.' );

$stored = $this->get_stored_root_key();
$manager = new WP_Secrets_Key_Manager( new WP_Secrets_Config_Key_Provider() );

if ( is_multisite() ) {
wp_cache_delete( get_current_network_id() . ':' . WP_Secrets_Key_Manager::ROOT_KEY_OPTION, 'site-options' );
} else {
wp_cache_delete( WP_Secrets_Key_Manager::ROOT_KEY_OPTION, 'options' );
}

// Clear the previous query's result, which a query that fails this way leaves in place.
$wpdb->flush();

$ready = $wpdb->ready;
$wpdb->ready = false;

$read = $manager->get_root_key( false );
$write = $manager->get_root_key();
$last_error = $wpdb->last_error;

$wpdb->ready = $ready;

$this->assertSame( '', $last_error, 'The failed reads should not have set a database error message.' );
$this->assertWPError( $read, 'Reading the root key should fail while the database is not ready.' );
$this->assertSame( WP_SECRETS_ERROR_STORE_UNAVAILABLE, $read->get_error_code(), 'The failed read should report an unavailable store, not a missing root key.' );
$this->assertWPError( $write, 'Reading the root key for a write should fail while the database is not ready.' );
$this->assertSame( WP_SECRETS_ERROR_STORE_UNAVAILABLE, $write->get_error_code(), 'The failed read for a write should report an unavailable store.' );
$this->assertSame( $stored, $this->get_stored_root_key(), 'An unreadable root key should not be replaced.' );
$this->assertSame( 'value-one', wp_get_secret( 'test/key' )->reveal(), 'The secret should decrypt again once the root key can be read.' );
}

/**
* Ensure that reading a secret never generates a root key.
*
Expand Down
Loading