Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#57512: Resolve warnings and logical error while parsing REST API authorization header #5438

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ function wp_populate_basic_auth_from_authorization_header() {
$token = substr( $header, 6 );
$userpass = base64_decode( $token );

list( $user, $pass ) = explode( ':', $userpass );
// There must be at least one colon in the string.
if ( ! str_contains( $userpass, ':' ) ) {
return;
}

list( $user, $pass ) = explode( ':', $userpass, 2 );

// Now shove them in the proper keys where we're expecting later on.
$_SERVER['PHP_AUTH_USER'] = $user;
Expand Down
42 changes: 42 additions & 0 deletions tests/phpunit/tests/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,46 @@ public function data_application_passwords_can_use_capability_checks_to_determin
'not allowed' => array( 'subscriber', false ),
);
}

/*
* @ticket 57512
* @covers ::wp_populate_basic_auth_from_authorization_header
*/
public function tests_basic_http_authentication_with_username_and_password() {
// Header passed as "username:password".
$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=';

wp_populate_basic_auth_from_authorization_header();

$this->assertSame( $_SERVER['PHP_AUTH_USER'], 'username' );
$this->assertSame( $_SERVER['PHP_AUTH_PW'], 'password' );
}

/*
* @ticket 57512
* @covers ::wp_populate_basic_auth_from_authorization_header
*/
public function tests_basic_http_authentication_with_username_only() {
// Malformed header passed as "username" with no password.
$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dXNlcm5hbWU=';

wp_populate_basic_auth_from_authorization_header();

$this->assertArrayNotHasKey( 'PHP_AUTH_USER', $_SERVER );
$this->assertArrayNotHasKey( 'PHP_AUTH_PW', $_SERVER );
}

/*
* @ticket 57512
* @covers ::wp_populate_basic_auth_from_authorization_header
*/
public function tests_basic_http_authentication_with_colon_in_password() {
// Header passed as "username:pass:word" where password contains colon.
$_SERVER['HTTP_AUTHORIZATION'] = 'Basic dXNlcm5hbWU6cGFzczp3b3Jk';

wp_populate_basic_auth_from_authorization_header();

$this->assertSame( $_SERVER['PHP_AUTH_USER'], 'username' );
$this->assertSame( $_SERVER['PHP_AUTH_PW'], 'pass:word' );
}
}
Loading