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)

Commits related to this issue

Most upvoted comments

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:

  • declared as not stateless
  • declared as stateless
  • no declaration on the request (this is treated as not stateless as far as the requirement is enforced, but this unconfigured state is relevant for other parts).

If a route declares the stateless flag, it declares that state for the request

When a firewall is marked as stateless, it has 2 impacts:

  • the ContextListener using the session to store the authentication token is disabled (meaning that your authenticator must be a stateless one able to authenticate each incoming request instead of relying on a previous request to a specific check path like form_login)
  • if the stateless state of the request is unknown at the time of executing the firewall, it will mark the request as stateless (making it easy for the common use case where parts of the app covered by a stateless firewall must be fully stateless)

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)

  • if the stateless state of the request is unknown at the time of executing the firewall, it will mark the request as stateless (making it easy for the common use case where parts of the app covered by a stateless firewall must be fully stateless)

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).

But that only works because we don’t make use of that stateless route flag at all at the moment.

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.