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