Skip to content

[SecurityBundle] Comma separated ips for security.access_control #38149

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
Sep 12, 2020
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Added `FirewallListenerFactoryInterface`, which can be implemented by security factories to add firewall listeners
* Added `SortFirewallListenersPass` to make the execution order of firewall listeners configurable by
leveraging `Symfony\Component\Security\Http\Firewall\FirewallListenerInterface`
* Added ability to use comma separated ip address list for `security.access_control`

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ imports:

parameters:
env(APP_IP): '127.0.0.1'
env(APP_IPS): '127.0.0.1, ::1'

security:
encoders:
Expand Down Expand Up @@ -47,7 +48,9 @@ security:
- { path: ^/secured-by-one-real-ip-with-mask$, ips: '203.0.113.0/24', roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured-by-one-real-ipv6$, ips: 0:0:0:0:0:ffff:c633:6400, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured-by-one-env-placeholder$, ips: '%env(APP_IP)%', roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured-by-one-env-placeholder-multiple-ips$, ips: '%env(APP_IPS)%', roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured-by-one-env-placeholder-and-one-real-ip$, ips: ['%env(APP_IP)%', 198.51.100.0], roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/secured-by-one-env-placeholder-multiple-ips-and-one-real-ip$, ips: ['%env(APP_IPS)%', 198.51.100.0], roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/highly_protected_resource$, roles: IS_ADMIN }
- { path: ^/protected-via-expression$, allow_if: "(is_anonymous() and request.headers.get('user-agent') matches '/Firefox/i') or is_granted('ROLE_USER')" }
- { path: .*, roles: IS_AUTHENTICATED_FULLY }
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ CHANGELOG
5.2.0
-----

* added support for `X-Forwarded-Prefix` header
* added support for `X-Forwarded-Prefix` header
* added `HeaderUtils::parseQuery()`: it does the same as `parse_str()` but preserves dots in variable names
* added `File::getContent()`
* added ability to use comma separated ip addresses for `RequestMatcher::matchIps()`

5.1.0
-----
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/HttpFoundation/RequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ public function matchIp(string $ip)
*/
public function matchIps($ips)
{
$this->ips = null !== $ips ? (array) $ips : [];
$ips = null !== $ips ? (array) $ips : [];

$this->ips = array_reduce($ips, static function (array $ips, string $ip) {
return array_merge($ips, preg_split('/\s*,\s*/', $ip));
Copy link
Member

Choose a reason for hiding this comment

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

for next reviewers, this is the core of the PR

}, []);
}

/**
Expand Down
34 changes: 34 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,38 @@ public function testAttributes()
$matcher->matchAttribute('foo', 'babar');
$this->assertFalse($matcher->matches($request));
}

public function testIps()
{
$matcher = new RequestMatcher();

$request = Request::create('', 'GET', [], [], [], ['REMOTE_ADDR' => '127.0.0.1']);

$matcher->matchIp('127.0.0.1');
$this->assertTrue($matcher->matches($request));

$matcher->matchIp('192.168.0.1');
$this->assertFalse($matcher->matches($request));

$matcher->matchIps('127.0.0.1');
$this->assertTrue($matcher->matches($request));

$matcher->matchIps('127.0.0.1, ::1');
$this->assertTrue($matcher->matches($request));

$matcher->matchIps('192.168.0.1, ::1');
$this->assertFalse($matcher->matches($request));

$matcher->matchIps(['127.0.0.1', '::1']);
$this->assertTrue($matcher->matches($request));

$matcher->matchIps(['192.168.0.1', '::1']);
$this->assertFalse($matcher->matches($request));

$matcher->matchIps(['1.1.1.1', '2.2.2.2', '127.0.0.1, ::1']);
$this->assertTrue($matcher->matches($request));

$matcher->matchIps(['1.1.1.1', '2.2.2.2', '192.168.0.1, ::1']);
$this->assertFalse($matcher->matches($request));
}
}