Problem/Motivation

When using real_aes as the encryption service, encrypting and decrypting our seeds may throw a EncryptionException since https://www.drupal.org/project/real_aes/issues/3157453

This can cause a 500 error on the Setup and Validation plugins when an exception is thrown.

Comments

michel.g created an issue. See original summary.

greggles’s picture

Title: real_aes returning Exception can break the TFA setup and validation forms » Gracefully handle real_aes returning Exception can break the TFA setup and validation forms
Version: 8.x-1.0 » 8.x-1.x-dev

Can you provide more "steps to repeat" this bug? How did you get Encrypt into a state where it's throwing the exception?

i guess TFA should catch the exception and display a reasonable error message.

Updating title to match.

For sites affected by this, I suggest fixing whatever is making encrypt broken?

michel.g’s picture

In my case the exception was thrown because we still HTTP authentication enabled on our website (hence the authenticator not being able to communicate with our test website)

Regardless I guess we could take inspiration from this patch for 2.x

https://git.drupalcode.org/project/tfa/-/merge_requests/17/diffs

In which we catch the exceptions and return FALSE, with the difference where we should properly log those exceptions in the Drupal log.

greggles’s picture

Status: Active » Postponed (maintainer needs more info)

If there was an exception and a 500 error there should be an error either displayed to the page or in watchdog or in the apache or php error log. Can you find those errors and copy and paste them here?

cmlara’s picture

Title: Gracefully handle real_aes returning Exception can break the TFA setup and validation forms » Gracefully handle EncryptionServiceInterface Exceptions
Version: 8.x-1.x-dev » 2.x-dev
Assigned: michel.g » Unassigned
Status: Postponed (maintainer needs more info) » Active

This is indeed a legitimate fault that we have.

An example workflow that could cause this in 1.x is TfaTotpValidation::ready()->TfaTotpValidation::getSeed()->tfaBasePlugin::Decrypt($data) which can throw \Drupal\encrypt\Exception\EncryptionMethodCanNotDecryptException or \Drupal\encrypt\Exception\EncryptException. Similar path exists in the HOTP plugin.

Real AES is known to throw this when its key is misconfiguration in some manner, however it could be any sort of issue from any of the various encryption engines, including network faults to remote systems.

We call ready() when a user logs in to check if they have a token configured, we have no provision in our API for raising an exception, and at the same time we also can not just return FALSE from the ready() method when an exception occurs as that could allow an access bypass. Returning true() would also pose issues.

Yes fixing the root fault in Real AES is the correct solution to solve however that doesn't really absolve us of our obligation to handle exceptions.

Realistically anything that calls tfaBasePlugin::Decrypt() needs to expect a failure to occur, the big question is how we present this. WSOD's and Raised Exceptions at least prevent a login from occurring, but its not the most user friendly presentation.

Moving this to 2.x, this could involve API changes.

ambient.impact’s picture

As a related aside (I skimmed some of this so apologies if you already addressed this) but I noticed the code referenced in the diff in #3 has this:

  public function submitSetupForm(array $form, FormStateInterface $form_state) {
    // Write seed for user.
    try {
      $this->storeSeed($this->seed);
      return TRUE;
    }
    catch (EncryptException $e) {
    }
    return FALSE;
  }

which is found in src/Plugin/Tfa/TfaHotp.php. I was trying to set up TFA on a site where I hadn't provided the key to the Encrypt module and couldn't figure out why TFA wasn't working until I dug into the code and found that catch (EncryptException $e) { is doing nothing with the exception that would have told me what the problem was (I did a dpm() call to the Devel module there to eventually figure it out). I feel like that needs to be addressed as part of this or a related issue as well.