Skip to content

Commit a8c6e8c

Browse files
llupachalasr
authored andcommitted
[Security] Ignore empty username or password login attempts
1 parent 90c1e44 commit a8c6e8c

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Authenticator/FormLoginAuthenticator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,20 @@ private function getCredentials(Request $request): array
129129

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

132+
if ('' === $credentials['username']) {
133+
throw new BadRequestHttpException(sprintf('The key "%s" must be a non-empty string.', $this->options['username_parameter']));
134+
}
135+
132136
$request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $credentials['username']);
133137

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

142+
if ('' === (string) $credentials['password']) {
143+
throw new BadRequestHttpException(sprintf('The key "%s" must be a non-empty string.', $this->options['password_parameter']));
144+
}
145+
138146
return $credentials;
139147
}
140148

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

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

1011
7.0
1112
---

Tests/Authenticator/FormLoginAuthenticatorTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ protected function setUp(): void
4242
$this->failureHandler = $this->createMock(AuthenticationFailureHandlerInterface::class);
4343
}
4444

45+
public function testHandleWhenUsernameEmpty()
46+
{
47+
$this->expectException(BadRequestHttpException::class);
48+
$this->expectExceptionMessage('The key "_username" must be a non-empty string.');
49+
50+
$request = Request::create('/login_check', 'POST', ['_username' => '', '_password' => 's$cr$t']);
51+
$request->setSession($this->createSession());
52+
53+
$this->setUpAuthenticator();
54+
$this->authenticator->authenticate($request);
55+
}
56+
57+
public function testHandleWhenPasswordEmpty()
58+
{
59+
$this->expectException(BadRequestHttpException::class);
60+
$this->expectExceptionMessage('The key "_password" must be a non-empty string.');
61+
62+
$request = Request::create('/login_check', 'POST', ['_username' => 'foo', '_password' => '']);
63+
$request->setSession($this->createSession());
64+
65+
$this->setUpAuthenticator();
66+
$this->authenticator->authenticate($request);
67+
}
68+
4569
/**
4670
* @dataProvider provideUsernamesForLength
4771
*/

0 commit comments

Comments
 (0)