Skip to content

Commit 45b419d

Browse files
committed
Adding second argument to setTrustedProxies() and removing old information
1 parent 2f83164 commit 45b419d

File tree

4 files changed

+31
-147
lines changed

4 files changed

+31
-147
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: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,10 @@ 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 call the :method:`Symfony\\Component\\HttpFoundation\\Request::setTrustedProxies`
23+
Remember to call the :ref:`Request::setTrustedProxies() <request-set-trusted-proxies>`
2424
method in your front controller so that Varnish is seen as a trusted proxy
2525
and the :ref:`X-Forwarded <varnish-x-forwarded-headers>` headers are used.
2626

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 {
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>`.
41-
4227
.. _varnish-x-forwarded-headers:
4328

4429
Routing and X-FORWARDED Headers

request/load_balancer_reverse_proxy.rst

Lines changed: 29 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ 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+
-----------------------------
23+
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:
2526

2627
.. code-block:: php
2728
@@ -30,23 +31,21 @@ and which reverse proxy IP addresses will be doing this type of thing:
3031
// ...
3132
$request = Request::createFromGlobals();
3233
33-
// use the setTrustedProxies() method to tell Symfony
34-
// about your reverse proxy IP addresses
35-
Request::setTrustedProxies(['127.0.0.1', '10.0.0.0/8']);
36-
37-
// ...
34+
// tell Symfony about your revers proxy
35+
Request::setTrustedProxies(
36+
// the ip address (or range) of your proxy
37+
['192.0.0.1', '10.0.0.0/8'],
3838
39-
In this example, you're saying that your reverse proxy (or proxies) has
40-
the IP address ``192.0.0.1`` or matches the range of IP addresses that use
41-
the CIDR notation ``10.0.0.0/8``.
39+
// trust *all* "X-Forwarded-*" headers
40+
Request::HEADER_X_FORWARDED_ALL
4241
43-
You are also saying that you trust that the proxy does not send conflicting
44-
headers, e.g. sending both ``X-Forwarded-For`` and ``Forwarded`` in the same
45-
request.
42+
// or, if your proxy instead uses the "Forwarded" header
43+
// Request::HEADER_FORWARDED
44+
);
4645
47-
That's it! Symfony will now look for the correct headers to get information
48-
like the client's IP address, host, port and whether the request is
49-
using HTTPS.
46+
The Request object has several ``Request::HEADER_*`` constants that control exactly
47+
*which* headers from your reverse proxy are trusted. The argument is a bit field,
48+
so you can also pass your own value (e.g. ``0b00110``).
5049

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

6160
#. Once you've guaranteed that traffic will only come from your trusted reverse
62-
proxies, configure Symfony to *always* trust incoming request. This is
63-
done inside of your front controller:
61+
proxies, configure Symfony to *always* trust incoming request:
6462

6563
.. code-block:: diff
6664
6765
// web/app.php
6866

6967
// ...
70-
$request = Request::createFromGlobals();
71-
+ Request::setTrustedProxies(array('127.0.0.1', $request->server->get('REMOTE_ADDR')));
72-
73-
// ...
68+
Request::setTrustedProxies(
69+
// trust *all* requests
70+
array('127.0.0.1', $request->server->get('REMOTE_ADDR')),
7471

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

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

82-
.. _request-untrust-header:
83-
84-
My Reverse Proxy Sends X-Forwarded-For but Does not Filter the Forwarded Header
85-
-------------------------------------------------------------------------------
86-
87-
Many popular proxy implementations do not yet support the ``Forwarded`` header
88-
and do not filter it by default. Ideally, you would configure this in your
89-
proxy. If this is not possible, you can tell Symfony to distrust the ``Forwarded``
90-
header, while still trusting your proxy's ``X-Forwarded-For`` header.
91-
92-
This is done inside of your front controller::
93-
94-
// web/app.php
95-
96-
// ...
97-
Request::setTrustedHeaderName(Request::HEADER_FORWARDED, null);
98-
99-
$response = $kernel->handle($request);
100-
// ...
101-
102-
Configuring the proxy server trust is very important, as not doing so will
103-
allow malicious users to "spoof" their IP address.
104-
105-
My Reverse Proxy Uses Non-Standard (not X-Forwarded) Headers
106-
------------------------------------------------------------
107-
108-
Although `RFC 7239`_ recently defined a standard ``Forwarded`` header to disclose
109-
all proxy information, most reverse proxies store information in non-standard
110-
``X-Forwarded-*`` headers.
111-
112-
But if your reverse proxy uses other non-standard header names, you can configure
113-
these (see ":doc:`/components/http_foundation/trusting_proxies`").
114-
115-
The code for doing this will need to live in your front controller (e.g. ``web/app.php``).
116-
11780
.. _`security groups`: http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-security-groups.html
11881
.. _`RFC 7239`: http://tools.ietf.org/html/rfc7239

0 commit comments

Comments
 (0)