8000 [HttpFoundation] Avoid warnings when checking malicious IPs · symfony/symfony@3067bdb · GitHub
[go: up one dir, main page]

Skip to content

Commit 3067bdb

Browse files
committed
[HttpFoundation] Avoid warnings when checking malicious IPs
1 parent fd4edff commit 3067bdb

File tree

4 files changed

+11
-2
lines changed

4 files changed

+11
-2
lines changed

src/Symfony/Component/HttpFoundation/IpUtils.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ public static function checkIp6($requestIp, $ip)
112112
$netmask = 128;
113113
}
114114

115-
$bytesAddr = unpack('n*', inet_pton($address));
116-
$bytesTest = unpack('n*', inet_pton($requestIp));
115+
$bytesAddr = unpack('n*', @inet_pton($address));
116+
$bytesTest = unpack('n*', @inet_pton($requestIp));
117+
118+
if (!$bytesAddr || !$bytesTest) {
119+
return false;
120+
}
117121

118122
for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
119123
$left = $netmask - 16 * ($i - 1);

src/Symfony/Component/HttpFoundation/Request.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,8 @@ public function getClientIps()
780780

781781
if (!filter_var($clientIp, FILTER_VALIDATE_IP)) {
782782
unset($clientIps[$key]);
783+
784+
continue;
783785
}
784786

785787
if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ public function testIpv6Provider()
6363
array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
6464
array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
6565
array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
66+
array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
67+
array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
6668
);
6769
}
6870

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ public function testGetClientIpsProvider()
866866

867867
// invalid forwarded IP is ignored
868868
array(array('88.88.88.88'), '127.0.0.1', 'unknown,88.88.88.88', array('127.0.0.1')),
869+
array(array('88.88.88.88'), '127.0.0.1', '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2,88.88.88.88', array('127.0.0.1')),
869870
);
870871
}
871872

0 commit comments

Comments
 (0)
0