Skip to content

Commit db24fa7

Browse files
committed
Security: Harden secret names, value parameters and the root key on All Settings.
Three small fixes to the Secrets API: * `wp_secrets_validate_name()` anchors its segment pattern with `\z`. With `$`, a name with a trailing newline passed, and since option names are trimmed it shared a row with the name without the newline while encrypting for itself. * Parameters that carry a plaintext value through `wp_set_secret()` and `wp_set_network_secret()` are marked `#[\SensitiveParameter]`, and so are the parameters that carry key material: `$key_material` on `WP_Secrets_Keyring::wrap()` and its implementations, and `$master_key` on the `WP_Secrets_Cipher` methods and `WP_Secrets_Libsodium_Provider::demote_slot()`. An exception thrown while saving does not print the value or a key in a stack trace. * Settings > All Settings masks `_wp_secrets_root_key` and excludes it from the save, as it already does for connector API keys. Replacing the root key made every stored secret unreadable. Props aryanjasala, ericmann, dhrupo. Fixes #66414.
1 parent eed6f20 commit db24fa7

11 files changed

Lines changed: 205 additions & 14 deletions

‎src/wp-admin/options.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,10 @@
439439
// Mask connector API keys and prevent updates from this screen.
440440
$value = _wp_connectors_mask_api_key( $option->option_value );
441441
$disabled = true;
442+
} elseif ( WP_Secrets_Key_Manager::ROOT_KEY_OPTION === $option->option_name ) {
443+
// Replacing the root key makes every stored secret unreadable.
444+
$value = _wp_connectors_mask_api_key( $option->option_value );
445+
$disabled = true;
442446
} else {
443447
$value = $option->option_value;
444448
$options_to_update[] = $option->option_name;

‎src/wp-includes/secrets.php‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ function wp_secrets_validate_name( string $name ) {
249249
);
250250
}
251251

252-
$segment_pattern = '/^[a-z0-9]([a-z0-9_-]*[a-z0-9])?$/';
252+
$segment_pattern = '/^[a-z0-9]([a-z0-9_-]*[a-z0-9])?\z/';
253253

254254
if ( 0 === $slashes ) {
255255
if ( ! preg_match( $segment_pattern, $name ) ) {
@@ -423,7 +423,11 @@ function wp_using_secrets_dropin(): bool {
423423
* @param string $value The plaintext value to store.
424424
* @return true|WP_Error
425425
*/
426-
function wp_set_secret( string $name, $value ) {
426+
function wp_set_secret(
427+
string $name,
428+
#[\SensitiveParameter]
429+
$value
430+
) {
427431
return _wp_secrets_set( $name, $value, false );
428432
}
429433

@@ -560,7 +564,11 @@ function wp_list_secrets( string $namespace = '' ) { // phpcs:ignore Universal.N
560564
* @param string $value The plaintext value to store.
561565
* @return true|WP_Error
562566
*/
563-
function wp_set_network_secret( string $name, $value ) {
567+
function wp_set_network_secret(
568+
string $name,
569+
#[\SensitiveParameter]
570+
$value
571+
) {
564572
return _wp_secrets_set( $name, $value, true );
565573
}
566574

@@ -724,7 +732,14 @@ function wp_secrets_provider_label(): string {
724732
* wp_secret_changed.
725733
* @return true|WP_Error
726734
*/
727-
function _wp_secrets_set( string $name, $value, bool $network, bool $needs_rotation = false, ?string $action_override = null ) {
735+
function _wp_secrets_set(
736+
string $name,
737+
#[\SensitiveParameter]
738+
$value,
739+
bool $network,
740+
bool $needs_rotation = false,
741+
?string $action_override = null
742+
) {
728743
return _wp_secrets_get_provider()->set( $name, $value, $network, $needs_rotation, $action_override );
729744
}
730745

‎src/wp-includes/secrets/class-wp-secrets-broken-keyring.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ final class WP_Secrets_Broken_Keyring implements WP_Secrets_Keyring {
3030
* @param string $key_material Ignored.
3131
* @return WP_Error
3232
*/
33-
public function wrap( string $key_material ): WP_Error {
33+
public function wrap(
34+
#[\SensitiveParameter]
35+
string $key_material
36+
): WP_Error {
3437
return $this->error();
3538
}
3639

‎src/wp-includes/secrets/class-wp-secrets-broken-provider.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ public function get( string $name, string $version, bool $network = false ): WP_
6666
* @param string|null $action Ignored.
6767
* @return WP_Error
6868
*/
69-
public function set( string $name, $value, bool $network = false, bool $needs_rotation = false, ?string $action = null ): WP_Error {
69+
public function set(
70+
string $name,
71+
#[\SensitiveParameter]
72+
$value,
73+
bool $network = false,
74+
bool $needs_rotation = false,
75+
?string $action = null
76+
): WP_Error {
7077
return $this->error();
7178
}
7279

‎src/wp-includes/secrets/class-wp-secrets-cipher.php‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,16 @@ final class WP_Secrets_Cipher {
6969
* @phpstan-param 'site'|'network' $scope
7070
* @phpstan-param WP_Secret_Version::CURRENT|WP_Secret_Version::PREVIOUS $slot
7171
*/
72-
public function encrypt_value( string $master_key, string $scope, int $site_id, string $name, string $slot, $plaintext ) {
72+
public function encrypt_value(
73+
#[\SensitiveParameter]
74+
string $master_key,
75+
string $scope,
76+
int $site_id,
77+
string $name,
78+
string $slot,
79+
#[\SensitiveParameter]
80+
$plaintext
81+
) {
7382
$check = $this->validate_common( $master_key, $scope, $site_id, $name, $slot );
7483

7584
if ( is_wp_error( $check ) ) {
@@ -142,7 +151,15 @@ public function encrypt_value( string $master_key, string $scope, int $site_id,
142151
* @phpstan-param 'site'|'network' $scope
143152
* @phpstan-param WP_Secret_Version::CURRENT|WP_Secret_Version::PREVIOUS $slot
144153
*/
145-
public function decrypt_value( string $master_key, string $scope, int $site_id, string $name, string $slot, $record ) {
154+
public function decrypt_value(
155+
#[\SensitiveParameter]
156+
string $master_key,
157+
string $scope,
158+
int $site_id,
159+
string $name,
160+
string $slot,
161+
$record
162+
) {
146163
$check = $this->validate_common( $master_key, $scope, $site_id, $name, $slot );
147164

148165
if ( is_wp_error( $check ) ) {
@@ -211,7 +228,12 @@ public function decrypt_value( string $master_key, string $scope, int $site_id,
211228
* @param string $plaintext Value to fingerprint.
212229
* @return string|WP_Error 32-character hex string on success. WP_Error on failure.
213230
*/
214-
public function fingerprint( string $master_key, $plaintext ) {
231+
public function fingerprint(
232+
#[\SensitiveParameter]
233+
string $master_key,
234+
#[\SensitiveParameter]
235+
$plaintext
236+
) {
215237
if ( ! $this->is_valid_master_key( $master_key ) ) {
216238
return new WP_Error(
217239
WP_SECRETS_ERROR_KEY_UNAVAILABLE,

‎src/wp-includes/secrets/class-wp-secrets-config-key-provider.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ public function __construct( bool $use_previous_key = false ) {
8484
* @param string $key_material Raw key material to protect.
8585
* @return string|WP_Error
8686
*/
87-
public function wrap( string $key_material ) {
87+
public function wrap(
88+
#[\SensitiveParameter]
89+
string $key_material
90+
) {
8891
if ( '' === $key_material ) {
8992
return new WP_Error(
9093
WP_SECRETS_ERROR_INVALID_VALUE,

‎src/wp-includes/secrets/class-wp-secrets-libsodium-provider.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,15 @@ private function slot_fingerprint( $slot ): string {
245245
* @phpstan-return array{dk: string, dk_nonce: string, ct: string, nonce: string, fingerprint: string, created: int, needs_rotation: bool}|WP_Error
246246
* @phpstan-param 'site'|'network' $scope
247247
*/
248-
private function demote_slot( WP_Secrets_Cipher $cipher, string $master_key, string $scope, int $site_id, string $name, array $current_slot ) {
248+
private function demote_slot(
249+
WP_Secrets_Cipher $cipher,
250+
#[\SensitiveParameter]
251+
string $master_key,
252+
string $scope,
253+
int $site_id,
254+
string $name,
255+
array $current_slot
256+
) {
249257
$plaintext = $cipher->decrypt_value( $master_key, $scope, $site_id, $name, WP_Secret_Version::CURRENT, $current_slot );
250258

251259
if ( is_wp_error( $plaintext ) ) {
@@ -289,7 +297,14 @@ private function demote_slot( WP_Secrets_Cipher $cipher, string $master_key, str
289297
* wp_import_option_as_secret() passes 'imported'.
290298
* @return true|WP_Error
291299
*/
292-
public function set( string $name, $value, bool $network = false, bool $needs_rotation = false, ?string $action = null ) {
300+
public function set(
301+
string $name,
302+
#[\SensitiveParameter]
303+
$value,
304+
bool $network = false,
305+
bool $needs_rotation = false,
306+
?string $action = null
307+
) {
293308
$name_check = wp_secrets_validate_name( $name );
294309

295310
if ( is_wp_error( $name_check ) ) {

‎src/wp-includes/secrets/interface-wp-secrets-keyring.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ interface WP_Secrets_Keyring {
3030
* re-wrapped value with update_site_option(), which reports an unchanged value
3131
* as a failure, and WP_Secrets_Keyring_Conformance checks this.
3232
*
33+
* Attributes are not inherited, so an implementation must mark $key_material
34+
* with #[\SensitiveParameter] itself to keep it out of stack traces.
35+
*
3336
* @since 7.2.0
3437
*
3538
* @param string $key_material Raw key material to protect.
3639
* @return string|WP_Error Opaque wrapped value on success, WP_Error on failure.
3740
*/
38-
public function wrap( string $key_material );
41+
public function wrap(
42+
#[\SensitiveParameter]
43+
string $key_material
44+
);
3945

4046
/**
4147
* Unwraps (decrypts) previously wrapped key material.

‎src/wp-includes/secrets/interface-wp-secrets-provider.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,14 @@ public function get( string $name, string $version, bool $network = false );
138138
* 'updated'.
139139
* @return true|WP_Error
140140
*/
141-
public function set( string $name, $value, bool $network = false, bool $needs_rotation = false, ?string $action = null );
141+
public function set(
142+
string $name,
143+
#[\SensitiveParameter]
144+
$value,
145+
bool $network = false,
146+
bool $needs_rotation = false,
147+
?string $action = null
148+
);
142149

143150
/**
144151
* Deletes a secret.

‎tests/phpunit/tests/secrets/wpSecretsValidateName.php‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public static function data_invalid_names(): array {
102102
'dot in key' => array( 'plugin/key.name' ),
103103
'non-ascii' => array( 'plügin/key' ),
104104
'one character over the max' => array( str_repeat( 'a', 85 ) . '/' . str_repeat( 'b', 87 ) ),
105+
'newline after key' => array( "plugin/key\n" ),
106+
'newline after namespace' => array( "plugin\n/key" ),
107+
'newline, unnamespaced' => array( "apikey\n" ),
105108
);
106109
}
107110
}

0 commit comments

Comments
 (0)