Skip to content

Commit b037319

Browse files
committed
bug symfony#40286 [Security] #[CurrentUser] arguments should resolve to null for "anon." (chalasr)
This PR was merged into the 5.2 branch. Discussion ---------- [Security] #[CurrentUser] arguments should resolve to null for "anon." | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - The UserValueResolver should only resolve `UserInterface` (or subtype) typed arguments: https://github.com/symfony/symfony/blob/bc9e946a56b3874b01d363772f396d8db879de8d/src/Symfony/Component/Security/Http/Controller/UserValueResolver.php#L54-L55 When using the `#CurrentUser` attribute with an AnonymousToken in the storage, the resolved argument value is `anon.`. This PR fixes it. /cc @jvasseur Commits ------- 8d3078d [Security] #[CurrentUser] argument should resolve to null when it is anonymous
2 parents 3a2906c + 8d3078d commit b037319

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/Symfony/Component/Security/Http/Controller/UserValueResolver.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,9 @@ public function __construct(TokenStorageInterface $tokenStorage)
3535

3636
public function supports(Request $request, ArgumentMetadata $argument): bool
3737
{
38-
if ($argument->getAttribute() instanceof CurrentUser) {
39-
return true;
40-
}
41-
42-
// only security user implementations are supported
43-
if (UserInterface::class !== $argument->getType()) {
38+
// with the attribute, the type can be any UserInterface implementation
39+
// otherwise, the type must be UserInterface
40+
if (UserInterface::class !== $argument->getType() && !$argument->getAttribute() instanceof CurrentUser) {
4441
return false;
4542
}
4643

src/Symfony/Component/Security/Http/Tests/Controller/UserValueResolverTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ public function testResolveWithAttribute()
8383
$this->assertSame([$user], iterator_to_array($resolver->resolve(Request::create('/'), $metadata)));
8484
}
8585

86+
public function testResolveWithAttributeAndNoUser()
87+
{
88+
$tokenStorage = new TokenStorage();
89+
$tokenStorage->setToken(new UsernamePasswordToken('username', 'password', 'provider'));
90+
91+
$resolver = new UserValueResolver($tokenStorage);
92+
$metadata = new ArgumentMetadata('foo', null, false, false, null, false, new CurrentUser());
93+
94+
$this->assertFalse($resolver->supports(Request::create('/'), $metadata));
95+
}
96+
8697
public function testIntegration()
8798
{
8899
$user = $this->createMock(UserInterface::class);

0 commit comments

Comments
 (0)