Skip to content

Commit 2751f0a

Browse files
[HttpFoundation] Add Request::setAllowedHttpMethodOverride() and allowed_http_method_override config option
1 parent c805014 commit 2751f0a

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
@@ -779,6 +779,10 @@ to the ``form()`` or the ``form_start()`` helper functions:
779779
``DELETE`` request. The :ref:`http_method_override <configuration-framework-http_method_override>`
780780
option must be enabled for this to work.
781781

782+
For security, you can restrict which HTTP methods can be overridden using the
783+
:ref:`allowed_http_method_override <configuration-framework-allowed_http_method_override>`
784+
option.
785+
782786
Changing the Form Name
783787
~~~~~~~~~~~~~~~~~~~~~~
784788

reference/configuration/framework.rst

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

2124+
.. _configuration-framework-allowed_http_method_override:
2125+
2126+
allowed_http_method_override
2127+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2128+
2129+
.. versionadded:: 7.4
2130+
2131+
The ``allowed_http_method_override`` option was introduced in Symfony 7.4.
2132+
2133+
**type**: ``array`` **default**: ``null``
2134+
2135+
This option controls which HTTP methods can be overridden via the ``_method``
2136+
request parameter or the ``X-HTTP-METHOD-OVERRIDE`` header when
2137+
:ref:`http_method_override <configuration-framework-http_method_override>` is enabled.
2138+
2139+
When set to ``null`` (the default), all HTTP methods can be overridden. When set
2140+
to an empty array (``[]``), HTTP method overriding is completely disabled. When set
2141+
to a specific list of methods, only those methods will be allowed to be used as overrides:
2142+
2143+
.. configuration-block::
2144+
2145+
.. code-block:: yaml
2146+
2147+
# config/packages/framework.yaml
2148+
framework:
2149+
http_method_override: true
2150+
# Only allow PUT, PATCH, and DELETE to be overridden
2151+
allowed_http_method_override: ['PUT', 'PATCH', 'DELETE']
2152+
2153+
.. code-block:: xml
2154+
2155+
<!-- config/packages/framework.xml -->
2156+
<?xml version="1.0" encoding="UTF-8" ?>
2157+
<container xmlns="http://symfony.com/schema/dic/services"
2158+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2159+
xmlns:framework="http://symfony.com/schema/dic/symfony"
2160+
xsi:schemaLocation="http://symfony.com/schema/dic/services
2161+
https://symfony.com/schema/dic/services/services-1.0.xsd
2162+
http://symfony.com/schema/dic/symfony
2163+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
2164+
2165+
<framework:config http-method-override="true">
2166+
<framework:allowed-http-method-override>PUT</framework:allowed-http-method-override>
2167+
<framework:allowed-http-method-override>PATCH</framework:allowed-http-method-override>
2168+
<framework:allowed-http-method-override>DELETE</framework:allowed-http-method-override>
2169+
</framework:config>
2170+
</container>
2171+
2172+
.. code-block:: php
2173+
2174+
// config/packages/framework.php
2175+
use Symfony\Config\FrameworkConfig;
2176+
2177+
return static function (FrameworkConfig $framework): void {
2178+
$framework
2179+
->httpMethodOverride(true)
2180+
->allowedHttpMethodOverride(['PUT', 'PATCH', 'DELETE'])
2181+
;
2182+
};
2183+
2184+
This security feature is useful for hardening your application by explicitly
2185+
defining which methods can be tunneled through POST requests. For example, if
2186+
your application only needs to override POST requests to PUT and DELETE, you
2187+
can restrict the allowed methods accordingly.
2188+
2189+
You can also configure this programmatically using the
2190+
:method:`Request::setAllowedHttpMethodOverride <Symfony\\Component\\HttpFoundation\\Request::setAllowedHttpMethodOverride>`
2191+
method::
2192+
2193+
// public/index.php
2194+
2195+
// ...
2196+
$kernel = new CacheKernel($kernel);
2197+
2198+
Request::enableHttpMethodParameterOverride();
2199+
Request::setAllowedHttpMethodOverride(['PUT', 'PATCH', 'DELETE']);
2200+
$request = Request::createFromGlobals();
2201+
// ...
2202+
21242203
.. _reference-framework-ide:
21252204

21262205
ide

routing.rst

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

256+
For security, you can restrict which HTTP methods can be overridden using the
257+
:ref:`framework.allowed_http_method_override <configuration-framework-allowed_http_method_override>`
258+
option.
259+
256260
Matching Environments
257261
~~~~~~~~~~~~~~~~~~~~~
258262

0 commit comments

Comments
 (0)