Skip to content

Navigation Menu

Sign in
Appearance settings
Sign up
Appearance settings

Commit 843dcf9

Browse filesBrowse the repository at this point in the historyBrowse files
Amoifrnicolas-grekas
authored andcommitted
[HttpFoundation] Throw when IpUtils::isPrivateIp() receives a non-canonical IP address
1 parent 8212c0d commit 843dcf9
Copy full SHA for 843dcf9

2 files changed

+30Lines changed: 30 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/Symfony/Component/HttpFoundation/IpUtils.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/IpUtils.php
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,15 @@ public static function anonymize(string $ip): string
231231

232232
/**
233233
* Checks if an IPv4 or IPv6 address is contained in the list of private IP subnets.
234+
*
235+
* @throws \ValueError When $requestIp is not a valid IP address
234236
*/
235237
public static function isPrivateIp(string $requestIp): bool
236238
{
239+
if (!filter_var($requestIp, \FILTER_VALIDATE_IP)) {
240+
throw new \ValueError(\sprintf('"%s" is not a valid IP address.', $requestIp));
241+
}
242+
237243
return self::checkIp($requestIp, self::PRIVATE_SUBNETS);
238244
}
239245

Collapse file

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,30 @@ public static function getIsPrivateIpData(): array
211211
];
212212
}
213213

214+
/**
215+
* @dataProvider getIsPrivateIpInvalidData
216+
*/
217+
public function testIsPrivateIpThrowsOnNonCanonicalIp(string $ip)
218+
{
219+
$this->expectException(\ValueError::class);
220+
$this->expectExceptionMessage(\sprintf('"%s" is not a valid IP address.', $ip));
221+
222+
IpUtils::isPrivateIp($ip);
223+
}
224+
225+
public static function getIsPrivateIpInvalidData(): array
226+
{
227+
return [
228+
'decimal' => ['2130706433'],
229+
'hexadecimal' => ['0x7f000001'],
230+
'leading zero' => ['010.0.0.1'],
231+
'short form' => ['127.1'],
232+
'zone id' => ['fe80::1%eth0'],
233+
'not an IP' => ['not-an-ip'],
234+
'empty string' => [''],
235+
];
236+
}
237+
214238
public function testCacheSizeLimit()
215239
{
216240
$ref = new \ReflectionClass(IpUtils::class);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.