Skip to content

Commit d0bfcce

Browse files
committed
Revert "minor symfony#46618 [Security] Centralize max username length enforcement (chalasr)"
This reverts commit aaa821b, reversing changes made to 292f6da.
1 parent e9a404f commit d0bfcce

File tree

6 files changed

+20
-9
lines changed

6 files changed

+20
-9
lines changed

src/Symfony/Component/Security/Core/Security.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ class Security implements AuthorizationCheckerInterface
4646
public const LAST_USERNAME = '_security.last_username';
4747

4848
/**
49-
* @deprecated since Symfony 6.2, use \Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge::MAX_USERNAME_LENGTH instead
49+
* @deprecated since Symfony 6.2, use \Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface::MAX_USERNAME_LENGTH instead
50+
*
51+
* In 7.0, move this constant to the NewSecurityHelper class and make it reference AuthenticatorInterface:MAX_USERNAME_LENGTH.
5052
*/
5153
public const MAX_USERNAME_LENGTH = 4096;
5254

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
*/
2727
interface AuthenticatorInterface
2828
{
29+
public const MAX_USERNAME_LENGTH = 4096;
30+
2931
/**
3032
* Does the authenticator support the given Request?
3133
*

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ private function getCredentials(Request $request): array
132132

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

135+
if (\strlen($credentials['username']) > self::MAX_USERNAME_LENGTH) {
136+
throw new BadCredentialsException('Invalid username.');
137+
}
138+
135139
$request->getSession()->set(SecurityRequestAttributes::LAST_USERNAME, $credentials['username']);
136140

137141
return $credentials;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ private function getCredentials(Request $request)
150150
if (!\is_string($credentials['username'])) {
151151
throw new BadRequestHttpException(sprintf('The key "%s" must be a string.', $this->options['username_path']));
152152
}
153+
154+
if (\strlen($credentials['username']) > self::MAX_USERNAME_LENGTH) {
155+
throw new BadCredentialsException('Invalid username.');
156+
}
153157
} catch (AccessException $e) {
154158
throw new BadRequestHttpException(sprintf('The key "%s" must be provided.', $this->options['username_path']), $e);
155159
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
2020
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
2121
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
22+
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
2223
use Symfony\Component\Security\Http\Authenticator\FormLoginAuthenticator;
2324
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
2425
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PasswordUpgradeBadge;
25-
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
2626
use Symfony\Component\Security\Http\HttpUtils;
2727
use Symfony\Component\Security\Http\Tests\Authenticator\Fixtures\PasswordUpgraderProvider;
2828

@@ -50,7 +50,7 @@ public function testHandleWhenUsernameLength($username, $ok)
5050
$this->expectNotToPerformAssertions();
5151
} else {
5252
$this->expectException(BadCredentialsException::class);
53-
$this->expectExceptionMessage('Username too long.');
53+
$this->expectExceptionMessage('Invalid username.');
5454
}
5555

5656
$request = Request::create('/login_check', 'POST', ['_username' => $username, '_password' => 's$cr$t']);
@@ -62,8 +62,8 @@ public function testHandleWhenUsernameLength($username, $ok)
6262

6363
public function provideUsernamesForLength()
6464
{
65-
yield [str_repeat('x', UserBadge::MAX_USERNAME_LENGTH + 1), false];
66-
yield [str_repeat('x', UserBadge::MAX_USERNAME_LENGTH - 1), true];
65+
yield [str_repeat('x', AuthenticatorInterface::MAX_USERNAME_LENGTH + 1), false];
66+
yield [str_repeat('x', AuthenticatorInterface::MAX_USERNAME_LENGTH - 1), true];
6767
}
6868

6969
/**

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\Security\Core\User\InMemoryUserProvider;
2020
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
2121
use Symfony\Component\Security\Http\Authenticator\JsonLoginAuthenticator;
22-
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
2322
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
2423
use Symfony\Component\Security\Http\HttpUtils;
2524
use Symfony\Component\Translation\Loader\ArrayLoader;
@@ -122,9 +121,9 @@ public function provideInvalidAuthenticateData()
122121
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], '{"username": "dunglas", "password": 1}');
123122
yield [$request, 'The key "password" must be a string.'];
124123

125-
$username = str_repeat('x', UserBadge::MAX_USERNAME_LENGTH + 1);
126-
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], sprintf('{"username": "%s", "password": "foo"}', $username));
127-
yield [$request, 'Username too long.', BadCredentialsException::class];
124+
$username = str_repeat('x', AuthenticatorInterface::MAX_USERNAME_LENGTH + 1);
125+
$request = new Request([], [], [], [], [], ['HTTP_CONTENT_TYPE' => 'application/json'], sprintf('{"username": "%s", "password": 1}', $username));
126+
yield [$request, 'Invalid username.', BadCredentialsException::class];
128127
}
129128

130129
public function testAuthenticationFailureWithoutTranslator()

0 commit comments

Comments
 (0)