From ee26ce59878bdff830fdff3c2b6f1230f74f8f6b Mon Sep 17 00:00:00 2001 From: Hugo Monteiro Date: Sun, 28 Feb 2021 08:11:25 +0000 Subject: [PATCH] [SecurityBundle] Empty line starting with dash under "access_control" causes all rules to be skipped --- .../DependencyInjection/SecurityExtension.php | 6 +++ .../SecurityExtensionTest.php | 50 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php index 365638dc4a..5b81d857e8 100644 --- a/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php +++ b/src/Symfony/Bundle/SecurityBundle/DependencyInjection/SecurityExtension.php @@ -191,6 +191,12 @@ class SecurityExtension extends Extension implements PrependExtensionInterface $attributes[] = $this->createExpression($container, $access['allow_if']); } + $emptyAccess = 0 === \count(array_filter($access)); + + if ($emptyAccess) { + throw new InvalidConfigurationException('One or more access control items are empty. Did you accidentally add lines only containing a "-" under "security.access_control"?'); + } + $container->getDefinition('security.access_map') ->addMethodCall('add', [$matcher, $attributes, $access['requires_channel']]); } diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php index 48d44bf554..9d96cbe36b 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/SecurityExtensionTest.php @@ -415,6 +415,56 @@ class SecurityExtensionTest extends TestCase $this->assertEquals(new Reference('security.user.provider.concrete.second'), $container->getDefinition('security.authentication.switchuser_listener.foobar')->getArgument(1)); } + public function testInvalidAccessControlWithEmptyRow() + { + $container = $this->getRawContainer(); + + $container->loadFromExtension('security', [ + 'providers' => [ + 'default' => ['id' => 'foo'], + ], + 'firewalls' => [ + 'some_firewall' => [ + 'pattern' => '/.*', + 'http_basic' => [], + ], + ], + 'access_control' => [ + [], + ['path' => '/admin', 'roles' => 'ROLE_ADMIN'], + ], + ]); + + $this->expectException(InvalidConfigurationException::class); + $this->expectExceptionMessage('One or more access control items are empty. Did you accidentally add lines only containing a "-" under "security.access_control"?'); + $container->compile(); + } + + public function testValidAccessControlWithEmptyRow() + { + $container = $this->getRawContainer(); + + $container->loadFromExtension('security', [ + 'providers' => [ + 'default' => ['id' => 'foo'], + ], + 'firewalls' => [ + 'some_firewall' => [ + 'pattern' => '/.*', + 'http_basic' => [], + ], + ], + 'access_control' => [ + ['path' => '^/login'], + ['path' => '^/', 'roles' => 'ROLE_USER'], + ], + ]); + + $container->compile(); + + $this->assertTrue(true, 'extension throws an InvalidConfigurationException if there is one more more empty access control items'); + } + protected function getRawContainer() { $container = new ContainerBuilder();