Skip to content

Commit 7ac1162

Browse files
committed
Adding a small document on the changes related to trusting proxy data
1 parent 3b9441c commit 7ac1162

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

components/http_foundation/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ HTTP Foundation
55
:maxdepth: 2
66

77
introduction
8+
trusting_proxies
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
.. index::
2+
single: Request; Trusted Proxies
3+
4+
Trusting Proxies
5+
================
6+
7+
If you find yourself behind some sort of proxy - like a load balancer - then
8+
certain header information may be sent to you using special ``X-Forwarded-*``
9+
headers. For example, the ``Host`` HTTP header is usually used to return
10+
the requested host. But when you're behind a proxy, the true host may be
11+
stored in a ``X-Forwarded-Host`` header.
12+
13+
Since HTTP headers can be spoofed, Symfony2 does *not* trust these proxy
14+
headers by default. If you are behind a proxy, you should manually whitelist
15+
your proxy::
16+
17+
use Symfony\Component\HttpFoundation\Request;
18+
19+
$request = Request::createFromGlobals();
20+
// only trust proxy headers coming from this IP address
21+
$request->setTrustedProxies(array(192.0.0.1));
22+
23+
Configuring Header Names
24+
------------------------
25+
26+
By default, the following proxy headers are trusted:
27+
28+
* ``X-Forwarded-For`` Used in :method:`Symfony\\Component\\HttpFoundation\\Request::getClientIp`;
29+
* ``X-Forwarded-Host`` Used in :method:`Symfony\\Component\\HttpFoundation\\Request::getHost`;
30+
* ``X-Forwarded-Port`` Used in :method:`Symfony\\Component\\HttpFoundation\\Request::getPort`;
31+
* ``X-Forwarded-Proto`` Used in :method:`Symfony\\Component\\HttpFoundation\\Request::getScheme` and :method:`Symfony\\Component\\HttpFoundation\\Request::isSecure`;
32+
33+
If your reverse proxy uses a different header name for any of these, you
34+
can configure that header name via :method:`Symfony\\Component\\HttpFoundation\\Request::setTrustedHeaderName`::
35+
36+
$request->setTrustedHeaderName(Request::HEADER_CLIENT_IP, 'X-Proxy-For');
37+
$request->setTrustedHeaderName(Request::HEADER_CLIENT_HOST, 'X-Proxy-Host');
38+
$request->setTrustedHeaderName(Request::HEADER_CLIENT_PORT, 'X-Proxy-Port');
39+
$request->setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, 'X-Proxy-Proto');
40+
41+
Not trusting certain Headers
42+
----------------------------
43+
44+
By default, if you whitelist your proxy's IP address, then all four headers
45+
listed above are trusted. If you need to trust some of these headers but
46+
not others, you can do that as well::
47+
48+
// disables trusting the ``X-Forwarded-Proto`` header, the default header is used
49+
$request->setTrustedHeaderName(Request::HEADER_CLIENT_PROTO, '');

components/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* :doc:`/components/http_foundation/index`
5050

5151
* :doc:`/components/http_foundation/introduction`
52+
* :doc:`/components/http_foundation/trusting_proxies`
5253

5354
* :doc:`/components/http_kernel/index`
5455

reference/configuration/framework.rst

+21
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,30 @@ services related to testing your application (e.g. ``test.client``) are loaded.
9191
This setting should be present in your ``test`` environment (usually via
9292
``app/config/config_test.yml``). For more information, see :doc:`/book/testing`.
9393

94+
trusted_proxies
95+
~~~~~~~~~~~~~~~
96+
97+
**type**: ``array``
98+
99+
Configures the IP addresses that should be trusted as proxies. For more details,
100+
see :doc:`/components/http_foundation/trusting_proxies`.
101+
102+
.. code-block:: yaml
103+
104+
framework:
105+
trusted_proxies: [192.0.0.1]
106+
94107
trust_proxy_headers
95108
~~~~~~~~~~~~~~~~~~~
96109

110+
.. caution::
111+
112+
The ``trust_proxy_headers`` option is deprecated and will be removed in
113+
Symfony 2.3. See `trusted_proxies`_ and :doc:`/components/http_foundation/trusting_proxies`
114+
for details on how to properly trust proxy data.
115+
116+
**Deprecated**: This option will be removed in Symfony 2.3. Instead, use
117+
97118
**type**: ``Boolean``
98119

99120
Configures if HTTP headers (like ``HTTP_X_FORWARDED_FOR``, ``X_FORWARDED_PROTO``, and

0 commit comments

Comments
 (0)