@@ -7,21 +7,22 @@ an AWS Elastic Load Balancer) or a reverse proxy (e.g. Varnish for
7
7
8
8
For the most part, this doesn't cause any problems with Symfony. But, when
9
9
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.
15
14
16
15
If you don't configure Symfony to look for these headers, you'll get incorrect
17
16
information about the client's IP address, whether or not the client is connecting
18
17
via HTTPS, the client's port and the hostname being requested.
19
18
20
- Solution: trusted_proxies
21
- -------------------------
19
+ .. _request-set-trusted-proxies :
22
20
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:
25
26
26
27
.. code-block :: php
27
28
@@ -30,23 +31,21 @@ and which reverse proxy IP addresses will be doing this type of thing:
30
31
// ...
31
32
$request = Request::createFromGlobals();
32
33
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'],
38
38
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
42
41
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
+ );
46
45
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 ``) .
50
49
51
50
But what if the IP of my Reverse Proxy Changes Constantly!
52
51
----------------------------------------------------------
@@ -59,60 +58,24 @@ In this case, you'll need to - *very carefully* - trust *all* proxies.
59
58
other than your load balancers. For AWS, this can be done with `security groups `_.
60
59
61
60
#. 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:
64
62
65
63
.. code-block :: diff
66
64
67
65
// web/app.php
68
66
69
67
// ...
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')),
74
71
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
+ );
77
75
78
76
That's it! It's critical that you prevent traffic from all non-trusted sources.
79
77
If you allow outside traffic, they could "spoof" their true IP address and
80
78
other information.
81
79
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
-
117
80
.. _`security groups` : http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-security-groups.html
118
81
.. _`RFC 7239` : http://tools.ietf.org/html/rfc7239
0 commit comments