Skip to content

Commit

Permalink
bug #18084 [HttpFoundation] Avoid warnings when checking malicious IP…
Browse files Browse the repository at this point in the history
…s (jakzal)

This PR was merged into the 2.3 branch.

Discussion
----------

[HttpFoundation] Avoid warnings when checking malicious IPs

| Q             | A
| ------------- | ---
| Branch        | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17386
| License       | MIT
| Doc PR        | -

Commits
-------

3067bdb [HttpFoundation] Avoid warnings when checking malicious IPs
  • Loading branch information
fabpot committed Mar 10, 2016
2 parents fd4edff + 3067bdb commit 5340cbd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Symfony/Component/HttpFoundation/IpUtils.php
Expand Up @@ -112,8 +112,12 @@ public static function checkIp6($requestIp, $ip)
$netmask = 128;
}

$bytesAddr = unpack('n*', inet_pton($address));
$bytesTest = unpack('n*', inet_pton($requestIp));
$bytesAddr = unpack('n*', @inet_pton($address));
$bytesTest = unpack('n*', @inet_pton($requestIp));

if (!$bytesAddr || !$bytesTest) {
return false;
}

for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
$left = $netmask - 16 * ($i - 1);
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -780,6 +780,8 @@ public function getClientIps()

if (!filter_var($clientIp, FILTER_VALIDATE_IP)) {
unset($clientIps[$key]);

continue;
}

if (IpUtils::checkIp($clientIp, self::$trustedProxies)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php
Expand Up @@ -63,6 +63,8 @@ public function testIpv6Provider()
array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Expand Up @@ -866,6 +866,7 @@ public function testGetClientIpsProvider()

// invalid forwarded IP is ignored
array(array('88.88.88.88'), '127.0.0.1', 'unknown,88.88.88.88', array('127.0.0.1')),
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')),
);
}

Expand Down

0 comments on commit 5340cbd

Please sign in to comment.