Skip to content

Commit

Permalink
Verify explicitly that the request IP is a valid IPv4 address
Browse files Browse the repository at this point in the history
  • Loading branch information
nesk authored and fabpot committed Aug 19, 2016
1 parent 56cdaf9 commit 17e418c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Symfony/Component/HttpFoundation/IpUtils.php
Expand Up @@ -61,11 +61,14 @@ public static function checkIp($requestIp, $ips)
*/
public static function checkIp4($requestIp, $ip)
{
if (!filter_var($requestIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return false;
}

if (false !== strpos($ip, '/')) {
list($address, $netmask) = explode('/', $ip, 2);

if ($netmask === '0') {
// Ensure IP is valid - using ip2long below implicitly validates, but we need to do it manually here
return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php
Expand Up @@ -37,6 +37,7 @@ public function testIpv4Provider()
array(true, '1.2.3.4', '0.0.0.0/0'),
array(true, '1.2.3.4', '192.168.1.0/0'),
array(false, '1.2.3.4', '256.256.256/0'), // invalid CIDR notation
array(false, 'an_invalid_ip', '192.168.1.0/24'),
);
}

Expand Down

0 comments on commit 17e418c

Please sign in to comment.