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 all commits
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
< 8000 td class="blob-num blob-num-deletion empty-cell">
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,48 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin(array $optio
}

/**
* @dataProvider provideInvalidCredentials
* @group time-sensitive
*/
public function testLoginThrottling($username, $password)
public function testLoginThrottling()
{
if (!class_exists(LoginThrottlingListener::class)) {
$this->markTestSkipped('Login throttling requires symfony/security-http:^5.2');
}

$client = $this->createClient(['test_case' => 'StandardFormLogin', 'root_config' => 'login_throttling.yml', 'enable_authenticator_manager' => true]);

$form = $client->request('GET', '/login')->selectButton('login')->form();
$form['_username'] = $username;
$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);
}

public function provideInvalidCredentials()
{
yield 'invalid_password' => ['johannes', 'wrong'];
yield 'invalid_username' => ['wrong', 'wrong'];
$attempts = [
['johannes', 'wrong'],
['johannes', 'also_wrong'],
['wrong', 'wrong'],
['johannes', 'wrong_again'],
];
foreach ($attempts as $i => $attempt) {
$form = $client->request('GET', '/login')->selectButton('login')->form();
$form['_username'] = $attempt[0];
$form['_password'] = $attempt[1];
$client->submit($form);

$text = $client->followRedirect()->text(null, true);
switch ($i) {
case 0: // First attempt : Invalid credentials (OK)
$this->assertStringContainsString('Invalid credentials', $text, 'Invalid response on 1st attempt');

break;
case 1: // Second attempt : login throttling !
$this->assertStringContainsString('Too many failed login attempts, please try again in 8 minutes.', $text, 'Invalid response on 2nd attempt');

break;
case 2: // Third attempt with unexisting username
$this->assertStringContainsString('Username could not be found.', $text, 'Invalid response on 3rd attempt');

break;
case 3: // Fourth attempt : still login throttling !
$this->assertStringContainsString('Too many failed login attempts, please try again in 8 minutes.', $text, 'Invalid response on 4th attempt');

break;
}
}
}

public function provideClientOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ security:
default:
login_throttling:
max_attempts: 1
interval: '8 minutes'
0