Skip to content

[SecurityBundle] Allow ips parameter in access_control to accept comma-separated string #40902

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
May 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ private function createRequestMatcher(ContainerBuilder $container, string $path
foreach ($ips as $ip) {
$container->resolveEnvPlaceholders($ip, null, $usedEnvs);

if (!$usedEnvs && !$this->isValidIp($ip)) {
if (!$usedEnvs && !$this->isValidIps($ip)) {
throw new \LogicException(sprintf('The given value "%s" in the "security.access_control" config option is not a valid IP address.', $ip));
}

Expand Down Expand Up @@ -930,6 +930,25 @@ public function getConfiguration(array $config, ContainerBuilder $container)
return new MainConfiguration($this->factories, $this->userProviderFactories);
}

private function isValidIps($ips): bool
{
$ipsList = array_reduce((array) $ips, static function (array $ips, string $ip) {
return array_merge($ips, preg_split('/\s*,\s*/', $ip));
}, []);

if (!$ipsList) {
return false;
}

foreach ($ipsList as $cidr) {
if (!$this->isValidIp($cidr)) {
return false;
}
}

return true;
}

private function isValidIp(string $cidr): bool
{
$cidrParts = explode('/', $cidr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,33 @@ public function testRememberMeCookieInheritFrameworkSessionCookie($config, $same
$this->assertEquals($secure, $definition->getArgument(3)['secure']);
}

/**
* @dataProvider acceptableIpsProvider
*/
public function testAcceptableAccessControlIps($ips)
{
$container = $this->getRawContainer();

$container->loadFromExtension('security', [
'providers' => [
'default' => ['id' => 'foo'],
],
'firewalls' => [
'some_firewall' => [
'pattern' => '/.*',
'http_basic' => [],
],
],
'access_control' => [
['ips' => $ips, 'path' => '/somewhere', 'roles' => 'IS_AUTHENTICATED_FULLY'],
],
]);

$container->compile();

$this->assertTrue(true, 'Ip addresses is successfully consumed: '.(\is_string($ips) ? $ips : json_encode($ips)));
}

public function sessionConfigurationProvider()
{
return [
Expand All @@ -408,6 +435,16 @@ public function sessionConfigurationProvider()
];
}

public function acceptableIpsProvider(): iterable
{
yield [['127.0.0.1']];
yield ['127.0.0.1'];
yield ['127.0.0.1, 127.0.0.2'];
yield ['127.0.0.1/8, 127.0.0.2/16'];
yield [['127.0.0.1/8, 127.0.0.2/16']];
yield [['127.0.0.1/8', '127.0.0.2/16']];
}

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