Skip to content

Commit a50a4ef

Browse files
Login and Registration: Check that $_POST value is a string in retrieve_password().
This prevents a fatal error from `trim()` if an array is passed instead. Follow-up to [6643], [19056], [41782], [50129], [50140], [59595]. Props leedxw, dilipbheda, mukesh27, SergeyBiryukov. Fixes #63433. git-svn-id: https://develop.svn.wordpress.org/trunk@60240 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8b466fc commit a50a4ef

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/wp-includes/user.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3172,7 +3172,7 @@ function retrieve_password( $user_login = '' ) {
31723172
$user_data = false;
31733173

31743174
// Use the passed $user_login if available, otherwise use $_POST['user_login'].
3175-
if ( ! $user_login && ! empty( $_POST['user_login'] ) ) {
3175+
if ( ! $user_login && ! empty( $_POST['user_login'] ) && is_string( $_POST['user_login'] ) ) {
31763176
$user_login = $_POST['user_login'];
31773177
}
31783178

tests/phpunit/tests/user/retrievePassword.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,23 @@ public function test_retrieve_password_should_fetch_user_by_login_if_not_found_b
8787
public function test_retrieve_password_does_not_throw_deprecation_notice_with_default_parameters() {
8888
$this->assertWPError( retrieve_password() );
8989
}
90+
91+
/**
92+
* Tests that a fatal error is not thrown when the login passed via `$_POST`
93+
* is an array instead of a string.
94+
*
95+
* The message that we should not see:
96+
* `TypeError: trim(): Argument #1 ($string) must be of type string, array given`.
97+
*
98+
* @ticket 62794
99+
*/
100+
public function test_retrieve_password_does_not_throw_fatal_error_with_array_parameters() {
101+
$_POST['user_login'] = array( 'example' );
102+
103+
$error = retrieve_password();
104+
$this->assertWPError( $error, 'The result should be an instance of WP_Error.' );
105+
106+
$error_codes = $error->get_error_codes();
107+
$this->assertContains( 'empty_username', $error_codes, 'The "empty_username" error code should be present.' );
108+
}
90109
}

0 commit comments

Comments
 (0)