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 for that attribute/permission and/or type. 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 14 15 16 17 18 19 namespace App\Security;

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

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

public function supportsType(string $subjectType): bool
{
    return true;
}

// ...

}

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 18 19 20 namespace App\Security;

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

class MyVoter extends Voter implements CacheableVoterInterface { public function supportsAttribute(string $attribute): bool { return true; }

public function supportsType(string $subjectType): bool
{
    return BlogPost::class === $subjectType;
}

// ...

}

Thanks to this change, in a real application that defines 40 voters and they 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.

http://feedproxy.google.com/~r/symfony/blog/~3/TmLUbYoFxsA/new-in-symfony-5-4-faster-security-voters

Établi 3y | 3 nov. 2021 à 08:20:06


Connectez-vous pour ajouter un commentaire

Autres messages de ce groupe

A Week of Symfony #943 (20-26 January 2025)

This week, the upcoming Symfony 7.3 version improved the invokable command feature, deprecated the use of option arrays for configuring validation constraints, and updated the JsonEncoder component to

26 janv. 2025 à 12:10:08 | Symfony
SymfonyLive Berlin 2025: Need a MACH-ready Search Engine?

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

As we are now unveiling th

24 janv. 2025 à 11:20:16 | Symfony
SymfonyLive Paris 2025 : Rôles & permissions : développez une marque blanche avec du Feature Flipping

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

23 janv. 2025 à 16:50:03 | Symfony
SymfonyLive Berlin 2025: So you think you know PHPUnit

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here.

First, a big thank you to

22 janv. 2025 à 08:20:10 | Symfony
SymfonyLive Paris 2025 : Passkeys pour une authentification fluide et sécurisée

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.

To

21 janv. 2025 à 11:30:10 | Symfony
Join us for SymfonyDay Chicago – March 17, 2025!

Mark your calendars for March 17, 2025 because SymfonyDay Chicago 2025 promises to be a one-of-a-kind event that you won’t want to miss! This full day is dedicated to celebrating the incredible contri

20 janv. 2025 à 19:20:03 | Symfony
A Week of Symfony #942 (13-19 January 2025)

This week, Symfony celebrated the SymfonyOnline January 2025 conference. In addition, it announced the new Symfony UX Core Team. Lastly, the upcoming Symfony 7.3 version simplified the configuration o

19 janv. 2025 à 08:30:08 | Symfony