Skip to content

warn of Symfony\Component\Security\Core\Authorization\Voter\Voter use with subjects #20732

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

Closed
landure opened this issue Mar 7, 2025 · 2 comments

Comments

@landure
Copy link

landure commented Mar 7, 2025

Symfony\Component\Security\Core\Authorization\Voter\Voter is a CacheableVoterInterface.

The results are cached by subject class, not by subject value, or ORM\Id.

When the Voter must depend on the subject value (or state), the Voter result should not be cached.

Please add a warning like this one:

The AccessDecisionManager cache voters' results by subject class, not by subject value. If the Voter result depends on the subject value, don't extend Symfony\Component\Security\Core\Authorization\Voter\Voter, implement VoterInterface directly.

@javiereguiluz
Copy link
Member

@landure looking at the code: https://github.com/symfony/symfony/blob/cadcc578af9ad9fe7a8dc87aa545083f23950e12/src/Symfony/Component/Security/Core/Authorization/AccessDecisionManager.php#L106

It's true that the cache is done by attribute + object class ... but the voter is called with the actual object every time. We don't cache the full voter result; we only cache the decision whether a voter should be called for some object.

E.g. "can I edit blog post with id = 3?" will cache: "attribute: 'edit' class: 'BlogPost::class'" and then calls the voter with blog post (id = 3)

But, if I later call "can I edit blog post with id = 5?", it will call the voter directly (because that check is cached) but this time it will call the voter with blog post (id = 5) instead of id = 3.

Do you see any problem with that behavior? Or maybe I missed something in your question.

@landure
Copy link
Author

landure commented Jun 3, 2025

My bad. I misinterpreted the behavior, since for a reason or another, my custom voter was not called. Thank you for taking the time to correct me.

@landure landure closed this as completed Jun 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy