Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ca5e561

Browse filesBrowse files
committed
bug #27454 [FrameworkBundle][TwigBridge] Fix BC break from strong dependency on CSRF token storage (tgalopin)
This PR was merged into the 4.1 branch. Discussion ---------- [FrameworkBundle][TwigBridge] Fix BC break from strong dependency on CSRF token storage | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - The PR #25197 introduced the `csrf_token` function in Twig. This extension relies on `CsrfTokenManagerInterface`, which itself relies on the session. In some contexts such as when sessions are stored in Redis and we try to warmup the cache in CLI without Redis available, this makes the process fails. This PR fixes this by using a Twig runtime instead of a direct extension to avoid a strong dependency on `CsrfTokenManagerInterface`. Commits ------- 68994a6 [FrameworkBundle][TwigBridge] Fix BC break from strong dependency on CSRF token storage
2 parents 8bbd738 + 68994a6 commit ca5e561
Copy full SHA for ca5e561

File tree

3 files changed

+40
-15
lines changed
Filter options

3 files changed

+40
-15
lines changed

‎src/Symfony/Bridge/Twig/Extension/CsrfExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/CsrfExtension.php
+2-14Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,22 @@
1111

1212
namespace Symfony\Bridge\Twig\Extension;
1313

14-
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
1514
use Twig\Extension\AbstractExtension;
1615
use Twig\TwigFunction;
1716

1817
/**
1918
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
19+
* @author Titouan Galopin <galopintitouan@gmail.com>
2020
*/
2121
class CsrfExtension extends AbstractExtension
2222
{
23-
private $csrfTokenManager;
24-
25-
public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
26-
{
27-
$this->csrfTokenManager = $csrfTokenManager;
28-
}
29-
3023
/**
3124
* {@inheritdoc}
3225
*/
3326
public function getFunctions(): array
3427
{
3528
return array(
36-
new TwigFunction('csrf_token', array($this, 'getCsrfToken')),
29+
new TwigFunction('csrf_token', array(CsrfRuntime::class, 'getCsrfToken')),
3730
);
3831
}
39-
40-
public function getCsrfToken(string $tokenId): string
41-
{
42-
return $this->csrfTokenManager->getToken($tokenId)->getValue();
43-
}
4432
}
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
15+
16+
/**
17+
* @author Christian Flothmann <christian.flothmann@sensiolabs.de>
18+
* @author Titouan Galopin <galopintitouan@gmail.com>
19+
*/
20+
class CsrfRuntime
21+
{
22+
private $csrfTokenManager;
23+
24+
public function __construct(CsrfTokenManagerInterface $csrfTokenManager)
25+
{
26+
$this->csrfTokenManager = $csrfTokenManager;
27+
}
28+
29+
public function getCsrfToken(string $tokenId): string
30+
{
31+
return $this->csrfTokenManager->getToken($tokenId)->getValue();
32+
}
33+
}

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/security_csrf.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/security_csrf.xml
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
</service>
2323
<service id="Symfony\Component\Security\Csrf\CsrfTokenManagerInterface" alias="security.csrf.token_manager" />
2424

25+
<service id="twig.runtime.security_csrf" class="Symfony\Bridge\Twig\Extension\CsrfRuntime">
26+
<tag name="twig.runtime" />
27+
<argument type="service" id="security.csrf.token_manager" />
28+
</service>
29+
2530
<service id="twig.extension.security_csrf" class="Symfony\Bridge\Twig\Extension\CsrfExtension">
2631
<tag name="twig.extension" />
27-
<argument type="service" id="security.csrf.token_manager" />
2832
</service>
2933
</services>
3034
</container>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.