Skip to content

[Security] Ignore empty username or password login attempts #53851

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 1 commit into from
Mar 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,20 @@ private function getCredentials(Request $request): array

$credentials['username'] = trim($credentials['username']);

if ('' === $credentials['username']) {
throw new BadRequestHttpException(sprintf('The key "%s" must be a non-empty string.', $this->options['username_parameter']));
}

$request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $credentials['username']);

if (!\is_string($credentials['password']) && (!\is_object($credentials['password']) || !method_exists($credentials['password'], '__toString'))) {
throw new BadRequestHttpException(sprintf('The key "%s" must be a string, "%s" given.', $this->options['password_parameter'], \gettype($credentials['password'])));
}

if ('' === (string) $credentials['password']) {
throw new BadRequestHttpException(sprintf('The key "%s" must be a non-empty string.', $this->options['password_parameter']));
}

return $credentials;
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Security/Http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Add `#[IsCsrfTokenValid]` attribute
* Add CAS 2.0 access token handler
* Make empty username or empty password on form login attempts return Bad Request (400)

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ protected function setUp(): void
$this->failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class);
}

public function testHandleWhenUsernameEmpty()
{
$this->expectException(BadRequestHttpException::class);
$this->expectExceptionMessage('The key "_username" must be a non-empty string.');

$request = Request::create('/login_check', 'POST', ['_username' => '', '_password' => 's$cr$t']);
$request->setSession($this->createSession());

$this->setUpAuthenticator();
$this->authenticator->authenticate($request);
}

public function testHandleWhenPasswordEmpty()
{
$this->expectException(BadRequestHttpException::class);
$this->expectExceptionMessage('The key "_password" must be a non-empty string.');

$request = Request::create('/login_check', 'POST', ['_username' => 'foo', '_password' => '']);
$request->setSession($this->createSession());

$this->setUpAuthenticator();
$this->authenticator->authenticate($request);
}

/**
* @dataProvider provideUsernamesForLength
*/
Expand Down