Skip to content

Commit 9e48546

Browse files
[HttpFoundation][FrameworkBundle] Revert "trusted proxies" BC break
1 parent 938e0c9 commit 9e48546

File tree

4 files changed

+103
-10
lines changed

4 files changed

+103
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ CHANGELOG
77
* Not defining the `type` option of the `framework.workflows.*` configuration entries is deprecated.
88
The default value will be `state_machine` in Symfony 4.0.
99
* Deprecated the `CompilerDebugDumpPass` class
10-
* [BC BREAK] Removed the "framework.trusted_proxies" configuration option and the corresponding "kernel.trusted_proxies" parameter
10+
* Deprecated the "framework.trusted_proxies" configuration option and the corresponding "kernel.trusted_proxies" parameter
1111
* Added a new new version strategy option called json_manifest_path
1212
that allows you to use the `JsonManifestVersionStrategy`.
1313
* Added `Symfony\Bundle\FrameworkBundle\Controller\AbstractController`. It provides

DependencyInjection/Configuration.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,38 @@ public function getConfigTreeBuilder()
6565
->info("Set true to enable support for the '_method' request parameter to determine the intended HTTP method on POST requests. Note: When using the HttpCache, you need to call the method in your front controller instead")
6666
->defaultTrue()
6767
->end()
68-
->arrayNode('trusted_proxies') // @deprecated in version 3.3, to be removed in 4.0
68+
->arrayNode('trusted_proxies')
6969
->beforeNormalization()
70-
->ifTrue(function ($v) { return empty($v); })
71-
->then(function () { @trigger_error('The "framework.trusted_proxies" configuration key has been removed in Symfony 3.3. Use the Request::setTrustedProxies() method in your front controller instead.', E_USER_DEPRECATED); })
70+
->ifTrue(function ($v) {
71+
@trigger_error('The "framework.trusted_proxies" configuration key has been deprecated in Symfony 3.3. Use the Request::setTrustedProxies() method in your front controller instead.', E_USER_DEPRECATED);
72+
73+
return !is_array($v) && null !== $v;
74+
})
75+
->then(function ($v) { return is_bool($v) ? array() : preg_split('/\s*,\s*/', $v); })
7276
->end()
73-
->beforeNormalization()
74-
->ifTrue(function ($v) { return !empty($v); })
75-
->thenInvalid('The "framework.trusted_proxies" configuration key has been removed in Symfony 3.3. Use the Request::setTrustedProxies() method in your front controller instead.')
77+
->prototype('scalar')
78+
->validate()
79+
->ifTrue(function ($v) {
80+
if (empty($v)) {
81+
return false;
82+
}
83+
84+
if (false !== strpos($v, '/')) {
85+
if ('0.0.0.0/0' === $v) {
86+
return false;
87+
}
88+
89+
list($v, $mask) = explode('/', $v, 2);
90+
91+
if (strcmp($mask, (int) $mask) || $mask < 1 || $mask > (false !== strpos($v, ':') ? 128 : 32)) {
92+
return true;
93+
}
94+
}
95+
96+
return !filter_var($v, FILTER_VALIDATE_IP);
97+
})
98+
->thenInvalid('Invalid proxy IP "%s"')
99+
->end()
76100
->end()
77101
->end()
78102
->scalarNode('ide')->defaultNull()->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ public function load(array $configs, ContainerBuilder $container)
146146

147147
$container->setParameter('kernel.http_method_override', $config['http_method_override']);
148148
$container->setParameter('kernel.trusted_hosts', $config['trusted_hosts']);
149+
if ($config['trusted_proxies']) {
150+
$container->setParameter('kernel.trusted_proxies', $config['trusted_proxies']);
151+
}
149152
$container->setParameter('kernel.default_locale', $config['default_locale']);
150153

151154
if (!$container->hasParameter('debug.file_link_format')) {

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testDoNoDuplicateDefaultFormResources()
4545

4646
/**
4747
* @group legacy
48-
* @expectedDeprecation The "framework.trusted_proxies" configuration key has been removed in Symfony 3.3. Use the Request::setTrustedProxies() method in your front controller instead.
48+
* @expectedDeprecation The "framework.trusted_proxies" configuration key has been deprecated in Symfony 3.3. Use the Request::setTrustedProxies() method in your front controller instead.
4949
*/
5050
public function testTrustedProxiesSetToNullIsDeprecated()
5151
{
@@ -56,7 +56,7 @@ public function testTrustedProxiesSetToNullIsDeprecated()
5656

5757
/**
5858
* @group legacy
59-
* @expectedDeprecation The "framework.trusted_proxies" configuration key has been removed in Symfony 3.3. Use the Request::setTrustedProxies() method in your front controller instead.
59+
* @expectedDeprecation The "framework.trusted_proxies" configuration key has been deprecated in Symfony 3.3. Use the Request::setTrustedProxies() method in your front controller instead.
6060
*/
6161
public function testTrustedProxiesSetToEmptyArrayIsDeprecated()
6262
{
@@ -66,7 +66,8 @@ public function testTrustedProxiesSetToEmptyArrayIsDeprecated()
6666
}
6767

6868
/**
69-
* @expectedException \InvalidArgumentException
69+
* @group legacy
70+
* @expectedDeprecation The "framework.trusted_proxies" configuration key has been deprecated in Symfony 3.3. Use the Request::setTrustedProxies() method in your front controller instead.
7071
*/
7172
public function testTrustedProxiesSetToNonEmptyArrayIsInvalid()
7273
{
@@ -75,6 +76,70 @@ public function testTrustedProxiesSetToNonEmptyArrayIsInvalid()
7576
$processor->processConfiguration($configuration, array(array('trusted_proxies' => array('127.0.0.1'))));
7677
}
7778

79+
/**
80+
* @group legacy
81+
* @dataProvider getTestValidTrustedProxiesData
82+
*/
83+
public function testValidTrustedProxies($trustedProxies, $processedProxies)
84+
{
85+
$processor = new Processor();
86+
$configuration = new Configuration(true);
87+
$config = $processor->processConfiguration($configuration, array(array(
88+
'secret' => 's3cr3t',
89+
'trusted_proxies' => $trustedProxies,
90+
)));
91+
92+
$this->assertEquals($processedProxies, $config['trusted_proxies']);
93+
}
94+
95+
public function getTestValidTrustedProxiesData()
96+
{
97+
return array(
98+
array(array('127.0.0.1'), array('127.0.0.1')),
99+
array(array('::1'), array('::1')),
100+
array(array('127.0.0.1', '::1'), array('127.0.0.1', '::1')),
101+
array(null, array()),
102+
array(false, array()),
103+
array(array(), array()),
104+
array(array('10.0.0.0/8'), array('10.0.0.0/8')),
105+
array(array('::ffff:0:0/96'), array('::ffff:0:0/96')),
106+
array(array('0.0.0.0/0'), array('0.0.0.0/0')),
107+
);
108+
}
109+
110+
/**
111+
* @group legacy
112+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
113+
*/
114+
public function testInvalidTypeTrustedProxies()
115+
{
116+
$processor = new Processor();
117+
$configuration = new Configuration(true);
118+
$processor->processConfiguration($configuration, array(
119+
array(
120+
'secret' => 's3cr3t',
121+
'trusted_proxies' => 'Not an IP address',
122+
),
123+
));
124+
}
125+
126+
/**
127+
* @group legacy
128+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
129+
*/
130+
public function testInvalidValueTrustedProxies()
131+
{
132+
$processor = new Processor();
133+
$configuration = new Configuration(true);
134+
135+
$processor->processConfiguration($configuration, array(
136+
array(
137+
'secret' => 's3cr3t',
138+
'trusted_proxies' => array('Not an IP address'),
139+
),
140+
));
141+
}
142+
78143
public function testAssetsCanBeEnabled()
79144
{
80145
$processor = new Processor();
@@ -156,6 +221,7 @@ protected static function getBundleDefaultConfig()
156221
{
157222
return array(
158223
'http_method_override' => true,
224+
'trusted_proxies' => array(),
159225
'ide' => null,
160226
'default_locale' => 'en',
161227
'csrf_protection' => array(

0 commit comments

Comments
 (0)