8000 feature #7952 [3.3] Finishing setTrustedProxies() changes & trusted_p… · symfony/symfony-docs@f89e2d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit f89e2d0

Browse files
committed
feature #7952 [3.3] Finishing setTrustedProxies() changes & trusted_proxies removal (JarJak, xabbuh, weaverryan)
This PR was merged into the 3.3 branch. Discussion ---------- [3.3] Finishing setTrustedProxies() changes & trusted_proxies removal Finishes #7868 Commits ------- c7087bf tweaks thanks to review 45b419d Adding second argument to setTrustedProxies() and removing old information 2f83164 replace trusted_proxies reference 3632c08 replace diff code block with PHP code block 1825c83 Remove deprecated trusted_proxies config option df63034 Remove deprecated trusted_proxies config option
2 parents 60e0eec + c7087bf commit f89e2d0

File tree

5 files changed

+39
-205
lines changed

5 files changed

+39
-205
lines changed

_build/redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,4 @@
338338
/security/target_path /security
339339
/service_container/third_party /service_container
340340
/templating/templating_service /templates
341+
/components/http_foundation/trusting_proxies /request/load_balancer_reverse_proxy

components/http_foundation/trusting_proxies.rst

Lines changed: 0 additions & 65 deletions
This file was deleted.

http_cache/varnish.rst

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,9 @@ Varnish automatically forwards the IP as ``X-Forwarded-For`` and leaves the
2020
trusted proxy, Symfony will see all requests as coming through insecure HTTP
2121
connections from the Varnish host instead of the real client.
2222

23-
Remember to configure :ref:`framework.trusted_proxies <reference-framework-trusted-proxies>`
24-
in the Symfony configuration so that Varnish is seen as a trusted proxy and the
25-
:ref:`X-Forwarded <varnish-x-forwarded-headers>` headers are used.
26-
27-
Varnish, in its default configuration, sends the ``X-Forwarded-For`` header but
28-
does not filter out the ``Forwarded`` header. If you have access to the Varnish
29-
configuration file, you can configure Varnish to remove the ``Forwarded``
30-
header:
31-
32-
.. code-block:: varnish4
33-
34-
sub vcl_recv {< 10000 /div>
35-
unset req.http.Forwarded;
36-
}
37-
38-
If you do not have access to your Varnish configuration, you can instead
39-
configure Symfony to distrust the ``Forwarded`` header as detailed in
40-
:ref:`How to Configure Symfony to Work behind a Load Balancer or a Reverse Proxy <request-untrust-header>`.
23+
Remember to call the :ref:`Request::setTrustedProxies() <request-set-trusted-proxies>`
24+
method in your front controller so that Varnish is seen as a trusted proxy
25+
and the :ref:`X-Forwarded-* <varnish-x-forwarded-headers>` headers are used.
4126

4227
.. _varnish-x-forwarded-headers:
4328

reference/configuration/framework.rst

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ Configuration
2222

2323
* `secret`_
2424
* `http_method_override`_
25-
* `trusted_proxies`_
2625
* `ide`_
2726
* `test`_
2827
* `default_locale`_
@@ -188,39 +187,7 @@ named ``kernel.http_method_override``.
188187
trusted_proxies
189188
~~~~~~~~~~~~~~~
190189

191-
**type**: ``array``
192-
193-
Configures the IP addresses that should be trusted as proxies. For more
194-
details, see :doc:`/request/load_balancer_reverse_proxy`.
195-
196-
.. configuration-block::
197-
198-
.. code-block:: yaml
199-
200-
# app/config/config.yml
201-
framework:
202-
trusted_proxies: [192.0.0.1, 10.0.0.0/8]
203-
204-
.. code-block:: xml
205- 57AE
206-
<!-- app/config/config.xml -->
207-
<?xml version="1.0" encoding="UTF-8" ?>
208-
<container xmlns="http://symfony.com/schema/dic/services"
209-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
210-
xmlns:framework="http://symfony.com/schema/dic/symfony"
211-
xsi:schemaLocation="http://symfony.com/schema/dic/services
212-
http://symfony.com/schema/dic/services/services-1.0.xsd
213-
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
214-
215-
<framework:config trusted-proxies="192.0.0.1, 10.0.0.0/8" />
216-
</container>
217-
218-
.. code-block:: php
219-
220-
// app/config/config.php
221-
$container->loadFromExtension('framework', array(
222-
'trusted_proxies' => array('192.0.0.1', '10.0.0.0/8'),
223-
));
190+
The ``trusted_proxies`` option was removed in Symfony 3.3. See :doc:`/request/load_balancer_reverse_proxy`.
224191

225192
ide
226193
~~~

request/load_balancer_reverse_proxy.rst

Lines changed: 34 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,48 @@ an AWS Elastic Load Balancer) or a reverse proxy (e.g. Varnish for
77

88
For the most part, this doesn't cause any problems with Symfony. But, when
99
a request passes through a proxy, certain request information is sent using
10-
either the standard ``Forwarded`` header or non-standard special ``X-Forwarded-*``
11-
headers. For example, instead of reading the ``REMOTE_ADDR`` header (which
12-
will now be the IP address of your reverse proxy), the user's true IP will be
13-
stored in a standard ``Forwarded: for="..."`` header or a non standard
14-
``X-Forwarded-For`` header.
10+
either the standard ``Forwarded`` header or ``X-Forwarded-*`` headers. For example,
11+
instead of reading the ``REMOTE_ADDR`` header (which will now be the IP address of
12+
your reverse proxy), the user's true IP will be stored in a standard ``Forwarded: for="..."``
13+
header or a ``X-Forwarded-For`` header.
1514

1615
If you don't configure Symfony to look for these headers, you'll get incorrect
1716
information about the client's IP address, whether or not the client is connecting
1817
via HTTPS, the client's port and the hostname being requested.
1918

20-
Solution: trusted_proxies
21-
-------------------------
19+
.. _request-set-trusted-proxies:
2220

23-
This is no problem, but you *do* need to tell Symfony what is happening
24-
and which reverse proxy IP addresses will be doing this type of thing:
21+
Solution: setTrustedProxies()
22+
-----------------------------
2523

26-
.. configuration-block::
24+
To fix this, you need to tell Symfony which reverse proxy IP addresses to trust
25+
and what headers your reverse proxy uses to send information:
2726

28-
.. code-block:: yaml
27+
.. code-block:: php
2928
30-
# app/config/config.yml
31-
# ...
32-
framework:
33-
trusted_proxies: [192.0.0.1, 10.0.0.0/8]
29+
// web/app.php
3430
35-
.. code-block:: xml
31+
// ...
32+
$request = Request::createFromGlobals();
3633
37-
<!-- app/config/config.xml -->
38-
<?xml version="1.0" encoding="UTF-8" ?>
39-
<container xmlns="http://symfony.com/schema/dic/services"
40-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
41-
xmlns:framework="http://symfony.com/schema/dic/symfony"
42-
xsi:schemaLocation="http://symfony.com/schema/dic/services
43-
http://symfony.com/schema/dic/services/services-1.0.xsd
44-
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
34+
// tell Symfony about your reverse proxy
35+
Request::setTrustedProxies(
36+
// the IP address (or range) of your proxy
37+
['192.0.0.1', '10.0.0.0/8'],
4538
46-
<framework:config trusted-proxies="192.0.0.1, 10.0.0.0/8">
47-
<!-- ... -->
48-
</framework:config>
49-
</container>
39+
// trust *all* "X-Forwarded-*" headers
40+
Request::HEADER_X_FORWARDED_ALL
5041
51-
.. code-block:: php
42+
// or, if your proxy instead uses the "Forwarded" header
43+
// Request::HEADER_FORWARDED
5244
53-
// app/config/config.php
54-
$container->loadFromExtension('framework', array(
55-
'trusted_proxies' => array('192.0.0.1', '10.0.0.0/8'),
56-
));
45+
// or, if you're using AWS ELB
46+
// Request::HEADER_X_FORWARDED_AWS_ELB
47+
);
5748
58-
In this example, you're saying that your reverse proxy (or proxies) has
59-
the IP address ``192.0.0.1`` or matches the range of IP addresses that use
60-
the CIDR notation ``10.0.0.0/8``. For more details, see the
61-
:ref:`framework.trusted_proxies <reference-framework-trusted-proxies>` option.
62-
63-
You are also saying that you trust that the proxy does not send conflicting
64-
headers, e.g. sending both ``X-Forwarded-For`` and ``Forwarded`` in the same
65-
request.
66-
67-
That's it! Symfony will now look for the correct headers to get information
68-
like the client's IP address, host, port and whether the request is
69-
using HTTPS.
49+
The Request object has several ``Request::HEADER_*`` constants that control exactly
50+
*which* headers from your reverse proxy are trusted. The argument is a bit field,
51+
so you can also pass your own value (e.g. ``0b00110``).
7052

7153
But what if the IP of my Reverse Proxy Changes Constantly!
7254
----------------------------------------------------------
@@ -79,60 +61,24 @@ In this case, you'll need to - *very carefully* - trust *all* proxies.
7961
other than your load balancers. For AWS, this can be done with `security groups`_.
8062

8163
#. Once you've guaranteed that traffic will only come from your trusted reverse
82-
proxies, configure Symfony to *always* trust incoming request. This is
83-
done inside of your front controller:
64+
proxies, configure Symfony to *always* trust incoming request:
8465

8566
.. code-block:: diff
8667
8768
// web/app.php
8869

8970
// ...
90 10000 -
$request = Request::createFromGlobals();
91-
+ Request::setTrustedProxies(array('127.0.0.1', $request->server->get('REMOTE_ADDR')));
92-
93-
// ...
71+
Request::setTrustedProxies(
72+
// trust *all* requests
73+
array('127.0.0.1', $request->server->get('REMOTE_ADDR')),
9474

95-
#. Ensure that the trusted_proxies setting in your ``app/config/config.yml``
96-
is not set or it will overwrite the ``setTrustedProxies()`` call above.
75+
// if you're using ELB, otherwise use a constant from above
76+
Request::HEADER_X_FORWARDED_AWS_ELB
77+
);
9778

9879
That's it! It's critical that you prevent traffic from all non-trusted sources.
9980
If you allow outside traffic, they could "spoof" their true IP address and
10081
other information.
10182

102-
.. _request-untrust-header:
103-
104-
My Reverse Proxy Sends X-Forwarded-For but Does not Filter the Forwarded Header
105-
-------------------------------------------------------------------------------
106-
107-
Many popular proxy implementations do not yet support the ``Forwarded`` header
108-
and do not filter it by default. Ideally, you would configure this in your
109-
proxy. If this is not possible, you can tell Symfony to distrust the ``Forwarded``
110-
header, while still trusting your proxy's ``X-Forwarded-For`` header.
111-
112-
This is done inside of your front controller::
113-
114-
// web/app.php
115-
116-
// ...
117-
Request::setTrustedHeaderName(Request::HEADER_FORWARDED, null);
118-
119-
$response = $kernel->handle($request);
120-
// ...
121-
122-
Configuring the proxy server trust is very important, as not doing so will
123-
allow malicious users to "spoof" their IP address.
124-
125-
My Reverse Proxy Uses Non-Standard (not X-Forwarded) Headers
126-
------------------------------------------------------------
127-
128-
Although `RFC 7239`_ recently defined a standard ``Forwarded`` header to disclose
129-
all proxy information, most reverse proxies store information in non-standard
130-
``X-Forwarded-*`` headers.
131-
132-
But if your reverse proxy uses other non-standard header names, you can configure
133-
these (see ":doc:`/components/http_foundation/trusting_proxies`").
134-
135-
The code for doing this will need to live in your front controller (e.g. ``web/app.php``).
136-
13783
.. _`security groups`: http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-security-groups.html
13884
.. _`RFC 7239`: http://tools.ietf.org/html/rfc7239

0 commit comments

Comments
 (0)
0