@@ -22,28 +22,69 @@ Solution: ``setTrustedProxies()``
22
22
---------------------------------
23
23
24
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::
26
-
27
- // public/index.php
28
-
29
- // ...
30
- $request = Request::createFromGlobals();
31
-
32
- // tell Symfony about your reverse proxy
33
- Request::setTrustedProxies(
34
- // the IP address (or range) of your proxy
35
- ['192.0.0.1', '10.0.0.0/8'],
36
-
37
- // trust *all* "X-Forwarded-*" headers
38
- Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO
39
-
40
- // or, if your proxy instead uses the "Forwarded" header
41
- // Request::HEADER_FORWARDED
42
-
43
- // or, if you're using a well-known proxy
44
- // Request::HEADER_X_FORWARDED_AWS_ELB
45
- // Request::HEADER_X_FORWARDED_TRAEFIK
46
- );
25
+ and what headers your reverse proxy uses to send information:
26
+
27
+ .. configuration-block ::
28
+
29
+ .. config-block :: yaml
30
+
31
+ # config/packages/framework.yaml
32
+ framework:
33
+ # ...
34
+ // the IP address (or range) of your proxy
35
+ trusted_proxies: '192.0.0.1,10.0.0.0/8'
36
+ // trust *all * "X-Forwarded-*" headers (the ! prefix means to not trust those headers)
37
+ trusted_headers: ['x-forwarded-all', '!x-forwarded-host', '!x-forwarded-prefix']
38
+ // or, if your proxy instead uses the "Forwarded" header
39
+ trusted_headers: ['forwarded', '!x-forwarded-host', '!x-forwarded-prefix']
40
+ // or, if you're using a wellknown proxy
41
+ trusted_headers: [!php/const Symfony\\ Component\\ HttpFoundation\\ Request::HEADER_X_FORWARDED_AWS_ELB, '!x-forwarded-host', '!x-forwarded-prefix']
42
+ trusted_headers: [!php/const Symfony\\ Component\\ HttpFoundation\\ Request::HEADER_X_FORWARDED_TRAEFIK, '!x-forwarded-host', '!x-forwarded-prefix']
43
+
44
+ .. config-block :: xml
45
+
46
+ <!-- config/packages/framework.xml -->
47
+ <?xml version="1.0" encoding="UTF-8" ?>
48
+ <container xmlns="http://symfony.com/schema/dic/services"
49
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
50
+ xmlns:framework="http://symfony.com/schema/dic/symfony"
51
+ xsi:schemaLocation="http://symfony.com/schema/dic/services
52
+ https://symfony.com/schema/dic/services/services-1.0.xsd
53
+ http://symfony.com/schema/dic/symfony
54
+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
55
+
56
+ <framework:config>
57
+ <!-- the IP address (or range) of your proxy -->
58
+ <framework:trusted-proxies>192.0.0.1,10.0.0.0/8</framework:trusted-proxies>
59
+
60
+ <!-- trust *all * "X-Forwarded-*" headers (the ! prefix means to not trust those headers) -->
61
+ <framework:trusted-header>x-forwarded-all</framework:trusted-header>
62
+ <framework:trusted-header>!x-forwarded-host</framework:trusted-header>
63
+ <framework:trusted-header>!x-forwarded-prefix</framework:trusted-header>
64
+
65
+ <!-- or, if your proxy instead uses the "Forwarded" header -->
66
+ <framework:trusted-header>forwarded</framework:trusted-header>
67
+ <framework:trusted-header>!x-forwarded-host</framework:trusted-header>
68
+ <framework:trusted-header>!x-forwarded-prefix</framework:trusted-header>
69
+ </framework:config>
70
+ </container>
71
+
72
+ .. config-block :: php
73
+
74
+ // config/packages/framework.php
75
+ use Symfony\C omponent\H ttpFoundation\R equest;
76
+
77
+ $container->loadFromExtension('framework', [
78
+ // the IP address (or range) of your proxy
79
+ 'trusted_proxies' => '192.0.0.1,10.0.0.0/8',
80
+ // trust *all * "X-Forwarded-*" headers (the ! prefix means to not trust those headers)
81
+ 'trusted_headers' => ['x-forwarded-all', '!x-forwarded-host', '!x-forwarded-prefix'],
82
+ // or, if your proxy instead uses the "Forwarded" header
83
+ 'trusted_headers' => ['forwarded', '!x-forwarded-host', '!x-forwarded-prefix'],
84
+ // or, if you're using a wellknown proxy
85
+ 'trusted_headers' => [Request::HEADER_X_FORWARDED_AWS_ELB, '!x-forwarded-host', '!x-forwarded-prefix'],
86
+ 'trusted_headers' => [Request::HEADER_X_FORWARDED_TRAEFIK, '!x-forwarded-host', '!x-forwarded-prefix'],
87
+ ]);
47
88
48
89
.. deprecated :: 5.2
49
90
@@ -61,6 +102,13 @@ The Request object has several ``Request::HEADER_*`` constants that control exac
61
102
*which * headers from your reverse proxy are trusted. The argument is a bit field,
62
103
so you can also pass your own value (e.g. ``0b00110 ``).
63
104
105
+ .. versionadded :: 5.2
106
+
107
+ The feature to configure trusted proxies and headers with ``trusted_proxies ``
108
+ and ``trusted_headers `` options was introduced in Symfony 5.2. In earlier
109
+ Symfony versions you needed to use the ``Request::setTrustedProxies() ``
110
+ method in the ``public/index.php `` file.
111
+
64
112
But what if the IP of my Reverse Proxy Changes Constantly!
65
113
----------------------------------------------------------
66
114
@@ -74,17 +122,17 @@ In this case, you'll need to - *very carefully* - trust *all* proxies.
74
122
#. Once you've guaranteed that traffic will only come from your trusted reverse
75
123
proxies, configure Symfony to *always * trust incoming request::
76
124
77
- // public/index.php
125
+ .. config-block:: yaml
78
126
79
- // ...
80
- Request::setTrustedProxies(
81
- // trust *all* requests (the 'REMOTE_ADDR' string is replaced at
82
- // run time by $_SERVER['REMOTE_ADDR'])
83
- ['127.0.0.1', 'REMOTE_ADDR'],
127
+ # config/packages/framework.yaml
128
+ framework:
129
+ # ...
130
+ // trust *all* requests (the 'REMOTE_ADDR' string is replaced at
131
+ // run time by $_SERVER['REMOTE_ADDR'])
132
+ trusted_proxies: '127.0.0.1,REMOTE_ADDR'
84
133
85
- // if you're using ELB, otherwise use a constant from above
86
- Request::HEADER_X_FORWARDED_AWS_ELB
87
- );
134
+ // if you're using ELB, otherwise use another Request::HEADER-* constant
135
+ trusted_headers: [!php/const Symfony\\Component\\HttpFoundation\\Request::HEADER_X_FORWARDED_AWS_ELB, '!x-forwarded-host', '!x-forwarded-prefix']
88
136
89
137
That's it! It's critical that you prevent traffic from all non-trusted sources.
90
138
If you allow outside traffic, they could "spoof" their true IP address and
@@ -100,6 +148,12 @@ other information.
100
148
# .env
101
149
TRUSTED_PROXIES=127.0.0.1,REMOTE_ADDR
102
150
151
+ .. config-block :: yaml
152
+
153
+ # config/packages/framework.yaml
154
+ framework:
155
+ # ...
156
+ trusted_proxies: '%env(TRUSTED_PROXIES)%'
103
157
104
158
If you are also using a reverse proxy on top of your load balancer (e.g.
105
159
`CloudFront `_), calling ``$request->server->get('REMOTE_ADDR') `` won't be
@@ -111,11 +165,13 @@ trusted proxies.
111
165
Custom Headers When Using a Reverse Proxy
112
166
-----------------------------------------
113
167
114
- Some reverse proxies (like `CloudFront `_ with ``CloudFront-Forwarded-Proto ``) may force you to use a custom header.
115
- For instance you have ``Custom-Forwarded-Proto `` instead of ``X-Forwarded-Proto ``.
168
+ Some reverse proxies (like `CloudFront `_ with ``CloudFront-Forwarded-Proto ``)
169
+ may force you to use a custom header. For instance you have
170
+ ``Custom-Forwarded-Proto `` instead of ``X-Forwarded-Proto ``.
116
171
117
- In this case, you'll need to set the header ``X-Forwarded-Proto `` with the value of
118
- ``Custom-Forwarded-Proto `` early enough in your application, i.e. before handling the request::
172
+ In this case, you'll need to set the header ``X-Forwarded-Proto `` with the value
173
+ of ``Custom-Forwarded-Proto `` early enough in your application, i.e. before
174
+ handling the request::
119
175
120
176
// public/index.php
121
177
0 commit comments