Skip to content

[SecurityBundle] fix allow_if expression service compilation to support custom functions #24309

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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public function process(ContainerBuilder $container)
}

// security
if ($container->has('security.access.expression_voter')) {
$definition = $container->findDefinition('security.access.expression_voter');
if ($container->has('security.expression_language')) {
$definition = $container->findDefinition('security.expression_language');
foreach ($container->findTaggedServiceIds('security.expression_language_provider') as $id => $attributes) {
$definition->addMethodCall('addExpressionLanguageProvider', array(new Reference($id)));
$definition->addMethodCall('registerProvider', array(new Reference($id)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Parses expressions used in the security access_control configuration to make sure they are dumped as SerializedParsedExpression
* This compiler pass must be registered after AddExpressionLanguageProvidersPass so custom functions can also be parsed.
*
* @author David Maicher <mail@dmaicher.de>
*/
class ParseSecurityExpressionsPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->has('security.expression_language')) {
return;
}

$expressionLanguage = $container->get('security.expression_language');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

findDefinition()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this should actually get the instantiated service 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hm, I missed that. To be honest I am not really happy with having compiler passes that force the creation of the actual service during compilation. Can't we solve the problem by letting the serialization happening in a cache warmer where the service is actually available?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. I'm also not 100% happy with this solution.

The only other option I see is to use a proper persisting cache by default for the expression language service. Currently it only uses an ArrayAdapter:

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/ExpressionLanguage/ExpressionLanguage.php#L37

https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml#L82

On 3.3+ we could use the system cache pool for it?

And then we could add a CacheWarmer for it.

Or do you have another idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I had something like that in mind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will have a look and maybe also prepare some benchmarks to see the impact of the change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


foreach ($container->findTaggedServiceIds('security.expression') as $id => $attributes) {
$definition = $container->getDefinition($id);
$definition
->setClass('Symfony\Component\ExpressionLanguage\SerializedParsedExpression')
->addArgument(serialize($expressionLanguage->parse($definition->getArgument(0), array('token', 'user', 'object', 'roles', 'request', 'trust_resolver'))->getNodes()))
->setTags(array());
}
}
}
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ParseSecurityExpressionsPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RoutingResolverPass;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
Expand Down Expand Up @@ -85,6 +86,7 @@ public function build(ContainerBuilder $container)
$container->addCompilerPass(new AddCacheWarmerPass());
$container->addCompilerPass(new AddCacheClearerPass());
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());
$container->addCompilerPass(new ParseSecurityExpressionsPass()); // must be registered after AddExpressionLanguageProvidersPass
$container->addCompilerPass(new TranslationExtractorPass());
$container->addCompilerPass(new TranslationDumperPass());
$container->addCompilerPass(new FragmentRendererPass(), PassConfig::TYPE_AFTER_REMOVING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testProcessForRouter()
$container = new ContainerBuilder();
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());

$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TestProvider');
$definition = new Definition('\stdClass');
$definition->addTag('routing.expression_language_provider');
$container->setDefinition('some_routing_provider', $definition);

Expand All @@ -43,7 +43,7 @@ public function testProcessForRouterAlias()
$container = new ContainerBuilder();
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());

$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TestProvider');
$definition = new Definition('\stdClass');
$definition->addTag('routing.expression_language_provider');
$container->setDefinition('some_routing_provider', $definition);

Expand All @@ -63,17 +63,16 @@ public function testProcessForSecurity()
$container = new ContainerBuilder();
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());

$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TestProvider');
$definition = new Definition('\stdClass');
$definition->addTag('security.expression_language_provider');
$container->setDefinition('some_security_provider', $definition);

$container->register('security.access.expression_voter', '\stdClass');
$container->register('security.expression_language', '\stdClass');
$container->compile();

$router = $container->getDefinition('security.access.expression_voter');
$calls = $router->getMethodCalls();
$calls = $container->getDefinition('security.expression_language')->getMethodCalls();
$this->assertCount(1, $calls);
$this->assertEquals('addExpressionLanguageProvider', $calls[0][0]);
$this->assertEquals('registerProvider', $calls[0][0]);
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]);
}

Expand All @@ -82,22 +81,17 @@ public function testProcessForSecurityAlias()
$container = new ContainerBuilder();
$container->addCompilerPass(new AddExpressionLanguageProvidersPass());

$definition = new Definition('Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\TestProvider');
$definition = new Definition('\stdClass');
$definition->addTag('security.expression_language_provider');
$container->setDefinition('some_security_provider', $definition);

$container->register('my_security.access.expression_voter', '\stdClass');
$container->setAlias('security.access.expression_voter', 'my_security.access.expression_voter');
$container->register('my.security.expression_language', '\stdClass');
$container->setAlias('security.expression_language', 'my.security.expression_language');
$container->compile();

$router = $container->getDefinition('my_security.access.expression_voter');
$calls = $router->getMethodCalls();
$calls = $container->getDefinition('my.security.expression_language')->getMethodCalls();
$this->assertCount(1, $calls);
$this->assertEquals('addExpressionLanguageProvider', $calls[0][0]);
$this->assertEquals('registerProvider', $calls[0][0]);
$this->assertEquals(new Reference('some_security_provider'), $calls[0][1][0]);
}
}

class TestProvider
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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\FrameworkBundle\Tests\DependencyInjection\Compiler;

use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ParseSecurityExpressionsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ParseSecurityExpressionsPassTest extends TestCase
{
public function testProcess()
{
$container = new ContainerBuilder();
$container->addCompilerPass(new ParseSecurityExpressionsPass());
$container->register('security.expression_language', 'Symfony\Component\Security\Core\Authorization\ExpressionLanguage');

$container->register('security.expression.one', 'Symfony\Component\ExpressionLanguage\Expression')
->addArgument('true or false')
->addTag('security.expression');

$container->register('security.expression.two', 'Symfony\Component\ExpressionLanguage\Expression')
->addArgument('false or true')
->addTag('security.expression');

$container->compile();

$expressionOne = $container->getDefinition('security.expression.one');
$this->assertSame('Symfony\Component\ExpressionLanguage\SerializedParsedExpression', $expressionOne->getClass());
$this->assertInstanceOf('Symfony\Component\ExpressionLanguage\Node\BinaryNode', unserialize($expressionOne->getArgument(1)));

$expressionTwo = $container->getDefinition('security.expression.one');
$this->assertSame('Symfony\Component\ExpressionLanguage\SerializedParsedExpression', $expressionTwo->getClass());
$this->assertInstanceOf('Symfony\Component\ExpressionLanguage\Node\BinaryNode', unserialize($expressionTwo->getArgument(1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class SecurityExtension extends Extension
private $listenerPositions = array('pre_auth', 'form', 'http', 'remember_me');
private $factories = array();
private $userProviderFactories = array();
private $expressionLanguage;

public function __construct()
{
Expand Down Expand Up @@ -583,11 +582,15 @@ private function createExpression($container, $expression)
return $this->expressions[$id];
}

if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
}

$container
->register($id, 'Symfony\Component\ExpressionLanguage\SerializedParsedExpression')
->register($id, 'Symfony\Component\ExpressionLanguage\Expression')
->setPublic(false)
->addArgument($expression)
->addArgument(serialize($this->getExpressionLanguage()->parse($expression, array('token', 'user', 'object', 'roles', 'request', 'trust_resolver'))->getNodes()))
->addTag('security.expression')
;

return $this->expressions[$id] = new Reference($id);
Expand Down Expand Up @@ -651,16 +654,4 @@ public function getConfiguration(array $config, ContainerBuilder $container)
// first assemble the factories
return new MainConfiguration($this->factories, $this->userProviderFactories);
}

private function getExpressionLanguage()
{
if (null === $this->expressionLanguage) {
if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
}
$this->expressionLanguage = new ExpressionLanguage();
}

return $this->expressionLanguage;
}
}