Skip to content

[HttpFoundation] Add IpUtils::isPrivateIp #49726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions src/Symfony/Component/HttpClient/NoPrivateNetworkHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,6 @@ final class NoPrivateNetworkHttpClient implements HttpClientInterface, LoggerAwa
{
use HttpClientTrait;

private const PRIVATE_SUBNETS = [
'127.0.0.0/8',
'10.0.0.0/8',
'192.168.0.0/16',
'172.16.0.0/12',
'169.254.0.0/16',
'0.0.0.0/8',
'240.0.0.0/4',
'::1/128',
'fc00::/7',
'fe80::/10',
'::ffff:0:0/96',
'::/128',
];

private HttpClientInterface $client;
private string|array|null $subnets;

Expand Down Expand Up @@ -74,7 +59,7 @@ public function request(string $method, string $url, array $options = []): Respo

$options['on_progress'] = function (int $dlNow, int $dlSize, array $info) use ($onProgress, $subnets, &$lastPrimaryIp): void {
if ($info['primary_ip'] !== $lastPrimaryIp) {
if ($info['primary_ip'] && IpUtils::checkIp($info['primary_ip'], $subnets ?? self::PRIVATE_SUBNETS)) {
if ($info['primary_ip'] && IpUtils::checkIp($info['primary_ip'], $subnets ?? IpUtils::PRIVATE_SUBNETS)) {
throw new TransportException(sprintf('IP "%s" is blocked for "%s".', $info['primary_ip'], $info['url']));
}

Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/HttpClient/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
"symfony/process": "^5.4|^6.0",
"symfony/stopwatch": "^5.4|^6.0"
},
"conflict": {
"symfony/http-foundation": "<6.3"
},
"autoload": {
"psr-4": { "Symfony\\Component\\HttpClient\\": "" },
"exclude-from-classmap": [
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Create migration for session table when pdo handler is used
* Add support for Relay PHP extension for Redis
* The `Response::sendHeaders()` method now takes an optional HTTP status code as parameter, allowing to send informational responses such as Early Hints responses (103 status code)
* Add `IpUtils::isPrivateIp`
* Deprecate conversion of invalid values in `ParameterBag::getInt()` and `ParameterBag::getBoolean()`,
* Deprecate ignoring invalid values when using `ParameterBag::filter()`, unless flag `FILTER_NULL_ON_FAILURE` is set

Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/HttpFoundation/IpUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
*/
class IpUtils
{
public const PRIVATE_SUBNETS = [
'127.0.0.0/8', // RFC1700 (Loopback)
'10.0.0.0/8', // RFC1918
'192.168.0.0/16', // RFC1918
'172.16.0.0/12', // RFC1918
'169.254.0.0/16', // RFC3927
'0.0.0.0/8', // RFC5735
'240.0.0.0/4', // RFC1112
'::1/128', // Loopback
'fc00::/7', // Unique Local Address
'fe80::/10', // Link Local Address
'::ffff:0:0/96', // IPv4 translations
'::/128', // Unspecified address
];

private static array $checkedIps = [];

/**
Expand Down Expand Up @@ -191,4 +206,12 @@ public static function anonymize(string $ip): string

return $ip;
}

/**
* Checks if an IPv4 or IPv6 address is contained in the list of private IP subnets.
*/
public static function isPrivateIp(string $requestIp): bool
{
return self::checkIp($requestIp, self::PRIVATE_SUBNETS);
}
}
31 changes: 31 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,35 @@ public static function getIp4SubnetMaskZeroData()
[false, '1.2.3.4', '256.256.256/0'], // invalid CIDR notation
];
}

/**
* @dataProvider getIsPrivateIpData
*/
public function testIsPrivateIp(string $ip, bool $matches)
{
$this->assertSame($matches, IpUtils::isPrivateIp($ip));
}

public static function getIsPrivateIpData(): array
{
return [
// private
['127.0.0.1', true],
['10.0.0.1', true],
['192.168.0.1', true],
['172.16.0.1', true],
['169.254.0.1', true],
['0.0.0.1', true],
['240.0.0.1', true],
['::1', true],
['fc00::1', true],
['fe80::1', true],
['::ffff:0:1', true],
['fd00::1', true],

// public
['104.26.14.6', false],
['2606:4700:20::681a:e06', false],
];
}
}