symfony: Symfony 5.3: the csrf token is invalid on all form protected by CSRF when using "remember me"
Symfony version(s) affected: 5.3.1
Description
I’ve upgraded from Symfony 5.2 to 5.3.1 and since then, in production submitting any form protected by CSRF fail with
the csrf token is invalid. please try to resubmit the form
Even form not handle by me (for example I have EasyAdminBundle and they fail too)
How to reproduce
- Install Symfony + EasyAdminBundle
- for session I use
framework:
session:
handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
cookie_secure: auto
cookie_samesite: lax
3.login in EasyAdmin 4. try to submit a form -> you got the error
Possible Solution
Additional context
Is it related to https://github.com/symfony/symfony/pull/39919 ?
it happens only in production
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 45 (10 by maintainers)
@ovidals and I were able to fix our error, we are finally not receiving the CSRF invalid token. We defined a custom authenticator, like in the official documentation, and the login process was returning that CSRF error. Then debugging we found that the CSRF ID was wrong, so in
LoginAuthenticator:authenticatemethod instead of havingWe replaced to
And now it’s working. Maybe it’s only our particular case, but I came to explain it here just in case someone else had the same error.
Same here, CRSF not working when trying to update to 5.3.
{“message”:“Invalid CSRF token.”}
if you are on dev env and using base http for request then you should define cookie_secure: false. For https usage then cookie_secure should be set to true, because auto would not change properly to true (which is done with SessionListener)
@allan-simon, @cmmata was who fixed the issue with his last comment approach. With ‘login’ the CSRF token does not work but it does with ‘authenticate’.
I’m having same issue on Symfony 5.4. (actually started to get, after update from 5.3 to 5.4) But I notice that when I open prod, I instantly have PHPSESSID cookie set 😃 when I open my dev env site, I have no cookies at all.
I found what is going on. It happens when the prod is in HTTP, if you’re using HTTPS CSRF are working
I am on Symfony 6.0.0 and have this error.
I see both setcoockies from public/index.php
I cannot use https because of internal local access without certificate
What have I to do to solve this problem? I am not much familiar with symfony.
so just if you run in a similar issue , try the following
ok I’m making progress, I’ve activated the web profiler in lambda in “prod” setting and so I’m able to add
dumphere and thereso first finding , the token is considered invalid because of this test fail
https://github.com/symfony/security-csrf/blob/5.3/CsrfTokenManager.php#L111
the hasToken returns false
the namespaceid variable value
(it’s the name of the entity of the form)
and the
storagebeing an instance ofsymfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage@derrabus I will try to make one