-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Do not remove existing session on stateless requests #57854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
It always seemed weird to me #57372 added a stateless check in the Wouldn’t it be better to remove said check?
Ping @VincentLanglet |
@MatTheCat Before the change in #57372, Symfony will try to fetch the token from the session and refresh the user (even if the request is stateless). The problem now is that the change only updated the Maybe another option would be to revert the change made to the |
When I tried to debug #57372, I got error because the session was used and it wasn't voluntary. I don't remember having done anything to use the ContextListener.
Either the check should be kept and some others fixes are required, either something else need to be find in order to not trigger this listener on stateless request.
I like this idea @micheh. |
Your firewall was stateful, which didn't match the route configured as stateless. AFAIU the point of stateless requests was to warn you if you were using the session, not to stop Symfony from using it. |
I don't think so, I never declared a route as stateless, I just had firewall stateful (and route stateful) or firewall stateless (and route stateless).
I don't think so, the check done in the AbstractSessionListener symfony/src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php Lines 213 to 219 in 05e61ff
doesn't check if the session was use by Symfony or the developer. That's why there was already previous fix as #54742 or #51350. When the request is stateless, IMHO the session is supposed to never be used. Which lead some questions like #57502 (comment) about throwing an exception in |
Then I don’t see how you could get an error coming from the
I don’t think there is a need for that. But, as a user, you can trigger session usage by Symfony: like when you have a stateful firewall, the In the first case I’d prefer to be warned because the session was used because of me. Considering every case as the second means you end up hiding developers’ mistakes, which I don’t think is wise 🤷♂️ |
Honestly I dunno either, I just tried and didn't reproduce but I don't remember about the time I debug this and I surely had a reason because I didn't add the stateless check on every Then, wouldn't the best to have the following method
WDYT about updating your PR @micheh ? |
Well, no 😅 A stateful firewall will (de)serialize the current user using the session. If you're saying a request matching this firewall is stateless, you either misconfigured the request, or the firewall. |
Indeed, my bad. Anyway, I agree the stateless check should be reverted and then I'll see if have session usage. Should this PR be updated or do you want to open another PR @MatTheCat ? |
I guess this PR can be updated since its goal stays the same? |
I disagree. It should be possible to have some stateless routes in a stateful firewall. Routes can configure a stateless boolean option in order to declare that the session shouldn't be used when matching a request. This includes fetching the user from the session, which the If you revert the changes in #57372 it will throw an exception, because the listener will try to fetch the user from the session, which is not allowed in a stateless route. With the change in #57372 and in this PR, it will prevent the |
Then wouldn't it better to return false in the ContextListener::support method when the request is stateless ? |
Well it all boils down if you consider The blog post which introduced it reads
Note that it is written Following your suggestion, there wouldn’t be any difference between a stateful and stateless firewall. Feels weird to me (also, it probably would be a new feature more than a bugfix 🤔). (I’m unsubscribing from this thread because we’re running in circles.) |
I agree with @MatTheCat about the intention of the |
Stateful and Lazy firewalls should be compatible with |
Then should #57372 be reverted @nicolas-grekas ? |
Yes, we should consider reverting indeed. And reconsidering the original issue from this more-accurate angle. |
-1 for this PR. If a firewall is configured as being stateful, the ContextListener should always run (as that's exactly what a stateful firewall is about). If a route is explicitly configured as being stateless, it must either not perform any security checks (to never trigger lazy authentication) or perform it using a stateless firewall. Otherwise it cannot be stateless. |
Then #57852 would still be useful IMHO |
I don't think so. Removing the security check in your controller because you marked the request as stateless does not make any sense. If your controller needs a security check, it should perform it. And then, if the security check involved using the session, you will get an exception (in debug mode) or a warning in logs telling you that you marked a request as stateless while it is not, which means a mistake in your project (and then, the right fix is either to change your firewall to be stateless or to allow the request to be stateful). I don't see a lot of use cases for explicit checking on I would even say that introducing |
I personally have multiple use-cases in our code base:
|
Currently they are not compatible because
symfony/src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php Line 70 in a8098b2
Third party event listeners like Sentry also check if a token is available in order to enrich the logs with user information (if it is avaialble). The event listener of the Blameable Doctrine Extension also fetches the token on each request. In those event listeners it would be possible to only call |
There is a service called |
…centLanglet) This PR was merged into the 5.4 branch. Discussion ---------- [Security] Revert stateless check for ContextListener This reverts commit 40341a1. | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix ##57851 | License | MIT Closes #57854 Cf #57854 (comment) Commits ------- 188e2d2 Revert stateless check
Fixed by #58002 |
This fixes the bug where an existing session was removed on stateless requests, forcing the user to reauthenticate after visiting a route with
stateless: true
.