Skip to content

Commit f891686

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]`, so an exception thrown while saving does not print the value 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, dhrupo. Fixes #66414.
1 parent eed6f20 commit f891686

8 files changed

Lines changed: 163 additions & 9 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-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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ 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+
string $master_key,
74+
string $scope,
75+
int $site_id,
76+
string $name,
77+
string $slot,
78+
#[\SensitiveParameter]
79+
$plaintext
80+
) {
7381
$check = $this->validate_common( $master_key, $scope, $site_id, $name, $slot );
7482

7583
if ( is_wp_error( $check ) ) {
@@ -211,7 +219,11 @@ public function decrypt_value( string $master_key, string $scope, int $site_id,
211219
* @param string $plaintext Value to fingerprint.
212220
* @return string|WP_Error 32-character hex string on success. WP_Error on failure.
213221
*/
214-
public function fingerprint( string $master_key, $plaintext ) {
222+
public function fingerprint(
223+
string $master_key,
224+
#[\SensitiveParameter]
225+
$plaintext
226+
) {
215227
if ( ! $this->is_valid_master_key( $master_key ) ) {
216228
return new WP_Error(
217229
WP_SECRETS_ERROR_KEY_UNAVAILABLE,

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,14 @@ private function demote_slot( WP_Secrets_Cipher $cipher, string $master_key, str
289289
* wp_import_option_as_secret() passes 'imported'.
290290
* @return true|WP_Error
291291
*/
292-
public function set( string $name, $value, bool $network = false, bool $needs_rotation = false, ?string $action = null ) {
292+
public function set(
293+
string $name,
294+
#[\SensitiveParameter]
295+
$value,
296+
bool $network = false,
297+
bool $needs_rotation = false,
298+
?string $action = null
299+
) {
293300
$name_check = wp_secrets_validate_name( $name );
294301

295302
if ( is_wp_error( $name_check ) ) {

‎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
}

‎tests/phpunit/tests/secrets/wpSetSecretAndWpGetSecret.php‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,4 +231,103 @@ function ( ...$args ) use ( &$captured_args ) {
231231
$this->assertStringNotContainsString( 'a-plaintext-value-that-must-not-leak', (string) $arg );
232232
}
233233
}
234+
235+
/**
236+
* The options API trims option names, so a name with a trailing newline
237+
* would share a row with the name without it while encrypting for itself.
238+
*
239+
* @ticket 66414
240+
*/
241+
public function test_a_trailing_newline_name_cannot_overwrite_the_secret_without_it(): void {
242+
wp_set_secret( 'myplugin/api-key', 'value-plain' );
243+
244+
$result = wp_set_secret( "myplugin/api-key\n", 'value-newline' );
245+
246+
$this->assertWPError( $result );
247+
$this->assertSame( WP_SECRETS_ERROR_INVALID_NAME, $result->get_error_code() );
248+
249+
$secret = wp_get_secret( 'myplugin/api-key' );
250+
251+
$this->assertInstanceOf( WP_Secret::class, $secret );
252+
$this->assertSame( 'value-plain', $secret->reveal() );
253+
}
254+
255+
/**
256+
* @ticket 66414
257+
*
258+
* @requires PHP 8.2
259+
*/
260+
public function test_an_exception_while_setting_does_not_expose_the_value_in_the_trace(): void {
261+
if ( ini_get( 'zend.exception_ignore_args' ) ) {
262+
$this->markTestSkipped( 'Exception traces do not include arguments with zend.exception_ignore_args enabled.' );
263+
}
264+
265+
add_action(
266+
'wp_secret_changed',
267+
static function () {
268+
throw new RuntimeException( 'Listener failed.' );
269+
}
270+
);
271+
272+
$exception = null;
273+
274+
try {
275+
wp_set_secret( 'myplugin/api-key', 'a-plaintext-value-that-must-not-leak' );
276+
} catch ( RuntimeException $e ) {
277+
$exception = $e;
278+
}
279+
280+
$this->assertInstanceOf( RuntimeException::class, $exception );
281+
282+
$string_args = array();
283+
foreach ( $exception->getTrace() as $frame ) {
284+
foreach ( $frame['args'] ?? array() as $arg ) {
285+
if ( is_string( $arg ) ) {
286+
$string_args[] = $arg;
287+
}
288+
}
289+
}
290+
291+
$this->assertContains( 'myplugin/api-key', $string_args, 'The trace should include the other arguments.' );
292+
$this->assertNotContains( 'a-plaintext-value-that-must-not-leak', $string_args );
293+
}
294+
295+
/**
296+
* @ticket 66414
297+
*
298+
* @requires PHP 8.2
299+
*
300+
* @dataProvider data_parameters_that_receive_a_secret_value
301+
*
302+
* @param callable-string|array{0: class-string, 1: string} $target The function or method.
303+
* @param string $parameter The parameter name.
304+
*/
305+
public function test_parameters_that_receive_a_secret_value_are_sensitive( $target, string $parameter ): void {
306+
$reflection = is_array( $target ) ? new ReflectionMethod( $target[0], $target[1] ) : new ReflectionFunction( $target );
307+
308+
foreach ( $reflection->getParameters() as $reflection_parameter ) {
309+
if ( $parameter === $reflection_parameter->getName() ) {
310+
$this->assertNotEmpty( $reflection_parameter->getAttributes( SensitiveParameter::class ) );
311+
return;
312+
}
313+
}
314+
315+
$this->fail( "Parameter \${$parameter} not found." );
316+
}
317+
318+
/**
319+
* @return array<string, array{0: callable-string|array{0: class-string, 1: string}, 1: string}>
320+
*/
321+
public static function data_parameters_that_receive_a_secret_value(): array {
322+
return array(
323+
'wp_set_secret()' => array( 'wp_set_secret', 'value' ),
324+
'wp_set_network_secret()' => array( 'wp_set_network_secret', 'value' ),
325+
'_wp_secrets_set()' => array( '_wp_secrets_set', 'value' ),
326+
'WP_Secrets_Provider::set()' => array( array( WP_Secrets_Provider::class, 'set' ), 'value' ),
327+
'WP_Secrets_Libsodium_Provider::set()' => array( array( WP_Secrets_Libsodium_Provider::class, 'set' ), 'value' ),
328+
'WP_Secrets_Broken_Provider::set()' => array( array( WP_Secrets_Broken_Provider::class, 'set' ), 'value' ),
329+
'WP_Secrets_Cipher::encrypt_value()' => array( array( WP_Secrets_Cipher::class, 'encrypt_value' ), 'plaintext' ),
330+
'WP_Secrets_Cipher::fingerprint()' => array( array( WP_Secrets_Cipher::class, 'fingerprint' ), 'plaintext' ),
331+
);
332+
}
234333
}

0 commit comments

Comments
 (0)