Skip to content

Commit

Permalink
bug #36176 [Security] Check if firewall is stateless before checking …
Browse files Browse the repository at this point in the history
…for session/previous session (koenreiniers)

This PR was submitted for the 4.4 branch but it was squashed and merged into the 3.4 branch instead.

Discussion
----------

[Security] Check if firewall is stateless before checking for session/previous session

| Q             | A
| ------------- | ---
| Branch?       | 4.4 <!-- see below -->
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | - <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | -

For one of our applications we had the issue that the session was always initialized, even for routes behind stateless firewalls. Using the redis session adapter this sometimes lead to exceptions if the connection failed. This change prevents the session from being initialized in the guard authentication handler for stateless firewalls

Commits
-------

9bb1230 [Security] Check if firewall is stateless before checking for session/previous session
  • Loading branch information
nicolas-grekas committed Mar 23, 2020
2 parents 5b5b61f + 9bb1230 commit 881fa02
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -134,7 +134,7 @@ public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyIn

private function migrateSession(Request $request, TokenInterface $token, $providerKey)
{
if (!$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession() || \in_array($providerKey, $this->statelessProviderKeys, true)) {
if (\in_array($providerKey, $this->statelessProviderKeys, true) || !$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
return;
}

Expand Down
Expand Up @@ -149,6 +149,25 @@ public function testSessionStrategyIsNotCalledWhenStateless()
$handler->authenticateWithToken($this->token, $this->request, 'some_provider_key');
}

/**
* @requires function \Symfony\Component\HttpFoundation\Request::setSessionFactory
*/
public function testSessionIsNotInstantiatedOnStatelessFirewall()
{
$sessionFactory = $this->getMockBuilder(\stdClass::class)
->setMethods(['__invoke'])
->getMock();

$sessionFactory->expects($this->never())
->method('__invoke');

$this->request->setSessionFactory($sessionFactory);

$handler = new GuardAuthenticatorHandler($this->tokenStorage, $this->dispatcher, ['stateless_provider_key']);
$handler->setSessionAuthenticationStrategy($this->sessionStrategy);
$handler->authenticateWithToken($this->token, $this->request, 'stateless_provider_key');
}

protected function setUp()
{
$this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')->getMock();
Expand Down

0 comments on commit 881fa02

Please sign in to comment.