Skip to content

Commit 3a1699a

Browse files
committed
handle disaled csrf protection in the Twig FormExtension
1 parent 2a998e0 commit 3a1699a

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class FormExtension extends \Twig_Extension
3333
protected $varStack;
3434
protected $template;
3535

36-
public function __construct(CsrfProviderInterface $csrfProvider, array $resources = array())
36+
public function __construct(CsrfProviderInterface $csrfProvider = null, array $resources = array())
3737
{
3838
$this->csrfProvider = $csrfProvider;
3939
$this->themes = new \SplObjectStorage();
@@ -298,6 +298,10 @@ protected function render(FormView $view, $section, array $variables = array())
298298
*/
299299
public function getCsrfToken($intention)
300300
{
301+
if (!$this->csrfProvider instanceof CsrfProviderInterface) {
302+
throw new \BadMethodCallException('CSRF token can only be generated if the "form.csrf_provider" service is available');
303+
}
304+
301305
return $this->csrfProvider->generateCsrfToken($intention);
302306
}
303307

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public function rest(FormView $view, array $variables = array())
202202
*/
203203
public function csrfToken($intention)
204204
{
205-
if (! $this->csrfProvider instanceof CsrfProviderInterface) {
205+
if (!$this->csrfProvider instanceof CsrfProviderInterface) {
206206
throw new \BadMethodCallException('CSRF token can only be generated if the "form.csrf_provider" service is available');
207207
}
208208

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +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" />
78+
<argument type="service" id="form.csrf_provider" on-invalid="null" />
7979
<argument>%twig.form.resources%</argument>
8080
</service>
8181

0 commit comments

Comments
 (0)