Skip to content

[Security] Return default value instead of deferring to lower prio resolvers when using #[CurrentUser] and no user is found #49034

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
Jan 19, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public function supports(Request $request, ArgumentMetadata $argument): bool
return false;
}

// if no user is present but a default value exists we delegate to DefaultValueResolver
if ($argument->hasDefaultValue() && null === $this->tokenStorage->getToken()?->getUser()) {
return false;
}

return true;
}

Expand All @@ -62,20 +57,21 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
if (UserInterface::class !== $argument->getType() && !$argument->getAttributesOfType(CurrentUser::class, ArgumentMetadata::IS_INSTANCEOF)) {
return [];
}
$user = $this->tokenStorage->getToken()?->getUser();

// if no user is present but a default value exists we delegate to DefaultValueResolver
if ($argument->hasDefaultValue() && null === $user) {
return [];
}
if (null === $user = $this->tokenStorage->getToken()?->getUser()) {
// if no user is present but a default value exists we use it to prevent the EntityValueResolver or others
// from attempting resolution of the User as the current logged in user was requested here
if ($argument->hasDefaultValue()) {
return [$argument->getDefaultValue()];
}

if (null === $user) {
if (!$argument->isNullable()) {
throw new AccessDeniedException(sprintf('There is no logged-in user to pass to $%s, make the argument nullable if you want to allow anonymous access to the action.', $argument->getName()));
}

return [null];
}

if (null === $argument->getType() || $user instanceof ($argument->getType())) {
return [$user];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class UserValueResolverTest extends TestCase
{
/**
* In Symfony 7, keep this test case but remove the call to supports()
* In Symfony 7, keep this test case but remove the call to supports().
*
* @group legacy
*/
Expand All @@ -43,18 +43,18 @@ public function testSupportsFailsWithNoType()
}

/**
* In Symfony 7, keep this test case but remove the call to supports()
* In Symfony 7, keep this test case but remove the call to supports().
*
* @group legacy
*/
public function testSupportsFailsWhenDefaultValAndNoUser()
{
$tokenStorage = new TokenStorage();
$resolver = new UserValueResolver($tokenStorage);
$metadata = new ArgumentMetadata('foo', UserInterface::class, false, true, new InMemoryUser('username', 'password'));
$metadata = new ArgumentMetadata('foo', UserInterface::class, false, true, $default = new InMemoryUser('username', 'password'));

$this->assertSame([], $resolver->resolve(Request::create('/'), $metadata));
$this->assertFalse($resolver->supports(Request::create('/'), $metadata));
$this->assertSame([$default], $resolver->resolve(Request::create('/'), $metadata));
$this->assertTrue($resolver->supports(Request::create('/'), $metadata));
}

public function testResolveSucceedsWithUserInterface()
Expand Down