Skip to content

Commit 0f76b32

Browse files
committed
Merge branch '7.0' into 7.1
* 7.0: [Form] Mention that enabling CSRF in forms will start sessions
2 parents ad745a7 + 14d035b commit 0f76b32

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

security/csrf.rst

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ protected forms. As an alternative, you can:
101101
load the CSRF token with an uncached AJAX request and replace the form
102102
field value with it.
103103

104+
.. _csrf-protection-forms:
105+
104106
CSRF Protection in Symfony Forms
105107
--------------------------------
106108

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

113115
By default Symfony adds the CSRF token in a hidden field called ``_token``, but
114-
this can be customized on a form-by-form basis::
116+
this can be customized (1) globally for all forms and (2) on a form-by-form basis.
117+
Globally, you can configure it under the ``framework.form`` option:
118+
119+
.. configuration-block::
120+
121+
.. code-block:: yaml
122+
123+
# config/packages/framework.yaml
124+
framework:
125+
# ...
126+
form:
127+
csrf_protection:
128+
enabled: true
129+
field_name: 'custom_token_name'
130+
131+
.. code-block:: xml
132+
133+
<!-- config/packages/framework.xml -->
134+
<?xml version="1.0" encoding="UTF-8" ?>
135+
<container xmlns="http://symfony.com/schema/dic/services"
136+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
137+
xmlns:framework="http://symfony.com/schema/dic/symfony"
138+
xsi:schemaLocation="http://symfony.com/schema/dic/services
139+
https://symfony.com/schema/dic/services/services-1.0.xsd
140+
http://symfony.com/schema/dic/symfony
141+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
142+
143+
<framework:config>
144+
<framework:form>
145+
<framework:csrf-protection enabled="true" field-name="custom_token_name"/>
146+
</framework:form>
147+
</framework:config>
148+
</container>
149+
150+
.. code-block:: php
151+
152+
// config/packages/framework.php
153+
use Symfony\Config\FrameworkConfig;
154+
155+
return static function (FrameworkConfig $framework) {
156+
$framework->form()->csrfProtection()
157+
->enabled(true)
158+
->fieldName('custom_token_name')
159+
;
160+
};
161+
162+
On a form-by-form basis, you can configure the CSRF protection in the ``setDefaults()``
163+
method of each form::
115164

116165
// src/Form/TaskType.php
117166
namespace App\Form;

session.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ By default, session attributes are key-value pairs managed with the
107107
:class:`Symfony\\Component\\HttpFoundation\\Session\\Attribute\\AttributeBag`
108108
class.
109109

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

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

118120
.. _flash-messages:
119121

0 commit comments

Comments
 (0)