Cleanup TfaTrustedBrowser Plugin save logic

Created on 20 October 2023, about 1 year ago
Updated 31 October 2023, about 1 year ago

Problem/Motivation


The website encountered an unexpected error. Please try again later.

  Error: Typed property Drupal\tfa\Plugin\Tfa\TfaTrustedBrowser::$trustBrowser must not be accessed before initialization in Drupal\tfa\Plugin\Tfa\TfaTrustedBrowser->finalize() (line 200 of modules/contrib/tfa/src/Plugin/Tfa/TfaTrustedBrowser.php).
  Drupal\tfa\Form\EntryForm->finalize() (Line: 282)
  Drupal\tfa\Form\EntryForm->submitForm(Array, Object)
  call_user_func_array(Array, Array) (Line: 114)
  Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 52)
  Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
  Drupal\Core\Form\FormBuilder->processForm('tfa_entry_form', Array, Object) (Line: 325)
  Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
  Drupal\Core\Controller\FormController->getContentResult(Object, Object)
  call_user_func_array(Array, Array) (Line: 123)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 592)
  Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 182)
  Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
  Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 44)
  Drupal\redirect_after_login\RedirectMiddleware->handle(Object, 1, 1) (Line: 58)
  Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
  Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
  Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
  Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
  Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
  Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
  Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
  Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Steps to reproduce

1. Login to site.
2. enter username and password
3. enter the TFA code
4. Click submit you will observe the error.

I am having TFA module DEV version with commit ID f7dbd18d4d5d7e4e890f3e7e8163eccdfc6e51c9

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

๐Ÿ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia bhanu951

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @bhanu951
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia Shreya_98

    Shreya_th โ†’ made their first commit to this issueโ€™s fork.

  • @shreya_th opened merge request.
  • @bhanu951 opened merge request.
  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia bhanu951

    I have created fix for the patch, please review the MR #50

  • Status changed to Needs review about 1 year ago
  • Status changed to Needs work about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cmlara

    @Shreya_th I'm not sure what your MR was trying to accomplish? It doesn't appear to be related to this issues fault.

    Regarding the root fault of this, yes the changes in !MR50 should stop the error, this originates from adding strict type hints in ๐Ÿ“Œ Add return, property, and parameter type hints to code Fixed , however there is a wider scope question, is this property even necessary?

      public function submitForm(array $form, FormStateInterface &$form_state): void {
        $trust_browser = $form_state->getValue('trust_browser');
        if (!empty($trust_browser)) {
          $this->setTrusted($this->generateBrowserId(), $this->getAgent());
        }
      }
    
    
      public function finalize(): void {
        if ($this->trustBrowser) {
          $name = $this->getAgent();
          $this->setTrusted($this->generateBrowserId(), $name);
        }
      }
    

    $this->trustBrowser is never going to be anything but FALSE, it is never set during our login operations. Even if it were set to a non-false value it is attempting to duplicate actions that are done previously.

    This holds true back to the 8.x-1.x branch as well, so we have been doing it this way for a long time.

    The significant question of this is, should this be done in finalize() or submitForm(), and was this part of a conversion process that was never completed, or a portion of our API in 8.x-1.x that just didn't develop into being needed. I have not yet had time to look through the commit logs to determine what issues were involved in this code being created in this way.

    We should find those answers first before we choose our solution as that will determine what fix we need.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cmlara

    Related commit: ebef2da3a9d4cee63f18fa0e874e2be0bef9745d
    Reverting the relevant plugin change appears to be functional against current dev appears to work. Unknown why they were changed.

    TfaLoginInterface::class does not actually list the finalize() method on interface so in that regard its an optional call.

    Since the finalize() in TfaTrustedBrowser always requires that a user confirm if they wish to add the browser this isn't a scenario where we can make the call without the form.

    We have already proceed through form validation by the time submitForm() is called so there is no logical reason that I can see that it needs to be handled in a separate step, especially since its handled as part of the same EntryForm::submitForm() call.

    Given this, I would suggest we:

    • Remove the finalize method from TfaTrustedBrower
    • Remove the $trustBrowser property

    We will likely want a followup call to discuss the finalize() method call in EntryForm, either it should be added to the TfaLoginInterface if we want it to continue existing, or it should be removed.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia bhanu951

    I would suggest first lets fix the current issue ASAP by committing changes in the current changes in MR , and work on making the proper fix as sites break due to this issue.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cmlara

    I would suggest first lets fix the current issue ASAP by committing changes in the current changes in MR , and work on making the proper fix as sites break due to this issue.

    I'm generally accepting of expedited fixes when the larger fix will take significant amount of time and effort and that the expedited fix has minimal negative impact. While this MR as it exists appears to have minimal(no) negative impact the issue itself does not appear as if it would take significant time or effort to resolve.

    As this issue stands currently it is just going to be a removal of a property, and removal of a method. I'm not even expecting there to be any test code to cleanup since the testing branch is currently passing. The comments in #8 should be a very quick fix to implement (quick glance it appears we just need to delete 19 lines of code).

    Additionally this currently only impacts the dev branch of 2.x. However this also means it should not be impacting production sites as 2.x is still in early alpha, and due to open work on ๐Ÿ“Œ SA-CONTRIB-2023-030 and 2.x Active the 2.x branch (and alpha2 release is also code that should not be used anywhere (sites should be on 8.x-1.x.)

  • Status changed to Needs review about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cmlara
  • Status changed to Fixed about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States cmlara
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024