From d0b11a4f646ac0f3602f7eebb78a9112b94f9352 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 8 Nov 2026 10:52:45 +0000 Subject: [PATCH] Pass unslashed values to application password hooks. `create_new_application_password()` and `update_application_password()` now unslash their input, so the `wp_create_application_password` and `wp_update_application_password` actions, the created notification email, and the returned details all receive unslashed values. `set_user_application_passwords()` now slashes the passwords before saving them, because the metadata API unslashes its input. Previously, saving any application password stripped backslashes from the names of the user's other application passwords, and updating an application password with its existing name returned an error when the name contained a quote or backslash. --- .../class-wp-application-passwords.php | 45 +++---- src/wp-includes/functions.php | 24 +--- .../tests/rest-api/application-passwords.php | 127 ++++++++++++++++++ 3 files changed, 153 insertions(+), 43 deletions(-) diff --git a/src/wp-includes/class-wp-application-passwords.php b/src/wp-includes/class-wp-application-passwords.php index 30c65399c9fde..a7a952e66d876 100644 --- a/src/wp-includes/class-wp-application-passwords.php +++ b/src/wp-includes/class-wp-application-passwords.php @@ -61,13 +61,14 @@ public static function is_in_use() { * @since 5.6.0 * @since 5.7.0 Returns WP_Error if application name already exists. * @since 6.8.0 The hashed password value now uses wp_fast_hash() instead of phpass. + * @since 7.2.0 The returned application password details are unslashed. * * @param int $user_id User ID. * @param array $args { * Arguments used to create the application password. Expected to be slashed. * - * @type string $name The name of the application password. Slashed. - * @type string $app_id A UUID provided by the application to uniquely identify it. Slashed. + * @type string $name The name of the application password. + * @type string $app_id A UUID provided by the application to uniquely identify it. * } * @return array|WP_Error { * Application password details, or a WP_Error instance if an error occurs. @@ -87,6 +88,8 @@ public static function is_in_use() { * } */ public static function create_new_application_password( $user_id, $args = array() ) { + $args = wp_unslash( $args ); + if ( ! empty( $args['name'] ) ) { $args['name'] = sanitize_text_field( $args['name'] ); } @@ -124,21 +127,17 @@ public static function create_new_application_password( $user_id, $args = array( /** * Fires when an application password is created. * - * The user-supplied values in `$new_item` (`name` and `app_id`) and in `$args` - * are slashed, as received by WP_Application_Passwords::create_new_application_password(). - * Callbacks should pass them through wp_unslash() before using them for display, - * comparison, or storage outside of the metadata API. - * * @since 5.6.0 * @since 6.8.0 The hashed password value now uses wp_fast_hash() instead of phpass. + * @since 7.2.0 The values in `$new_item` and `$args` are unslashed. * * @param int $user_id The user ID. * @param array $new_item { * The details about the created password. * * @type string $uuid The unique identifier for the application password. - * @type string $app_id A UUID provided by the application to uniquely identify it. Slashed. - * @type string $name The name of the application password. Slashed. + * @type string $app_id A UUID provided by the application to uniquely identify it. + * @type string $name The name of the application password. * @type string $password A one-way hash of the password. * @type int $created Unix timestamp of when the password was created. * @type null $last_used Null. @@ -146,10 +145,10 @@ public static function create_new_application_password( $user_id, $args = array( * } * @param string $new_password The generated application password in plain text. * @param array $args { - * Slashed arguments used to create the application password. + * Arguments used to create the application password. * - * @type string $name The name of the application password. Slashed. - * @type string $app_id A UUID provided by the application to uniquely identify it. Slashed. + * @type string $name The name of the application password. + * @type string $app_id A UUID provided by the application to uniquely identify it. * } */ do_action( 'wp_create_application_password', $user_id, $new_item, $new_password, $args ); @@ -265,7 +264,7 @@ public static function application_name_exists_for_user( $user_id, $name ) { * * @type string $uuid The unique identifier for the application password. * @type string $app_id A UUID provided by the application to uniquely identify it. - * @type string $name The name of the application password. Slashed. + * @type string $name The name of the application password. * @type string $password A one-way hash of the password. * @type int $created Unix timestamp of when the password was created. * @type int|null $last_used The Unix timestamp of the GMT date the application password was last used. @@ -274,6 +273,7 @@ public static function application_name_exists_for_user( $user_id, $name ) { * @return true|WP_Error True if successful, otherwise a WP_Error instance is returned on error. */ public static function update_application_password( $user_id, $uuid, $update = array() ) { + $update = wp_unslash( $update ); $passwords = static::get_user_application_passwords( $user_id ); foreach ( $passwords as &$item ) { @@ -303,16 +303,10 @@ public static function update_application_password( $user_id, $uuid, $update = a /** * Fires when an application password is updated. * - * The values in `$update` are slashed, as received by - * WP_Application_Passwords::update_application_password(). When `$update` - * contains a `name`, `$item['name']` is set to that slashed value; otherwise - * `$item['name']` is the stored, unslashed value. Callbacks should pass slashed - * values through wp_unslash() before using them for display, comparison, or - * storage outside of the metadata API. - * * @since 5.6.0 * @since 6.8.0 The password is now hashed using wp_fast_hash() instead of phpass. * Existing passwords may still be hashed using phpass. + * @since 7.2.0 The values in `$item` and `$update` are unslashed. * * @param int $user_id The user ID. * @param array $item { @@ -320,14 +314,13 @@ public static function update_application_password( $user_id, $uuid, $update = a * * @type string $uuid The unique identifier for the application password. * @type string $app_id A UUID provided by the application to uniquely identify it. - * @type string $name The name of the application password. Slashed if it was - * provided in `$update`, otherwise unslashed. + * @type string $name The name of the application password. * @type string $password A one-way hash of the password. * @type int $created Unix timestamp of when the password was created. * @type int|null $last_used The Unix timestamp of the GMT date the application password was last used. * @type string|null $last_ip The IP address the application password was last used by. * } - * @param array $update The slashed information to update. + * @param array $update The information to update. */ do_action( 'wp_update_application_password', $user_id, $item, $update ); @@ -446,10 +439,11 @@ public static function delete_all_application_passwords( $user_id ) { * Sets a user's application passwords. * * @since 5.6.0 + * @since 7.2.0 The passwords are slashed before saving, so values containing backslashes are stored intact. * * @param int $user_id User ID. * @param array $passwords { - * The list of application passwords. + * The list of unslashed application passwords. * * @type array ...$0 { * @type string $uuid The unique identifier for the application password. @@ -466,7 +460,8 @@ public static function delete_all_application_passwords( $user_id ) { * is the same as the one that is already in the database. */ protected static function set_user_application_passwords( $user_id, $passwords ) { - return update_user_meta( $user_id, static::USERMETA_KEY_APPLICATION_PASSWORDS, $passwords ); + // The metadata API unslashes its input, so slash it to store the values as given. + return update_user_meta( $user_id, static::USERMETA_KEY_APPLICATION_PASSWORDS, wp_slash( $passwords ) ); } /** diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index e9ea627f8da97..4b1c2e3ce0d1d 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -9561,8 +9561,7 @@ function wp_verify_fast_hash( * @since 7.2.0 * * @param int $user_id The user ID. - * @param array $new_item The application password details. The `name` and `app_id` - * values are slashed. + * @param array $new_item The application password details. */ function wp_application_password_created_notification( $user_id, $new_item ) { $send = true; @@ -9586,10 +9585,6 @@ function wp_application_password_created_notification( $user_id, $new_item ) { /** * Filters whether to send the application password created notification email. * - * The user-supplied values in `$new_item` (`name` and `app_id`) are slashed, - * matching the `wp_create_application_password` action. Callbacks should pass - * them through wp_unslash() before using them for display or comparison. - * * @since 7.2.0 * * @param bool $send Whether to send the email notification. @@ -9598,8 +9593,8 @@ function wp_application_password_created_notification( $user_id, $new_item ) { * The details about the created password. * * @type string $uuid The unique identifier for the application password. - * @type string $app_id A UUID provided by the application to uniquely identify it. Slashed. - * @type string $name The name of the application password. Slashed. + * @type string $app_id A UUID provided by the application to uniquely identify it. + * @type string $name The name of the application password. * @type string $password A one-way hash of the password. * @type int $created Unix timestamp of when the password was created. * @type null $last_used Null. @@ -9647,13 +9642,6 @@ function wp_application_password_created_notification( $user_id, $new_item ) { /** * Filters the contents of the email notification sent to a user when a new application password is created. * - * The user-supplied values in `$new_item` (`name` and `app_id`) are slashed, - * matching the `wp_create_application_password` action. Callbacks should pass - * them through wp_unslash() before using them for display or comparison, - * including when adding them to the email message. - * - * The values in `$email` and `$user` are not slashed. - * * @since 7.2.0 * * @param array $email { @@ -9676,8 +9664,8 @@ function wp_application_password_created_notification( $user_id, $new_item ) { * The details about the created password. * * @type string $uuid The unique identifier for the application password. - * @type string $app_id A UUID provided by the application to uniquely identify it. Slashed. - * @type string $name The name of the application password. Slashed. + * @type string $app_id A UUID provided by the application to uniquely identify it. + * @type string $name The name of the application password. * @type string $password A one-way hash of the password. * @type int $created Unix timestamp of when the password was created. * @type null $last_used Null. @@ -9687,7 +9675,7 @@ function wp_application_password_created_notification( $user_id, $new_item ) { $email = apply_filters( 'wp_application_password_created_email', $email, $user, $new_item ); $email['message'] = str_replace( '###USERNAME###', $user->display_name, $email['message'] ); - $email['message'] = str_replace( '###APPLICATION_PASSWORD_NAME###', wp_unslash( $new_item['name'] ), $email['message'] ); + $email['message'] = str_replace( '###APPLICATION_PASSWORD_NAME###', $new_item['name'], $email['message'] ); $email['message'] = str_replace( '###ADMIN_EMAIL###', get_option( 'admin_email' ), $email['message'] ); $email['message'] = str_replace( '###EMAIL###', $user->user_email, $email['message'] ); $email['message'] = str_replace( '###SITENAME###', $site_name, $email['message'] ); diff --git a/tests/phpunit/tests/rest-api/application-passwords.php b/tests/phpunit/tests/rest-api/application-passwords.php index b1f2ca957d48d..35c9694ef3a5c 100644 --- a/tests/phpunit/tests/rest-api/application-passwords.php +++ b/tests/phpunit/tests/rest-api/application-passwords.php @@ -198,4 +198,131 @@ public function test_can_create_duplicate_app_password_names() { $created = WP_Application_Passwords::create_new_application_password( self::$user_id, array( 'name' => 'My App' ) ); $this->assertNotWPError( $created, 'Second attempt to create an application password should not return an error' ); } + + /** + * @ticket 63927 + * + * @covers WP_Application_Passwords::create_new_application_password + */ + public function test_create_new_application_password_unslashes_values() { + $name = 'Bob\'s "Tool" \\ Backslash'; + $app_id = '\\"app\''; + $action = new MockAction(); + add_action( 'wp_create_application_password', array( $action, 'action' ), 10, 4 ); + + list( , $new_item ) = WP_Application_Passwords::create_new_application_password( + self::$user_id, + wp_slash( + array( + 'name' => $name, + 'app_id' => $app_id, + ) + ) + ); + + $this->assertSame( $name, $new_item['name'], 'The returned name should be unslashed.' ); + $this->assertSame( $app_id, $new_item['app_id'], 'The returned app ID should be unslashed.' ); + + $args = $action->get_args(); + $this->assertCount( 1, $args, 'The action should fire once.' ); + $this->assertSame( $new_item, $args[0][1], 'The action should receive the unslashed item.' ); + $this->assertSame( $name, $args[0][3]['name'], 'The action should receive the unslashed name argument.' ); + $this->assertSame( $app_id, $args[0][3]['app_id'], 'The action should receive the unslashed app ID argument.' ); + + $stored = WP_Application_Passwords::get_user_application_password( self::$user_id, $new_item['uuid'] ); + $this->assertSame( $name, $stored['name'], 'The stored name should match the unslashed name.' ); + $this->assertSame( $app_id, $stored['app_id'], 'The stored app ID should match the unslashed app ID.' ); + } + + /** + * @ticket 63927 + * + * @covers WP_Application_Passwords::update_application_password + */ + public function test_update_application_password_unslashes_values() { + $name = 'Bob\'s "Tool" \\ Backslash'; + + list( , $original ) = WP_Application_Passwords::create_new_application_password( self::$user_id, array( 'name' => 'App' ) ); + + $action = new MockAction(); + add_action( 'wp_update_application_password', array( $action, 'action' ), 10, 3 ); + + $updated = WP_Application_Passwords::update_application_password( self::$user_id, $original['uuid'], wp_slash( array( 'name' => $name ) ) ); + + $this->assertTrue( $updated, 'The application password should be updated.' ); + + $args = $action->get_args(); + $this->assertCount( 1, $args, 'The action should fire once.' ); + $this->assertSame( $name, $args[0][1]['name'], 'The action should receive the unslashed item name.' ); + $this->assertSame( array( 'name' => $name ), $args[0][2], 'The action should receive the unslashed update.' ); + + $stored = WP_Application_Passwords::get_user_application_password( self::$user_id, $original['uuid'] ); + $this->assertSame( $name, $stored['name'], 'The stored name should match the unslashed name.' ); + } + + /** + * @ticket 63927 + * + * @covers WP_Application_Passwords::update_application_password + */ + public function test_update_application_password_passes_stored_name_when_name_not_updated() { + $name = 'Bob\'s "Tool" \\ Backslash'; + + list( , $original ) = WP_Application_Passwords::create_new_application_password( self::$user_id, wp_slash( array( 'name' => $name ) ) ); + + $action = new MockAction(); + add_action( 'wp_update_application_password', array( $action, 'action' ), 10, 3 ); + + WP_Application_Passwords::update_application_password( self::$user_id, $original['uuid'], array() ); + + $args = $action->get_args(); + $this->assertCount( 1, $args, 'The action should fire once.' ); + $this->assertSame( $name, $args[0][1]['name'] ); + } + + /** + * @ticket 63927 + * + * @covers WP_Application_Passwords::update_application_password + */ + public function test_update_application_password_with_same_slashed_name_does_not_save() { + $name = 'Bob\'s "Tool" \\ Backslash'; + + list( , $original ) = WP_Application_Passwords::create_new_application_password( self::$user_id, wp_slash( array( 'name' => $name ) ) ); + + $action = new MockAction(); + add_action( 'update_user_meta', array( $action, 'action' ) ); + + $updated = WP_Application_Passwords::update_application_password( self::$user_id, $original['uuid'], wp_slash( array( 'name' => $name ) ) ); + + $this->assertTrue( $updated, 'Updating with an unchanged name should succeed.' ); + $this->assertSame( 0, $action->get_call_count(), 'Updating with an unchanged name should not save.' ); + } + + /** + * Ensures saving one application password does not strip backslashes from the others. + * + * @ticket 63927 + * + * @covers WP_Application_Passwords::create_new_application_password + * @covers WP_Application_Passwords::update_application_password + * @covers WP_Application_Passwords::record_application_password_usage + * @covers WP_Application_Passwords::delete_application_password + */ + public function test_backslashes_in_stored_names_survive_later_saves() { + $name = 'C:\\Apps\\Tool'; + + list( , $item ) = WP_Application_Passwords::create_new_application_password( self::$user_id, wp_slash( array( 'name' => $name ) ) ); + list( , $other ) = WP_Application_Passwords::create_new_application_password( self::$user_id, array( 'name' => 'Other' ) ); + $this->assertSame( $name, WP_Application_Passwords::get_user_application_password( self::$user_id, $item['uuid'] )['name'], 'After creating another password.' ); + + WP_Application_Passwords::update_application_password( self::$user_id, $other['uuid'], array( 'name' => 'Other Updated' ) ); + $this->assertSame( $name, WP_Application_Passwords::get_user_application_password( self::$user_id, $item['uuid'] )['name'], 'After updating another password.' ); + + WP_Application_Passwords::record_application_password_usage( self::$user_id, $other['uuid'] ); + $this->assertSame( $name, WP_Application_Passwords::get_user_application_password( self::$user_id, $item['uuid'] )['name'], 'After recording usage of another password.' ); + + WP_Application_Passwords::delete_application_password( self::$user_id, $other['uuid'] ); + $this->assertSame( $name, WP_Application_Passwords::get_user_application_password( self::$user_id, $item['uuid'] )['name'], 'After deleting another password.' ); + } }