Skip to content

Commit 753c067

Browse files
committed
[FrameworkBundle] added $view['form']->csrfToken() helper
1 parent e1aced8 commit 753c067

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating_php.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
<service id="templating.helper.form" class="%templating.helper.form.class%">
9898
<tag name="templating.helper" alias="form" />
9999
<argument type="service" id="templating.engine.php" />
100+
<argument type="service" id="form.csrf_provider" />
100101
<argument>%templating.helper.form.resources%</argument>
101102
</service>
102103

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Templating\EngineInterface;
1616
use Symfony\Component\Form\FormView;
1717
use Symfony\Component\Form\Exception\FormException;
18+
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
1819
use Symfony\Component\Form\Util\FormUtil;
1920

2021
/**
@@ -27,6 +28,8 @@ class FormHelper extends Helper
2728
{
2829
protected $engine;
2930

31+
protected $csrfProvider;
32+
3033
protected $varStack;
3134

3235
protected $context;
@@ -38,14 +41,16 @@ class FormHelper extends Helper
3841
protected $templates;
3942

4043
/**
41-
* Constructor;
44+
* Constructor.
4245
*
43-
* @param EngineInterface $engine The templating engine
44-
* @param array $resources An array of theme name
46+
* @param EngineInterface $engine The templating engine
47+
* @param CsrfProviderInterface $csrfProvider The CSRF provider
48+
* @param array $resources An array of theme names
4549
*/
46-
public function __construct(EngineInterface $engine, array $resources)
50+
public function __construct(EngineInterface $engine, CsrfProviderInterface $csrfProvider, array $resources)
4751
{
4852
$this->engine = $engine;
53+
$this->csrfProvider = $csrfProvider;
4954
$this->resources = $resources;
5055
$this->varStack = array();
5156
$this->context = array();
@@ -172,6 +177,34 @@ public function rest(FormView $view, array $variables = array())
172177
return $this->renderSection($view, 'rest', $variables);
173178
}
174179

180+
/**
181+
* Returns a CSRF token.
182+
*
183+
* Use this helper for CSRF protection without the overhead of creating a
184+
* form.
185+
*
186+
* <code>
187+
* echo $view['form']->csrfToken('rm_user_'.$user->getId());
188+
* </code>
189+
*
190+
* Check the token in your action using the same intention.
191+
*
192+
* <code>
193+
* $csrfProvider = $this->get('form.csrf_provider');
194+
* if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) {
195+
* throw new \RuntimeException('CSRF attack detected.');
196+
* }
197+
* </code>
198+
*
199+
* @param string $intention The intention of the protected action
200+
*
201+
* @return string A CSRF token
202+
*/
203+
public function csrfToken($intention)
204+
{
205+
return $this->csrfProvider->generateCsrfToken($intention);
206+
}
207+
175208
/**
176209
* Renders a template.
177210
*

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperDivLayoutTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function setUp()
3737
$loader = new FilesystemLoader(array());
3838
$engine = new PhpEngine($templateNameParser, $loader);
3939

40-
$this->helper = new FormHelper($engine, array('FrameworkBundle:Form'));
40+
$this->helper = new FormHelper($engine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array('FrameworkBundle:Form'));
4141

4242
$engine->setHelpers(array(
4343
$this->helper,

src/Symfony/Bundle/FrameworkBundle/Tests/Templating/Helper/FormHelperTableLayoutTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function setUp()
3737
$loader = new FilesystemLoader(array());
3838
$engine = new PhpEngine($templateNameParser, $loader);
3939

40-
$this->helper = new FormHelper($engine, array(
40+
$this->helper = new FormHelper($engine, $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface'), array(
4141
'FrameworkBundle:Form',
4242
'FrameworkBundle:FormTable'
4343
));

0 commit comments

Comments
 (0)