Skip to content

Adding CSRF token #16973

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions session.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ 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 that, you
must *completely* avoid accessing the session.
By default, every Symfony form starts a session to store a CSRF token. To disable
this, see :doc:`CSRF Protection </security/csrf>`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To disable sessions in form contexts one should use the form config instead.

Now I'm realizing that there is no mention to that config anywhere in the docs (https://github.com/symfony/symfony/blob/4.4/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php#L163).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I tried to add was just a "reverse" link for what is said on https://symfony.com/doc/current/security/csrf.html:

That's why a session is started automatically as soon as you render a form with CSRF protection.

So in which file is form config happening? Or do you mean csrf_protection in framework config?: https://symfony.com/doc/current/reference/configuration/framework.html#csrf-protection

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two available config for csrf, the one you referring to, enabling it globally at the framework level:

// config/packages/framework.yaml
framework:
    csrf_protection: true/false

And the one I'm referring to (which does not seem documented and that I linked above), to enable it at the form level only:

// config/packages/form.yaml
framework:
    form:
        csrf_protection: true/false

(https://github.com/symfony/symfony/blob/4.4/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php#L169)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't fully understand the source code :-(
The best page to document this would probably be https://symfony.com/doc/current/forms.html - since it's probably not worth an own page?!
But some other questions first:
What happens if csrf_protection is true in one, and false in the other file?
What else can be configured in form.yaml?
What's the purpose of form.yaml at all? Is there any advantage over doing it in framework.yaml? Both ways are for users of the full framework only, right?

Copy link
Contributor

@HeahDude HeahDude Jul 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if csrf_protection is true in one, and false in the other file?

framework.csrf_protection defines the default value of framework.form.csrf_protection (see the comment in the link from my previous comment).

So defining the global as true defines the form protection as true, then switching the form one to false would be my approach in the current PR context.

In the other hand, defining the global as false would make the form protection disabled, then trying to enable it at the form level wouldn't work as expected (however I have not tested if it throws a clear exception or if it just fails silently).

What else can be configured in form.yaml?

The best documentation is your console ;), using:

bin/console config:dump framework
# or
bin/console config:dump framework form
bin/console config:dump framework csrf_protection

What's the purpose of form.yaml at all?

Same as mailer.yaml or any other component configured by the FrameworkBundle (i.e serializer, messenger, lock, ...) to avoid having hundred of lines in framework.yaml.
But feel free to decide to add this file or not by inlining this config directly in framework.yaml :).

Both ways are for users of the full framework only, right?

Right :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unable to find node at path "framework.csrf".

Sorry it's bin/console config:dump framework csrf_protection 🤦 (I've edited my comment).

In the other hand, defining the global as false would make the form protection disabled, then trying to enable it at the form level wouldn't work as expected (however I have not tested if it throws a clear exception or if it just fails silently).

So, I've just tested:

# config/packages/framework.yaml
framework:
    csrf_protection: false
    form:
        csrf_protection: true

and I got the following:

The service "form.type_extension.csrf" has a dependency on a non-existent service "security.csrf.token_manager".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've opened symfony/symfony#46960 to try to improve this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but still: If you do it the other way around:

# config/packages/framework.yaml
framework:
    csrf_protection: true
    form:
        csrf_protection: false

... where will CSRF be enabled then?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The framework is using the global config to wire services symfony/security-csrf component, they can be used elsewhere, not only forms, and can rely on other implementations not using a session (i.e. a specific cookie).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't have a clear enough picture to suggest something. Maybe somebody else has an idea - ping @javiereguiluz


More about Sessions
-------------------
Expand Down