-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Security] Configurable execution order for firewall listeners #37337
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/Symfony/Bundle/SecurityBundle/DependencyInjection/Compiler/SortFirewallListenersPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface; | ||
|
||
/** | ||
* Sorts firewall listeners based on the execution order provided by FirewallListenerInterface::getPriority(). | ||
* | ||
* @author Christian Scheb <me@christianscheb.de> | ||
*/ | ||
class SortFirewallListenersPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
if (!$container->hasParameter('security.firewalls')) { | ||
return; | ||
} | ||
|
||
foreach ($container->getParameter('security.firewalls') as $firewallName) { | ||
$firewallContextDefinition = $container->getDefinition('security.firewall.map.context.'.$firewallName); | ||
$this->sortFirewallContextListeners($firewallContextDefinition, $container); | ||
} | ||
} | ||
|
||
private function sortFirewallContextListeners(Definition $definition, ContainerBuilder $container): void | ||
{ | ||
/** @var IteratorArgument $listenerIteratorArgument */ | ||
$listenerIteratorArgument = $definition->getArgument(0); | ||
$prioritiesByServiceId = $this->getListenerPriorities($listenerIteratorArgument, $container); | ||
|
||
$listeners = $listenerIteratorArgument->getValues(); | ||
usort($listeners, function (Reference $a, Reference $b) use ($prioritiesByServiceId) { | ||
return $prioritiesByServiceId[(string) $b] <=> $prioritiesByServiceId[(string) $a]; | ||
}); | ||
|
||
$listenerIteratorArgument->setValues(array_values($listeners)); | ||
} | ||
|
||
private function getListenerPriorities(IteratorArgument $listeners, ContainerBuilder $container): array | ||
{ | ||
$priorities = []; | ||
|
||
foreach ($listeners->getValues() as $reference) { | ||
$id = (string) $reference; | ||
$def = $container->getDefinition($id); | ||
|
||
// We must assume that the class value has been correctly filled, even if the service is created by a factory | ||
$class = $def->getClass(); | ||
|
||
if (!$r = $container->getReflectionClass($class)) { | ||
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); | ||
} | ||
|
||
$priority = 0; | ||
if ($r->isSubclassOf(FirewallListenerInterface::class)) { | ||
$priority = $r->getMethod('getPriority')->invoke(null); | ||
} | ||
|
||
$priorities[$id] = $priority; | ||
} | ||
|
||
return $priorities; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...undle/SecurityBundle/Tests/DependencyInjection/Compiler/SortFirewallListenersPassTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\DependencyInjection\Compiler; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Compiler\SortFirewallListenersPass; | ||
use Symfony\Bundle\SecurityBundle\Security\FirewallContext; | ||
use Symfony\Component\DependencyInjection\Argument\IteratorArgument; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Security\Http\Firewall\FirewallListenerInterface; | ||
|
||
class SortFirewallListenersPassTest extends TestCase | ||
{ | ||
public function testSortFirewallListeners() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->setParameter('security.firewalls', ['main']); | ||
|
||
$container->register('listener_priority_minus1', FirewallListenerPriorityMinus1::class); | ||
$container->register('listener_priority_1', FirewallListenerPriority1::class); | ||
$container->register('listener_priority_2', FirewallListenerPriority2::class); | ||
$container->register('listener_interface_not_implemented', \stdClass::class); | ||
|
||
$firewallContext = $container->register('security.firewall.map.context.main', FirewallContext::class); | ||
$firewallContext->addTag('security.firewall_map_context'); | ||
|
||
$listeners = new IteratorArgument([ | ||
new Reference('listener_priority_minus1'), | ||
new Reference('listener_priority_1'), | ||
new Reference('listener_priority_2'), | ||
new Reference('listener_interface_not_implemented'), | ||
]); | ||
|
||
$firewallContext->setArgument(0, $listeners); | ||
|
||
$compilerPass = new SortFirewallListenersPass(); | ||
$compilerPass->process($container); | ||
|
||
$sortedListeners = $firewallContext->getArgument(0); | ||
$expectedSortedlisteners = [ | ||
new Reference('listener_priority_2'), | ||
new Reference('listener_priority_1'), | ||
new Reference('listener_interface_not_implemented'), | ||
new Reference('listener_priority_minus1'), | ||
]; | ||
$this->assertEquals($expectedSortedlisteners, $sortedListeners->getValues()); | ||
} | ||
} | ||
|
||
class FirewallListenerPriorityMinus1 implements FirewallListenerInterface | ||
{ | ||
public static function getPriority(): int | ||
{ | ||
return -1; | ||
} | ||
} | ||
|
||
class FirewallListenerPriority1 implements FirewallListenerInterface | ||
{ | ||
public static function getPriority(): int | ||
{ | ||
return 1; | ||
} | ||
} | ||
|
||
class FirewallListenerPriority2 implements FirewallListenerInterface | ||
{ | ||
public static function getPriority(): int | ||
{ | ||
return 2; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/Symfony/Component/Security/Http/Firewall/FirewallListenerInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Firewall; | ||
|
||
/** | ||
* Can be implemented by firewall listeners to define their priority in execution. | ||
* | ||
* @author Christian Scheb <me@christianscheb.de> | ||
*/ | ||
interface FirewallListenerInterface | ||
{ | ||
/** | ||
* Defines the priority of the listener. | ||
* The higher the number, the earlier a listener is executed. | ||
*/ | ||
public static function getPriority(): int; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.