Skip to content

Fix: Login issue for users with 2FA - #412

Open
karthikeya-io wants to merge 3 commits into
developfrom
fix/issues-two-fa
Open

karthikeya-io wants to merge 3 commits into
developfrom
fix/issues-two-fa

Conversation

@karthikeya-io

@karthikeya-io karthikeya-io commented Oct 16, 2026 •

Copy link
Copy Markdown

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_login callback redirected and called exit before Two-Factor's wp_login callback (priority PHP_INT_MAX) could render the 2FA challenge. This PR moves the post-login redirect to the core login_redirect filter.

Before After
Screen.Recording.2026-10-20.at.3.15.42.PM.mov
Screen.Recording.2026-10-20.at.3.17.06.PM.mov

Issue

#416

@karthikeya-io
karthikeya-io marked this pull request as ready for review October 19, 2026 11:36
Copilot AI balanced review requested due to automatic review settings October 19, 2026 11:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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_redirect and 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

Comment thread src/Modules/Login.php Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No blocking defects remain, and the Two-Factor flow was manually verified as documented in the developer reply.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@karthikeya-io karthikeya-io linked an issue Oct 20, 2026 that may be closed by this pull request
@karthikeya-io

karthikeya-io commented Nov 6, 2026 •

Copy link
Copy Markdown
Author

One Tap + 2FA: 1.4.4 vs 1.4.5 (develop), and with this PR

Merged develop with this branch noticed the new changes with onetap login.

  • 1.4.4: One Tap set the auth cookies directly from the AJAX request, so users with Two-Factor were logged in without the 2FA prompt (hence the settings warning).
  • 1.4.5 (develop): One Tap now runs the authenticate / wp_authenticate_user filters, and for Two-Factor users it doesn't log them in. It redirects to wp-login.php?login_with_google_2fa=1&redirect_to=… with "Please sign in again to complete two-factor authentication." The user then has to sign in again, e.g. with the Google button. (But user cannot login)
  • With this PR: that Google button login now shows Two-Factor's prompt. On develop alone, it still hits this bug and sends the user back to the login page. Re-tested after merging develop: One Tap → notice → Login with Google → 2FA code → logged in ✅ (But user needs to click the Login with Google button on login page)
1.4.4 master 1.4.5 develop this branch
Screen.Recording.2026-11-06.at.1.52.06.PM.mov
Screen.Recording.2026-11-06.at.1.53.10.PM.mov
Screen.Recording.2026-11-06.at.1.48.35.PM.mov

Comment thread src/Modules/Login.php Outdated
* 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' ] );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Admin email confirmation: users with manage_options will now get the action=confirm_admin_email screen when admin_email_lifespan has expired. Before, we skipped it.
  2. Users without edit_posts: when the target is admin_url() (the default state_redirect() puts in the state), core now sends them to profile.php, or to home_url() if they don't have read. Before, they landed on /wp-admin/.
  3. Other login_redirect filters: state_redirect() always fills redirect_to (it falls back to admin_url()), so $this->redirect_to is effectively always set. That means this callback at priority 10 overrides any login_redirect filter registered before it, such as role-based or membership redirects, and any filter registered after it overrides us. The old exit was 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?

Comment thread src/Modules/Login.php

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'] ), '' );

Copy link
Copy Markdown

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) 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.

Comment thread src/Modules/Login.php Outdated

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
$_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() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, and
  • login_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.

Comment thread tests/php/Unit/Modules/LoginTest.php Outdated

$data = $this->testee->login_redirect();
$this->assertNull( $data );
unset( $_REQUEST['redirect_to'] );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'] ); }?

@karthikeya-io

Copy link
Copy Markdown
Author

Hi @Debarghya-Banerjee Thanks for the feedback addressed them. Could you check these two

1. Changelog for this fix

Could we add them to the changelog / release notes?

I see 1.4.5 is prepared on develop but not tagged yet. Should this fix ship in 1.4.5? If so, I'd add these under = 1.4.5 = in readme.txt as a separate commit:

Fix: Allow users with the Two-Factor plugin enabled to log in with Google, including the One Tap two-factor hand-off.
Fix: Preserve all query parameters of `redirect_to` after Google login.
Update: Google logins now use the core `login_redirect` filter, so other `login_redirect` filters and core post-login screens (e.g. admin email confirmation) apply as they do for password logins.

If it's going into a later release, I'll leave readme.txt to the release PR.

2. One Tap 2FA warning in settings
The One Tap setting still says "One Tap login is more convenient, but it bypasses two-factor authentication (2FA)." (Settings.php). Since 1.4.5, that's no longer true for the Two-Factor plugin: One Tap sends those users back to the login page to complete 2FA, which works end-to-end with this PR. It's still true for other 2FA plugins (the hand-off is specific to Two_Factor_Core).

Suggested wording:

Note: If the Two-Factor plugin is active and enabled for a user, One Tap will ask them to sign in again with the Google button to complete two-factor authentication. Other 2FA plugins may not be enforced during One Tap login.

Should I update it in this PR, or open a separate issue/PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Users with Two-Factor enabled can't log in with Google

3 participants