Skip to content

Commit f3feb14

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 2f7f0b7 + 5c790f7 commit f3feb14

File tree

4 files changed

+46
-7
lines changed

4 files changed

+46
-7
lines changed

Resources/config/security.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@
106106
->set('security.authentication.trust_resolver', AuthenticationTrustResolver::class)
107107

108108
->set('security.authentication.session_strategy', SessionAuthenticationStrategy::class)
109-
->args([param('security.authentication.session_strategy.strategy')])
109+
->args([
110+
param('security.authentication.session_strategy.strategy'),
111+
service('security.csrf.token_storage')->ignoreOnInvalid(),
112+
])
110113
->alias(SessionAuthenticationStrategyInterface::class, 'security.authentication.session_strategy')
111114

112115
->set('security.authentication.session_strategy_noop', SessionAuthenticationStrategy::class)

Tests/Functional/CsrfFormLoginTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
1313

14+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpKernel\Event\RequestEvent;
18+
use Symfony\Component\HttpKernel\KernelEvents;
19+
1420
class CsrfFormLoginTest extends AbstractWebTestCase
1521
{
1622
/**
@@ -20,6 +26,10 @@ public function testFormLoginAndLogoutWithCsrfTokens($options)
2026
{
2127
$client = $this->createClient($options);
2228

29+
$this->callInRequestContext($client, function () {
30+
static::getContainer()->get('security.csrf.token_storage')->setToken('foo', 'bar');
31+
});
32+
2333
$form = $client->request('GET', '/login')->selectButton('login')->form();
2434
$form['user_login[username]'] = 'johannes';
2535
$form['user_login[password]'] = 'test';
@@ -40,6 +50,10 @@ public function testFormLoginAndLogoutWithCsrfTokens($options)
4050
$client->click($logoutLinks[0]);
4151

4252
$this->assertRedirect($client->getResponse(), '/');
53+
54+
$this->callInRequestContext($client, function () {
55+
$this->assertFalse(static::getContainer()->get('security.csrf.token_storage')->hasToken('foo'));
56+
});
4357
}
4458

4559
/**
@@ -49,6 +63,10 @@ public function testFormLoginWithInvalidCsrfToken($options)
4963
{
5064
$client = $this->createClient($options);
5165

66+
$this->callInRequestContext($client, function () {
67+
static::getContainer()->get('security.csrf.token_storage')->setToken('foo', 'bar');
68+
});
69+
5270
$form = $client->request('GET', '/login')->selectButton('login')->form();
5371
$form['user_login[_token]'] = '';
5472
$client->submit($form);
@@ -57,6 +75,10 @@ public function testFormLoginWithInvalidCsrfToken($options)
5775

5876
$text = $client->followRedirect()->text(null, true);
5977
$this->assertStringContainsString('Invalid CSRF token.', $text);
78+
79+
$this->callInRequestContext($client, function () {
80+
$this->assertTrue(static::getContainer()->get('security.csrf.token_storage')->hasToken('foo'));
81+
});
6082
}
6183

6284
/**
@@ -105,4 +127,22 @@ public function provideClientOptions()
105127
yield [['test_case' => 'CsrfFormLogin', 'root_config' => 'config.yml']];
106128
yield [['test_case' => 'CsrfFormLogin', 'root_config' => 'routes_as_path.yml']];
107129
}
130+
131+
private function callInRequestContext(KernelBrowser $client, callable $callable): void
132+
{
133+
/** @var EventDispatcherInterface $eventDispatcher */
134+
$eventDispatcher = static::getContainer()->get(EventDispatcherInterface::class);
135+
$wrappedCallable = function (RequestEvent $event) use (&$callable) {
136+
$callable();
137+
$event->setResponse(new Response(''));
138+
$event->stopPropagation();
139+
};
140+
141+
$eventDispatcher->addListener(KernelEvents::REQUEST, $wrappedCallable);
142+
try {
143+
$client->request('GET', '/'.uniqid('', true));
144+
} finally {
145+
$eventDispatcher->removeListener(KernelEvents::REQUEST, $wrappedCallable);
146+
}
147+
}
108148
}

Tests/Functional/LogoutTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,14 @@ public function testCsrfTokensAreClearedOnLogout()
2424
{
2525
$client = $this->createClient(['test_case' => 'LogoutWithoutSessionInvalidation', 'root_config' => 'config.yml']);
2626
$client->disableReboot();
27-
$this->callInRequestContext($client, function () {
28-
static::getContainer()->get('security.csrf.token_storage')->setToken('foo', 'bar');
29-
});
3027

3128
$client->request('POST', '/login', [
3229
'_username' => 'johannes',
3330
'_password' => 'test',
3431
]);
3532

3633
$this->callInRequestContext($client, function () {
37-
$this->assertTrue(static::getContainer()->get('security.csrf.token_storage')->hasToken('foo'));
38-
$this->assertSame('bar', static::getContainer()->get('security.csrf.token_storage')->getToken('foo'));
34+
static::getContainer()->get('security.csrf.token_storage')->setToken('foo', 'bar');
3935
});
4036

4137
$client->request('GET', '/logout');

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"symfony/password-hasher": "^5.4|^6.0",
2828
"symfony/security-core": "^6.2",
2929
"symfony/security-csrf": "^5.4|^6.0",
30-
"symfony/security-http": "^6.2"
30+
"symfony/security-http": "^6.2.6"
3131
},
3232
"require-dev": {
3333
"doctrine/annotations": "^1.10.4|^2",

0 commit comments

Comments
 (0)