@@ -249,33 +249,81 @@ used. It becomes the service container parameter called
249
249
trusted_hosts
250
250
~~~~~~~~~~~~~
251
251
252
- **type **: ``array `` | ``string ``
252
+ **type **: ``array `` | ``string `` ** default **: `` array() ``
253
253
254
- To prevent `HTTP Host header attacks `_, you need to configure a list of trusted
255
- hosts. This is an array of regexes (or a single regex) which define the trusted
256
- hosts.
254
+ A lot of different attacks have been discovered relying on inconsistencies
255
+ between the handling of the ``Host `` header by various software (web servers,
256
+ reverse proxies, web frameworks, etc.). Basically, everytime the framework is
257
+ generating an absolute URL (when sending an email to reset a password for
258
+ instance), the host might have been manipulated by an attacker.
259
+
260
+ .. seealso ::
261
+
262
+ You can read "`HTTP Host header attacks `_" for more information about these
263
+ kinds of attacks.
264
+
265
+ The Symfony :method: `Request::getHost()
266
+ <Symfony\\ Component\\ HttpFoundation\\ Request:getHost> ` method might be
267
+ vulnerable to some of these attacks because it depends on the configuration of
268
+ your web server. One simple solution to avoid these attacks is to whitelist the
269
+ hosts that your Symfony application can respond to. That's the purpose of this
270
+ ``trusted_hosts `` option. If the incoming request's hostname doesn't match one
271
+ in this list, the application won't respond and the user will receive a 500
272
+ response.
257
273
258
274
.. configuration-block ::
259
275
260
276
.. code-block :: yaml
261
277
262
278
framework :
263
- trusted_hosts : ' (^|\.)trusted\. com$ '
279
+ trusted_hosts : ['acme. com', 'acme.org']
264
280
265
281
.. code-block :: xml
266
282
267
283
<framework : config >
268
- <trusted-host >(^|\.)trusted\.com$</trusted-host >
269
- </framework : config >
284
+ <trusted-host >acme.com</trusted-host >
285
+ <trusted-host >acme.org</trusted-host >
286
+ <!-- ... -->
287
+ </framework >
270
288
271
289
.. code-block :: php
272
290
273
291
$container->loadFromExtension('framework', array(
274
- 'trusted_hosts' => '(^|\.)trusted\. com$' ,
292
+ 'trusted_hosts' => array('acme. com', 'acme.org') ,
275
293
));
276
294
277
- The above configuration allows for the ``trusted.com `` host (and all its
278
- subdomains).
295
+ Hosts can also be configured using regular expressions, which make it easier to
296
+ respond to any subdomain:
297
+
298
+ .. configuration-block ::
299
+
300
+ .. code-block :: yaml
301
+
302
+ framework :
303
+ trusted_hosts : ['.*\.?acme.com$', '.*\.?acme.org$']
304
+
305
+ .. code-block :: xml
306
+
307
+ <framework : config >
308
+ <trusted-host >.*\.?acme.com$</trusted-host >
309
+ <trusted-host >.*\.?acme.org$</trusted-host >
310
+ <!-- ... -->
311
+ </framework >
312
+
313
+ .. code-block :: php
314
+
315
+ $container->loadFromExtension('framework', array(
316
+ 'trusted_hosts' => array('.*\.?acme.com$', '.*\.?acme.org$'),
317
+ ));
318
+
319
+ In addition, you can also set the trusted hosts in the front controller using
320
+ the ``Request::setTrustedHosts() `` method::
321
+
322
+ // web/app.php
323
+ Request::setTrustedHosts(array('.*\.?acme.com$', '.*\.?acme.org$'));
324
+
325
+ The default value for this option is an empty array, meaning that the application
326
+ can respond to any given host.
279
327
280
328
.. seealso ::
281
329
0 commit comments