Skip to content
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

Adding CSRF token #16973

Closed
wants to merge 1 commit into from
Closed

Conversation

ThomasLandauer
Copy link
Contributor

No description provided.

@carsonbot carsonbot added this to the 4.4 milestone Jul 12, 2022
@@ -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

fabpot added a commit to symfony/symfony that referenced this pull request Jul 19, 2022
…CSRF (HeahDude)

This PR was merged into the 4.4 branch.

Discussion
----------

[FrameworkBundle] Fail gracefully when forms use disabled CSRF

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | kind of
| New feature?  | no
| Deprecations? | no
| Tickets       | ~
| License       | MIT
| Doc PR        | ~

Relates to symfony/symfony-docs#16973.

Currently with the following config in Symfony demo:
```yaml
# config/packages/framework.yaml
framework:
    csrf_protection: false
    form:
        csrf_protection: true
```
we get:
>The service "form.type_extension.csrf" has a dependency on a non-existent service "security.csrf.token_manager".

We should consider this PR as a bug fix to make this exception actionable.

Commits
-------

5990182 [FrameworkBundle] Fail gracefully when forms use disabled CSRF
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Jul 19, 2022
…CSRF (HeahDude)

This PR was merged into the 4.4 branch.

Discussion
----------

[FrameworkBundle] Fail gracefully when forms use disabled CSRF

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | kind of
| New feature?  | no
| Deprecations? | no
| Tickets       | ~
| License       | MIT
| Doc PR        | ~

Relates to symfony/symfony-docs#16973.

Currently with the following config in Symfony demo:
```yaml
# config/packages/framework.yaml
framework:
    csrf_protection: false
    form:
        csrf_protection: true
```
we get:
>The service "form.type_extension.csrf" has a dependency on a non-existent service "security.csrf.token_manager".

We should consider this PR as a bug fix to make this exception actionable.

Commits
-------

5990182698 [FrameworkBundle] Fail gracefully when forms use disabled CSRF
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Jul 28, 2023
…CSRF (HeahDude)

This PR was merged into the 4.4 branch.

Discussion
----------

[FrameworkBundle] Fail gracefully when forms use disabled CSRF

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | kind of
| New feature?  | no
| Deprecations? | no
| Tickets       | ~
| License       | MIT
| Doc PR        | ~

Relates to symfony/symfony-docs#16973.

Currently with the following config in Symfony demo:
```yaml
# config/packages/framework.yaml
framework:
    csrf_protection: false
    form:
        csrf_protection: true
```
we get:
>The service "form.type_extension.csrf" has a dependency on a non-existent service "security.csrf.token_manager".

We should consider this PR as a bug fix to make this exception actionable.

Commits
-------

5990182698 [FrameworkBundle] Fail gracefully when forms use disabled CSRF
@javiereguiluz javiereguiluz modified the milestones: 4.4, 5.4 Jul 1, 2024
@javiereguiluz
Copy link
Member

Thanks for this PR and for the discussion. I decided to create a new PR in #20008 to continue the work needed here, so let's close this PR in favor of the new one.

@ThomasLandauer ThomasLandauer deleted the patch-28 branch July 1, 2024 11:36
javiereguiluz added a commit that referenced this pull request Jul 2, 2024
…ssions (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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants