Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Security] Do not try to clear CSRF on stateless request #54742

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

Closed
wants to merge 3 commits into from

Conversation

Seb33300
Copy link
Contributor

@Seb33300 Seb33300 commented Apr 26, 2024

Q A
Branch? 6.4
Bug fix? yes
New feature? no
Deprecations? no
Issues -
License MIT

In my application, I am getting many logs Session was used while the request was declared stateless. triggered by users logout on our API.

We should not try to clear CSRF on stateless sessions.
Similar to #51350

@Seb33300 Seb33300 requested a review from chalasr as a code owner April 26, 2024 08:19
@carsonbot carsonbot added this to the 6.4 milestone Apr 26, 2024
@Seb33300 Seb33300 force-pushed the patch-1 branch 2 times, most recently from d71784c to 3d4b434 Compare April 26, 2024 09:03

if (
$this->csrfTokenStorage instanceof SessionTokenStorage
&& ($request->attributes->getBoolean('_stateless') || !$request->hasPreviousSession())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But why do we have a previous request in the first place in your situation?
If there is a previous session, we should clean the session even of this contradicts the stateless flag, or we might create a security issue, or?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our API and website are sharing the same domain.
API is accessible from domain.com/api/
The user can be authenticated on the website using session and also using JWT on the API on the same device.

When the user already has a session on the website and try to logout from the API, it results in this log message.
(On the API we use the logout endpoint to delete refresh tokens of the user in our db)
Because the logout listener is only checking if the request ha a previous session when all other listeners also check if the request is stateless.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, this is also how works others logout listeners.
If the user logout on a stateless request (and also has a session on the same device), his session is not cleared.
So we should also clear the CSRF token too on statleless requests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the API using the same firewall than the website ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we had a separated stateless firewall for ^/api routes.
I was not able to find how to reproduce that issue myself by using our mobile app, but I was suspecting the mobile app to call some website routes in some rare scenarios.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then I think the right fix would be that the CSRF clearing logout listener should be registered only for stateful firewalls (note that I'm not talking about the flag on the request here, but on the firewall config).

@Seb33300
Copy link
Contributor Author

Seb33300 commented Jun 3, 2024

@chalasr @nicolas-grekas Any chance this could be merged?

@Seb33300
Copy link
Contributor Author

image

@Seb33300
Copy link
Contributor Author

Seb33300 commented Jul 3, 2024

@chalasr @nicolas-grekas any update?

@xabbuh You recently approved a similar PR, maybe you can review this one too?

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That doesn't sound right to me. This attribute shouldn't be use to make such decisions.
Instead, what about checking if the firewall is stateless? Would that make sense?

@nicolas-grekas
Copy link
Member

(note that #57372 has been reverted recently)

@Seb33300
Copy link
Contributor Author

(note that #57372 has been reverted recently)

Maybe @VincentLanglet can provide more context about the revert of that PR, but it looks like the PR was not totally reverted, only 1 line from what I see

That doesn't sound right to me. This attribute shouldn't be use to make such decisions. Instead, what about checking if the firewall is stateless? Would that make sense?

This was @chalasr suggestion from a previous PR (#51320 (comment)) to use the _stateless attribute to check if the firewall is stateless.
And this is how stateless firewalls are checked everywhere else in Symfony.

@nicolas-grekas
Copy link
Member

this is how stateless firewalls are checked everywhere else in Symfony

not anymore, this was all reverted recently

@chalasr
Copy link
Member

chalasr commented Aug 23, 2024

This was @chalasr suggestion from a previous PR (#51320 (comment)) to use the _stateless attribute to check if the firewall is stateless.

Sorry for the (now) bad suggestion, we've been reviewing and clarifying the scope of this attribute lately. The statelessness of a firewall can be check using getFirewallConfig($request)->isStateless() on the security.firewall_map service (FirewallMapInterface).

@Seb33300 Seb33300 force-pushed the patch-1 branch 4 times, most recently from cc15f6d to 1be07ff Compare August 24, 2024 04:20
@Seb33300
Copy link
Contributor Author

Thank for the clarification.

I tried to updated my PR. But FirewallMap is part of SecurityBundle.
Is it OK to have a dependency to a bundle in a component?

@derrabus
Copy link
Member

Is it OK to have a dependency to a bundle in a component?

No.

@derrabus
Copy link
Member

Please keep in mind that sateless firewall !== stateless request. I can have stateful requests behind a stateless firewall. And I can have stateless requests without any firewall.

@Seb33300
Copy link
Contributor Author

I re created my original PR here: #58082

$request = new Request();
$request->setSession($session);
$request->cookies->set($session->getName(), 'previous_session');
$request->attributes->set('_stateless', true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove this since it's not related anymore, isn't it?


public function __construct(ClearableTokenStorageInterface $csrfTokenStorage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding an argument is a BC break, let's make it nullable?

@Seb33300
Copy link
Contributor Author

Is it OK to have a dependency to a bundle in a component?

No.

@nicolas-grekas I can fix your review suggestions, but what about this?
This PR introduce a dependency on the security bundle and I don't really like it.

@nicolas-grekas
Copy link
Member

Could we augment the LogoutEvent with an isStatelessFirewall method?
That'd require telling LogoutListener that the firewall it monitors is lazy, which is doable in SecurityExtension?
Can you please dig this idea?

@nicolas-grekas
Copy link
Member

Let me close here as another approach has been hinted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants