Skip to content

[Form] Mention that enabling CSRF in forms will start sessions #20008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion security/csrf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
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 @@ -82,7 +84,54 @@
.. _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'

Check failure on line 100 in security/csrf.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Cache Warmup] In FrameworkExtension.php line 675: To use form CSRF protection, "framework.csrf_protection" must be enabled.

.. 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 @@ -110,13 +110,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 created when using features that rely on them internally,
such as the :ref:`CSRF protection in forms <csrf-protection-forms>`.

.. _flash-messages:

Expand Down
Loading