symfony: Automagically flagging requests as stateless breaks valid session access
Symfony version(s) affected
6.3.0
Description
There are already several comments on #48044 that indicate that that change is a BC break.
The main problem is that flagging a firewall stateless has never meant that each request that passed that firewall is meant to be stateless. It was merely a way to tell Symfony not to persist the authentication token in the session. The application could still have valid reasons to access the session later on. @tucksaun mentioned CSRF checks and flash messages as examples.
My problem is that #48044 changes the semantics of the firewall attribute stateless. This might be a surprise for applications that suddenly raise exceptions on routes that worked perfectly fine with Symfony 6.2. However, this exception is not raised with production settings as far as I can tell, so it’s “only” the local dev environment that breaks.
I think that this behavior should’ve been opt-in, maybe with a deprecation layer.
How to reproduce
Declare a stateless firewall and access the session from a controller behind that firewall on a route with no explicit stateless setting.
Possible Solution
Revert #48044 or make that functionality opt-in.
Additional Context
In my case, my firewall was declared stateless because Symfony was not supposed to manage the session of the application. This is part of a typical temporary authenticator I use when migrating legacy applications to Symfony: I can make Symfony aware of the authenticated user without migrating the login form. If I wouldn’t declare the firewall stateless, Symfony would remember the authenticated token which would break the logout mechanism of the legacy app.
I “solved” this in my project by declaring an event listener:
#[AsEventListener(priority: 1024)]
final class StatelessListener
{
public function __invoke(RequestEvent $event): void
{
$event->getRequest()->attributes->set('_stateless', false);
}
}
But that only works because we don’t make use of that stateless route flag at all at the moment.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 15 (12 by maintainers)
Got bit by #48044 again. Is there a chance a PR reverting this behavior would be accepted? AFAICT there wouldn’t be any BC break doing so.
When a request is declared as stateless, there must not be any usage of the session during the handling of that request. That’s the whole point. A request can actually have 3 states:
If a route declares the
statelessflag, it declares that state for the requestWhen a firewall is marked as stateless, it has 2 impacts:
So if you need a stateless firewall without making the request fully stateless (because you use the session for something else), the solution is to explicitly mark the request as not stateless before the firewall runs (see my previous comment)
But that is the BC break. Being able to define a request (or route) as stateless is fine, but a stateless firewall should not automatically assume that all requests are stateless (only the authentication is).
You could make your solution compatible with that by running your listener between the RouterListener (priority 32) and the FirewallListener (priority 8) instead of using priority 1024. This way, you could check whether the route already added this attribute in the request.
Let’s discuss this on a PR indeed.
@javaDeveloperKid Marking a firewall as stateless means that the firewall itself will not use the session anymore. Then, we have an additional smart behavior based on that about marking the request as stateless which I explained in https://github.com/symfony/symfony/issues/50715#issuecomment-1691784169. Once a request is marked as stateless, the framework ensures that nothing used the session during the request handling so that it actually is stateless. If your template uses the session to read flash messages, the template will prevent marking things as stateless.