Skip to content

Fix isAnonymous() always returning false on Symfony 6.x#1240

Merged
Nyholm merged 2 commits intooverblog:masterfrom
mfaivre-simplis:fix/is-anonymous-symfony6
Mar 12, 2026
Merged

Fix isAnonymous() always returning false on Symfony 6.x#1240
Nyholm merged 2 commits intooverblog:masterfrom
mfaivre-simplis:fix/is-anonymous-symfony6

Conversation

@mfaivre-simplis
Copy link
Contributor

@mfaivre-simplis mfaivre-simplis commented Mar 11, 2026

Summary

Fixes #1205

BaseSecurity::isAnonymous() calls isGranted('IS_AUTHENTICATED_ANONYMOUSLY'), which was removed from AuthenticatedVoter in Symfony 6.x (deprecated since 5.4) : https://github.com/symfony/symfony/blob/6.0/UPGRADE-6.0.md#security:~:text=Remove%20AuthenticatedVoter%3A%3AIS_AUTHENTICATED_ANONYMOUSLY%20and%20AuthenticatedVoter%3A%3AIS_ANONYMOUS%2C%20use%20AuthenticatedVoter%3A%3APUBLIC_ACCESS%20instead.

The access decision manager does not work and returns false. Every field annotated with #[GQL\Access('isAnonymous()')] gets "Access denied to this field" for all requests.

The fix uses PUBLIC_ACCESS on Symfony 6.x, which has the same behavior (IS_AUTHENTICATED_ANONYMOUSLY and PUBLIC_ACCESS both always grant access) and is the official replacement.

Changes

  • src/Security/Security.php: use PUBLIC_ACCESS on Symfony >= 6.0, keep IS_AUTHENTICATED_ANONYMOUSLY for 5.x

Reproduction

A minimal reproduction repo is available at: https://github.com/mfaivre-simplis/graphqlbundle-issue-1205

#[GQL\Provider]
#[GQL\Access('isAnonymous()')]
final readonly class ConnectedUserQuery
{
    #[GQL\Query(name: 'connectedUser', type: 'Boolean!')]
    #[GQL\Access('isAnonymous()')]
    public function __invoke(): bool
    {
        return true;
    }
}

With Symfony 6.x, querying { connectedUser } always returns "Access denied to this field" before this fix.

IS_AUTHENTICATED_ANONYMOUSLY was removed from AuthenticatedVoter in
Symfony 6.x (deprecated since 5.4). When using #[GQL\Access('isAnonymous()')]
on a Provider field, the access check always returned false, causing every
request to get "Access denied to this field" regardless of auth state.

PUBLIC_ACCESS is the correct replacement since Symfony 5.4 and has the
same semantics: it always grants access.
Copy link
Collaborator

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Thank you. But let's drop IS_AUTHENTICATED_ANONYMOUSLY completely.

@mfaivre-simplis mfaivre-simplis force-pushed the fix/is-anonymous-symfony6 branch from 047639f to f387b69 Compare March 12, 2026 13:55
@mfaivre-simplis mfaivre-simplis requested a review from Nyholm March 12, 2026 13:56
@Nyholm Nyholm merged commit 7e9f36e into overblog:master Mar 12, 2026
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Access denied on this field on query built with PHP way + attributes

2 participants