Skip to content

Commit 83afb68

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: Minor tweak [HttpFoundation] Add `Request::setAllowedHttpMethodOverride()` and `allowed_http_method_override` config option
2 parents db15305 + 28196ae commit 83afb68

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

forms.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,10 @@ to the ``form()`` or the ``form_start()`` helper functions:
761761
``DELETE`` request. The :ref:`http_method_override <configuration-framework-http_method_override>`
762762
option must be enabled for this to work.
763763

764+
For security, you can restrict which HTTP methods can be overridden using the
765+
:ref:`allowed_http_method_override <configuration-framework-allowed_http_method_override>`
766+
option.
767+
764768
Changing the Form Name
765769
~~~~~~~~~~~~~~~~~~~~~~
766770

reference/configuration/framework.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,85 @@ named ``kernel.http_method_override``.
17751775
$request = Request::createFromGlobals();
17761776
// ...
17771777

1778+
.. _configuration-framework-allowed_http_method_override:
1779+
1780+
allowed_http_method_override
1781+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1782+
1783+
.. versionadded:: 7.4
1784+
1785+
The ``allowed_http_method_override`` option was introduced in Symfony 7.4.
1786+
1787+
**type**: ``array`` **default**: ``null``
1788+
1789+
This option controls which HTTP methods can be overridden via the ``_method``
1790+
request parameter or the ``X-HTTP-METHOD-OVERRIDE`` header when
1791+
:ref:`http_method_override <configuration-framework-http_method_override>` is enabled.
1792+
1793+
When set to ``null`` (the default), all HTTP methods can be overridden. When set
1794+
to an empty array (``[]``), HTTP method overriding is completely disabled. When
1795+
set to a specific list of methods, only those methods will be allowed as overrides:
1796+
1797+
.. configuration-block::
1798+
1799+
.. code-block:: yaml
1800+
1801+
# config/packages/framework.yaml
1802+
framework:
1803+
http_method_override: true
1804+
# Only allow PUT, PATCH, and DELETE to be overridden
1805+
allowed_http_method_override: ['PUT', 'PATCH', 'DELETE']
1806+
1807+
.. code-block:: xml
1808+
1809+
<!-- config/packages/framework.xml -->
1810+
<?xml version="1.0" encoding="UTF-8" ?>
1811+
<container xmlns="http://symfony.com/schema/dic/services"
1812+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
1813+
xmlns:framework="http://symfony.com/schema/dic/symfony"
1814+
xsi:schemaLocation="http://symfony.com/schema/dic/services
1815+
https://symfony.com/schema/dic/services/services-1.0.xsd
1816+
http://symfony.com/schema/dic/symfony
1817+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
1818+
1819+
<framework:config http-method-override="true">
1820+
<framework:allowed-http-method-override>PUT</framework:allowed-http-method-override>
1821+
<framework:allowed-http-method-override>PATCH</framework:allowed-http-method-override>
1822+
<framework:allowed-http-method-override>DELETE</framework:allowed-http-method-override>
1823+
</framework:config>
1824+
</container>
1825+
1826+
.. code-block:: php
1827+
1828+
// config/packages/framework.php
1829+
use Symfony\Config\FrameworkConfig;
1830+
1831+
return static function (FrameworkConfig $framework): void {
1832+
$framework
1833+
->httpMethodOverride(true)
1834+
->allowedHttpMethodOverride(['PUT', 'PATCH', 'DELETE'])
1835+
;
1836+
};
1837+
1838+
This security feature is useful for hardening your application by explicitly
1839+
defining which methods can be tunneled through POST requests. For example, if
1840+
your application only needs to override POST requests to PUT and DELETE, you
1841+
can restrict the allowed methods accordingly.
1842+
1843+
You can also configure this programmatically using the
1844+
:method:`Request::setAllowedHttpMethodOverride <Symfony\\Component\\HttpFoundation\\Request::setAllowedHttpMethodOverride>`
1845+
method::
1846+
1847+
// public/index.php
1848+
1849+
// ...
1850+
$kernel = new CacheKernel($kernel);
1851+
1852+
Request::enableHttpMethodParameterOverride();
1853+
Request::setAllowedHttpMethodOverride(['PUT', 'PATCH', 'DELETE']);
1854+
$request = Request::createFromGlobals();
1855+
// ...
1856+
17781857
.. _reference-framework-ide:
17791858

17801859
ide

routing.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ Use the ``methods`` option to restrict the verbs each route should respond to:
206206
automatically for you when the :ref:`framework.http_method_override <configuration-framework-http_method_override>`
207207
option is ``true``.
208208

209+
For security, you can restrict which HTTP methods can be overridden using the
210+
:ref:`framework.allowed_http_method_override <configuration-framework-allowed_http_method_override>`
211+
option.
212+
209213
Matching Environments
210214
~~~~~~~~~~~~~~~~~~~~~
211215

0 commit comments

Comments
 (0)