[1.1.x] Rest Password Reset

Created on 13 May 2024, about 1 month ago
Updated 11 June 2024, 14 days ago

This module provides a way to request your username via email or to change your Drupal user password on a decoupled website via REST api endpoints. It builds on the core REST module and uses REST UI to further configure the custom endpoints provided by the module.

Project link

https://www.drupal.org/project/rest_password_reset

📌 Task
Status

Needs work

Component

module

Created by

🇳🇱Netherlands jb044 Leeuwarden

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

Comments & Activities

  • Issue created by @jb044
  • 🇮🇳India vishal.kadam Mumbai

    Thank you for applying!

    Please read Review process for security advisory coverage: What to expect for more details and Security advisory coverage application checklist to understand what reviewers look for. Tips for ensuring a smooth review gives some hints for a smoother review.

    The important notes are the following.

    • If you have not done it yet, you should run phpcs --standard=Drupal,DrupalPractice on the project, which alone fixes most of what reviewers would report.
    • For the time this application is open, only your commits are allowed.
    • The purpose of this application is giving you a new drupal.org role that allows you to opt projects into security advisory coverage, either projects you already created, or projects you will create. The project status won't be changed by this application and no other user will be able to opt projects into security advisory policy.
    • We only accept an application per user. If you change your mind about the project to use for this application, or it is necessary to use a different project for the application, please update the issue summary with the link to the correct project and the issue title with the project name and the branch to review.

    To the reviewers

    Please read How to review security advisory coverage applications , Application workflow , What to cover in an application review , and Tools to use for reviews .

    The important notes are the following.

    • It is preferable to wait for a Code Review Administrator before commenting on newly created applications. Code Review Administrators will do some preliminary checks that are necessary before any change on the project files is suggested.
    • Reviewers should show the output of a CLI tool only once per application.
    • It may be best to have the applicant fix things before further review.

    For new reviewers, I would also suggest to first read In which way the issue queue for coverage applications is different from other project queues .

  • Status changed to Needs work about 1 month ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹

    Thank you for applying! The project page should at least mention there is also the REST Password Request module and describe the differences between the modules.

  • 🇳🇱Netherlands jb044 Leeuwarden

    Good point, added a disclaimer.

  • 🇮🇳India vishal.kadam Mumbai

    FILE: src/Plugin/rest/resource/PasswordReset.php

      /**
       * Class constructor.
       *
       * @param array $configuration
       *   The configuration array.
       * @param string $plugin_id
       *   The plugin id.
       * @param array $plugin_definition
       *   The plugin definition array.
       * @param array $serializer_formats
       *   The serializer formats array.
       * @param \Psr\Log\LoggerInterface $logger
       *   The logger service.
       * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
       *   The entity type manager service.
       * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
       *   The configuration factory service.
       * @param \Drupal\Component\Datetime\TimeInterface $time
       *   The time service.
       */
      public function __construct(

    FILE: src/Plugin/rest/resource/ResetLink.php

      /**
       * The class constructor.
       *
       * @param array $configuration
       *   The configuration.
       * @param string $plugin_id
       *   The plugin id.
       * @param array $plugin_definition
       *   The plugin definition.
       * @param array $serializer_formats
       *   The serializer formats.
       * @param \Psr\Log\LoggerInterface $logger
       *   The logger.
       * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
       *   The entity type manager service.
       * @param \Drupal\Core\Utility\Token $token
       *   The token service.
       * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
       *   The config factory.
       * @param \Drupal\Component\Utility\EmailValidatorInterface $emailValidator
       *   The email validator service.
       * @param \Drupal\Core\Mail\MailManagerInterface $mailManager
       *   The mail manager service.
       * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
       *   The language manager service.
       * @param \Drupal\Core\State\StateInterface $state
       *   The state service.
       * @param \Drupal\Component\Datetime\TimeInterface $time
       *   The time service.
       */
      public function __construct(

    FILE: src/Plugin/rest/resource/Username.php

      /**
       * The class constructor.
       *
       * @param array $configuration
       *   The configuration.
       * @param string $plugin_id
       *   The plugin id.
       * @param array $plugin_definition
       *   The plugin definition.
       * @param array $serializer_formats
       *   The serializer formats.
       * @param \Psr\Log\LoggerInterface $logger
       *   The logger.
       * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
       *   The entity type manager service.
       * @param \Drupal\Core\Utility\Token $token
       *   The token service.
       * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
       *   The config factory.
       * @param \Drupal\Component\Utility\EmailValidatorInterface $emailValidator
       *   The email validator service.
       * @param \Drupal\Core\Mail\MailManagerInterface $mailManager
       *   The mail manager service.
       * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
       *   The language manager service.
       * @param \Drupal\Core\State\StateInterface $state
       *   The state service.
       * @param \Drupal\Component\Datetime\TimeInterface $time
       *   The time service.
       */
      public function __construct(

    The documentation comment for constructors is not mandatory anymore, If it is given, the description must be Constructs a new [class name] object. where [class name] includes the class namespace.

  • 🇳🇱Netherlands jb044 Leeuwarden

    @vishal.kadam Thank you, fixed in the latest minor update.

  • Status changed to RTBC about 1 month ago
  • 🇮🇳India vishal.kadam Mumbai

    Rest looks fine to me.

    Let’s wait for a Code Review Administrator to take a look and if everything goes fine, you will get the role.

  • Status changed to Needs work 14 days ago
  • 🇮🇹Italy apaderno Brescia, 🇮🇹
    • The following points are just a start and don't necessarily encompass all of the changes that may be necessary
    • A specific point may just be an example and may apply in other places
    • A review is about code that doesn't follow the coding standards, contains possible security issue, or does not correctly use the Drupal API; the single points are not ordered, not even by importance

    src/Form/PasswordResetConfigForm.php

        $this->config('rest_password_reset.password_reset')
          ->set('fe_uri', $form_state->getValue('fe_uri'))
          ->set('name_mail_subject', Xss::filter($form_state->getValue('name_mail_subject')))
          ->set('name_mail_body', Xss::filter($form_state->getValue('name_mail_body')))
          ->set('reset_mail_subject', Xss::filter($form_state->getValue('reset_mail_subject')))
          ->set('reset_mail_body', Xss::filter($form_state->getValue('reset_mail_body')))
          ->save();
    

    Values are not sanitized when stored, but when used inside markup, for example the user interface or an email. The email subject and body are already sanitized from Drupal core; there is no need to sanitize them twice.

    src/Plugin/rest/resource/PasswordReset.php

        $hash = Xss::filter($params['hash']);
        $new_password = Xss::filter($params['new_password']);
    
        try {
          /** @var \Drupal\user\UserInterface|NULL $user */
          $user = $this->entityTypeManager->getStorage('user')
            ->load($params['uid']);
          if (!$user) {
            $this->logger->warning('REST resource rest_password_reset_password: unable to load user by id.');
            return new ResourceResponse(['message' => $this->t('Unable to load user.')], 500);
          }
        }
        catch (PluginNotFoundException | InvalidPluginDefinitionException $e) {
          $this->logger->error('Internal server error: :error', [':error' => $e]);
          return new ResourceResponse(['message' => $this->t('Internal server error: :error', [':error' => $e])], 500);
        }
    
        $config = $this->configFactory->get('user.settings');
        $timeout = $config->get('password_reset_timeout');
        $current = $this->time->getRequestTime();
        if ($current - $params['timestamp'] > $timeout) {
          return new ResourceResponse(['message' => $this->t('Your one-time login link has expired.')], 404);
        }
    
        if (!hash_equals($hash, user_pass_rehash($user, $params['timestamp']))) {
          return new ResourceResponse(['message' => $this->t('Invalid reset code.')], 404);
        }
    

    Comparing a hash after it is passed to Xss::filter() with a hash that is not passed to Xss::filter() will fail.
    This is an error for the same reason the previous code is not correct: User input is sanitized only when it is used inside markup. When the user input is used for other purposes, including showing it in plain text, it does not need to be sanitized.

    src/Plugin/rest/resource/ResetLink.php

              if ($mail['send'] && !$mail['result']) {
                $this->logger->error('There was a problem sending the rest_password_reset_link REST resource email.');
                $response = new ModifiedResourceResponse(['message' => $this->t('Could not send email, please try again.')], 500);
              }
    

    An error in sending an email is already logged by MailManager::doMail(). It is not necessary to log it twice.

Production build 0.69.0 2024