8000 Updated trusted_hosts documentation with #3876 · wouterj/symfony-docs@8a29ad9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a29ad9

Browse files
committed
Updated trusted_hosts documentation with symfony#3876
1 parent a7a16c4 commit 8a29ad9

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
@@ -254,19 +254,35 @@ used. It becomes the service container parameter called
254254
trusted_hosts
255255
~~~~~~~~~~~~~
256256

257-
**type**: ``array`` | ``string``
257+
**type**: ``array`` | ``string`` **default**: ``array()``
258258

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

263279
.. configuration-block::
264280

265281
.. code-block:: yaml
266282
267283
# app/config/config.yml
268284
framework:
269-
trusted_hosts: '(^|\.)trusted\.com$'
285+
trusted_hosts: ['acme.com', 'acme.org']
270286
271287
.. code-block:: xml
272288
@@ -279,19 +295,51 @@ hosts.
279295
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
280296
281297
<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>
284302
</container>
285303
286304
.. code-block:: php
287305
288306
// app/config/config.php
289307
$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$'),
291333
));
292334
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.
295343

296344
.. seealso::
297345

0 commit comments

Comments
 (0)
0