Skip to content

[SecurityBundle] Allow to specify a RequestMatcher directly in an ACL definition #44670

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 1 commit into from
Jan 31, 2022
Merged
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
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

6.1
---
* The `security.access_control` now accepts a `RequestMatcherInterface` under the `request_matcher` option as scope configuration

6.0
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private function addAccessControlSection(ArrayNodeDefinition $rootNode)
->fixXmlConfig('ip')
->fixXmlConfig('method')
->children()
->scalarNode('request_matcher')->defaultNull()->end()
->scalarNode('requires_channel')->defaultNull()->end()
->scalarNode('path')
->defaultNull()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,24 @@ private function createRoleHierarchy(array $config, ContainerBuilder $container)
private function createAuthorization(array $config, ContainerBuilder $container)
{
foreach ($config['access_control'] as $access) {
$matcher = $this->createRequestMatcher(
$container,
$access['path'],
$access['host'],
$access['port'],
$access['methods'],
$access['ips']
);
if (isset($access['request_matcher'])) {
if (
isset($access['path']) || isset($access['host']) || isset($access['port'])
|| [] !== $access['ips'] || [] !== $access['methods']
) {
throw new InvalidConfigurationException('The "request_matcher" option should not be specified alongside other options. Consider integrating your constraints inside your RequestMatcher directly.');
}
$matcher = new Reference($access['request_matcher']);
} else {
$matcher = $this->createRequestMatcher(
$container,
$access['path'],
$access['host'],
$access['port'],
$access['methods'],
$access['ips']
);
}

$attributes = $access['roles'];
if ($access['allow_if']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
Expand Down Expand Up @@ -250,6 +251,80 @@ public function testRegisterRequestMatchersWithAllowIfExpression()
);
}

public function testRegisterAccessControlWithSpecifiedRequestMatcherService()
{
$container = $this->getRawContainer();

$requestMatcherId = 'My\Test\RequestMatcher';
$requestMatcher = new RequestMatcher('/');
$container->set($requestMatcherId, $requestMatcher);

$container->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'providers' => [
'default' => ['id' => 'foo'],
],
'firewalls' => [
'some_firewall' => [
'pattern' => '/.*',
'http_basic' => [],
],
],
'access_control' => [
['request_matcher' => $requestMatcherId],
],
]);

$container->compile();
$accessMap = $container->getDefinition('security.access_map');
$this->assertCount(1, $accessMap->getMethodCalls());
$call = $accessMap->getMethodCalls()[0];
$this->assertSame('add', $call[0]);
$args = $call[1];
$this->assertCount(3, $args);
$this->assertSame($requestMatcherId, (string) $args[0]);
}

/** @dataProvider provideAdditionalRequestMatcherConstraints */
public function testRegisterAccessControlWithRequestMatcherAndAdditionalOptionsThrowsInvalidException(array $additionalConstraints)
{
$container = $this->getRawContainer();

$requestMatcherId = 'My\Test\RequestMatcher';
$requestMatcher = new RequestMatcher('/');
$container->set($requestMatcherId, $requestMatcher);

$container->loadFromExtension('security', [
'enable_authenticator_manager' => true,
'providers' => [
'default' => ['id' => 'foo'],
],
'firewalls' => [
'some_firewall' => [
'pattern' => '/.*',
'http_basic' => [],
],
],
'access_control' => [
array_merge(['request_matcher' => $requestMatcherId], $additionalConstraints),
],
]);

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('The "request_matcher" option should not be specified alongside other options. Consider integrating your constraints inside your RequestMatcher directly.');

$container->compile();
}

public function provideAdditionalRequestMatcherConstraints()
{
yield 'Invalid configuration with path' => [['path' => '^/url']];
yield 'Invalid configuration with host' => [['host' => 'example.com']];
yield 'Invalid configuration with port' => [['port' => 80]];
yield 'Invalid configuration with methods' => [['methods' => ['POST']]];
yield 'Invalid configuration with ips' => [['ips' => ['0.0.0.0']]];
}

public function testRemovesExpressionCacheWarmerDefinitionIfNoExpressions()
{
$container = $this->getRawContainer();
Expand Down