Skip to content

Commit 1a049b7

Browse files
Merge branch '4.4' into 5.4
* 4.4: [Security/Http] Remove CSRF tokens from storage on successful login [HttpKernel] Remove private headers before storing responses with HttpCache
2 parents e16ac30 + 076fd20 commit 1a049b7

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

Resources/config/security.php

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

105105
->set('security.authentication.session_strategy', SessionAuthenticationStrategy::class)
106-
->args([param('security.authentication.session_strategy.strategy')])
106+
->args([
107+
param('security.authentication.session_strategy.strategy'),
108+
service('security.csrf.token_storage')->ignoreOnInvalid(),
109+
])
107110
->alias(SessionAuthenticationStrategyInterface::class, 'security.authentication.session_strategy')
108111

109112
->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
/**
@@ -202,4 +224,22 @@ public function provideLegacyClientOptions()
202224
yield [['test_case' => 'CsrfFormLogin', 'root_config' => 'legacy_config.yml', 'enable_authenticator_manager' => false]];
203225
yield [['test_case' => 'CsrfFormLogin', 'root_config' => 'legacy_routes_as_path.yml', 'enable_authenticator_manager' => false]];
204226
}
227+
228+
private function callInRequestContext(KernelBrowser $client, callable $callable): void
229+
{
230+
/** @var EventDispatcherInterface $eventDispatcher */
231+
$eventDispatcher = static::getContainer()->get(EventDispatcherInterface::class);
232+
$wrappedCallable = function (RequestEvent $event) use (&$callable) {
233+
$callable();
234+
$event->setResponse(new Response(''));
235+
$event->stopPropagation();
236+
};
237+
238+
$eventDispatcher->addListener(KernelEvents::REQUEST, $wrappedCallable);
239+
try {
240+
$client->request('GET', '/'.uniqid('', true));
241+
} finally {
242+
$eventDispatcher->removeListener(KernelEvents::REQUEST, $wrappedCallable);
243+
}
244+
}
205245
}

Tests/Functional/LogoutTest.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,14 @@ public function testCsrfTokensAreClearedOnLogout()
2424
{
2525
$client = $this->createClient(['enable_authenticator_manager' => true, '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');
@@ -52,18 +48,14 @@ public function testLegacyCsrfTokensAreClearedOnLogout()
5248
{
5349
$client = $this->createClient(['enable_authenticator_manager' => false, 'test_case' => 'LogoutWithoutSessionInvalidation', 'root_config' => 'config.yml']);
5450
$client->disableReboot();
55-
$this->callInRequestContext($client, function () {
56-
static::getContainer()->get('security.csrf.token_storage')->setToken('foo', 'bar');
57-
});
5851

5952
$client->request('POST', '/login', [
6053
'_username' => 'johannes',
6154
'_password' => 'test',
6255
]);
6356

6457
$this->callInRequestContext($client, function () {
65-
$this->assertTrue(static::getContainer()->get('security.csrf.token_storage')->hasToken('foo'));
66-
$this->assertSame('bar', static::getContainer()->get('security.csrf.token_storage')->getToken('foo'));
58+
static::getContainer()->get('security.csrf.token_storage')->setToken('foo', 'bar');
6759
});
6860

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

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"symfony/security-core": "^5.4|^6.0",
3030
"symfony/security-csrf": "^4.4|^5.0|^6.0",
3131
"symfony/security-guard": "^5.3",
32-
"symfony/security-http": "^5.4|^6.0"
32+
"symfony/security-http": "^5.4.20|~6.0.20|~6.1.12|^6.2.6"
3333
},
3434
"require-dev": {
3535
"doctrine/annotations": "^1.10.4|^2",

0 commit comments

Comments
 (0)