-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Using IsCsrfTokenValid attribute with invalid token redirects user to login page #57343
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
Comments
I think InvalidCsrfTokenException must extends AccessDeniedException instaed of AuthenticationException to get a 403 response instead a login page redirection. For the moment it's possible in a custom authenticator to set the start function as mentioned in #16026 (comment) |
Can you create a small example application that allows to reproduce your issue? |
sure, do you want I make the PR ? (Bug or New Feature ? ) This symptome is joining issues : |
With reflexion, BadRequestHttpException will be a better choice for extends, beacause a Csrf Error is not automaticly linked to an authentification |
What about the HTTP code ? 403 is for acces denied, Is really CSRF error an acces denied ? For a contact form can we speak about Access ? |
for a temporary solution, I did the following code: My software was redirecting to the login page once an invalid CSRF using the new attribute #[IsCsrfTokenValid] and now a custom JSON message. |
Yeah I created a Exception Transformer near you made, But it can be better to be in core |
please help us to improve that one. You can create a PR to this repo |
I already did the PR, juste waiting for someone valid it : #57622 |
friendly ping @yguedidi |
Symfony version(s) affected
7.1.1
Description
Hi,
I tried the new
IsCsrfTokenValid
attribute on a route to handle a form. When the token is invalid, the user is redirected to the login page instead of an error page. Probably becauseIsCsrfTokenValidAttributeListener
throws aInvalidCsrfTokenException
witch extendsAuthenticationException
.How to reproduce
Add
IsCsrfTokenValid
attribute on a route handling a formLoad the page and wait for the token to expire (or generate an invalid token)
Possible Solution
Maybe we could throw a
BadRequestHttpException
instead of anInvalidCsrfTokenException
inIsCsrfTokenValidAttributeListener
? But I guess there is a good reason forInvalidCsrfTokenException
to extendAuthenticationException
, so I can't really see the implications.If this solution looks good to you, I can create a PR.
Additional Context
No response
The text was updated successfully, but these errors were encountered: