Skip to content

Commit

Permalink
Merge branch '5.4' into 6.4
Browse files Browse the repository at this point in the history
* 5.4:
  [Form] Mention that enabling CSRF in forms will start sessions
  • Loading branch information
javiereguiluz committed Jul 2, 2024
2 parents e9f7375 + f009a04 commit 9c4d4c4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
51 changes: 50 additions & 1 deletion security/csrf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ protected forms. As an alternative, you can:
load the CSRF token with an uncached AJAX request and replace the form
field value with it.

.. _csrf-protection-forms:

CSRF Protection in Symfony Forms
--------------------------------

Expand All @@ -111,7 +113,54 @@ o do anything to be protected against CSRF attacks.
.. _form-csrf-customization:

By default Symfony adds the CSRF token in a hidden field called ``_token``, but
this can be customized on a form-by-form basis::
this can be customized (1) globally for all forms and (2) on a form-by-form basis.
Globally, you can configure it under the ``framework.form`` option:

.. configuration-block::

.. code-block:: yaml
# config/packages/framework.yaml
framework:
# ...
form:
csrf_protection:
enabled: true
field_name: 'custom_token_name'
.. code-block:: xml
<!-- config/packages/framework.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config>
<framework:form>
<framework:csrf-protection enabled="true" field-name="custom_token_name"/>
</framework:form>
</framework:config>
</container>
.. code-block:: php
// config/packages/framework.php
use Symfony\Config\FrameworkConfig;
return static function (FrameworkConfig $framework) {
$framework->form()->csrfProtection()
->enabled(true)
->fieldName('custom_token_name')
;
};
On a form-by-form basis, you can configure the CSRF protection in the ``setDefaults()``
method of each form::

// src/Form/TaskType.php
namespace App\Form;
Expand Down
14 changes: 8 additions & 6 deletions session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ By default, session attributes are key-value pairs managed with the
:class:`Symfony\\Component\\HttpFoundation\\Session\\Attribute\\AttributeBag`
class.

.. tip::
Sessions are automatically started whenever you read, write or even check for
the existence of data in the session. This may hurt your application performance
because all users will receive a session cookie. In order to prevent starting
sessions for anonymous users, you must *completely* avoid accessing the session.

.. note::

Sessions are automatically started whenever you read, write or even check
for the existence of data in the session. This may hurt your application
performance because all users will receive a session cookie. In order to
prevent starting sessions for anonymous users, you must *completely* avoid
accessing the session.
Sessions will also be started when using features that rely on them internally,
such as the :ref:`CSRF protection in forms <csrf-protection-forms>`.

.. _flash-messages:

Expand Down

0 comments on commit 9c4d4c4

Please sign in to comment.