Problem/Motivation

#992540: Nothing clears the "5 failed login attempts" security message when a user resets their own password clears the user login flood lock when user try to reset password. However, it doesn't work for admin resets user password.

Steps to reproduce

  1. Register as a normal user
  2. Try to login with wrong password for 5 times
  3. Login as admin and change user's password
  4. Try to login as user with new password

Expected
Allow user to login with new password.

Expected
- Still getting "There have been more than 5 failed login attempts for this account. It is temporarily blocked. Try again later or request a new password."

Proposed resolution

Dispatch an event on password reset to clear flood lock.

Remaining tasks

- One scenario current patch doesn't work - When admin reset the password and new password == old password.

Issue fork drupal-2881572

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

vijaycs85 created an issue. See original summary.

vijaycs85’s picture

Status: Active » Needs review
StatusFileSize
new10.45 KB

Initial patch.

vijaycs85’s picture

Title: User login flood lock doesn't clear when resets password as admin » User login flood lock doesn't clear when reset password as admin
vijaycs85’s picture

Issue tags: +Needs tests
cilefen’s picture

Category: Feature request » Bug report
mahalingam_cs’s picture

@vijaycs85 Does the patch from #2 has the fix for the issue.

valthebald’s picture

I would argue that the current behavior (not clearing flood after changing user password) is the correct one, and automatic resetting of flood after changing user password by admin leads to many questions: who is identified as admin? How do we distinguish changing password from user account form from saving User entity?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

catch’s picture

I would argue that the current behavior (not clearing flood after changing user password) is the correct one, and automatic resetting of flood after changing user password by admin leads to many questions: who is identified as admin?

Anyone with permissions to change someone else's password should be counted as a user admin.

How do we distinguish changing password from user account form from saving User entity?

We should be able to detect that the password field value has changed and only reset the flood table in that case.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

catch’s picture

Issue tags: -Needs tests
StatusFileSize
new2.75 KB

Just ran into this one, but the patch here is quite out of date and I'm not sure about adding an event here. We already have user CRUD hooks, and the event could be confused with those, also a password reset isn't CRUD.

Instead I've just copied the logic wholesale. I think if we want to make this more generic, we should instead add a UserLoginFlood service with helpers for clearing the different identifiers based on configuration and etc. - that could then encapsulate setting the flood records as well as clearing them.

There's a further problem here that when uid_only is disabled, the identifier includes the IP address - and you need the full identifier to clear the flood records - since we don't have the user's IP address, we're stuck. So... we'd need to add a method to the flood service to clear identifiers by prefix - which would let us use $uid . '-' and clear all records for that user. We don't need that when the user themselves requests a password reset, so a working version of the patch would not have 1-1 matching logic anyway. This is easy for the database flood backend, but not necessarily for alternatives - and it'd need a new interface.

It would be possible to have a patch and test that works for uid_only and open a follow-up for the uid + ip version, depending on the API addition to flood.

Here's patch (up to where I realised the IP address problem), just added a @todo, and some initial test coverage that should fail.

catch’s picture

StatusFileSize
new3.02 KB

Slight expansion of the test coverage so it tests the uid_only case too, and first.

The last submitted patch, 15: 2881572-15.patch, failed testing. View results

Status: Needs review » Needs work

The last submitted patch, 16: 2881572-16.patch, failed testing. View results

heddn’s picture

Using the latest patch from #16, there are still a small edge issue.

As user 3, I reset user 3’s password. User 3’s user account is locked out after trying (in another browser) the wrong password. The password reset doesn’t reset the flood status on user 3.

As user 3, I reset user 4’s password. User 4’s account was locked after trying (in another browser) the wrong password. The password reset did reset the flood status on user 4.

catch’s picture

So #992540: Nothing clears the "5 failed login attempts" security message when a user resets their own password means that in practice I don't think people would run into #19 - however I can't think of any reason not to clear the flood control when a user changes their own password.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

paulocs’s picture

Status: Needs work » Needs review
StatusFileSize
new2.95 KB
new2.87 KB

Adding new patch to clear the flood when user changes their own password.
It also fixes the test case.

Status: Needs review » Needs work

The last submitted patch, 22: 2881572-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

munish.kumar’s picture

Status: Needs work » Needs review
StatusFileSize
new2.82 KB
new2.98 KB

Hi @paulocs, Please review the patch it will fix the test case issues.

catch’s picture

There's already an issue at #992540: Nothing clears the "5 failed login attempts" security message when a user resets their own password covering users resetting their own password, this issue is for when admins reset.

vikashsoni’s picture

StatusFileSize
new38.65 KB

I have applied #24, #22, #16 but not working getting error can u tell me sharing screenshot ---

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

isa.bel’s picture

I can confirm patch #24 worked for me. Also for the password reset problem, where the flood table wasn't being cleared as well.

nicer’s picture

Patch #24 confirmed on core 9.2.1. Flood control cleared on user password reset, admin pw reset and admin pw reset when old pw == new pw.

NOTE: Had to manually apply patch due to file differences.

ranjith_kumar_k_u’s picture

StatusFileSize
new2.92 KB

The last patch failed to apply on 9.3 so re-rolled it for 9.3

Status: Needs review » Needs work

The last submitted patch, 30: 2881572-30.patch, failed testing. View results

paulocs’s picture

Status: Needs work » Needs review
StatusFileSize
new2.49 KB
new2.5 KB
new3.62 KB

I set back the logic to reset the flood for only when the user password is changed by another user.

I also fixed the tests for it.

paulocs’s picture

Issue tags: +Bug Smash Initiative

The last submitted patch, 32: 2881572-TEST-ONLY.patch, failed testing. View results

spokje’s picture

Looks good, a few nitpicks:

1.

--- a/core/modules/user/src/Entity/User.php
+++ b/core/modules/user/src/Entity/User.php
@@ -125,6 +125,19 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
[...]
+          // @todo: this doesn't work, and flood doesn't have a method to
+          // clear entries by prefix.
+          // Otherwise use the uid and IP address.

Not too sure about this @todo, or at least the phrasing. For me now it looks like the next lines won't work (which they do) and I don't see a real action about what needs to be done in the @todo.

I, disclaimer: Not a native speaker, would leave out the 2 lines of the @todo all together and rephrase it into something like

+          // Since flood doesn't have a method to clear entries by prefix, we 
+          // use the uid and IP address.

2.

--- a/core/modules/user/tests/src/Functional/UserPasswordResetTest.php
+++ b/core/modules/user/tests/src/Functional/UserPasswordResetTest.php
@@ -485,6 +485,56 @@ public function testUserResetPasswordUserFloodControlIsCleared() {
+    // The next request should *not* trigger flood control, since the
+    // password change should have cleared flood events for this user.
[...]
+    $this->drupalGet('user/login');
+    $this->submitForm($edit, 'Log in');
+    $this->assertSession()->pageTextNotContains('There have been more than 3 failed login attempts for this account. It is temporarily blocked.');

We now have successfully confirmed the error message isn't there any more, but still haven't got confirmation the previously-flood-blocked user has now successfully logged in. I think we need to add a $this->assertSession()->pageTextContains() looking for text that is there after a successful login?

catch’s picture

Status: Needs review » Needs work

We can't remove the @todo but it needs a better explanation and linking to an issue.

+++ b/core/modules/user/src/Entity/User.php
@@ -125,6 +125,19 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
+        }
+        else {
+          // @todo: this doesn't work, and flood doesn't have a method to
+          // clear entries by prefix.
+          // Otherwise use the uid and IP address.
+          $identifier = $this->id() . '-' . \Drupal::request()->getClientIP();

The problem here is it really doesn't work - the IP address of the administrator isn't going to be the same as the IP address of the person trying to log in, so we'll be clearing a flood entry that doesn't exist, and leaving the one that's blocking login.

I think we need a follow-up for this (which might depend on adding a 'clearByPrefix' method to the flood API).

paulocs’s picture

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

voleger’s picture

Issue tags: +Needs reroll
ayush.khare’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new3.28 KB
new3.61 KB

Reroll for #32.

Status: Needs review » Needs work

The last submitted patch, 42: 2881572-42.patch, failed testing. View results

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

bhanu951’s picture

Assigned: Unassigned » bhanu951

bhanu951’s picture

Assigned: bhanu951 » Unassigned
Status: Needs work » Needs review
StatusFileSize
new3.61 KB
new68 bytes
deepalij’s picture

StatusFileSize
new187.51 KB
new63.18 KB

Able to reproduce the issue by using the steps in IS
Tested and applied patch #47 on Drupal 10.1.x-dev
The patch applied cleanly.

The patch did work and the issue got resolved after applying the patch.
The user login flood lock gets cleared now after resetting the password by admin. And the user is able to login with the new password successfully.

Refer to the attached screenshots

catch’s picture

acbramley’s picture

Status: Needs review » Needs work

I'd also agree with #35 that we should assert something positive

spokje’s picture

Maybe we can postpone this on #3225354: Add a clearByPrefix() method to the flood API (which is currently RTBC) so we can truly say "User login flood lock doesn't clear when reset password as admin" is solved when we commit this?

spokje’s picture

catch’s picture

#3225354: Add a clearByPrefix() method to the flood API is in, so we can drop the @todo here and use the new method instead. Will need an instanceof check.

paulocs’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Reviewing MR 3241 - hiding the files to avoid confusion

Following the steps in the issue summary I can confirm the issue
Applying the patch fixes it.

MR has no open threads.
Test-only patch shows the test coverage works.

Looks good.

lauriii’s picture

Status: Reviewed & tested by the community » Needs work

Posted few comments on the MR

bhanu951’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work

Left a comment on MR.

paulocs’s picture

Status: Needs work » Needs review
bhanu951’s picture

I believe this is more confirmative that user is logged in. Than just asserting the response message on page.

$this->drupalGet('user/' . $this->account->id() . '/edit');
$this->assertSession()->statusCodeEquals(200);
smustgrave’s picture

Status: Needs review » Needs work

Or could just use

$this->assertSession()->addressEquals('user/' . $this->account->id());

Don't have to check response message or do an unnecessary drupalGet

And @paulocs sorry didn't respond to the last message

bhanu951’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

Left a couple of questions/suggestions on the MR

bhanu951’s picture

Updated test as per review comment.

> Should we make sure the user isn't being blocked and that is what triggered the save event? I'm not sure blocking the user should clear flood entries.

I will leave it for more knowledgable person to answer it.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bhanu951’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

bhanu951’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new90 bytes

The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

bhanu951’s picture

StatusFileSize
new3.79 KB

Rebased MR #3241 to 11.x , attached patch for backup.

bhanu951’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Think this is ready for committer review.

Though wonder if there is any security implication.

  • catch committed 57a9309a on 11.x
    Issue #2881572 by Bhanu951, paulocs, catch, Spokje, munish.kumar,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

There sort of is but it's fine - if a user completely locks themselves out of their account, then they might contact an admin to reset their password, and once they've done so, that user needs to be able to log in again without waiting x amount of time.

The only security implication is that an admin edits a user account that's for some reason already locked out as a co-incidence, and this wipes the flood attempts - but we still have flood control on the account overall, so it'll just kick in again on the next three incorrect attempts. As a user admin you'd have to continually be resaving user accounts to bypass it overall.

Status: Fixed » Closed (fixed)

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