Skip to content

[Security] Prevent creating session in stateless firewalls #51320

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 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio

$this->logger?->debug('Authentication failure, redirect triggered.', ['failure_path' => $options['failure_path']]);

$request->getSession()->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $exception);
if ($request->hasPreviousSession()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Better if we could check if !$options['stateless'] here, but that option is not passed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We cannot check on $request->hasPreviousSession() this is the reason why the test is failing...

Copy link
Member

Choose a reason for hiding this comment

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

hasPreviousSession is absolutely not the same than 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.

So we need a way to access the stateless option from the firewall.
If someone has an idea how to do this, please help on that. I will close that PR for now.

Copy link
Member

Choose a reason for hiding this comment

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

@Seb33300 You can check if the _stateless attribute is set to true instead

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! I modified my PR and reopened it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oups cannot reopen because I force pushed...
Here is the new PR: #51350

$request->getSession()->set(SecurityRequestAttributes::AUTHENTICATION_ERROR, $exception);
}

return $this->httpUtils->createRedirectResponse($request, $options['failure_path']);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function determineTargetUrl(Request $request): string
}

$firewallName = $this->getFirewallName();
if (null !== $firewallName && $targetUrl = $this->getTargetPath($request->getSession(), $firewallName)) {
if (null !== $firewallName && $request->hasPreviousSession() && $targetUrl = $this->getTargetPath($request->getSession(), $firewallName)) {
$this->removeTargetPath($request->getSession(), $firewallName);

return $targetUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function testExceptionIsPersistedInSession()
$this->session->expects($this->once())
->method('set')->with(SecurityRequestAttributes::AUTHENTICATION_ERROR, $this->exception);

// hasPreviousSession
$this->session->expects($this->once())->method('getName')->willReturn('test_session_name');
$this->request->cookies->set('test_session_name', 'session_cookie_val');

$handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, [], $this->logger);
$handler->onAuthenticationFailure($this->request, $this->exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ public function testRequestRedirectionsWithTargetPathInSessions()
$session = $this->createMock(SessionInterface::class);
$session->expects($this->once())->method('get')->with('_security.admin.target_path')->willReturn('/admin/dashboard');
$session->expects($this->once())->method('remove')->with('_security.admin.target_path');
$session->expects($this->once())->method('getName')->willReturn('test_session_name');
$requestWithSession = Request::create('/');
$requestWithSession->setSession($session);
$requestWithSession->cookies->set('test_session_name', 'session_cookie_val');

$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->any())->method('generate')->willReturn('http://localhost/login');
Expand Down