Repository navigation
Fix: Login issue for users with 2FA #412
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,18 @@ public function setUp(): void { | |
| $this->testee = new Testee( $this->ghClientMock, $this->authenticatorMock ); | ||
| } | ||
|
|
||
| /** | ||
| * Run after each test. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function tearDown(): void { | ||
| // Login::authenticate() sets this superglobal; clear it even when an assertion fails. | ||
| unset( $_REQUEST['redirect_to'] ); | ||
|
|
||
| parent::tearDown(); | ||
| } | ||
|
|
||
| /** | ||
| * @covers ::name | ||
| */ | ||
|
|
@@ -78,7 +90,7 @@ public function testInit() { | |
| WP_Mock::expectActionAdded( 'authenticate', [ $this->testee, 'authenticate' ], 20 ); | ||
| WP_Mock::expectActionAdded( 'rtcamp.google_register_user', [ $this->authenticatorMock, 'register' ] ); | ||
| WP_Mock::expectActionAdded( 'rtcamp.google_user_created', [ $this->testee, 'user_meta' ] ); | ||
| WP_Mock::expectActionAdded( 'wp_login', [ $this->testee, 'login_redirect' ] ); | ||
| WP_Mock::expectFilterAdded( 'login_redirect', [ $this->testee, 'login_redirect' ], 1 ); | ||
| WP_Mock::expectFilterAdded( 'rtcamp.google_redirect_url', [ $this->testee, 'redirect_url' ] ); | ||
| WP_Mock::expectFilterAdded( 'rtcamp.google_login_state', [ $this->testee, 'state_redirect' ] ); | ||
|
|
||
|
|
@@ -431,7 +443,8 @@ public function testStateRedirectWithRedirectTo() { | |
| [ | ||
| INPUT_GET, | ||
| 'redirect_to', | ||
| FILTER_SANITIZE_FULL_SPECIAL_CHARS | ||
| FILTER_SANITIZE_URL, | ||
| 0 | ||
| ] | ||
| )->andReturn( 'https://example.com/state-page' ); | ||
|
|
||
|
|
@@ -450,7 +463,8 @@ public function testStateRedirectWithoutRedirectTo() { | |
| [ | ||
| INPUT_GET, | ||
| 'redirect_to', | ||
| FILTER_SANITIZE_FULL_SPECIAL_CHARS | ||
| FILTER_SANITIZE_URL, | ||
| 0 | ||
| ] | ||
| )->andReturn( null ); | ||
|
|
||
|
|
@@ -470,17 +484,153 @@ public function testStateRedirectWithoutRedirectTo() { | |
| /** | ||
| * @covers ::login_redirect | ||
| */ | ||
| public function testLoginRedirectWithNotStateAuthenticated() { | ||
| public function testLoginRedirectWhenNotAuthenticated() { | ||
| $data = $this->testee->login_redirect( 'https://example.com/wp-admin/' ); | ||
| $this->assertSame( 'https://example.com/wp-admin/', $data ); | ||
| } | ||
|
|
||
| /** | ||
| * @covers ::authenticate | ||
| * @covers ::login_redirect | ||
| */ | ||
| public function testLoginRedirectUsesStateRedirectAfterAuthentication() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add the negative case as well:
This is the open-redirect guard, so it's the branch that matters most to cover. A non-string |
||
| $this->mockSuccessfulGoogleAuthentication( 'https://example.com/state-page' ); | ||
|
|
||
| $this->wpMockFunction( | ||
| 'wp_sanitize_redirect', | ||
| [ 'https://example.com/state-page' ], | ||
| 1, | ||
| 'https://example.com/state-page' | ||
| ); | ||
|
|
||
| $this->wpMockFunction( | ||
| 'wp_validate_redirect', | ||
| [ 'https://example.com/state-page', '' ], | ||
| 1, | ||
| 'https://example.com/state-page' | ||
| ); | ||
|
|
||
| $this->wpMockFunction( | ||
| 'wp_slash', | ||
| [ 'https://example.com/state-page' ], | ||
| 1, | ||
| 'https://example.com/state-page' | ||
| ); | ||
|
|
||
| $this->testee->authenticate(); | ||
|
|
||
| $this->assertSame( 'https://example.com/state-page', $_REQUEST['redirect_to'] ); | ||
| $this->assertSame( 'https://example.com/state-page', $this->testee->login_redirect( 'https://example.com/wp-admin/' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Open-redirect guard: an off-site destination in the state is dropped. | ||
| * | ||
| * @covers ::authenticate | ||
| * @covers ::login_redirect | ||
| */ | ||
| public function testLoginRedirectIgnoresOffsiteStateRedirect() { | ||
| $this->mockSuccessfulGoogleAuthentication( 'https://evil.example/' ); | ||
|
|
||
| $this->wpMockFunction( | ||
| 'wp_sanitize_redirect', | ||
| [ 'https://evil.example/' ], | ||
| 1, | ||
| 'https://evil.example/' | ||
| ); | ||
|
|
||
| // `wpMockFunction()` skips empty return values, so set `''` directly. | ||
| WP_Mock::userFunction( | ||
| 'wp_validate_redirect', | ||
| [ | ||
| 'times' => 1, | ||
| 'args' => [ 'https://evil.example/', '' ], | ||
| 'return' => '', | ||
| ] | ||
| ); | ||
|
|
||
| WP_Mock::userFunction( 'wp_slash', [ 'times' => 0 ] ); | ||
|
|
||
| $this->testee->authenticate(); | ||
|
|
||
| $this->assertArrayNotHasKey( 'redirect_to', $_REQUEST ); | ||
| $this->assertSame( 'https://example.com/wp-admin/', $this->testee->login_redirect( 'https://example.com/wp-admin/' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * A non-string `redirect_to` in the state is ignored without being validated. | ||
| * | ||
| * @covers ::authenticate | ||
| * @covers ::login_redirect | ||
| */ | ||
| public function testLoginRedirectIgnoresNonStringStateRedirect() { | ||
| $this->mockSuccessfulGoogleAuthentication( [ 'https://example.com/state-page' ] ); | ||
|
|
||
| WP_Mock::userFunction( 'wp_sanitize_redirect', [ 'times' => 0 ] ); | ||
| WP_Mock::userFunction( 'wp_validate_redirect', [ 'times' => 0 ] ); | ||
| WP_Mock::userFunction( 'wp_slash', [ 'times' => 0 ] ); | ||
|
|
||
| $this->testee->authenticate(); | ||
|
|
||
| $this->assertArrayNotHasKey( 'redirect_to', $_REQUEST ); | ||
| $this->assertSame( 'https://example.com/wp-admin/', $this->testee->login_redirect( 'https://example.com/wp-admin/' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Mock a successful Google authentication with the given `redirect_to` in the state. | ||
| * | ||
| * @param mixed $redirect_to Value of `redirect_to` in the state. | ||
| * | ||
| * @return void | ||
| */ | ||
| private function mockSuccessfulGoogleAuthentication( $redirect_to ): void { | ||
| $state = base64_encode( | ||
| json_encode( | ||
| [ | ||
| 'provider' => 'google', | ||
| 'nonce' => 'testnonce', | ||
| 'state_token' => 'valid_token', | ||
| 'redirect_to' => $redirect_to, | ||
| ] | ||
| ) | ||
| ); | ||
|
|
||
| $helperMock = Mockery::mock( 'alias:' . Helper::class ); | ||
| $helperMock->expects( 'filter_input' )->once()->withArgs( | ||
| [ | ||
| INPUT_GET, | ||
| 'code', | ||
| FILTER_SANITIZE_FULL_SPECIAL_CHARS | ||
| ] | ||
| )->andReturn( 'abc' ); | ||
|
|
||
| $helperMock->expects( 'filter_input' )->once()->withArgs( | ||
| [ | ||
| INPUT_GET, | ||
| 'state', | ||
| FILTER_SANITIZE_FULL_SPECIAL_CHARS | ||
| ] | ||
| )->andReturn( [] ); | ||
| )->andReturn( $state ); | ||
|
|
||
| $helperMock->expects( 'verify_state_token' )->once()->with( 'valid_token' )->andReturn( true ); | ||
| $helperMock->expects( 'set_state_cookie' )->once()->andReturnNull(); | ||
|
|
||
| $this->wpMockFunction( | ||
| 'wp_verify_nonce', | ||
| [ | ||
| 'testnonce', | ||
| 'login_with_google', | ||
| ], | ||
| 1, | ||
| true | ||
| ); | ||
|
|
||
| $this->ghClientMock->expects( $this->once() ) | ||
| ->method( 'user' ) | ||
| ->willReturn( (object) [ 'email' => 'fakeemail@domain.com' ] ); | ||
|
|
||
| $data = $this->testee->login_redirect(); | ||
| $this->assertNull( $data ); | ||
| $this->authenticatorMock->expects( $this->once() ) | ||
| ->method( 'authenticate' ) | ||
| ->willReturn( Mockery::mock( 'WP_User' ) ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking, and older than this PR, but this PR now owns redirect handling, so flagging it here.
state_redirect()(L245) readsredirect_towithFILTER_SANITIZE_FULL_SPECIAL_CHARS, so a target likehttps://example.com/page/?a=1&b=2is stored in the state as?a=1&b=2.wp_sanitize_redirect()keeps&,;and letters, so we end up redirecting to a URL where the second param is namedamp;b.Helper::get_redirect_url()already usesFILTER_SANITIZE_URLfor the same param. Could we switchstate_redirect()to that, here or in a follow-up issue?wp_validate_redirect()here already handles the safety side.