Skip to content

Commit 4b1da48

Browse files
mpeshevclaude
authored andcommitted
Secrets API: Stop Site Health advice that locks stored secrets.
The key source Site Health test recommended defining `WP_SECRETS_KEY`, or regenerating it, "with a value from `wp secret generate-key`". That command is not part of core or WP-CLI. Following the advice after any secret was stored also makes every stored secret unreadable and blocks saving new ones: the root key stays wrapped with the previous site key, and core has no path that re-wraps it. The key source test then reported the new key as good. * Report a configured key that cannot unwrap the stored root key as critical, and say that restoring the previous value fixes it. * Recommend a dedicated key only before any secret is stored. Once secrets exist, warn that changing the key or the salts it derives from makes them unreadable. * Drop the reference to the WP-CLI command. * Say in the undecryptable secrets test that restoring the previous key makes the secrets readable again, instead of that there is no way to recover them. Follow-up to [64337]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 47bf423 commit 4b1da48

2 files changed

Lines changed: 62 additions & 5 deletions

File tree

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,41 @@ function wp_secrets_site_health_test_key_source(): array {
123123
);
124124
}
125125

126+
/*
127+
* Once a root key exists, it is wrapped with the current site key, and core has no way
128+
* to re-wrap it under another one. Changing the key source then locks every stored
129+
* secret, and blocks saving new ones, until the change is reverted.
130+
*/
131+
$has_root_key = false !== get_site_option( WP_Secrets_Key_Manager::ROOT_KEY_OPTION );
132+
133+
if ( $has_root_key ) {
134+
$root_key = _wp_secrets_get_key_manager()->get_root_key();
135+
136+
if ( is_wp_error( $root_key ) ) {
137+
return wp_secrets_site_health_result(
138+
'secrets_api_key_source',
139+
__( 'Secrets API key does not match the stored secrets' ),
140+
'critical',
141+
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.' )
144+
)
145+
);
146+
}
147+
148+
wp_secrets_memzero( $root_key );
149+
}
150+
126151
if ( str_contains( $key_source, 'legacy interpretation' ) ) {
127152
return wp_secrets_site_health_result(
128153
'secrets_api_key_source',
129-
__( 'Secrets API key should be regenerated' ),
154+
__( 'Secrets API key is not in the recommended form' ),
130155
'recommended',
131156
sprintf(
132157
'<p>%s</p>',
133-
esc_html__( 'WP_SECRETS_KEY is defined, but not in the recommended base64-encoded 32-byte form. Regenerate it with `wp secret generate-key`.' )
158+
$has_root_key
159+
? esc_html__( 'WP_SECRETS_KEY is defined, but not as a base64-encoded random 32-byte value. Keep its current value: secrets are already encrypted with it, and changing it makes them unreadable.' )
160+
: esc_html__( 'WP_SECRETS_KEY is defined, but not as a base64-encoded random 32-byte value. No secrets are stored yet, so it can still be replaced with such a value.' )
134161
)
135162
);
136163
}
@@ -228,9 +255,9 @@ function wp_secrets_site_health_test_undecryptable(): array {
228255
'critical',
229256
sprintf(
230257
'<p>%s</p><ul>%s</ul><p>%s</p>',
231-
esc_html__( 'The following secrets exist but could not be decrypted with the current key. There is no way to recover the original value. The credential must be re-entered.' ),
258+
esc_html__( 'The following secrets exist but could not be decrypted with the current key.' ),
232259
$items,
233-
esc_html__( 'This can happen after losing WP_SECRETS_KEY, restoring a database backup without its matching key, or a failed key rotation.' )
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.' )
234261
)
235262
);
236263
}

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,37 @@ public function test_key_source_test_recommends_regeneration_for_a_legacy_key():
8989
$result = wp_secrets_site_health_test_key_source();
9090

9191
$this->assertSame( 'recommended', $result['status'] );
92-
$this->assertStringContainsString( 'generate-key', $result['description'] );
92+
$this->assertStringContainsString( 'can still be replaced', $result['description'] );
93+
$this->assertStringNotContainsString( 'generate-key', $result['description'], 'There is no such WP-CLI command in core.' );
94+
}
95+
96+
/**
97+
* Replacing the key once secrets are stored would make them unreadable.
98+
*
99+
* @runInSeparateProcess
100+
* @preserveGlobalState disabled
101+
*/
102+
public function test_key_source_test_warns_to_keep_a_legacy_key_once_secrets_are_stored(): void {
103+
define( 'WP_SECRETS_KEY', 'not-base64-32!!' );
104+
105+
$this->assertTrue( wp_set_secret( 'myplugin/api-key', 'value' ) );
106+
107+
$result = wp_secrets_site_health_test_key_source();
108+
109+
$this->assertSame( 'recommended', $result['status'] );
110+
$this->assertStringContainsString( 'Keep its current value', $result['description'] );
111+
}
112+
113+
public function test_key_source_test_is_critical_when_the_key_does_not_match_the_stored_secrets(): void {
114+
wp_set_secret( 'myplugin/api-key', 'value' );
115+
116+
// Stands in for a root key that was wrapped with a different site key.
117+
update_site_option( WP_Secrets_Key_Manager::ROOT_KEY_OPTION, base64_encode( random_bytes( 72 ) ) );
118+
119+
$result = wp_secrets_site_health_test_key_source();
120+
121+
$this->assertSame( 'critical', $result['status'] );
122+
$this->assertStringContainsString( 'Restoring the previous value', $result['description'] );
93123
}
94124

95125
/**

0 commit comments

Comments
 (0)