Skip to content

Commit

Permalink
[HttpFoundation] Fix bad return type in IpUtils::checkIp4()
Browse files Browse the repository at this point in the history
  • Loading branch information
tristankretzer authored and nicolas-grekas committed Jan 29, 2023
1 parent 70fd0eb commit d043536
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion IpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function checkIp4(?string $requestIp, string $ip)
[$address, $netmask] = explode('/', $ip, 2);

if ('0' === $netmask) {
return self::$checkedIps[$cacheKey] = filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4);
return self::$checkedIps[$cacheKey] = false !== filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4);
}

if ($netmask < 0 || $netmask > 32) {
Expand Down
17 changes: 17 additions & 0 deletions Tests/IpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,21 @@ public function anonymizedIpData()
['::123.234.235.236', '::123.234.235.0'], // deprecated IPv4-compatible IPv6 address
];
}

/**
* @dataProvider getIp4SubnetMaskZeroData
*/
public function testIp4SubnetMaskZero($matches, $remoteAddr, $cidr)
{
$this->assertSame($matches, IpUtils::checkIp4($remoteAddr, $cidr));
}

public function getIp4SubnetMaskZeroData()
{
return [
[true, '1.2.3.4', '0.0.0.0/0'],
[true, '1.2.3.4', '192.168.1.0/0'],
[false, '1.2.3.4', '256.256.256/0'], // invalid CIDR notation
];
}
}

0 comments on commit d043536

Please sign in to comment.