Skip to content

[Security] Deprecate empty user identifier #58007

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
Aug 19, 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
2 changes: 2 additions & 0 deletions UPGRADE-7.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Security

* Add `$token` argument to `UserCheckerInterface::checkPostAuth()`
* Deprecate argument `$secret` of `RememberMeToken` and `RememberMeAuthenticator`
* Deprecate passing an empty string as `$userIdentifier` argument to `UserBadge` constructor
* Deprecate returning an empty string in `UserInterface::getUserIdentifier()`

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

* Add `$token` argument to `UserCheckerInterface::checkPostAuth()`
* Deprecate argument `$secret` of `RememberMeToken`
* Deprecate returning an empty string in `UserInterface::getUserIdentifier()`

7.0
---
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Security/Core/User/UserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function eraseCredentials(): void;

/**
* Returns the identifier for this user (e.g. username or email address).
*
* @return non-empty-string
*/
public function getUserIdentifier(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function __construct(
?callable $userLoader = null,
private ?array $attributes = null,
) {
if ('' === $userIdentifier) {
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.');
// throw new BadCredentialsException('Empty user identifier.');
}

if (\strlen($userIdentifier) > self::MAX_USERNAME_LENGTH) {
throw new BadCredentialsException('Username too long.');
}
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

* Pass the current token to the `checkPostAuth()` method of user checkers
* Deprecate argument `$secret` of `RememberMeAuthenticator`
* Deprecate passing an empty string as `$userIdentifier` argument to `UserBadge` constructor

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,29 @@
namespace Symfony\Component\Security\Http\Tests\Authenticator\Passport\Badge;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectUserDeprecationMessageTrait;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;

class UserBadgeTest extends TestCase
{
use ExpectUserDeprecationMessageTrait;

public function testUserNotFound()
{
$badge = new UserBadge('dummy', fn () => null);
$this->expectException(UserNotFoundException::class);
$badge->getUser();
}

/**
* @group legacy
*/
public function testEmptyUserIdentifier()
{
$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.');
// $this->expectException(BadCredentialsException::class)
new UserBadge('', fn () => null);
}
}
Loading