Problem/Motivation

On a project using the module, users are sometimes loosing the destination parameter during the course of the 'email_tfa_email_tfa_verify_login'.
So far, my undersanding is that it is related to Lines 160 and 162 of email_tfa\src\Form\EmailTfaVerifyLoginForm.php

// save the destination in the $form_state to be used on submit.
$form_state->set('destination', $this->getRequest()->query->get('destination'));
// remove the destination from the query string.
$this->getRequest()->query->remove('destination');

The first part doesn't seem to work in some cases :
$form_state->set('destination', $this->getRequest()->query->get('destination'));
So the second, removing the destination, is actually killing it.
Is removing the query string obligatory ?

Also L 262 :
$form_state->setRedirect(
'entity.user.canonical',
['user' => $user->id()]
);
Why redirecting to the user profile if there are no destination ?
Some projects don't exploit much the user profile page. The frontpage would be a more relevant option in IMHO.
I would say that more people would want to land on the frontpage instead of the profile page after signing in.

Steps to reproduce

I've not yet clearly identified the reason why in some cases "$form_state->set('destination', $this->getRequest()->query->get('destination'));" doens't seem to apply.

Proposed resolution

Avoid removing the query string :
// $this->getRequest()->query->remove('destination');

And in the submitForm changing the Redirect to the frontpage.

Remaining tasks

If you agree with these changes, I can provide a patch.

User interface changes

none

API changes

none

Data model changes

none

Issue fork email_tfa-3620695

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tritof created an issue. See original summary.

tritof’s picture

Issue summary: View changes
tritof’s picture

Issue summary: View changes
tritof’s picture

Issue summary: View changes

dalin made their first commit to this issue’s fork.

dalin’s picture

Priority: Normal » Major
Status: Active » Needs review
StatusFileSize
new1.46 KB

Found the root cause — the destination is only ever read from the query string, but in the 403 login flow it arrives in the POST body.

Steps to reproduce (any site where the 403 page is set to /user/login, TFA enabled for the account):

  1. As anonymous, request a URL you're denied on (e.g. /admin/reports). Drupal renders the login form as the 403 response, and the form's action is the denied URL itself.
  2. Submit credentials. That POST 403s again, and core's DefaultExceptionHtmlSubscriber::makeSubrequest() adds the destination to the POST parameters of the sub-request ($sub_request->isMethod('GET') ? $sub_request->query : $sub_request->request) — not the query string.
  3. EmailTfaLoginForm::buildForm() only checks $this->getRequest()->query->get('destination'), so the destination is silently dropped and after entering the email code you land on /user/{uid} instead of the page you were originally denied on.
  4. Direct visits to /user/login?destination=... work fine, which is why this looks intermittent: it only breaks when the login form is reached through a 403.

Fix: buildForm() should read the destination from either bag:

$request = $this->getRequest();
$destination = $request->query->get('destination') ?? $request->request->get('destination');

and stash it in $form_state as before. Everything downstream (the verify-form redirect and the final RedirectResponseSubscriber handling) already works once the destination survives that first hop.

Patch attached (email_tfa-3620695-403-post-destination.patch, applies to 2.0.7), verified end-to-end against a scripted login flow.

NOTE: I did not change the fallback destination to <front>. This is a problem with just Core even without this module. If you don't want this you can fix with a simple alter hook or redirect. If we do want to fix it should be in a separate issue to add a config UI for it.

abdulaziz zaid’s picture

Status: Needs review » Fixed

Thanks, @dalin! Tested on Drupal 11 and confirmed the fix works. Committed to 3.0.x.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.