Skip to content

Commit 5990182

Browse files
committed
[FrameworkBundle] Fail gracefully when forms use disabled CSRF
1 parent 92c7e6e commit 5990182

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+4
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
496496
}
497497

498498
if ($this->isConfigEnabled($container, $config['form']['csrf_protection'])) {
499+
if (!$container->hasDefinition('security.csrf.token_generator')) {
500+
throw new \LogicException('To use form CSRF protection `framework.csrf_protection` must be enabled.');
501+
}
502+
499503
$loader->load('form_csrf.xml');
500504

501505
$container->setParameter('form.type_extension.csrf.enabled', true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'csrf_protection' => false,
5+
'form' => [
6+
'csrf_protection' => true,
7+
],
8+
]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services
7+
https://symfony.com/schema/dic/services/services-1.0.xsd
8+
http://symfony.com/schema/dic/symfony
9+
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
10+
>
11+
<framework:config>
12+
<framework:csrf-protection enabled="false"/>
13+
<framework:form enabled="true">
14+
<framework:csrf-protection enabled="true"/>
15+
</framework:form>
16+
</framework:config>
17+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
framework:
2+
csrf_protection: false
3+
form:
4+
csrf_protection: true

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ public function testFormCsrfProtection()
8484
$this->assertEquals('%form.type_extension.csrf.field_name%', $def->getArgument(2));
8585
}
8686

87+
public function testFormCsrfProtectionWithCsrfDisabled()
88+
{
89+
$this->expectException(\LogicException::class);
90+
$this->expectExceptionMessage('To use form CSRF protection `framework.csrf_protection` must be enabled.');
91+
92+
$this->createContainerFromFile('form_csrf_disabled');
93+
}
94+
8795
public function testPropertyAccessWithDefaultValue()
8896
{
8997
$container = $this->createContainerFromFile('full');

0 commit comments

Comments
 (0)