Skip to content

Commit b8c7604

Browse files
bug #49141 [HttpFoundation] Fix bad return type in IpUtils::checkIp4() (tristankretzer)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [HttpFoundation] Fix bad return type in IpUtils::checkIp4() | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | see below | License | MIT | Doc PR | - `filter_var` returns the value if it passes the applied filters. This leads to `IpUtils::checkIp4()` returning the address part of the CIDR notation (instead of `true` which is expected) if it is a valid IPv4 address with subnet mask 0. This change fixes this behaviour. Commits ------- f694aa8 [HttpFoundation] Fix bad return type in IpUtils::checkIp4()
2 parents 387a6b0 + f694aa8 commit b8c7604

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/Symfony/Component/HttpFoundation/IpUtils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function checkIp4(?string $requestIp, string $ip)
8686
[$address, $netmask] = explode('/', $ip, 2);
8787

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

9292
if ($netmask < 0 || $netmask > 32) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,21 @@ public function anonymizedIpData()
164164
['::123.234.235.236', '::123.234.235.0'], // deprecated IPv4-compatible IPv6 address
165165
];
166166
}
167+
168+
/**
169+
* @dataProvider getIp4SubnetMaskZeroData
170+
*/
171+
public function testIp4SubnetMaskZero($matches, $remoteAddr, $cidr)
172+
{
173+
$this->assertSame($matches, IpUtils::checkIp4($remoteAddr, $cidr));
174+
}
175+
176+
public function getIp4SubnetMaskZeroData()
177+
{
178+
return [
179+
[true, '1.2.3.4', '0.0.0.0/0'],
180+
[true, '1.2.3.4', '192.168.1.0/0'],
181+
[false, '1.2.3.4', '256.256.256/0'], // invalid CIDR notation
182+
];
183+
}
167184
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy