Skip to content

Commit 1b65952

Browse files
authored
Merge pull request #289 from fjarrett/user-check-password-escape-chars
2 parents 21c56a7 + e07e595 commit 1b65952

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

features/user.feature

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,28 @@ Feature: Manage WordPress users
8585
"""
8686
And the return code should be 1
8787

88+
When I run `wp user create testuser3b testuser3b@example.com --user_pass="test\"user3b's\pass\!"`
89+
Then STDOUT should not contain:
90+
"""
91+
Password:
92+
"""
93+
94+
# Check password without the `--escape-chars` option.
95+
When I try `wp user check-password testuser3b "test\"user3b's\pass\!"`
96+
Then STDERR should be:
97+
"""
98+
Warning: Password contains characters that need to be escaped. Please escape them manually or use the `--escape-chars` option.
99+
"""
100+
And the return code should be 1
101+
102+
# Check password with the `--escape-chars` option.
103+
When I try `wp user check-password testuser3b "test\"user3b's\pass\!" --escape-chars`
104+
Then the return code should be 0
105+
106+
# Check password with manually escaped characters.
107+
When I try `wp user check-password testuser3b "test\\\"user3b\'s\\\pass\\\!"`
108+
Then the return code should be 0
109+
88110
Scenario: Reassigning user posts
89111
Given a WP multisite install
90112

src/User_Command.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,6 +1268,9 @@ private function update_msuser_status( $user_ids, $pref, $value ) {
12681268
* <user_pass>
12691269
* : A string that contains the plain text password for the user.
12701270
*
1271+
* [--escape-chars]
1272+
* : Escape password with `wp_slash()` to mimic the same behavior as `wp-login.php`.
1273+
*
12711274
* ## EXAMPLES
12721275
*
12731276
* # Check whether given credentials are valid; exit status 0 if valid, otherwise 1
@@ -1282,10 +1285,15 @@ private function update_msuser_status( $user_ids, $pref, $value ) {
12821285
*
12831286
* @subcommand check-password
12841287
*/
1285-
public function check_password( $args ) {
1288+
public function check_password( $args, $assoc_args ) {
1289+
$escape_chars = Utils\get_flag_value( $assoc_args, 'escape-chars', false );
1290+
1291+
if ( ! $escape_chars && wp_slash( wp_unslash( $args[1] ) ) !== $args[1] ) {
1292+
WP_CLI::warning( 'Password contains characters that need to be escaped. Please escape them manually or use the `--escape-chars` option.' );
1293+
}
12861294

12871295
$user = $this->fetcher->get_check( $args[0] );
1288-
$user_pass = $args[1];
1296+
$user_pass = $escape_chars ? wp_slash( $args[1] ) : $args[1];
12891297

12901298
if ( wp_check_password( $user_pass, $user->data->user_pass, $user->ID ) ) {
12911299
WP_CLI::halt( 0 );

0 commit comments

Comments
 (0)