8000 [Twig] added {{ csrf_token() }} helper · symfony/symfony@e1aced8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1aced8

Browse files
committed
[Twig] added {{ csrf_token() }} helper
1 parent 009e6d7 commit e1aced8

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

src/Symfony/Bridge/Twig/Extension/FormExtension.php

+33-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\Twig\TokenParser\FormThemeTokenParser;
1515
use Symfony\Component\Form\FormView;
1616
use Symfony\Component\Form\Exception\FormException;
17+
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
1718
use Symfony\Component\Form\Util\FormUtil;
1819

1920
/**
@@ -24,15 +25,17 @@
2425
*/
2526
class FormExtension extends \Twig_Extension
2627
{
28+
protected $csrfProvider;
2729
protected $resources;
2830
protected $blocks;
2931
protected $environment;
3032
protected $themes;
3133
protected $varStack;
3234
protected $template;
3335

34-
public function __construct(array $resources = array())
36+
public function __construct(CsrfProviderInterface $csrfProvider, array $resources = array())
3537
{
38+
$this->csrfProvider = $csrfProvider;
3639
$this->themes = new \SplObjectStorage();
3740
$this->varStack = array();
3841
$this->blocks = new \SplObjectStorage();
@@ -81,6 +84,7 @@ public function getFunctions()
8184
'form_label' => new \Twig_Function_Method($this, 'renderLabel', array('is_safe' => array('html'))),
8285
'form_row' => new \Twig_Function_Method($this, 'renderRow', array('is_safe' => array('html'))),
8386
'form_rest' => new \Twig_Function_Method($this, 'renderRest', array('is_safe' => array('html'))),
87+
'csrf_token' => new \Twig_Function_Method($this, 'getCsrfToken'),
8488
'_form_is_choice_group' => new \Twig_Function_Method($this, 'isChoiceGroup', array('is_safe' => array('html'))),
8589
'_form_is_choice_selected' => new \Twig_Function_Method($this, 'isChoiceSelected', array('is_safe' => array('html'))),
8690
);
@@ -269,6 +273,34 @@ protected function render(FormView $view, $section, array $variables = array())
269273
));
270274
}
271275

276+
/**
277+
* Returns a CSRF token.
278+
*
279+
* Use this helper for CSRF protection without the overhead of creating a
280+
* form.
281+
*
282+
* <code>
283+
* <input type="hidden" name="token" value="{{ csrf_token('rm_user_' ~ user.id) }}">
284+
* </code>
285+
*
286+
* Check the token in your action using the same intention.
287+
*
288+
* <code>
289+
* $csrfProvider = $this->get('form.csrf_provider');
290+
* if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) {
291+
* throw new \RuntimeException('CSRF attack detected.');
292+
* }
293+
* </code>
294+
*
295+
* @param string $intention The intention of the protected action
296+
*
297+
* @return string A CSRF token
298+
*/
299+
public function getCsrfToken($intention)
300+
{
301+
return $this->csrfProvider->generateCsrfToken($intention);
302+
}
303+
272304
/**
273305
* Returns the name of the extension.
274306
*

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
<service id="twig.extension.form" class="%twig.extension.form.class%" public="false">
7777
<tag name="twig.extension" />
78+
<argument type="service" id="form.csrf_provider" />
7879
<argument>%twig.form.resources%</argument>
7980
</service>
8081

tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionDivLayoutTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function setUp()
3838
__DIR__,
3939
));
4040

41-
$this->extension = new FormExtension(array(
41+
$this->extension = new FormExtension($this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array(
4242
'form_div_layout.html.twig',
4343
'custom_widgets.html.twig',
4444
));

tests/Symfony/Tests/Bridge/Twig/Extension/FormExtensionTableLayoutTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function setUp()
3838
__DIR__,
3939
));
4040

41-
$this->extension = new FormExtension(array(
41+
$this->extension = new FormExtension($this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array(
4242
'form_table_layout.html.twig',
4343
'custom_widgets.html.twig',
4444
));

0 commit comments

Comments
 (0)
0