8000 [RateLimiter][Security] Add a `login_throttling.interval` (in `security.firewalls`) option to change the default throttling interval. by damienfa · Pull Request #40339 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[RateLimiter][Security] Add a login_throttling.interval (in security.firewalls) option to change the default throttling interval. #40339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* [BC break] Add `login_throttling.lock_factory` setting defaulting to `null` (instead of `lock.factory`)
* Add a `login_throttling.interval` (in `security.firewalls`) option to change the default throttling interval.
* Add the `debug:firewall` command.
* Deprecate `UserPasswordEncoderCommand` class and the corresponding `user:encode-password` command,
use `UserPasswordHashCommand` and `user:hash-password` instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function addConfiguration(NodeDefinition $builder)
->children()
->scalarNode('limiter')->info(sprintf('A service id implementing "%s".', RequestRateLimiterInterface::class))->end()
->integerNode('max_attempts')->defaultValue(5)->end()
->scalarNode('interval')->defaultValue('1 minute')->end()
->scalarNode('lock_factory')->info('The service ID of the lock factory used by the login rate limiter (or null to disable locking)')->defaultNull()->end()
->end();
}
Expand All @@ -76,7 +77,7 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
$limiterOptions = [
'policy' => 'fixed_window',
'limit' => $config['max_attempts'],
'interval' => '1 minute',
'interval' => $config['interval'],
'lock_factory' => $config['lock_factory'],
];
FrameworkExtension::registerRateLimiter($container, $localId = '_login_local_'.$firewallName, $limiterOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin(array $optio
* @dataProvider provideInvalidCredentials
* @group time-sensitive
*/
public function testLoginThrottling($username, $password)
public function testLoginThrottling(string $username, string $password, int $attemptIndex)
{
if (!class_exists(LoginThrottlingListener::class)) {
$this->markTestSkipped('Login throttling requires symfony/security-http:^5.2');
Expand All @@ -125,19 +125,28 @@ public function testLoginThrottling($username, $password)
$form['_password'] = $password;
$client->submit($form);

$client->followRedirect()->selectButton('login')->form();
$form['_username'] = $username;
$form['_password'] = $password;
$client->submit($form);

$text = $client->followRedirect()->text(null, true);
$this->assertStringMatchesFormat('%sToo many failed login attempts, please try again in %d minute%s', $text);
if (1 === $attemptIndex) {
// First attempt : Invalid credentials (OK)
$this->assertStringMatchesFormat('%sInvalid credentials%s', $text);
} elseif (2 === $attemptIndex) {
// Second attempt : login throttling !
$this->assertStringMatchesFormat('%sToo many failed login attempts, please try again in 8 minutes%s', $text);
} elseif (3 === $attemptIndex) {
// Third attempt with unexisting username
$this->assertStringMatchesFormat('%sUsername could not be found.%s', $text);
} elseif (4 === $attemptIndex) {
// Fourth attempt : still login throttling !
$this->assertStringMatchesFormat('%sToo many failed login attempts, please try again in 8 minutes%s', $text);
}
}

public function provideInvalidCredentials()
{
yield 'invalid_password' => ['johannes', 'wrong'];
yield 'invalid_username' => ['wrong', 'wrong'];
yield 'invalid_password' => ['johannes', 'wrong', 1];
yield 'invalid_password_again' => ['johannes', 'also_wrong', 2];
yield 'invalid_username' => ['wrong', 'wrong', 3];
yield 'invalid_password_again_bis' => ['johannes', 'wrong_again', 4];
}

public function provideClientOptions()
Expand Down
0