Skip to content

Commit 94c5f8d

Browse files
Merge branch '5.4' into 6.0
* 5.4: [Console] Fixes "Incorrectly nested style tag found" error when using multi-line header content Fix LDAP connection options fix probably undefined variable $expireAt Fix aliases handling in command name completion Fix division by zero Allow ErrorHandler ^5.0 to be used in HttpKernel [Security/Http] Ignore invalid URLs found in failure/success paths Fix typo
2 parents ce6e0a5 + 6e456f2 commit 94c5f8d

4 files changed

+66
-12
lines changed

Authentication/DefaultAuthenticationFailureHandler.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,34 @@ public function setOptions(array $options)
6969
*/
7070
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response
7171
{
72-
if ($failureUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['failure_path_parameter'])) {
73-
$this->options['failure_path'] = $failureUrl;
74-
}
72+
$options = $this->options;
73+
$failureUrl = ParameterBagUtils::getRequestParameterValue($request, $options['failure_path_parameter']);
7574

76-
if (null === $this->options['failure_path']) {
77-
$this->options['failure_path'] = $this->options['login_path'];
75+
if (\is_string($failureUrl) && str_starts_with($failureUrl, '/')) {
76+
$options['failure_path'] = $failureUrl;
77+
} elseif ($this->logger && $failureUrl) {
78+
$this->logger->debug(sprintf('Ignoring query parameter "%s": not a valid URL.', $options['failure_path_parameter']));
7879
}
7980

80-
if ($this->options['failure_forward']) {
81+
$options['failure_path'] ?? $options['failure_path'] = $options['login_path'];
82+
83+
if ($options['failure_forward']) {
8184
if (null !== $this->logger) {
82-
$this->logger->debug('Authentication failure, forward triggered.', ['failure_path' => $this->options['failure_path']]);
85+
$this->logger->debug('Authentication failure, forward triggered.', ['failure_path' => $options['failure_path']]);
8386
}
8487

85-
$subRequest = $this->httpUtils->createRequest($request, $this->options['failure_path']);
88+
$subRequest = $this->httpUtils->createRequest($request, $options['failure_path']);
8689
$subRequest->attributes->set(Security::AUTHENTICATION_ERROR, $exception);
8790

8891
return $this->httpKernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
8992
}
9093

9194
if (null !== $this->logger) {
92-
$this->logger->debug('Authentication failure, redirect triggered.', ['failure_path' => $this->options['failure_path']]);
95+
$this->logger->debug('Authentication failure, redirect triggered.', ['failure_path' => $options['failure_path']]);
9396
}
9497

9598
$request->getSession()->set(Security::AUTHENTICATION_ERROR, $exception);
9699

97-
return $this->httpUtils->createRedirectResponse($request, $this->options['failure_path']);
100+
return $this->httpUtils->createRedirectResponse($request, $options['failure_path']);
98101
}
99102
}

Authentication/DefaultAuthenticationSuccessHandler.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Http\Authentication;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -30,6 +31,7 @@ class DefaultAuthenticationSuccessHandler implements AuthenticationSuccessHandle
3031
use TargetPathTrait;
3132

3233
protected $httpUtils;
34+
protected $logger;
3335
protected $options;
3436
protected $firewallName;
3537
protected $defaultOptions = [
@@ -43,9 +45,10 @@ class DefaultAuthenticationSuccessHandler implements AuthenticationSuccessHandle
4345
/**
4446
* @param array $options Options for processing a successful authentication attempt
4547
*/
46-
public function __construct(HttpUtils $httpUtils, array $options = [])
48+
public function __construct(HttpUtils $httpUtils, array $options = [], LoggerInterface $logger = null)
4749
{
4850
$this->httpUtils = $httpUtils;
51+
$this->logger = $logger;
4952
$this->setOptions($options);
5053
}
5154

@@ -89,10 +92,16 @@ protected function determineTargetUrl(Request $request): string
8992
return $this->options['default_target_path'];
9093
}
9194

92-
if ($targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter'])) {
95+
$targetUrl = ParameterBagUtils::getRequestParameterValue($request, $this->options['target_path_parameter']);
96+
97+
if (\is_string($targetUrl) && str_starts_with($targetUrl, '/')) {
9398
return $targetUrl;
9499
}
95100

101+
if ($this->logger && $targetUrl) {
102+
$this->logger->debug(sprintf('Ignoring query parameter "%s": not a valid URL.', $this->options['target_path_parameter']));
103+
}
104+
96105
$firewallName = $this->getFirewallName();
97106
if (null !== $firewallName && $targetUrl = $this->getTargetPath($request->getSession(), $firewallName)) {
98107
$this->removeTargetPath($request->getSession(), $firewallName);

Tests/Authentication/DefaultAuthenticationFailureHandlerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,26 @@ public function testFailurePathParameterCanBeOverwritten()
187187
$handler->onAuthenticationFailure($this->request, $this->exception);
188188
}
189189

190+
public function testFailurePathFromRequestWithInvalidUrl()
191+
{
192+
$options = ['failure_path_parameter' => '_my_failure_path'];
193+
194+
$this->request->expects($this->once())
195+
->method('get')->with('_my_failure_path')
196+
->willReturn('some_route_name');
197+
198+
$this->logger->expects($this->exactly(2))
199+
->method('debug')
200+
->withConsecutive(
201+
['Ignoring query parameter "_my_failure_path": not a valid URL.'],
202+
['Authentication failure, redirect triggered.', ['failure_path' => '/login']]
203+
);
204+
205+
$handler = new DefaultAuthenticationFailureHandler($this->httpKernel, $this->httpUtils, $options, $this->logger);
206+
207+
$handler->onAuthenticationFailure($this->request, $this->exception);
208+
}
209+
190210
private function getRequest()
191211
{
192212
$request = $this->createMock(Request::class);

Tests/Authentication/DefaultAuthenticationSuccessHandlerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Security\Http\Tests\Authentication;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\HttpFoundation\Request;
1617
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1718
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -113,4 +114,25 @@ public function getRequestRedirections()
113114
],
114115
];
115116
}
117+
118+
public function testTargetPathFromRequestWithInvalidUrl()
119+
{
120+
$httpUtils = $this->createMock(HttpUtils::class);
121+
$options = ['target_path_parameter' => '_my_target_path'];
122+
$token = $this->createMock(TokenInterface::class);
123+
124+
$request = $this->createMock(Request::class);
125+
$request->expects($this->once())
126+
->method('get')->with('_my_target_path')
127+
->willReturn('some_route_name');
128+
129+
$logger = $this->createMock(LoggerInterface::class);
130+
$logger->expects($this->once())
131+
->method('debug')
132+
->with('Ignoring query parameter "_my_target_path": not a valid URL.');
133+
134+
$handler = new DefaultAuthenticationSuccessHandler($httpUtils, $options, $logger);
135+
136+
$handler->onAuthenticationSuccess($request, $token);
137+
}
116138
}

0 commit comments

Comments
 (0)