Problem/Motivation
This issue is a follow-up of #2881572: User login flood lock doesn't clear when reset password as admin.
Currently it is not possible to clear the flood based on the flood identifier prefix.
Proposed resolution
Implement a method clearByPrefix to the Flood API so it will be possible to clear a flood register by the identifier prefix.
| Comment | File | Size | Author |
|---|
Issue fork drupal-3225354
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
Comment #2
paulocsOn it.
Comment #3
paulocsA patch for it.
I'm not sure about
MemoryBackend::clearByPrefix.Please review.
Comment #4
paulocsComment #5
berdirFWIW, this might not be easy to implement for alternative backends. Or at least not efficient. Redis for example.
Comment #6
daffie commentedCan we add typehints: Something like:
clearByPrefix(string $name, string $prefix): void;Comment #7
eddie_c commentedA new patch which includes tests and suggestions from @daffie.
I also made a change to MemoryBackend::clearByPrefix: The second parameter passed to explode() was an array, not a string. It's the key of the array we are interested in exploding I think.
Comment #8
eddie_c commentedComment #9
daffie commentedLets see if a patch with only the added tests fails.
Comment #11
daffie commentedBack to needs review.
I will do a review later.
@eddie_c: Thanks for writing the tests.
Comment #12
paulocsShould we provide a data provider for the test functions created to avoid duplicated code?
I'm talking about:
Comment #13
catchThis is the same problem we had with cache backends, which eventually led to cache tags.
The use case is #2881572: User login flood lock doesn't clear when reset password as admin where we want to remove flood entries for a user, but the flood entries include their IP address which we don't know.
Comment #14
berdirYeah, and I assume currently we'd be forced to implement similar workarounds like we had to in the memcache module in D7, store prefix deletions and then check them on read.
Comment #15
daffie commentedThe patch look good to me. Just a couple of minor remarks:
Can we change
'-%'to'%'or is there a very good reason why the dash is added?This looks wrong to me. Should we not do the explode() first and then get the first element with reset()?
The 2 new test method are almost the same. The only difference I can see is MemoryBackend vs. DatabaseBackend. Can we change it to a single test method with a dataProvider method. See: https://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html#writing-... for more info or do a code base search for "dataProvider".
Comment #16
berdirChecking the implementations again. the thing with - is coming from this:
```
MariaDB [d8_import]> select * from flood;
+-----+------------------------+-------------+------------+------------+
| fid | event | identifier | timestamp | expiration |
+-----+------------------------+-------------+------------+------------+
| 3 | user.failed_login_ip | 127.0.0.1 | 1628326866 | 1628330466 |
| 4 | user.failed_login_ip | 127.0.0.1 | 1628326873 | 1628330473 |
| 5 | user.failed_login_user | 1-127.0.0.1 | 1628326873 | 1628348473 |
+-----+------------------------+-------------+------------+------------+
```
The user/ip combination uses - as separator. However, right now, that is an implementation detail, the flood API does, so far, not care at all what's in the identifier.
I like limiting this API somewhat. It makes more more feasible to implement in alternative backends that have no wildcard support. A bit like the memory backend, although we can expect that the number of entries there is very limited and it should IMHO be a lot easier to implement by just checking each array key.
However, in that case, this needs to be more clearly documented. We should document on the existing methods, or somewhere central that the identifier may consist of two sections split by a -. And that flood backends can optionally (at least for BC) support clearing by the first section. We should also put the new method on an optional interface I guess so that implementations can check whether that is supported.
Comment #17
catch#16 should help alternative backends a lot, also agreed we should add a new optional interface for this.
Comment #18
eddie_c commentedThis patch reduces the duplication in tests. Still need to look at #15 point 2 and @Berdir's suggestion of adding an optional interface.
Comment #19
daffie commented@eddie_c: Could you also post an interdiff file. It makes review your changes a lot easier.
Comment #20
eddie_c commented@daffie Here's an updated patch with interdiff to the patch in #7.
The patch includes:
- The optional interface for the clearByPrefix method
- A correction which addresses your point in #15.2
Still to add: documentation
Comment #21
paulocsUnused statement
use Drupal\Core\Flood\FloodInterface;inFloodTest.php.Comment #22
eddie_c commentedRemoving unused use statement
Comment #23
eddie_c commented...And correcting misspelling in comment
Comment #24
paulocsI was working in this issue and I'm not sure about the solution proposed.I think it makes sense to add that interface because its a block for #2881572: User login flood lock doesn't clear when reset password as admin. The user module set a flood control register that its not possible to be cleared later and its causing the issue.
But maybe the right class to implement that interface its not the
MemoryBackendor theDatabaseBackendbecause we can not ensure that all identifiers will be separated by a hyphen. We are now considering that approach because the user module set the identifier like:$identifier = $account->id() . '-' . $this->getRequest()->getClientIP();IMHO the right thing to do in this issue is:1) Create the interface
PrefixFloodInterface2) Let the
MemoryBackendand theDatabaseBackendas it is because we can not ensure that the prefix will be always separated with a hyphen because that is not their responsibility.3) Make the
UserFloodControlInterfaceimplement thePrefixFloodInterface4) Write the method to clear the flood by prefix.
Please let me know if you agree...
Comment #25
paulocsI added some documentation in
FloodInterface::register().Comment #26
eddie_c commentedThanks for the docs @paulocs. I wonder if they could be made a little bit clearer. Something like this would expand on what you wrote:
Comment #32
bhanu951 commentedCreated a MR against 10.x branch and updated the comment description as per #26.
Comment #34
spokjeRebased but didn't touch anything else, which (probably?) leaves me eligible to RTBC.
- Code changes make sense to me.
- All remarks made seem to be addressed.
- Green testbot
RTBC for me.
Comment #35
catchThis needs a change record for the new interface.
Comment #36
spokjeDraft CR added.
Comment #37
spokjeComment #39
catchLooks good now, hopefully the '-' prefix limitation will help Redis et al implement support for this without too much pain.
Committed be7d65e and pushed to 10.1.x. Thanks!
Comment #40
bhanu951 commentedSeems we forgot to publish CR. Published it.