Skip to content

Commit c8adc20

Browse files
feature #52924 [FrameworkBundle] add a private_ranges shortcut for trusted_proxies (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [FrameworkBundle] add a private_ranges shortcut for trusted_proxies | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #51826 | License | MIT Commits ------- 14dff82 add a private_ranges shortcut for trusted_proxies
2 parents b39c939 + 14dff82 commit c8adc20

File tree

6 files changed

+32
-1
lines changed

6 files changed

+32
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
7.1
55
---
66

7+
* Add `private_ranges` as a shortcut for private IP address ranges to the `trusted_proxies` option
78
* Mark classes `ConfigBuilderCacheWarmer`, `Router`, `SerializerCacheWarmer`, `TranslationsCacheWarmer`, `Translator` and `ValidatorCacheWarmer` as `final`
89
* Move the Router `cache_dir` to `kernel.build_dir`
910
* Deprecate the `router.cache_dir` config option

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
3030
use Symfony\Component\HttpClient\HttpClient;
3131
use Symfony\Component\HttpFoundation\Cookie;
32+
use Symfony\Component\HttpFoundation\IpUtils;
3233
use Symfony\Component\Lock\Lock;
3334
use Symfony\Component\Lock\Store\SemaphoreStore;
3435
use Symfony\Component\Mailer\Mailer;
@@ -111,7 +112,12 @@ public function getConfigTreeBuilder(): TreeBuilder
111112
->beforeNormalization()->ifString()->then(fn ($v) => [$v])->end()
112113
->prototype('scalar')->end()
113114
->end()
114-
->scalarNode('trusted_proxies')->end()
115+
->scalarNode('trusted_proxies')
116+
->beforeNormalization()
117+
->ifTrue(fn ($v) => 'private_ranges' === $v)
118+
->then(fn ($v) => implode(',', IpUtils::PRIVATE_SUBNETS))
119+
->end()
120+
->end()
115121
->arrayNode('trusted_headers')
116122
->fixXmlConfig('trusted_header')
117123
->performNoDeepMerging()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'trusted_proxies' => 'private_ranges',
5+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<framework:config trusted-proxies="private_ranges" />
9+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
framework:
2+
trusted_proxies: private_ranges

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTestCase.php

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Symfony\Component\HttpClient\MockHttpClient;
5353
use Symfony\Component\HttpClient\RetryableHttpClient;
5454
use Symfony\Component\HttpClient\ScopingHttpClient;
55+
use Symfony\Component\HttpFoundation\IpUtils;
5556
use Symfony\Component\HttpKernel\DependencyInjection\LoggerPass;
5657
use Symfony\Component\HttpKernel\Fragment\FragmentUriGeneratorInterface;
5758
use Symfony\Component\Messenger\Bridge\AmazonSqs\Transport\AmazonSqsTransportFactory;
@@ -2265,6 +2266,13 @@ public function testNotifierWithSpecificMessageBus()
22652266
$this->assertEquals(new Reference('app.another_bus'), $container->getDefinition('notifier.channel.sms')->getArgument(1));
22662267
}
22672268

2269+
public function testTrustedProxiesWithPrivateRanges()
2270+
{
2271+
$container = $this->createContainerFromFile('trusted_proxies_private_ranges');
2272+
2273+
$this->assertSame(IpUtils::PRIVATE_SUBNETS, array_map('trim', explode(',', $container->getParameter('kernel.trusted_proxies'))));
2274+
}
2275+
22682276
public function testWebhook()
22692277
{
22702278
if (!class_exists(WebhookController::class)) {

0 commit comments

Comments
 (0)