Skip to content

Commit 92f9cc6

Browse files
committed
Merge branch '7.3' into 7.4
* 7.3: fix test Restore Relay 8.5 test account for PHP_ZTS being a boolean value on PHP 8.4+ [Intl] Update data to ICU 78.1 [Notifier][Smsbox] Add tests for `Mode` enum [DependencyInjection] Remove unused variable [Console] Fix exception message when abbreviation matches multiple hidden commands [FrameworkBundle] Fix TypeError when traversing scalar values in debug:config [DependencyInjection] Fix loop corruption in CheckTypeDeclarationsPass [Security] Fix UserBadge validation bypass via identifier normalizer [DependencyInjection] Fix invalid PHP syntax for nullable TypedReference in PhpDumper Fix typo in comment [Translation][Routing] Fix typos [Config] Fix nullable EnumNode with BackedEnum [String] Fix normalization in trimPrefix/trimSuffix
2 parents 296007d + 64b65f2 commit 92f9cc6

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

Authenticator/Passport/Badge/UserBadge.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,8 @@ public function __construct(
5454
private ?array $attributes = null,
5555
?\Closure $identifierNormalizer = null,
5656
) {
57-
if ('' === $userIdentifier) {
58-
trigger_deprecation('symfony/security-http', '7.2', 'Using an empty string as user identifier is deprecated and will throw an exception in Symfony 8.0.');
59-
// throw new BadCredentialsException('Empty user identifier.');
60-
}
57+
$this->validateUserIdentifier($userIdentifier);
6158

62-
if (\strlen($userIdentifier) > self::MAX_USERNAME_LENGTH) {
63-
throw new BadCredentialsException('Username too long.');
64-
}
6559
if ($identifierNormalizer) {
6660
$this->identifierNormalizer = static fn () => $identifierNormalizer($userIdentifier);
6761
}
@@ -74,6 +68,8 @@ public function getUserIdentifier(): string
7468
if (isset($this->identifierNormalizer)) {
7569
$this->userIdentifier = ($this->identifierNormalizer)();
7670
$this->identifierNormalizer = null;
71+
72+
$this->validateUserIdentifier($this->userIdentifier);
7773
}
7874

7975
return $this->userIdentifier;
@@ -132,4 +128,16 @@ public function isResolved(): bool
132128
{
133129
return true;
134130
}
131+
132+
private function validateUserIdentifier(string $userIdentifier): void
133+
{
134+
if ('' === $userIdentifier) {
135+
trigger_deprecation('symfony/security-http', '7.2', 'Using an empty string as user identifier is deprecated and will throw an exception in Symfony 8.0.');
136+
// throw new BadCredentialsException('Empty user identifier.');
137+
}
138+
139+
if (\strlen($userIdentifier) > self::MAX_USERNAME_LENGTH) {
140+
throw new BadCredentialsException('Username too long.');
141+
}
142+
}
135143
}

Tests/Authenticator/Passport/Badge/UserBadgeTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\Attributes\Group;
1616
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
1717
use PHPUnit\Framework\TestCase;
18+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1819
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
1920
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
2021
use Symfony\Component\String\Slugger\AsciiSlugger;
@@ -66,4 +67,25 @@ public static function provideUserIdentifierNormalizationData(): iterable
6667
yield 'Greek to ASCII' => ['ΝιΚόΛΑος', 'NIKOLAOS', $upperAndAscii];
6768
yield 'Katakana to ASCII' => ['たなかそういち', 'TANAKASOUICHI', $upperAndAscii];
6869
}
70+
71+
#[IgnoreDeprecations]
72+
#[Group('legacy')]
73+
public function testUserIdentifierNormalizationTriggersDeprecationForEmptyString()
74+
{
75+
$badge = new UserBadge('valid_input', null, null, fn () => '');
76+
77+
$this->expectUserDeprecationMessage('Since symfony/security-http 7.2: Using an empty string as user identifier is deprecated and will throw an exception in Symfony 8.0.');
78+
79+
$this->assertSame('', $badge->getUserIdentifier());
80+
}
81+
82+
public function testUserIdentifierNormalizationEnforcesMaxLength()
83+
{
84+
$badge = new UserBadge('valid_input', null, null, fn () => str_repeat('a', UserBadge::MAX_USERNAME_LENGTH + 1));
85+
86+
$this->expectException(BadCredentialsException::class);
87+
$this->expectExceptionMessage('Username too long.');
88+
89+
$badge->getUserIdentifier();
90+
}
6991
}

0 commit comments

Comments
 (0)