Skip to content

Commit f2ffb32

Browse files
committed
Security: Require WP_SECRETS_KEY for the Secrets API.
Without `WP_SECRETS_KEY`, the default keyring derived its key from `LOGGED_IN_KEY . LOGGED_IN_SALT`. Rotating the salts, which is routine advice, then locked every stored secret. The salts were also read directly rather than through `wp_salt()`, so an unfilled or translated placeholder from `wp-config-sample.php` could become the key. This removes the fallback. Without a usable `WP_SECRETS_KEY`, every call that needs the key returns `secret_key_unavailable`. A key is not usable when it is empty or only whitespace, when it is the `wp-config-sample.php` placeholder, or when it equals any of the authentication keys and salts. Site Health reports a missing or unusable key as recommended and shows a newly generated `define()` line to add. It reads the key's state from the new `WP_Secrets_Config_Key_Provider::get_key_status()` instead of parsing the translated key source. This also introduces `wp_is_secrets_api_available()`, so a caller can check before moving a credential into the Secrets API. It returns `false` when the `secrets.php` drop-in failed to load, when the default keyring has no usable key, or when the key cannot unwrap the stored root key. A site that stored secrets under the fallback keeps them. They can be read again once `WP_SECRETS_KEY` is set to a new key, `WP_SECRETS_KEY_PREVIOUS` is set to the old `LOGGED_IN_KEY . LOGGED_IN_SALT`, and the root key is re-wrapped with `WP_Secrets_Key_Manager::rotate_site_key()`. Follow-up to r64337. Props ericmann, peterwilsoncc, Clorith, dhrupo, wildworks, johnbillion, jorbin, digitalpritam. See #66187. git-svn-id: https://develop.svn.wordpress.org/trunk@65385 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0747041 commit f2ffb32

8 files changed

Lines changed: 613 additions & 139 deletions

File tree

‎src/wp-admin/includes/secrets-site-health.php‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ function wp_secrets_site_health_result( string $test, string $label, string $sta
7373
}
7474

7575
/**
76-
* Tests whether the site key comes from a dedicated constant or a weaker fallback.
76+
* Tests whether the site key is defined, and in the recommended form.
7777
*
7878
* @since 7.2.0
7979
*
@@ -82,7 +82,9 @@ function wp_secrets_site_health_result( string $test, string $label, string $sta
8282
* @phpstan-return array{test: string, label: string, status: 'good'|'recommended'|'critical', badge: array{label: string, color: string}, description: string}
8383
*/
8484
function wp_secrets_site_health_test_key_source(): array {
85-
$key_source = _wp_secrets_get_key_manager()->get_keyring()->get_key_source();
85+
$keyring = _wp_secrets_get_key_manager()->get_keyring();
86+
$key_source = $keyring->get_key_source();
87+
$key_status = $keyring instanceof WP_Secrets_Config_Key_Provider ? $keyring->get_key_status() : null;
8688

8789
if ( str_contains( $key_source, 'broken' ) ) {
8890
return wp_secrets_site_health_result(
@@ -96,7 +98,7 @@ function wp_secrets_site_health_test_key_source(): array {
9698
);
9799
}
98100

99-
if ( str_contains( $key_source, 'legacy interpretation' ) ) {
101+
if ( WP_Secrets_Config_Key_Provider::KEY_STATUS_LEGACY === $key_status ) {
100102
return wp_secrets_site_health_result(
101103
'secrets_api_key_source',
102104
__( 'Secrets API key should be regenerated' ),
@@ -108,14 +110,45 @@ function wp_secrets_site_health_test_key_source(): array {
108110
);
109111
}
110112

111-
if ( str_contains( $key_source, 'LOGGED_IN_KEY' ) ) {
113+
if ( WP_Secrets_Config_Key_Provider::KEY_STATUS_UNDEFINED === $key_status || WP_Secrets_Config_Key_Provider::KEY_STATUS_UNUSABLE === $key_status ) {
114+
$is_defined = WP_Secrets_Config_Key_Provider::KEY_STATUS_UNUSABLE === $key_status;
115+
$example = sprintf( "define( 'WP_SECRETS_KEY', '%s' );", base64_encode( random_bytes( 32 ) ) );
116+
112117
return wp_secrets_site_health_result(
113118
'secrets_api_key_source',
114-
__( 'Secrets API is using a fallback key' ),
119+
__( 'Secrets API has no encryption key' ),
115120
'recommended',
116121
sprintf(
117-
'<p>%s</p>',
118-
esc_html__( 'No dedicated WP_SECRETS_KEY constant is defined. Secrets are encrypted using a key derived from existing salts, which works but is not the recommended configuration. Define WP_SECRETS_KEY with a value from `wp secret generate-key`.' )
122+
'<p>%s</p><p>%s</p><p><code>%s</code></p><p>%s</p>',
123+
$is_defined
124+
? sprintf(
125+
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php, 3: wp-config-sample.php */
126+
__( '%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.' ),
127+
'<code>WP_SECRETS_KEY</code>',
128+
'<code>wp-config.php</code>',
129+
'<code>wp-config-sample.php</code>'
130+
)
131+
: sprintf(
132+
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php */
133+
__( '%1$s is not defined in %2$s. Plugins cannot store API keys and other credentials with the Secrets API until it is.' ),
134+
'<code>WP_SECRETS_KEY</code>',
135+
'<code>wp-config.php</code>'
136+
),
137+
$is_defined
138+
? sprintf(
139+
/* translators: 1: WP_SECRETS_KEY, 2: wp-config.php */
140+
__( 'Replace the %1$s line in %2$s with this one:' ),
141+
'<code>WP_SECRETS_KEY</code>',
142+
'<code>wp-config.php</code>'
143+
)
144+
: sprintf(
145+
/* translators: 1: wp-config.php, 2: Translated version of "That's all, stop editing! Happy publishing." */
146+
__( 'Add this line to %1$s, above the line reading %2$s:' ),
147+
'<code>wp-config.php</code>',
148+
'<code>/* ' . __( 'That&#8217;s all, stop editing! Happy publishing.' ) . ' */</code>'
149+
),
150+
esc_html( $example ),
151+
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.' )
119152
)
120153
);
121154
}

‎src/wp-includes/secrets.php‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,77 @@ function wp_secrets_provider_is_writable(): bool {
693693
return _wp_secrets_get_provider()->is_writable();
694694
}
695695

696+
/**
697+
* Whether the Secrets API can store and read secrets on this site.
698+
*
699+
* A plugin can check this before it moves a credential into the Secrets API, and
700+
* keep its existing storage while it returns false:
701+
*
702+
* if ( wp_is_secrets_api_available() ) {
703+
* // Move the credential, then use wp_set_secret() and wp_get_secret().
704+
* } else {
705+
* // Keep using the existing storage.
706+
* }
707+
*
708+
* Returns false when the secrets.php drop-in failed to load, when the default
709+
* keyring has no usable WP_SECRETS_KEY, or when WP_SECRETS_KEY cannot unwrap the
710+
* stored root key. The last happens when a site that already stored secrets adds
711+
* or changes the key without re-wrapping the root key.
712+
*
713+
* A provider or keyring from a working drop-in manages its own keys, so it is
714+
* reported as available. Its backend can still be unreachable on a given request,
715+
* which each call reports as a WP_Error.
716+
*
717+
* Whether the provider accepts writes is a separate question. See
718+
* wp_secrets_provider_is_writable().
719+
*
720+
* @since 7.2.0
721+
*
722+
* @global WP_Secrets_Provider $wp_secrets_provider Optional. Provider override set by the drop-in.
723+
*
724+
* @return bool
725+
*/
726+
function wp_is_secrets_api_available(): bool {
727+
if ( _wp_secrets_get_provider() instanceof WP_Secrets_Broken_Provider ) {
728+
return false;
729+
}
730+
731+
if ( isset( $GLOBALS['wp_secrets_provider'] ) ) {
732+
return true;
733+
}
734+
735+
$keyring = _wp_secrets_get_key_manager()->get_keyring();
736+
737+
if ( $keyring instanceof WP_Secrets_Broken_Keyring ) {
738+
return false;
739+
}
740+
741+
if ( $keyring instanceof WP_Secrets_Config_Key_Provider ) {
742+
$is_usable = in_array(
743+
$keyring->get_key_status(),
744+
array( WP_Secrets_Config_Key_Provider::KEY_STATUS_BASE64, WP_Secrets_Config_Key_Provider::KEY_STATUS_LEGACY ),
745+
true
746+
);
747+
748+
if ( ! $is_usable ) {
749+
return false;
750+
}
751+
752+
$root_key = _wp_secrets_get_key_manager()->get_root_key( false );
753+
754+
if ( ! is_wp_error( $root_key ) ) {
755+
wp_secrets_memzero( $root_key );
756+
757+
return true;
758+
}
759+
760+
// With no root key stored yet, the first write creates one under this key.
761+
return false === get_site_option( WP_Secrets_Key_Manager::ROOT_KEY_OPTION );
762+
}
763+
764+
return true;
765+
}
766+
696767
/**
697768
* A human-readable description of what is protecting this site's secrets.
698769
*

0 commit comments

Comments
 (0)