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

The #[IsCsrfTokenValid] attribute eliminates the need for a request parameter in your method. #19867

Merged
merged 1 commit into from
May 7, 2024

Conversation

rcsofttech85
Copy link
Contributor

@rcsofttech85 rcsofttech85 commented May 7, 2024

#[IsCsrfTokenValid] attribute in Symfony 7.1 does not require you to include a request parameter in the method itself. The attribute uses the request object internally to access the submitted data for validation.

for example

    #[Route(path: '/admin/post/delete', name: 'admin_post_delete', methods: ['POST'])]
    #[IsCsrfTokenValid('delete-item')]
    public function adminPostDelete(): Response
    {
        dd('deleted');
    }

@carsonbot carsonbot added this to the 7.1 milestone May 7, 2024
@OskarStark
Copy link
Contributor

Thank you @rcsofttech85.

@OskarStark OskarStark merged commit 8de101e into symfony:7.1 May 7, 2024
3 checks passed
@@ -176,7 +176,7 @@ attribute on the controller action::
// ...

#[IsCsrfTokenValid('delete-item', tokenKey: 'token')]
Copy link
Member

@javiereguiluz javiereguiluz May 7, 2024

Choose a reason for hiding this comment

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

This was asked in the Symfony blog: https://symfony.com/blog/new-in-symfony-7-1-iscsrftokenvalid-attribute#comment-25462

The 'token' value in tokenKey, should be '_token' instead?

Copy link
Contributor Author

@rcsofttech85 rcsofttech85 May 8, 2024

Choose a reason for hiding this comment

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

@javiereguiluz no, input type="hidden" name="token" value="{{ csrf_token('delete-item') }}" The value of the token key is determined by what you name it. By default, it's '_token', and when you override its value, you need to mention it in the attribute. Currently, in the documentation, it's correctly mentioned as #[IsCsrfTokenValid('delete-item', tokenKey: 'token')].

The blog you mentioned needs refactoring. Consider changing $request->request->getString('_token') to simply 'token', or alternatively, updating #[IsCsrfTokenValid('delete_example', tokenKey: 'token')] to
#[IsCsrfTokenValid('delete_example')]

Copy link
Member

Choose a reason for hiding this comment

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

I've updated the blog post. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've checked.You don't need to mention tokenKey: '_token' because that's the default value. You would only use the second parameter, tokenKey, if it's different from '_token'.

Copy link
Member

Choose a reason for hiding this comment

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

@rcsofttech85 I'm sorry 🙏 This is fixed now. Thanks a lot for checking it.

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.

5 participants