Skip to content

[LiveComponent] Invalid CSRF token exceptions are not correctly managedΒ #1150

@simondaigre

Description

@simondaigre

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 :

if (
$this->container->has(CsrfTokenManagerInterface::class)
&& $metadata->get('csrf')
&& !$this->container->get(CsrfTokenManagerInterface::class)->isTokenValid(new CsrfToken(LiveControllerAttributesCreator::getCsrfTokeName($componentName), $request->headers->get('X-CSRF-TOKEN')))) {
throw new BadRequestHttpException('Invalid CSRF token.');
}

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions