Repository navigation
Fix: Login issue for users with 2FA - #412
karthikeya-io wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🔵 Needs a closer look
The cross-request 2FA handoff still needs regression coverage and human integration validation.
1 open issue
What changed in this PR
Updates Google login redirects to let two-factor authentication callbacks run without an early redirect.
Changes:
- Moves redirect handling to
login_redirectand exposes the validated destination to 2FA plugins. - Guards against login forms without a Google login button.
- Updates hook expectations and redirect tests.
| File | Description |
|---|---|
| tests/php/Unit/Modules/LoginTest.php | Updates hook expectations and redirect coverage. |
| src/Modules/Login.php | Defers redirects and preserves the validated OAuth destination. |
| assets/src/js/login.js | Handles forms without the Google login button. |
Review effort: Balanced
One Tap + 2FA: 1.4.4 vs 1.4.5 (develop), and with this PRMerged develop with this branch noticed the new changes with onetap login.
|
| * 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' ] ); |
There was a problem hiding this comment.
Moving to login_redirect is the right fix for the Two-Factor ordering problem. It also changes behaviour for every Google login, though, not just 2FA users. We used to exit inside wp_login; now the rest of core's post-login handling in wp-login.php runs too:
- Admin email confirmation: users with
manage_optionswill now get theaction=confirm_admin_emailscreen whenadmin_email_lifespanhas expired. Before, we skipped it. - Users without
edit_posts: when the target isadmin_url()(the defaultstate_redirect()puts in the state), core now sends them toprofile.php, or tohome_url()if they don't haveread. Before, they landed on/wp-admin/. - Other
login_redirectfilters:state_redirect()always fillsredirect_to(it falls back toadmin_url()), so$this->redirect_tois effectively always set. That means this callback at priority 10 overrides anylogin_redirectfilter registered before it, such as role-based or membership redirects, and any filter registered after it overrides us. The oldexitwas worse, so this isn't a regression, but the order now depends on load order. Should we pick an explicit priority on purpose?
I think 1 and 2 are improvements, but they're user-visible. Could we add them to the changelog / release notes?
|
|
||
| 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'] ), '' ); |
There was a problem hiding this comment.
Non-blocking, and older than this PR, but this PR now owns redirect handling, so flagging it here. state_redirect() (L245) reads redirect_to with FILTER_SANITIZE_FULL_SPECIAL_CHARS, so a target like https://example.com/page/?a=1&b=2 is 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 named amp;b.
Helper::get_redirect_url() already uses FILTER_SANITIZE_URL for the same param. Could we switch state_redirect() to that, here or in a follow-up issue? wp_validate_redirect() here already handles the safety side.
|
|
||
| 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; |
There was a problem hiding this comment.
WordPress superglobals are slashed (wp_magic_quotes()), and Two-Factor reads this value with wp_unslash( $_REQUEST['redirect_to'] ). To keep the value consistent with how core fills it, I'd write it slashed:
| $_REQUEST['redirect_to'] = $this->redirect_to; | |
| $_REQUEST['redirect_to'] = wp_slash( $this->redirect_to ); |
It makes no practical difference today, because wp_sanitize_redirect() already strips backslashes. It just avoids surprises if the sanitization changes later. Also, could the comment say that this deliberately mutates a superglobal for the second (validate_2fa) request? That way nobody "cleans it up" later and breaks the 2FA redirect.
| * @covers ::authenticate | ||
| * @covers ::login_redirect | ||
| */ | ||
| public function testLoginRedirectUsesStateRedirectAfterAuthentication() { |
There was a problem hiding this comment.
Please add the negative case as well: wp_validate_redirect() returns '' for a host that isn't allowed (e.g. https://evil.example/). In that case we should assert that:
$_REQUEST['redirect_to']is not set, andlogin_redirect( 'https://example.com/wp-admin/' )returns its input unchanged.
This is the open-redirect guard, so it's the branch that matters most to cover. A non-string redirect_to in the state (e.g. an array or int) would be a cheap extra case for the is_string() check.
|
|
||
| $data = $this->testee->login_redirect(); | ||
| $this->assertNull( $data ); | ||
| unset( $_REQUEST['redirect_to'] ); |
There was a problem hiding this comment.
If either assertion above fails, this cleanup never runs, and $_REQUEST['redirect_to'] leaks into whichever tests run after it, which makes those failures confusing. Could we move it into tearDown(), or wrap the assertions in try { ... } finally { unset( $_REQUEST['redirect_to'] ); }?
|
Hi @Debarghya-Banerjee Thanks for the feedback addressed them. Could you check these two 1. Changelog for this fix
I see If it's going into a later release, I'll leave 2. One Tap 2FA warning in settings Suggested wording:
Should I update it in this PR, or open a separate issue/PR? |

Description
Allow Two-Factor challenge for Google login by redirecting via login_redirect
Users with Two-Factor enabled couldn't log in with Google, because our
wp_logincallback redirected and calledexitbefore Two-Factor'swp_logincallback (priorityPHP_INT_MAX) could render the 2FA challenge. This PR moves the post-login redirect to the corelogin_redirectfilter.Screen.Recording.2026-10-20.at.3.15.42.PM.mov
Screen.Recording.2026-10-20.at.3.17.06.PM.mov
Issue
#416