Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
MatTheCat commentedJul 28, 2024
It always seemed weird to me#57372 added a stateless check in the Wouldn’t it be better to remove said check?
Ping@VincentLanglet |
micheh commentedJul 28, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@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 |
VincentLanglet commentedJul 28, 2024
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. |
MatTheCat commentedJul 28, 2024
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. |
VincentLanglet commentedJul 28, 2024
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 in05e61ff
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 |
MatTheCat commentedJul 28, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 🤷♂️ |
VincentLanglet commentedJul 28, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 ? |
MatTheCat commentedJul 29, 2024
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. |
VincentLanglet commentedJul 29, 2024
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 ? |
MatTheCat commentedJul 29, 2024
I guess this PR can be updated since its goal stays the same? |
micheh commentedJul 29, 2024
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 |
VincentLanglet commentedJul 29, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Then wouldn't it better to return false in the ContextListener::support method when the request is stateless ? |
MatTheCat commentedJul 29, 2024 • edited by OskarStark
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by OskarStark
Uh oh!
There was an error while loading.Please reload this page.
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.) |
nicolas-grekas commentedAug 13, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
I agree with@MatTheCat about the intention of the |
nicolas-grekas commentedAug 13, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Stateful and Lazy firewalls should be compatible with |
VincentLanglet commentedAug 13, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Then should#57372 be reverted@nicolas-grekas ? |
nicolas-grekas commentedAug 13, 2024
Yes, we should consider reverting indeed. And reconsidering the original issue from this more-accurate angle. |
stof commentedAug 13, 2024
-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. |
VincentLanglet commentedAug 13, 2024
Then#57852 would still be useful IMHO |
stof commentedAug 13, 2024
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 |
VincentLanglet commentedAug 13, 2024
I personally have multiple use-cases in our code base:
|
micheh commentedAug 13, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Currently they are not compatible because
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 |
nicolas-grekas commentedAug 13, 2024
There is a service called |
…centLanglet)This PR was merged into the 5.4 branch.Discussion----------[Security] Revert stateless check for ContextListenerThis reverts commit40341a1.| Q | A| ------------- | ---| Branch? | 5.4| Bug fix? | yes| New feature? | no| Deprecations? | no| Issues | Fix ##57851| License | MITCloses#57854Cf#57854 (comment)Commits-------188e2d2 Revert stateless check
fabpot commentedAug 14, 2024
Fixed by#58002 |
Uh oh!
There was an error while loading.Please reload this page.
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.