Skip to content

Commit 1a80ed6

Browse files
author
Bikal Basnet
committed
[Security] Don't allow empty username or empty password
1 parent 84d35a2 commit 1a80ed6

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/Symfony/Component/Security/Http/Authenticator/JsonLoginAuthenticator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ private function getCredentials(Request $request)
147147
try {
148148
$credentials['username'] = $this->propertyAccessor->getValue($data, $this->options['username_path']);
149149

150-
if (!\is_string($credentials['username'])) {
151-
throw new BadRequestHttpException(sprintf('The key "%s" must be a string.', $this->options['username_path']));
150+
if (!\is_string($credentials['username']) || $credentials['username'] === "") {
151+
throw new BadRequestHttpException(sprintf('The key "%s" must be a non empty string.', $this->options['username_path']));
152152
}
153153

154154
if (\strlen($credentials['username']) > Security::MAX_USERNAME_LENGTH) {
@@ -161,8 +161,8 @@ private function getCredentials(Request $request)
161161
try {
162162
$credentials['password'] = $this->propertyAccessor->getValue($data, $this->options['password_path']);
163163

164-
if (!\is_string($credentials['password'])) {
165-
throw new BadRequestHttpException(sprintf('The key "%s" must be a string.', $this->options['password_path']));
164+
if (!\is_string($credentials['password']) || $credentials['password'] === "") {
165+
throw new BadRequestHttpException(sprintf('The key "%s" must be a non empty string.', $this->options['password_path']));
166166
}
167167
} catch (AccessException $e) {
168168
throw new BadRequestHttpException(sprintf('The key "%s" must be provided.', $this->options['password_path']), $e);

src/Symfony/Component/Security/Http/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Require non-empty-string username and password when using `JsonLoginAuthenticator`
8+
49
6.0
510
---
611

src/Symfony/Component/Security/Http/Tests/Authenticator/JsonLoginAuthenticatorTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,16 @@ public function provideInvalidAuthenticateData()
116116
yield [$request, 'The key "password" must be provided'];
117117

118118
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": 1, "password": "foo"}');
119-
yield [$request, 'The key "username" must be a string.'];
119+
yield [$request, 'The key "username" must be a non empty string.'];
120120

121121
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": 1}');
122-
yield [$request, 'The key "password" must be a string.'];
122+
yield [$request, 'The key "password" must be a non empty string.'];
123+
124+
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "", "password": ""}');
125+
yield [$request, 'The key "username" must be a non empty string.'];
126+
127+
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "rimas", "password": ""}');
128+
yield [$request, 'The key "password" must be a non empty string.'];
123129

124130
$username = str_repeat('x', Security::MAX_USERNAME_LENGTH + 1);
125131
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], sprintf('{"username": "%s", "password": 1}', $username));

0 commit comments

Comments
 (0)