Skip to content

Commit f6d8605

Browse files
committed
minor #20008 [Form] Mention that enabling CSRF in forms will start sessions (javiereguiluz)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Form] Mention that enabling CSRF in forms will start sessions Continues the work started in #16973. This adds the missing `framework.form` config mentioned by `@HeahDude`. Commits ------- 036a8a0 [Form] Mention that enabling CSRF in forms will start sessions
2 parents 9dd9689 + 036a8a0 commit f6d8605

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
@@ -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

Lines changed: 8 additions & 6 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy