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

Created 3y | Nov 3, 2021, 8:20:06 AM


Login to add comment

Other posts in this group

Introducing the new Twig Playground

I'm very excited to announce the launch of Twig Playground, a new tool that allows you to test and experiment with Twig templates in a sandbox environment. It is entirely web-based, with no backend.

Dec 26, 2024, 4:20:14 PM | Symfony
A Week of Symfony #938 (16-22 December 2024)

This week, Symfony development activity focused on fixing bugs in maintenance versions and adding new features for the upcoming Symfony 7.3 release. Meanwhile, we published blog posts about the new Tw

Dec 22, 2024, 10:20:10 AM | Symfony
New in Twig 3.15 (part 2)

In the first part of this blog post we introduced exciting new Twig features like inline comments, PHP enums support, improved operator precedence, the guard tag, and enhanced deprecation handling. Th

Dec 19, 2024, 10:30:05 AM | Symfony
Case study - Upply: The PHP advantage: How we resisted the switch to Scala, rust, or go

After a pause, we’re excited to relaunch the publication of case studies in the Symfony community. Whether you’ve tackled challenging upgrades, solved complex technical issues, or transformed your tea

Dec 18, 2024, 8:50:12 AM | Symfony
New in Twig 3.15 (part 1)

Twig 3.15 was released a few weeks ago and includes an impressive list of new features and improvements. This two-part blog post highlights the most important ones.

Inline Comments… https://symfony.c

Dec 17, 2024, 9:40:11 AM | Symfony
A Week of Symfony #937 (9-15 December 2024)

This week, Symfony 7.2.1 was released as the first maintenance version of the 7.2 branch. Meanwhile, the upcoming Symfony 7.3 version introduced a new JsonEncoder component that is 10 times faster tha

Dec 15, 2024, 9:10:06 AM | Symfony
Symfony 7.2.1 released

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

bug #59145 [TypeInfo] Make Type::nullable method no-op on every nullable type (@mtarld)

bug #5912

Dec 11, 2024, 12:30:06 PM | Symfony