From bcb5b271fe8768fa4c5198da348236ed7698f6fa Mon Sep 17 00:00:00 2001 From: karthikeya-io Date: Sat, 17 Oct 2026 02:24:05 +0530 Subject: [PATCH 1/2] Fix: Login issue for users with 2FA --- assets/src/js/login.js | 6 +++ src/Modules/Login.php | 44 ++++++++++------ tests/php/Unit/Modules/LoginTest.php | 75 +++++++++++++++++++++++++--- 3 files changed, 103 insertions(+), 22 deletions(-) diff --git a/assets/src/js/login.js b/assets/src/js/login.js index bcb5e361..cdc0fb92 100644 --- a/assets/src/js/login.js +++ b/assets/src/js/login.js @@ -40,6 +40,12 @@ const wpGoogleLogin = { } this.googleLoginButton = this.form.querySelector( '.wp_google_login' ); + + // Some screens reuse the form ID without the button, e.g. Two-Factor's challenge form. + if ( null === this.googleLoginButton ) { + return; + } + this.googleLoginButton.classList.remove( 'hidden' ); // HTML is cloned from existing HTML node. this.form.append( this.googleLoginButton ); diff --git a/src/Modules/Login.php b/src/Modules/Login.php index e1e9f16f..efa0b57c 100644 --- a/src/Modules/Login.php +++ b/src/Modules/Login.php @@ -16,7 +16,6 @@ use WP_User; use WP_Error; -use stdClass; use Throwable; use Exception; use RtCamp\GoogleLogin\Utils\Helper; @@ -53,6 +52,14 @@ class Login implements ModuleInterface { */ private $authenticated = false; + /** + * Redirect URL from the OAuth state, set once the user + * has been authenticated from plugin. + * + * @var string + */ + private $redirect_to = ''; + /** * Login constructor. * @@ -87,11 +94,12 @@ public function init(): void { add_action( 'authenticate', [ $this, 'authenticate' ], 20 ); add_action( 'rtcamp.google_register_user', [ $this->authenticator, 'register' ] ); add_action( 'rtcamp.google_user_created', [ $this, 'user_meta' ] ); - add_action( 'wp_login', [ $this, 'login_redirect' ] ); /** * Filters. */ + // Redirect via filter instead of `wp_login` so other `wp_login` callbacks (e.g. Two-Factor) can run. + add_filter( 'login_redirect', [ $this, 'login_redirect' ] ); add_filter( 'rtcamp.google_redirect_url', [ $this, 'redirect_url' ] ); add_filter( 'rtcamp.google_login_state', [ $this, 'state_redirect' ] ); } @@ -151,6 +159,16 @@ public function authenticate( $user = null ) { if ( $user instanceof WP_User ) { $this->authenticated = true; + if ( ! empty( $decoded_state['redirect_to'] ) && is_string( $decoded_state['redirect_to'] ) ) { + // Same sanitization and allowed-host validation as wp_safe_redirect(). + $this->redirect_to = wp_validate_redirect( wp_sanitize_redirect( $decoded_state['redirect_to'] ), '' ); + } + + if ( $this->redirect_to ) { + // Expose the redirect to plugins that read the requested redirect, e.g. Two-Factor's challenge form. + $_REQUEST['redirect_to'] = $this->redirect_to; + } + /** * Fires once the user has been authenticated via Google OAuth. * @@ -218,23 +236,17 @@ public function state_redirect( array $state ): array { } /** - * Add a redirect once user has been authenticated successfully. + * Redirect to the URL from state once user has been authenticated successfully. * - * @return void + * @param string $redirect_to Redirect destination URL. + * + * @return string */ - public function login_redirect(): void { - $state = Helper::filter_input( INPUT_GET, 'state', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); - - if ( ! $state || ! $this->authenticated ) { - return; + public function login_redirect( $redirect_to ) { + if ( ! $this->authenticated || empty( $this->redirect_to ) ) { + return $redirect_to; } - $state = base64_decode( $state ); - $state = $state ? json_decode( $state ) : null; - - if ( ( $state instanceof stdClass ) && ! empty( $state->provider ) && 'google' === $state->provider && ! empty( $state->redirect_to ) ) { - wp_safe_redirect( $state->redirect_to, 302, 'Login with Google' ); - exit; - } + return $this->redirect_to; } } diff --git a/tests/php/Unit/Modules/LoginTest.php b/tests/php/Unit/Modules/LoginTest.php index b1b900b9..e3e0513d 100644 --- a/tests/php/Unit/Modules/LoginTest.php +++ b/tests/php/Unit/Modules/LoginTest.php @@ -76,10 +76,10 @@ public function testInit() { WP_Mock::expectActionAdded( 'login_form', [ $this->testee, 'login_button' ] ); WP_Mock::expectActionAdded( 'authenticate', [ $this->testee, 'authenticate' ], 20 ); WP_Mock::expectActionAdded( 'rtcamp.google_register_user', [ $this->authenticatorMock, 'register' ] ); - WP_Mock::expectActionAdded( 'rtcamp.google_redirect_url', [ $this->testee, 'redirect_url' ] ); + WP_Mock::expectFilterAdded( 'rtcamp.google_redirect_url', [ $this->testee, 'redirect_url' ] ); WP_Mock::expectActionAdded( 'rtcamp.google_user_created', [ $this->testee, 'user_meta' ] ); WP_Mock::expectFilterAdded( 'rtcamp.google_login_state', [ $this->testee, 'state_redirect' ] ); - WP_Mock::expectActionAdded( 'wp_login', [ $this->testee, 'login_redirect' ] ); + WP_Mock::expectFilterAdded( 'login_redirect', [ $this->testee, 'login_redirect' ] ); $this->testee->init(); $this->assertConditionsMet(); @@ -451,17 +451,80 @@ 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() { + $state = base64_encode( + json_encode( + [ + 'provider' => 'google', + 'nonce' => 'testnonce', + 'redirect_to' => 'https://example.com/state-page', + ] + ) + ); + $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 ); + + $this->wpMockFunction( + 'wp_verify_nonce', + [ + 'testnonce', + 'login_with_google', + ], + 1, + true + ); + + $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->ghClientMock->expects( $this->once() ) + ->method( 'user' ) + ->willReturn( (object) [ 'email' => 'fakeemail@domain.com' ] ); + + $this->authenticatorMock->expects( $this->once() ) + ->method( 'authenticate' ) + ->willReturn( Mockery::mock( 'WP_User' ) ); + + $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/' ) ); - $data = $this->testee->login_redirect(); - $this->assertNull( $data ); + unset( $_REQUEST['redirect_to'] ); } } From de5de330bfabc1ee172f494f345d6dcae36dbb34 Mon Sep 17 00:00:00 2001 From: karthikeya-io Date: Fri, 6 Nov 2026 16:37:40 +0530 Subject: [PATCH 2/2] Fix: Address feedback --- src/Modules/Login.php | 15 ++- tests/php/Unit/Modules/LoginTest.php | 133 ++++++++++++++++++++++----- 2 files changed, 119 insertions(+), 29 deletions(-) diff --git a/src/Modules/Login.php b/src/Modules/Login.php index 3376fa76..97cea5dd 100644 --- a/src/Modules/Login.php +++ b/src/Modules/Login.php @@ -100,7 +100,9 @@ public function init(): void { * Filters. */ // Redirect via filter instead of `wp_login` so other `wp_login` callbacks (e.g. Two-Factor) can run. - add_filter( 'login_redirect', [ $this, 'login_redirect' ] ); + // Runs early (priority 1) so the state destination acts as the initial value, as `redirect_to` does + // for password logins in wp-login.php, and other `login_redirect` filters can still override it. + add_filter( 'login_redirect', [ $this, 'login_redirect' ], 1 ); add_filter( 'rtcamp.google_redirect_url', [ $this, 'redirect_url' ] ); add_filter( 'rtcamp.google_login_state', [ $this, 'state_redirect' ] ); add_filter( 'login_message', [ $this, 'filter_login_message' ] ); @@ -182,8 +184,12 @@ public function authenticate( $user = null ) { } if ( $this->redirect_to ) { - // Expose the redirect to plugins that read the requested redirect, e.g. Two-Factor's challenge form. - $_REQUEST['redirect_to'] = $this->redirect_to; + /* + * Deliberately set the requested redirect for this request. Two-Factor copies it into its + * challenge form, which carries it to the `validate_2fa` request; without this, 2FA users + * lose the destination. Slashed to match core's superglobals (Two-Factor unslashes it). + */ + $_REQUEST['redirect_to'] = wp_slash( $this->redirect_to ); } /** @@ -242,7 +248,8 @@ public function redirect_url( string $url ): string { * @return array */ public function state_redirect( array $state ): array { - $redirect_to = Helper::filter_input( INPUT_GET, 'redirect_to', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); + // FILTER_SANITIZE_URL keeps `&` intact (FULL_SPECIAL_CHARS turns it into `&`); `$options` must be an int. + $redirect_to = Helper::filter_input( INPUT_GET, 'redirect_to', FILTER_SANITIZE_URL, 0 ); /** * Filter the default redirect URL in case redirect_to param is not available. * Default to admin URL. diff --git a/tests/php/Unit/Modules/LoginTest.php b/tests/php/Unit/Modules/LoginTest.php index 141770c7..7af46411 100644 --- a/tests/php/Unit/Modules/LoginTest.php +++ b/tests/php/Unit/Modules/LoginTest.php @@ -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::expectFilterAdded( 'login_redirect', [ $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 ); @@ -480,13 +494,103 @@ public function testLoginRedirectWhenNotAuthenticated() { * @covers ::login_redirect */ public function testLoginRedirectUsesStateRedirectAfterAuthentication() { + $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' => 'https://example.com/state-page', + 'redirect_to' => $redirect_to, ] ) ); @@ -521,20 +625,6 @@ public function testLoginRedirectUsesStateRedirectAfterAuthentication() { true ); - $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->ghClientMock->expects( $this->once() ) ->method( 'user' ) ->willReturn( (object) [ 'email' => 'fakeemail@domain.com' ] ); @@ -542,12 +632,5 @@ public function testLoginRedirectUsesStateRedirectAfterAuthentication() { $this->authenticatorMock->expects( $this->once() ) ->method( 'authenticate' ) ->willReturn( Mockery::mock( 'WP_User' ) ); - - $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/' ) ); - - unset( $_REQUEST['redirect_to'] ); } }