New in Symfony 5.4: Faster Security Voters

Contributed by Jérémy Derussé in #43066.

Security Voters are the recommended way to check for permissions in Symfony applications. They allow to centralize the permission logic so you can reuse it from controllers, templates and services. During runtime, whenever Symfony finds a isGranted() method call, it iterates over all the voters, and stops when the configured access decision strategy is met. This works well in most applications, but it hurts performance in some scenarios. Consider a backend that displays a listing of 20 entities, each of them showing 6 properties and 3 actions (e.g. edit, show, delete). If you want to check permissions for accessing those properties and running those actions, you are calling each voter 20 x (6 + 3) = 180 times. If you have 5 voters, that's 900 calls. Most of the times voters only care about a certain permission/attribute (e.g. EDIT_BLOG_POST or APPROVE_EXTRA_DISCOUNT) or a certain object type (e.g. User or Invoice). That makes voters cacheable and that's why we're introducing the following CacheableVoterInterface in Symfony 5.4:

    1

2 3 4 5 6 7 8 9 namespace Symfony\Component\Security\Core\Authorization\Voter;

interface CacheableVoterInterface extends VoterInterface { public function supportsAttribute(string $attribute): bool;

// $subjectType is the value returned by `get_class($subject)` or `get_debug_type($subject)`
public function supportsType(string $subjectType): bool;

}

If your voter returns false in any (or all) of those methods, Symfony will cache that result and your voter won't be called again for that attribute/permission and/or type. When your voter extends the abstract Voter class, there's no need to implement the new interface explicitly because that parent class already does it for you. Instead, you only need to override the supportsAttribute() and/or supportsType() methods. For example, if your voter supports several object types but all attribute/permission names follow the APROVE_* pattern, do this:

    1

2 3 4 5 6 7 8 9 10 11 12 13 namespace App\Security;

use Symfony\Component\Security\Core\Authorization\Voter\Voter;

class MyVoter extends Voter { public function supportsAttribute(string $attribute): bool { return str_startswith($attribute, 'APPROVE'); }

// ...

}

If your voter supports many different attributes/permissions on some specific type, use this:

    1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 namespace App\Security;

use App\Entity\BlogPost; use Symfony\Component\Security\Core\Authorization\Voter\Voter;

class MyVoter extends Voter { public function supportsType(string $subjectType): bool { // you can't use a simple BlogPost::class === $subjectType comparison // here because the given subject type could be the proxy class used // by Doctrine when creating the entity object return is_a($subjectType, BlogPost::class, true); }

// ...

}

Thanks to this change, in a real application that defines 40 voters which are called 500 times via isGranted(), we measured a 40% performance improvement in the handling of security authorization. If you measure the improvement in your apps (e.g. using Blackfire.io) don't forget to share the results in the comments below.

                Sponsor the Symfony project.

https://symfony.com/blog/new-in-symfony-5-4-faster-security-voters?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Vytvorené 3y | 11. 11. 2021, 2:20:12


Ak chcete pridať komentár, prihláste sa

Ostatné príspevky v tejto skupine

SymfonyLive Paris 2025 :  Async avec Messenger, AMQP et Mercure

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here. 🚨 Tod

26. 2. 2025, 14:31:31 | Symfony
Symfony 6.4.19 released

Symfony 6.4.19 has just been released. Here is the list of the most important changes since 6.4.18:

bug #59198 [Messenger] Filter out non-consumable receivers when registering ConsumeMessagesComm
26. 2. 2025, 12:20:03 | Symfony
Symfony 7.2.4 released

Symfony 7.2.4 has just been released. Here is the list of the most important changes since 7.2.3:

bug #59198 [Messenger] Filter out non-consumable receivers when registering ConsumeMessagesComman
26. 2. 2025, 12:20:03 | Symfony
Just one month to go before SymfonyLive Paris 2025 workshops begin!

SymfonyLive Paris 2025, conference in French language only, will already start in 1 month with the workshops! Have a look on the topics and join us! Schedule details are available here.

📣

25. 2. 2025, 15:20:33 | Symfony
New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

24. 2. 2025, 16:20:03 | Symfony
SymfonyLive Paris 2025 : Du lego de composants pour un bundle Gotenberg !

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

24. 2. 2025, 13:50:07 | Symfony
A Week of Symfony #947 (17-23 February 2025)

This week, development activity focused on new security features. The upcoming Symfony 7.3 version added support for security voters to explain their vote, improved the IsGranted attribute to allow us

23. 2. 2025, 10:10:09 | Symfony