Skip to content

Commit 1b7c9d1

Browse files
committed
Secrets API: Report a keyring's own error when it cannot unlock the stored secrets.
Addresses review. Only the default keyring reads `wp-config.php`, so the critical key source result names `WP_SECRETS_KEY` only for it. A keyring from a `secrets.php` drop-in, such as a key service that is unreachable or was installed without re-wrapping the root key, has its own error shown instead. The label no longer says the key "does not match", which does not fit an outage. With `WP_SECRETS_KEY` now required, the key source and undecryptable results no longer mention `LOGGED_IN_KEY` and `LOGGED_IN_SALT`. When a drop-in replaces the provider, Site Health Info marks the keyring class and key source as not used, instead of describing a keyring that the site does not use. See #66380, #66187.
1 parent 4b1da48 commit 1b7c9d1

2 files changed

Lines changed: 108 additions & 17 deletions

File tree

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,21 @@ function wp_secrets_site_health_result( string $test, string $label, string $sta
7373
);
7474
}
7575

76+
/**
77+
* Whether the active provider protects secrets with the keyring.
78+
*
79+
* A secrets.php drop-in can replace the whole provider, which then protects secrets
80+
* with its own keys. The default keyring, and WP_SECRETS_KEY, are not used at all.
81+
*
82+
* @since 7.2.0
83+
*
84+
* @param WP_Secrets_Provider $provider The active provider.
85+
* @return bool
86+
*/
87+
function wp_secrets_site_health_uses_keyring( WP_Secrets_Provider $provider ): bool {
88+
return $provider instanceof WP_Secrets_Libsodium_Provider || $provider instanceof WP_Secrets_Broken_Provider;
89+
}
90+
7691
/**
7792
* Site Health test: is the site key defined, and in the recommended form?
7893
*
@@ -85,11 +100,7 @@ function wp_secrets_site_health_result( string $test, string $label, string $sta
85100
function wp_secrets_site_health_test_key_source(): array {
86101
$provider = _wp_secrets_get_provider();
87102

88-
/*
89-
* A secrets.php drop-in can replace the whole provider, which then protects secrets
90-
* with its own keys. The default keyring, and WP_SECRETS_KEY, are not used at all.
91-
*/
92-
if ( ! $provider instanceof WP_Secrets_Libsodium_Provider && ! $provider instanceof WP_Secrets_Broken_Provider ) {
103+
if ( ! wp_secrets_site_health_uses_keyring( $provider ) ) {
93104
return wp_secrets_site_health_result(
94105
'secrets_api_key_source',
95106
__( 'Secrets API keys are managed by the provider' ),
@@ -134,13 +145,27 @@ function wp_secrets_site_health_test_key_source(): array {
134145
$root_key = _wp_secrets_get_key_manager()->get_root_key();
135146

136147
if ( is_wp_error( $root_key ) ) {
148+
/*
149+
* Only the default keyring reads wp-config.php. A keyring from a secrets.php
150+
* drop-in fails for its own reasons, such as an unreachable key service, and
151+
* its error is the useful part.
152+
*/
153+
$cause = $keyring instanceof WP_Secrets_Config_Key_Provider
154+
? __( 'This happens when WP_SECRETS_KEY was changed or removed after secrets were stored. Restoring the previous value fixes it.' )
155+
: sprintf(
156+
/* translators: %s: Error message from the keyring. */
157+
__( 'The keyring of the secrets.php drop-in reported: %s' ),
158+
$root_key->get_error_message()
159+
);
160+
137161
return wp_secrets_site_health_result(
138162
'secrets_api_key_source',
139-
__( 'Secrets API key does not match the stored secrets' ),
163+
__( 'Secrets API cannot unlock the stored secrets' ),
140164
'critical',
141165
sprintf(
142-
'<p>%s</p>',
143-
esc_html__( 'The configured key cannot decrypt the root key that protects the stored secrets, so no secret can be read or saved. This happens when WP_SECRETS_KEY, LOGGED_IN_KEY, or LOGGED_IN_SALT changed after secrets were stored. Restoring the previous value fixes it.' )
166+
'<p>%s</p><p>%s</p>',
167+
esc_html__( 'The configured key cannot decrypt the root key that protects the stored secrets, so no secret can be read or saved.' ),
168+
esc_html( $cause )
144169
)
145170
);
146171
}
@@ -257,7 +282,7 @@ function wp_secrets_site_health_test_undecryptable(): array {
257282
'<p>%s</p><ul>%s</ul><p>%s</p>',
258283
esc_html__( 'The following secrets exist but could not be decrypted with the current key.' ),
259284
$items,
260-
esc_html__( 'This can happen after WP_SECRETS_KEY, LOGGED_IN_KEY, or LOGGED_IN_SALT changed, after restoring a database backup without its matching key, or after a failed key rotation. Restoring the previous key makes the secrets readable again. Otherwise, each credential must be entered again.' )
285+
esc_html__( 'This can happen after WP_SECRETS_KEY was changed or removed, after restoring a database backup without its matching key, or after a failed key rotation. Restoring the previous key makes the secrets readable again. Otherwise, each credential must be entered again.' )
261286
)
262287
);
263288
}
@@ -392,8 +417,9 @@ function wp_secrets_site_health_count_needing_rotation( bool $network ) {
392417
* @return array<string, array<string, mixed>>
393418
*/
394419
function wp_secrets_site_health_debug_info( $info ) {
395-
$key_manager = _wp_secrets_get_key_manager();
396-
$provider = _wp_secrets_get_provider();
420+
$key_manager = _wp_secrets_get_key_manager();
421+
$provider = _wp_secrets_get_provider();
422+
$uses_keyring = wp_secrets_site_health_uses_keyring( $provider );
397423

398424
$fields = array(
399425
'dropin_active' => array(
@@ -420,11 +446,11 @@ function wp_secrets_site_health_debug_info( $info ) {
420446
),
421447
'keyring_class' => array(
422448
'label' => __( 'Keyring class' ),
423-
'value' => get_class( $key_manager->get_keyring() ),
449+
'value' => $uses_keyring ? get_class( $key_manager->get_keyring() ) : __( 'Not used by the provider' ),
424450
),
425451
'key_source' => array(
426452
'label' => __( 'Key source' ),
427-
'value' => $key_manager->get_keyring()->get_key_source(),
453+
'value' => $uses_keyring ? $key_manager->get_keyring()->get_key_source() : __( 'Not used by the provider' ),
428454
),
429455
'record_version' => array(
430456
'label' => __( 'Record format version' ),

‎tests/phpunit/tests/secrets/siteHealth.php‎

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,53 @@ public function test_key_source_test_is_critical_when_the_key_does_not_match_the
122122
$this->assertStringContainsString( 'Restoring the previous value', $result['description'] );
123123
}
124124

125+
/**
126+
* Secrets were stored, then WP_SECRETS_KEY was removed.
127+
*
128+
* The root key is wrapped with a keyring that reads WP_SECRETS_KEY_PREVIOUS,
129+
* because WP_SECRETS_KEY can only be defined once in a process.
130+
*
131+
* @ticket 66380
132+
*
133+
* @runInSeparateProcess
134+
* @preserveGlobalState disabled
135+
*/
136+
public function test_key_source_test_is_critical_when_the_key_was_removed_after_secrets_were_stored(): void {
137+
define( 'WP_SECRETS_KEY_PREVIOUS', base64_encode( str_repeat( 'P', 32 ) ) );
138+
139+
$previous = new WP_Secrets_Config_Key_Provider( true );
140+
update_site_option( WP_Secrets_Key_Manager::ROOT_KEY_OPTION, $previous->wrap( random_bytes( 32 ) ) );
141+
142+
$result = wp_secrets_site_health_test_key_source();
143+
144+
$this->assertSame( 'critical', $result['status'] );
145+
$this->assertStringContainsString( 'WP_SECRETS_KEY was changed or removed', $result['description'] );
146+
}
147+
148+
/**
149+
* A keyring from a secrets.php drop-in fails for its own reasons, such as an
150+
* unreachable key service. The result reports its error, and does not send
151+
* the site owner to wp-config.php.
152+
*
153+
* @ticket 66380
154+
*
155+
* @runInSeparateProcess
156+
* @preserveGlobalState disabled
157+
*/
158+
public function test_key_source_test_reports_the_error_of_a_dropin_keyring_that_cannot_unwrap(): void {
159+
$keyring = new Mock_Keyring();
160+
update_site_option( WP_Secrets_Key_Manager::ROOT_KEY_OPTION, $keyring->wrap( random_bytes( 32 ) ) );
161+
$keyring->configure_fail_unwrap();
162+
163+
$GLOBALS['wp_secrets_keyring'] = $keyring;
164+
165+
$result = wp_secrets_site_health_test_key_source();
166+
167+
$this->assertSame( 'critical', $result['status'] );
168+
$this->assertStringContainsString( 'unwrap() configured to fail', $result['description'] );
169+
$this->assertStringNotContainsString( 'WP_SECRETS_KEY', $result['description'] );
170+
}
171+
125172
/**
126173
* @runInSeparateProcess
127174
* @preserveGlobalState disabled
@@ -169,10 +216,6 @@ static function ( $translation, $text ) {
169216
$this->assertSame( 'critical', $result['status'] );
170217
}
171218

172-
/**
173-
* @runInSeparateProcess
174-
* @preserveGlobalState disabled
175-
*/
176219
/**
177220
* A provider from a secrets.php drop-in does not use the default keyring.
178221
*
@@ -308,6 +351,28 @@ public function test_debug_info_reports_expected_fields(): void {
308351
$this->assertSame( '1', $fields['site_secret_count']['value'] );
309352
}
310353

354+
/**
355+
* A provider from a secrets.php drop-in does not use the default keyring, so
356+
* the Info tab does not describe it.
357+
*
358+
* @ticket 66380
359+
*
360+
* @runInSeparateProcess
361+
* @preserveGlobalState disabled
362+
*/
363+
public function test_debug_info_does_not_describe_the_keyring_when_a_dropin_replaces_the_provider(): void {
364+
$provider = $this->createMock( WP_Secrets_Provider::class );
365+
$provider->method( 'get_label' )->willReturn( 'Example Vault' );
366+
$provider->method( 'list_secrets' )->willReturn( array() );
367+
368+
$GLOBALS['wp_secrets_provider'] = $provider;
369+
370+
$fields = $this->debug_fields( wp_secrets_site_health_debug_info( array() ) );
371+
372+
$this->assertSame( 'Not used by the provider', $fields['keyring_class']['value'] );
373+
$this->assertSame( 'Not used by the provider', $fields['key_source']['value'] );
374+
}
375+
311376
public function test_debug_info_reports_no_dropin_by_default(): void {
312377
$info = wp_secrets_site_health_debug_info( array() );
313378

0 commit comments

Comments
 (0)