symfony: CSRF is invalid with Stateless CSRF

Symfony version(s) affected

7.2.0

Description

Hi,

After upgrading from Symfony 7.1.6 to 7.2.0 and updating the associated recipes, I encountered a bug when submitting forms: “CSRF token is invalid”.

Key Details:

  • Environment: The issue only occurs in the prod environment;
  • CSRF Token Issue: The HTML generated for the CSRF token has a value attribute that is undefined:
<input type=“hidden” id=“meeting__token” name=“meeting[_token]” data-controller=“csrf-protection” autocomplete=“off” value=“csrf-token”>

Also, I created a fresh Symfony webapp with a simple CRUD setup. The same issue occurred.

How to reproduce

https://github.com/smbpunt/bug-sf-59065

It’s a simple new “webapp” with an entity and an associated crud (all generated via maker-bundle).

  • Clone
  • Install
  • Run server
  • Go to /new
  • Submit a form

Possible Solution

The only working solution/workaround was to comment out the stateless configuration in csrf.yaml :

# Enable stateless CSRF protection for forms and logins/logouts
framework:
  form:
    csrf_protection:
      token_id: submit

#    csrf_protection:
#        stateless_token_ids:
#            - submit
#            - authenticate
#            - logout

Additional Context

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Are you behind a reverse proxy? If yes, ensure you configured trusted proxy headers.

Are you behind a reverse proxy? If yes, ensure you configured trusted proxy headers.

It’s now working. Thanks !

Hello,

I think there’s something to look into. Indeed, on the test I provided, if you compile the assets, it seems to work… However, on the project I updated to 7.2, I was sure I had compiled the assets (because I had installed it on a server…). I went to check, re-compiled the assets to be sure, and I’m still getting the “CSRF Token is invalid”…

Is it possible that this is linked to a web server configuration? Locally (and env prod + assets compiled) I don’t encounter the bug, whereas on my server I do encounter the bug. Knowing that everything was functional before the 7.2 update…