Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e7c1696

Browse filesBrowse files
committed
[HttpFoundation] refactored code to avoid code duplication
1 parent 1695067 commit e7c1696
Copy full SHA for e7c1696

File tree

Expand file treeCollapse file tree

5 files changed

+27
-19
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+27
-19
lines changed

‎src/Symfony/Component/HttpFoundation/IpUtils.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/IpUtils.php
+14-6Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,26 @@ private function __construct() {}
2626
/**
2727
* Validates an IPv4 or IPv6 address.
2828
*
29-
* @param string $requestIp
30-
* @param string $ip
29+
* @param string $requestIp
30+
* @param string|array $ips
3131
*
3232
* @return boolean Whether the IP is valid
3333
*/
34-
public static function checkIp($requestIp, $ip)
34+
public static function checkIp($requestIp, $ips)
3535
{
36-
if (false !== strpos($requestIp, ':')) {
37-
return self::checkIp6($requestIp, $ip);
36+
if (!is_array($ips)) {
37+
$ips = array($ips);
38+
}
39+
40+
$method = false !== strpos($requestIp, ':') ? 'checkIp6': 'checkIp4';
41+
42+
foreach ($ips as $ip) {
43+
if (self::$method($requestIp, $ip)) {
44+
return true;
45+
}
3846
}
3947

40-
return self::checkIp4($requestIp, $ip);
48+
return false;
4149
}
4250

4351
/**

‎src/Symfony/Component/HttpFoundation/Request.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Request.php
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -690,12 +690,10 @@ public function getClientIps()
690690
$ip = $clientIps[0];
691691

692692
foreach ($clientIps as $key => $clientIp) {
693-
foreach ($trustedProxies as $trustedProxy) {
694-
if (IpUtils::checkIp($clientIp, $trustedProxy)) {
695-
unset($clientIps[$key]);
693+
if (IpUtils::checkIp($clientIp, $trustedProxies)) {
694+
unset($clientIps[$key]);
696695

697-
continue 2;
698-
}
696+
continue;
699697
}
700698
}
701699

‎src/Symfony/Component/HttpFoundation/RequestMatcher.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/RequestMatcher.php
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,8 @@ public function matches(Request $request)
153153
return false;
154154
}
155155

156-
foreach ($this->ips as $ip) {
157-
if (IpUtils::checkIp($request->getClientIp(), $ip)) {
158-
return true;
159-
}
156+
if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
157+
return true;
160158
}
161159

162160
// Note to future implementors: add additional checks above the

‎src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public function testIpv4Provider()
3131
array(true, '192.168.1.1', '192.168.1.0/24'),
3232
array(false, '192.168.1.1', '1.2.3.4/1'),
3333
array(false, '192.168.1.1', '192.168.1/33'),
34+
array(true, '192.168.1.1', array('1.2.3.4/1', '192.168.1.0/24')),
35+
array(true, '192.168.1.1', array('192.168.1.0/24', '1.2.3.4/1')),
36+
array(false, '192.168.1.1', array('1.2.3.4/1', '4.3.2.1/1')),
3437
);
3538
}
3639

@@ -54,6 +57,9 @@ public function testIpv6Provider()
5457
array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
5558
array(true, '0:0:0:0:0:0:0:1', '::1'),
5659
array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
60+
array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
61+
array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
62+
array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
5763
);
5864
}
5965

‎src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/FragmentListener.php
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ protected function validateRequest(Request $request)
8080
// does the Request come from a trusted IP?
8181
$trustedIps = array_merge($this->getLocalIpAddresses(), $request->getTrustedProxies());
8282
$remoteAddress = $request->server->get('REMOTE_ADDR');
83-
foreach ($trustedIps as $ip) {
84-
if (IpUtils::checkIp($remoteAddress, $ip)) {
85-
return;
86-
}
83+
if (IpUtils::checkIp($remoteAddress, $trustedIps)) {
84+
return;
8785
}
8886

8987
// is the Request signed?

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.