Skip to content

Commit 77c95ea

Browse files
Merge branch '6.1' into 6.2
* 6.1: [HttpFoundation] Fix bad return type in IpUtils::checkIp4() [DependencyInjection] Fix order of arguments when mixing positional and named ones [HttpClient] Fix collecting data non-late for the profiler [Security/Http] Fix compat of persistent remember-me with legacy tokens Bump Symfony version to 6.1.12 Update VERSION for 6.1.11 Update CHANGELOG for 6.1.11 Bump Symfony version to 6.0.20 Update VERSION for 6.0.19 Update CHANGELOG for 6.0.19 Bump Symfony version to 5.4.20 Update VERSION for 5.4.19 Update CONTRIBUTORS for 5.4.19 Update CHANGELOG for 5.4.19 [Security/Http] Remove CSRF tokens from storage on successful login [HttpKernel] Remove private headers before storing responses with HttpCache
2 parents 5c8f064 + e671c97 commit 77c95ea

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

RememberMe/PersistentRememberMeHandler.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ final class PersistentRememberMeHandler extends AbstractRememberMeHandler
3434
{
3535
private TokenProviderInterface $tokenProvider;
3636
private ?TokenVerifierInterface $tokenVerifier;
37-
private string $secret;
3837

3938
public function __construct(TokenProviderInterface $tokenProvider, #[\SensitiveParameter] string $secret, UserProviderInterface $userProvider, RequestStack $requestStack, array $options, LoggerInterface $logger = null, TokenVerifierInterface $tokenVerifier = null)
4039
{
@@ -45,7 +44,6 @@ public function __construct(TokenProviderInterface $tokenProvider, #[\SensitiveP
4544
}
4645
$this->tokenProvider = $tokenProvider;
4746
$this->tokenVerifier = $tokenVerifier;
48-
$this->secret = $secret;
4947
}
5048

5149
public function createRememberMeCookie(UserInterface $user): void

RememberMe/RememberMeDetails.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public function __construct(string $userFqcn, string $userIdentifier, int $expir
3636

3737
public static function fromRawCookie(string $rawCookie): self
3838
{
39+
if (!str_contains($rawCookie, self::COOKIE_DELIMITER)) {
40+
$rawCookie = base64_decode($rawCookie);
41+
}
3942
$cookieParts = explode(self::COOKIE_DELIMITER, $rawCookie, 4);
4043
if (4 !== \count($cookieParts)) {
4144
throw new AuthenticationException('The cookie contains invalid data.');

Session/SessionAuthenticationStrategy.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\Request;
1515
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16+
use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface;
1617

1718
/**
1819
* The default session strategy implementation.
@@ -31,10 +32,15 @@ class SessionAuthenticationStrategy implements SessionAuthenticationStrategyInte
3132
public const INVALIDATE = 'invalidate';
3233

3334
private string $strategy;
35+
private ?ClearableTokenStorageInterface $csrfTokenStorage = null;
3436

35-
public function __construct(string $strategy)
37+
public function __construct(string $strategy, ClearableTokenStorageInterface $csrfTokenStorage = null)
3638
{
3739
$this->strategy = $strategy;
40+
41+
if (self::MIGRATE === $strategy) {
42+
$this->csrfTokenStorage = $csrfTokenStorage;
43+
}
3844
}
3945

4046
public function onAuthentication(Request $request, TokenInterface $token)
@@ -44,10 +50,12 @@ public function onAuthentication(Request $request, TokenInterface $token)
4450
return;
4551

4652
case self::MIGRATE:
47-
// Note: this logic is duplicated in several authentication listeners
48-
// until Symfony 5.0 due to a security fix with BC compat
4953
$request->getSession()->migrate(true);
5054

55+
if ($this->csrfTokenStorage) {
56+
$this->csrfTokenStorage->clear();
57+
}
58+
5159
return;
5260

5361
case self::INVALIDATE:

Tests/RememberMe/PersistentRememberMeHandlerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,19 @@ public function testConsumeRememberMeCookieExpired()
156156

157157
$this->handler->consumeRememberMeCookie(new RememberMeDetails(InMemoryUser::class, 'wouter', 360, 'series1:tokenvalue'));
158158
}
159+
160+
public function testBase64EncodedTokens()
161+
{
162+
$this->tokenProvider->expects($this->any())
163+
->method('loadTokenBySeries')
164+
->with('series1')
165+
->willReturn(new PersistentToken(InMemoryUser::class, 'wouter', 'series1', 'tokenvalue', new \DateTime('-10 min')))
166+
;
167+
168+
$this->tokenProvider->expects($this->once())->method('updateToken')->with('series1');
169+
170+
$rememberMeDetails = new RememberMeDetails(InMemoryUser::class, 'wouter', 360, 'series1:tokenvalue');
171+
$rememberMeDetails = RememberMeDetails::fromRawCookie(base64_encode($rememberMeDetails->toString()));
172+
$this->handler->consumeRememberMeCookie($rememberMeDetails);
173+
}
159174
}

Tests/Session/SessionAuthenticationStrategyTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Session\SessionInterface;
1717
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18+
use Symfony\Component\Security\Csrf\TokenStorage\ClearableTokenStorageInterface;
1819
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
1920

2021
class SessionAuthenticationStrategyTest extends TestCase
@@ -57,6 +58,18 @@ public function testSessionIsInvalidated()
5758
$strategy->onAuthentication($this->getRequest($session), $this->createMock(TokenInterface::class));
5859
}
5960

61+
public function testCsrfTokensAreCleared()
62+
{
63+
$session = $this->createMock(SessionInterface::class);
64+
$session->expects($this->once())->method('migrate')->with($this->equalTo(true));
65+
66+
$csrfStorage = $this->createMock(ClearableTokenStorageInterface::class);
67+
$csrfStorage->expects($this->once())->method('clear');
68+
69+
$strategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE, $csrfStorage);
70+
$strategy->onAuthentication($this->getRequest($session), $this->createMock(TokenInterface::class));
71+
}
72+
6073
private function getRequest($session = null)
6174
{
6275
$request = $this->createMock(Request::class);

0 commit comments

Comments
 (0)