[Security] Add methods param in IsCsrfTokenValid attribute#60007
[Security] Add methods param in IsCsrfTokenValid attribute#60007nicolas-grekas merged 1 commit intosymfony:7.3from
Conversation
|
Hi @Oviglo, I believe the best approach would be to separate the logic into two distinct methods: one for |
Yes, but it will make my controller heavier. Another solution is to create an empty form for using native csrf validation. Is it an official recommendation to use only one method per action ? |
|
I’m not sure what you mean by "heavier" in this context. There is no official recommendation to use only one method per action, it is more about the single-responsibility principle. Here’s an example that should work as expected: #[Route('/delete/{id}', name: 'get', methods: ['GET'], requirements: ['id' => Requirement::UUID])]
public function get(User $user): Response
{
return $this->render('/admin/user/delete.html.twig', ['entity' => $user]);
}
#[Route('/delete/{id}', name: 'delete', methods: ['DELETE'], requirements: ['id' => Requirement::UUID])]
#[IsCsrfTokenValid(new Expression('"delete" ~ args["user"].getId()'))]
public function delete(User $user, UserManager $userManager): Response
{
$userManager->remove($user);
return $this->redirectToRoute('admin_user_index');
} |
|
So, better close? |
|
If you don’t find any utility, yes 😥 Merci à vous. |
nicolas-grekas
left a comment
There was a problem hiding this comment.
Work for me despite the proposed alternative.
src/Symfony/Component/Security/Http/Attribute/IsCsrfTokenValid.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/Attribute/IsCsrfTokenValid.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Security/Http/EventListener/IsCsrfTokenValidAttributeListener.php
Outdated
Show resolved
Hide resolved
|
Instead of a list of methods, what about checking if it is a non-safe method? |
|
Yes you are right, a csrf token is only used for POST PUT DELETE and PATCH is’nt ? |
|
CSRF tokens can also be used for GET requests, eg for logout links (that's a common practice, even if not recommended). |
adea370 to
28a9102
Compare
0b4edd1 to
640e7a4
Compare
|
Thank you @Oviglo. |
…tribute (Oviglo) This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Security] Add methods param doc for isCsrfTokenValid attribute Add new params for isCsrfTokenValid attribute PR: symfony/symfony#60007 Issue: #20810 Commits ------- 6d7c87f [Security] Add methods param doc for isCsrfTokenValid attribute
I use a controller action to show a confirmation message for delete entity, i think it could be usefull to add 'methods' param in
#[IsCsrfTokenValid]attribute.The
isCsrfTokenValidfunction is ignored if request method is not settings in param.What do you think about this ?