Skip to content

Commit 036a8a0

Browse files
committed
[Form] Mention that enabling CSRF in forms will start sessions
1 parent 4412af0 commit 036a8a0

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

security/csrf.rst

+50-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ protected forms. As an alternative, you can:
7272
load the CSRF token with an uncached AJAX request and replace the form
7373
field value with it.
7474

75+
.. _csrf-protection-forms:
76+
7577
CSRF Protection in Symfony Forms
7678
--------------------------------
7779

@@ -82,7 +84,54 @@ protected against CSRF attacks.
8284
.. _form-csrf-customization:
8385

8486
By default Symfony adds the CSRF token in a hidden field called ``_token``, but
85-
this can be customized on a form-by-form basis::
87+
this can be customized (1) globally for all forms and (2) on a form-by-form basis.
88+
Globally, you can configure it under the ``framework.form`` option:
89+
90+
.. configuration-block::
91+
92+
.. code-block:: yaml
93+
94+
# config/packages/framework.yaml
95+
framework:
96+
# ...
97+
form:
98+
csrf_protection:
99+
enabled: true
100+
field_name: 'custom_token_name'
101+
102+
.. code-block:: xml
103+
104+
<!-- config/packages/framework.xml -->
105+
<?xml version="1.0" encoding="UTF-8" ?>
106+
<container xmlns="http://symfony.com/schema/dic/services"
107+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
108+
xmlns:framework="http://symfony.com/schema/dic/symfony"
109+
xsi:schemaLocation="http://symfony.com/schema/dic/services
110+
https://symfony.com/schema/dic/services/services-1.0.xsd
111+
http://symfony.com/schema/dic/symfony
112+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
113+
114+
<framework:config>
115+
<framework:form>
116+
<framework:csrf-protection enabled="true" field-name="custom_token_name"/>
117+
</framework:form>
118+
</framework:config>
119+
</container>
120+
121+
.. code-block:: php
122+
123+
// config/packages/framework.php
124+
use Symfony\Config\FrameworkConfig;
125+
126+
return static function (FrameworkConfig $framework) {
127+
$framework->form()->csrfProtection()
128+
->enabled(true)
129+
->fieldName('custom_token_name')
130+
;
131+
};
132+
133+
On a form-by-form basis, you can configure the CSRF protection in the ``setDefaults()``
134+
method of each form::
86135

87136
// src/Form/TaskType.php
88137
namespace App\Form;

session.rst

+8-6
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,15 @@ By default, session attributes are key-value pairs managed with the
110110
:class:`Symfony\\Component\\HttpFoundation\\Session\\Attribute\\AttributeBag`
111111
class.
112112

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

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

121123
.. _flash-messages:
122124

0 commit comments

Comments
 (0)