mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-01 07:40:07 +00:00
App Passwords: Improve validation and sanitization of the application name.
Application names are now required to be unique and cannot contain solely whitespace characters. Additionally, invalid characters are now stripped from the application name using `sanitize_text_field()`. Props Boniu91, hellofromTonya, engahmeds3ed, xkon, francina. Fixes #51941. git-svn-id: https://develop.svn.wordpress.org/trunk@50030 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -57,7 +57,7 @@
|
||||
$newAppPassButton.prop( 'disabled', false );
|
||||
|
||||
$newAppPassForm.after( tmplNewAppPass( {
|
||||
name: name,
|
||||
name: response.name,
|
||||
password: response.password
|
||||
} ) );
|
||||
$( '.new-application-password-notice' ).focus();
|
||||
|
||||
@@ -98,7 +98,7 @@
|
||||
.append( '<p>' + wp.i18n.__( 'Be sure to save this in a safe location. You will not be able to retrieve it.' ) + '</p>' );
|
||||
|
||||
// We're using .text() to write the variables to avoid any chance of XSS.
|
||||
$( 'strong', $notice ).text( name );
|
||||
$( 'strong', $notice ).text( response.name );
|
||||
$( 'input', $notice ).val( response.password );
|
||||
|
||||
$form.replaceWith( $notice );
|
||||
|
||||
@@ -58,6 +58,7 @@ class WP_Application_Passwords {
|
||||
* Creates a new application password.
|
||||
*
|
||||
* @since 5.6.0
|
||||
* @since 5.7.0 Returns WP_Error if application name already exists.
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @param array $args Information about the application password.
|
||||
@@ -65,8 +66,16 @@ class WP_Application_Passwords {
|
||||
* A WP_Error instance is returned on error.
|
||||
*/
|
||||
public static function create_new_application_password( $user_id, $args = array() ) {
|
||||
if ( ! empty( $args['name'] ) ) {
|
||||
$args['name'] = sanitize_text_field( $args['name'] );
|
||||
}
|
||||
|
||||
if ( empty( $args['name'] ) ) {
|
||||
return new WP_Error( 'application_password_empty_name', __( 'An application name is required to create an application password.' ) );
|
||||
return new WP_Error( 'application_password_empty_name', __( 'An application name is required to create an application password.' ), array( 'status' => 400 ) );
|
||||
}
|
||||
|
||||
if ( self::application_name_exists_for_user( $user_id, $args['name'] ) ) {
|
||||
return new WP_Error( 'application_password_duplicate_name', __( 'Each application name should be unique.' ), array( 'status' => 409 ) );
|
||||
}
|
||||
|
||||
$new_password = wp_generate_password( static::PW_LENGTH, false );
|
||||
@@ -162,6 +171,28 @@ class WP_Application_Passwords {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if application name exists before for this user.
|
||||
*
|
||||
* @since 5.7.0
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @param string $name Application name.
|
||||
*
|
||||
* @return bool Provided application name exists or not.
|
||||
*/
|
||||
public static function application_name_exists_for_user( $user_id, $name ) {
|
||||
$passwords = static::get_user_application_passwords( $user_id );
|
||||
|
||||
foreach ( $passwords as $password ) {
|
||||
if ( strtolower( $password['name'] ) === strtolower( $name ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an application password.
|
||||
*
|
||||
@@ -180,6 +211,10 @@ class WP_Application_Passwords {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ! empty( $update['name'] ) ) {
|
||||
$update['name'] = sanitize_text_field( $update['name'] );
|
||||
}
|
||||
|
||||
$save = false;
|
||||
|
||||
if ( ! empty( $update['name'] ) && $item['name'] !== $update['name'] ) {
|
||||
|
||||
@@ -631,6 +631,8 @@ class WP_REST_Application_Passwords_Controller extends WP_REST_Controller {
|
||||
'type' => 'string',
|
||||
'required' => true,
|
||||
'context' => array( 'view', 'edit', 'embed' ),
|
||||
'minLength' => 1,
|
||||
'pattern' => '.*\S.*',
|
||||
),
|
||||
'password' => array(
|
||||
'description' => __( 'The generated password. Only available after adding an application.' ),
|
||||
|
||||
Reference in New Issue
Block a user