Skip to content

Commit 14e6dac

Browse files
guillaume-sainthillierscheb
authored andcommitted
Read the token untracked in TwoFactorAccessListener, so public responses stay cacheable
1 parent c7dc0ff commit 14e6dac

4 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎app/src/Controller/HomeController.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@ public function alwaysAccessible(): Response
2121
{
2222
return new Response('This page is accessible for any user any time');
2323
}
24+
25+
#[Route('/cacheable', name: 'cacheable')]
26+
public function cacheable(): Response
27+
{
28+
$response = new Response('This page is the same for any user');
29+
$response->setPublic();
30+
$response->setMaxAge(3600);
31+
32+
return $response;
33+
}
2434
}

‎app/tests/TestCase.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ protected function navigateToPathWithoutAnyAccessControl(): Crawler
209209
return $this->client->request('GET', '/');
210210
}
211211

212+
protected function navigateToCacheablePathWithoutAnyAccessControl(): Crawler
213+
{
214+
return $this->client->request('GET', '/cacheable');
215+
}
216+
212217
protected function navigateToPathWithAnonymousAccessControl()
213218
{
214219
return $this->client->request('GET', '/alwaysAccessible');
@@ -317,6 +322,21 @@ protected function assertCsrfError(Crawler $page): void
317322
);
318323
}
319324

325+
protected function assertIsCacheablePage(Crawler $page): void
326+
{
327+
$this->assertResponseStatusCode(200);
328+
$this->assertStringContainsString(
329+
'This page is the same for any user',
330+
$page->html(),
331+
'The page shown must be the "cacheable" page'
332+
);
333+
}
334+
335+
protected function assertResponseIsPubliclyCacheable(): void
336+
{
337+
$this->assertSame('max-age=3600, public', $this->client->getResponse()->headers->get('Cache-Control'));
338+
}
339+
320340
protected function assertResponseIs403Forbidden()
321341
{
322342
$this->assertResponseStatusCode(403);

‎app/tests/TwoFactorAccessControlTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ public function test2faProtectsPathWithoutAnyAccessControl(): void
2828
$this->assert2faIsRequired($page);
2929
}
3030

31+
public function test2faProtectsCacheablePathWithoutAnyAccessControl(): void
32+
{
33+
$this->setAll2faProvidersEnabled(true);
34+
35+
$pageAfterLogin = $this->performLogin();
36+
$this->assert2faIsRequired($pageAfterLogin);
37+
38+
$page = $this->navigateToCacheablePathWithoutAnyAccessControl();
39+
$this->assert2faIsRequired($page);
40+
}
41+
42+
public function testAnonymousAccessToPathWithoutAnyAccessControlKeepsResponseCacheable(): void
43+
{
44+
$page = $this->navigateToCacheablePathWithoutAnyAccessControl();
45+
$this->assertIsCacheablePage($page);
46+
$this->assertResponseIsPubliclyCacheable();
47+
}
48+
3149
public function test2faAllowsAccessToPageWithAccessControlAnonymous(): void
3250
{
3351
$this->setAll2faProvidersEnabled(true);

‎src/bundle/Resources/config/security.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@
118118
->set('scheb_two_factor.security.access_listener', TwoFactorAccessListener::class)
119119
->args([
120120
abstract_arg('Two-factor firewall config'),
121-
service('security.token_storage'),
121+
// Untracked, like Symfony's firewall listeners: reading the token on every request must not flag the
122+
// session as used, which would make every response private (lazy firewalls)
123+
service('security.untracked_token_storage'),
122124
service('scheb_two_factor.security.access.access_decider'),
123125
])
124126

0 commit comments

Comments
 (0)