Skip to content

Commit b5415d9

Browse files
committed
Updated trusted_hosts documentation with #3876
1 parent 6c09522 commit b5415d9

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

reference/configuration/framework.rst

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -249,33 +249,81 @@ used. It becomes the service container parameter called
249249
trusted_hosts
250250
~~~~~~~~~~~~~
251251

252-
**type**: ``array`` | ``string``
252+
**type**: ``array`` | ``string`` **default**: ``array()``
253253

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.
257273

258274
.. configuration-block::
259275

260276
.. code-block:: yaml
261277
262278
framework:
263-
trusted_hosts: '(^|\.)trusted\.com$'
279+
trusted_hosts: ['acme.com', 'acme.org']
264280
265281
.. code-block:: xml
266282
267283
<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>
270288
271289
.. code-block:: php
272290
273291
$container->loadFromExtension('framework', array(
274-
'trusted_hosts' => '(^|\.)trusted\.com$',
292+
'trusted_hosts' => array('acme.com', 'acme.org'),
275293
));
276294
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.
279327

280328
.. seealso::
281329

0 commit comments

Comments
 (0)