ux
ux copied to clipboard
[LiveComponent] Invalid CSRF token exceptions are not correctly managed
Scenario
- Enable CSRF in Symfony
- Without being logged in, browse a page with a Symfony form
- Fill the form then submit it
- Go to /login, login normally then go back to the form with your browser and re-submit it
Actual behaviour (without Live component)
- Form error is correctly displayed with HTTP 422 and "Invalid CSRF token" message.
Actual behaviour (with form in a Live component)
- Error 500 is thrown and a popup appears with the exception
Error is thrown here : https://github.com/symfony/ux/blob/d993b1283e32aa190553261e9fda0a3d4e72e0f1/src/LiveComponent/src/EventListener/LiveComponentSubscriber.php#L112-L117
This issue occurs because CSRF are reset on each login/logout (which is normal behaviour) I think we need to find a better way to handle this, since in prod environnements this leads to a 500 error to end users.
Ok, so it's not the form's csrf that's causing the problem but the live component's csrf...
I agree we should provide a better method to handle this but not quite sure how yet. I think, currently, you can handle with a component js hook but it would be complex. Feels like we could use something similar to loading states but for errors.
@kbond thanks for the idea. Possible workaround here : https://github.com/symfony/symfony/pull/51724
Not sure it would be a solution viable for the entire "symfony stack", but in UX packages, i think a lazy load of the CSRF would not be too hard ... and (yeah i'm a broken record) this would help performance / sobriety. (the CSRF token on public page restrict the caching strategies)
I have the same problem, getting random invalid csfr token on components with data-pool enabled. Does any solution exist?
@gremo thank you for reporting this!
Could you open a new issue please with ideally a small reproducer ?
@smnandre I currently receiving those errors in my inbox from an app that is already published. I deleted those emails a few moments ago 😄 but I not sure that the strack trace alone would be helpuful. New errors expected soon btw.
Right now I can't replicate that error while developing 😢
Ok i'm closing this issue for now, and you create a new one when you have those emails again, ok for you ? (it's easier for us to maintain / track)
@smnandre actually I've got another error yeasterday. But it won't be easy to digg into it.
The App:AttendanceActions is a live component:
- Configured with the data-poll attribute (around 60 secs)
- Has a live action (triggered by the user) that
emitin order to update another component in the page
Matched route "ux_live_component".
route:
ux_live_component
route_parameters:
{
"_route": "ux_live_component",
"_live_action": "refresh",
"_live_component": "App:AttendanceActions"
}
request_uri:
https://myproject/_components/App:AttendanceActions/refresh
method:
POST
Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\BadRequestHttpException: "Invalid CSRF token." at LiveComponentSubscriber.php line 114
{
"class": "Symfony\\Component\\HttpKernel\\Exception\\BadRequestHttpException",
"message": "Invalid CSRF token.",
"code": 0,
"file": "/var/www/vhosts/myproject/httpdocs/releases/171/vendor/symfony/ux-live-component/src/EventListener/LiveComponentSubscriber.php:114",
"trace": [
"/var/www/vhosts/myproject/httpdocs/releases/171/vendor/symfony/event-dispatcher/EventDispatcher.php:246",
"/var/www/vhosts/myproject/httpdocs/releases/171/vendor/symfony/event-dispatcher/EventDispatcher.php:206",
"/var/www/vhosts/myproject/httpdocs/releases/171/vendor/symfony/event-dispatcher/EventDispatcher.php:56",
"/var/www/vhosts/myproject/httpdocs/releases/171/vendor/symfony/http-kernel/HttpKernel.php:154",
"/var/www/vhosts/myproject/httpdocs/releases/171/vendor/symfony/http-kernel/HttpKernel.php:76",
"/var/www/vhosts/myproject/httpdocs/releases/171/vendor/symfony/http-kernel/Kernel.php:185",
"/var/www/vhosts/myproject/httpdocs/releases/171/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php:35",
"/var/www/vhosts/myproject/httpdocs/releases/171/vendor/autoload_runtime.php:29",
"/var/www/vhosts/myproject/httpdocs/releases/171/public/index.php:5"
]
}
Do you know if it's my bad or a bug? I can paste the source code no problem for me.
But data-poll should not use CSRF for me .. or you are talking about the action ?
@smnandre about the action:
App:AttendanceActionshas a live action namedrecord(triggered by a button) whichemittherefresheventApp:AttendanceWidgedis a live component on the same page and has a live listener to update itself whenrefreshevent is emitted
If data-poll doens't use CSFR than the problem is emitting from App:AttendanceActions or receiving from App:AttendanceWidged. Indeed, the stack show that the exception is thown in LiveComponentSubscriber
Again i think is will be so difficult to debug, I'll try to understand the exact condition to replicate it.
Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?
Should be fixed by #2251 ?
Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?
Could I get a reply or should I close this?
Hey,
I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!
Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?
Unfortunately, issue is still there.