Skip to content

[HttpFoundation] Add PRIVATE_SUBNETS as a shortcut for private IP address ranges to Request::setTrustedProxies() #58154

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
Sep 3, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
->prototype('scalar')->end()
->end()
->scalarNode('trusted_proxies')
->variableNode('trusted_proxies')
->beforeNormalization()
->ifTrue(fn ($v) => 'private_ranges' === $v)
->then(fn ($v) => implode(',', IpUtils::PRIVATE_SUBNETS))
->ifTrue(fn ($v) => 'private_ranges' === $v || 'PRIVATE_SUBNETS' === $v)
->then(fn () => IpUtils::PRIVATE_SUBNETS)
->end()
->end()
->arrayNode('trusted_headers')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2354,7 +2354,7 @@ public function testTrustedProxiesWithPrivateRanges()
{
$container = $this->createContainerFromFile('trusted_proxies_private_ranges');

$this->assertSame(IpUtils::PRIVATE_SUBNETS, array_map('trim', explode(',', $container->getParameter('kernel.trusted_proxies'))));
$this->assertSame(IpUtils::PRIVATE_SUBNETS, $container->getParameter('kernel.trusted_proxies'));
}

public function testWebhook()
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 @@ -6,6 +6,7 @@ CHANGELOG

* Add optional `$requests` parameter to `RequestStack::__construct()`
* Add optional `$v4Bytes` and `$v6Bytes` parameters to `IpUtils::anonymize()`
* Add `PRIVATE_SUBNETS` as a shortcut for private IP address ranges to `Request::setTrustedProxies()`

7.1
---
Expand Down
24 changes: 15 additions & 9 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,20 +520,26 @@ public function overrideGlobals(): void
*
* You should only list the reverse proxies that you manage directly.
*
* @param array $proxies A list of trusted proxies, the string 'REMOTE_ADDR' will be replaced with $_SERVER['REMOTE_ADDR']
* @param int $trustedHeaderSet A bit field of Request::HEADER_*, to set which headers to trust from your proxies
* @param array $proxies A list of trusted proxies, the string 'REMOTE_ADDR' will be replaced with $_SERVER['REMOTE_ADDR'] and 'PRIVATE_SUBNETS' by IpUtils::PRIVATE_SUBNETS
* @param int-mask-of<Request::HEADER_*> $trustedHeaderSet A bit field to set which headers to trust from your proxies
*/
public static function setTrustedProxies(array $proxies, int $trustedHeaderSet): void
{
self::$trustedProxies = array_reduce($proxies, function ($proxies, $proxy) {
if ('REMOTE_ADDR' !== $proxy) {
$proxies[] = $proxy;
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$proxies[] = $_SERVER['REMOTE_ADDR'];
if (false !== $i = array_search('REMOTE_ADDR', $proxies, true)) {
if (isset($_SERVER['REMOTE_ADDR'])) {
$proxies[$i] = $_SERVER['REMOTE_ADDR'];
} else {
unset($proxies[$i]);
$proxies = array_values($proxies);
}
}

if (false !== ($i = array_search('PRIVATE_SUBNETS', $proxies, true)) || false !== ($i = array_search('private_ranges', $proxies, true))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should private_ranges be deprecated in favor of PRIVATE_SUBNETS or no ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's worth the trouble for the community.

unset($proxies[$i]);
$proxies = array_merge($proxies, IpUtils::PRIVATE_SUBNETS);
}

return $proxies;
}, []);
self::$trustedProxies = $proxies;
self::$trustedHeaderSet = $trustedHeaderSet;
}

Expand Down
31 changes: 21 additions & 10 deletions src/Symfony/Component/HttpFoundation/Tests/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\HttpFoundation\Exception\JsonException;
use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\IpUtils;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
Expand Down Expand Up @@ -2564,6 +2565,26 @@ public function testTrustedProxiesRemoteAddr($serverRemoteAddr, $trustedProxies,
$this->assertSame($result, Request::getTrustedProxies());
}

public static function trustedProxiesRemoteAddr()
{
return [
['1.1.1.1', ['REMOTE_ADDR'], ['1.1.1.1']],
['1.1.1.1', ['REMOTE_ADDR', '2.2.2.2'], ['1.1.1.1', '2.2.2.2']],
[null, ['REMOTE_ADDR'], []],
[null, ['REMOTE_ADDR', '2.2.2.2'], ['2.2.2.2']],
];
}

/**
* @testWith ["PRIVATE_SUBNETS"]
* ["private_ranges"]
*/
public function testTrustedProxiesPrivateSubnets(string $key)
{
Request::setTrustedProxies([$key], Request::HEADER_X_FORWARDED_FOR);
$this->assertSame(IpUtils::PRIVATE_SUBNETS, Request::getTrustedProxies());
}

public function testTrustedValuesCache()
{
$request = Request::create('http://example.com/');
Expand All @@ -2581,16 +2602,6 @@ public function testTrustedValuesCache()
$this->assertFalse($request->isSecure());
}

public static function trustedProxiesRemoteAddr()
{
return [
['1.1.1.1', ['REMOTE_ADDR'], ['1.1.1.1']],
['1.1.1.1', ['REMOTE_ADDR', '2.2.2.2'], ['1.1.1.1', '2.2.2.2']],
[null, ['REMOTE_ADDR'], []],
[null, ['REMOTE_ADDR', '2.2.2.2'], ['2.2.2.2']],
];
}

/**
* @dataProvider preferSafeContentData
*/
Expand Down
Loading