Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9ac571

Browse files
ultrafezfabpot
authored andcommitted
[HttpFoundation] Fixes /0 subnet handling in IpUtils
1 parent 2406cc7 commit d9ac571

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/Symfony/Component/HttpFoundation/IpUtils.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,19 @@ public static function checkIp($requestIp, $ips)
5757
* @param string $requestIp IPv4 address to check
5858
* @param string $ip IPv4 address or subnet in CIDR notation
5959
*
60-
* @return bool Whether the IP is valid
60+
* @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet.
6161
*/
6262
public static function checkIp4($requestIp, $ip)
6363
{
6464
if (false !== strpos($ip, '/')) {
65-
if ('0.0.0.0/0' === $ip) {
66-
return true;
67-
}
68-
6965
list($address, $netmask) = explode('/', $ip, 2);
7066

71-
if ($netmask < 1 || $netmask > 32) {
67+
if ($netmask === '0') {
68+
// Ensure IP is valid - using ip2long below implicitly validates, but we need to do it manually here
69+
return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
70+
}
71+
72+
if ($netmask < 0 || $netmask > 32) {
7273
return false;
7374
}
7475
} else {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public function testIpv4Provider()
3030
array(true, '192.168.1.1', '192.168.1.1/1'),
3131
array(true, '192.168.1.1', '192.168.1.0/24'),
3232
array(false, '192.168.1.1', '1.2.3.4/1'),
33-
array(false, '192.168.1.1', '192.168.1/33'),
33+
array(false, '192.168.1.1', '192.168.1.1/33'), // invalid subnet
3434
array(true, '192.168.1.1', array('1.2.3.4/1', '192.168.1.0/24')),
3535
array(true, '192.168.1.1', array('192.168.1.0/24', '1.2.3.4/1')),
3636
array(false, '192.168.1.1', array('1.2.3.4/1', '4.3.2.1/1')),
3737
array(true, '1.2.3.4', '0.0.0.0/0'),
38-
array(false, '1.2.3.4', '256.256.256/0'),
39-
array(false, '1.2.3.4', '192.168.1.0/0'),
38+
array(true, '1.2.3.4', '192.168.1.0/0'),
39+
array(false, '1.2.3.4', '256.256.256/0'), // invalid CIDR notation
4040
);
4141
}
4242

0 commit comments

Comments
 (0)