-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Add support for dynamic CSRF id with Expression in #[IsCsrfTokenValid]
#54443
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
[Security] Add support for dynamic CSRF id with Expression in #[IsCsrfTokenValid]
#54443
Conversation
src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/Attribute/IsCsrfTokenValid.php
Outdated
Show resolved
Hide resolved
will this works with for example an object in the controller action ? Expression (delete item « post.slug ») |
e40a7e1
to
a9643cb
Compare
@94noni yes it should as it used the ExpressionLanguage component, like in |
src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php
Outdated
Show resolved
Hide resolved
I'm curious what is the problem this scenario help to solve ? You still have to write manually this id on the template right ? |
@smnandre it's for example on a list page with delete action per item, and you want a CSRF token per item, so in the template you have something like the following: {# in a loop over multiple posts #}
<form action="{{ path('post_delete', {post: post.id}) }}" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token('delete-post-' ~ post.id) }}">
...
</form> |
...ymfony/Component/Security/Http/Tests/EventListener/IsCsrfTokenValidAttributeListenerTest.php
Outdated
Show resolved
Hide resolved
a9643cb
to
6858285
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code snippet in the description is also wrong. Accessing a key in an array will be args["post"]
, not args.post
. In ExpressionLanguage, the .
operator is a strict equivalent of the ->
operator in PHP. It does not have the Twig magic.
src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php
Outdated
Show resolved
Hide resolved
good catch! was written from memory without checking, thank you, description updated |
src/Symfony/Bundle/SecurityBundle/DependencyInjection/Compiler/RegisterCsrfFeaturesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php
Outdated
Show resolved
Hide resolved
#[IsCsrfTokenValid]
#[IsCsrfTokenValid]
4dc452b
to
7db4866
Compare
src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php
Outdated
Show resolved
Hide resolved
7db4866
to
c09c734
Compare
c09c734
to
8f99ca5
Compare
Thank you @yguedidi. |
Use case is for example on a list page with delete action per item, and you want a CSRF token per item, so in the template you have something like the following:
The new feature will allow:
Maybe this need more tests but need help identify which test cases are useful.
Hope this can pass before the feature freeze