Skip to content

Commit e5928f7

Browse files
committed
bug symfony#16521 [3.0] Revert removal of framework.csrf_protection section (WouterJ)
This PR was merged into the 3.0-dev branch. Discussion ---------- [3.0] Revert removal of framework.csrf_protection section | Q | A | ------------- | --- | Bug fix? | [yes|no] | New feature? | [yes|no] | BC breaks? | [yes|no] | Deprecations? | [yes|no] | Tests pass? | yes | Fixed tickets | symfony#16508 | License | MIT | Doc PR | - This section was incorrectly removed from Symfony 3, only the `field_name` setting was removed. Disabling/enabling CSRF seperately from the froms is not deprecated and should not be removed. /cc @symfony/deciders please merge quickly, it's holding up bundles with functional tests wanting to support to Symfony 3 Commits ------- 6f2782b Revert removal of framework.csrf_protection section
2 parents 779833b + 6f2782b commit e5928f7

26 files changed

+66
-96
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function getConfigTreeBuilder()
8585
->end()
8686
;
8787

88+
$this->addCsrfSection($rootNode);
8889
$this->addFormSection($rootNode);
8990
$this->addEsiSection($rootNode);
9091
$this->addSsiSection($rootNode);
@@ -105,6 +106,17 @@ public function getConfigTreeBuilder()
105106
return $treeBuilder;
106107
}
107108

109+
private function addCsrfSection(ArrayNodeDefinition $rootNode)
110+
{
111+
$rootNode
112+
->children()
113+
->arrayNode('csrf_protection')
114+
->canBeEnabled()
115+
->end()
116+
->end()
117+
;
118+
}
119+
108120
private function addFormSection(ArrayNodeDefinition $rootNode)
109121
{
110122
$rootNode
@@ -114,8 +126,12 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
114126
->canBeEnabled()
115127
->children()
116128
->arrayNode('csrf_protection')
117-
->canBeEnabled()
129+
->treatFalseLike(array('enabled' => false))
130+
->treatTrueLike(array('enabled' => true))
131+
->treatNullLike(array('enabled' => true))
132+
->addDefaultsIfNotSet()
118133
->children()
134+
->booleanNode('enabled')->defaultNull()->end() // defaults to framework.csrf_protection.enabled
119135
->scalarNode('field_name')->defaultValue('_token')->end()
120136
->end()
121137
->end()

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function load(array $configs, ContainerBuilder $container)
9797
}
9898
}
9999

100-
$this->registerSecurityCsrfConfiguration($config['form']['csrf_protection'], $container, $loader);
100+
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
101101

102102
if (isset($config['assets'])) {
103103
$this->registerAssetsConfiguration($config['assets'], $container, $loader);
@@ -198,6 +198,9 @@ public function getConfiguration(array $config, ContainerBuilder $container)
198198
private function registerFormConfiguration($config, ContainerBuilder $container, XmlFileLoader $loader)
199199
{
200200
$loader->load('form.xml');
201+
if (null === $config['form']['csrf_protection']['enabled']) {
202+
$config['form']['csrf_protection']['enabled'] = $config['csrf_protection']['enabled'];
203+
}
201204

202205
if ($this->isConfigEnabled($container, $config['form']['csrf_protection'])) {
203206
$loader->load('form_csrf.xml');

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<xsd:all>
1212
<xsd:element name="assets" type="assets" minOccurs="0" maxOccurs="1" />
1313
<xsd:element name="form" type="form" minOccurs="0" maxOccurs="1" />
14+
<xsd:element name="csrf-protection" type="csrf_protection" minOccurs="0" maxOccurs="1" />
1415
<xsd:element name="esi" type="esi" minOccurs="0" maxOccurs="1" />
1516
<xsd:element name="fragments" type="fragments" minOccurs="0" maxOccurs="1" />
1617
<xsd:element name="profiler" type="profiler" minOccurs="0" maxOccurs="1" />
@@ -46,6 +47,10 @@
4647
<xsd:attribute name="field-name" type="xsd:string" />
4748
</xsd:complexType>
4849

50+
<xsd:complexType name="csrf_protection">
51+
<xsd:attribute name="enabled" type="xsd:boolean" />
52+
</xsd:complexType>
53+
4954
<xsd:complexType name="esi">
5055
<xsd:attribute name="enabled" type="xsd:boolean" />
5156
</xsd:complexType>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,13 @@ protected static function getBundleDefaultConfig()
123123
'trusted_proxies' => array(),
124124
'ide' => null,
125125
'default_locale' => 'en',
126+
'csrf_protection' => array(
127+
'enabled' => false,
128+
),
126129
'form' => array(
127130
'enabled' => false,
128131
'csrf_protection' => array(
129-
'enabled' => false,
132+
'enabled' => null, // defaults to csrf_protection.enabled
130133
'field_name' => '_token',
131134
),
132135
),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/csrf.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<?php
22

33
$container->loadFromExtension('framework', array(
4-
'form' => array(
5-
'enabled' => true,
6-
'csrf_protection' => true,
7-
),
4+
'csrf_protection' => true,
5+
'form' => true,
86
'session' => array(
97
'handler_id' => null,
108
),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/csrf_disabled.php

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

33
$container->loadFromExtension('framework', array(
4-
'form' => array(
5-
'csrf_protection' => array(
6-
'enabled' => true,
7-
),
4+
'csrf_protection' => array(
5+
'enabled' => true,
86
),
97
));

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/form_csrf_sets_field_name.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/form_csrf_under_form_sets_field_name.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/form_no_csrf.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
$container->loadFromExtension('framework', array(
44
'form' => array(
5-
'enabled' => true,
6-
'csrf_protection' => false,
5+
'csrf_protection' => array(
6+
'enabled' => false,
7+
),
78
),
89
));

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
$container->loadFromExtension('framework', array(
44
'secret' => 's3cr3t',
55
'default_locale' => 'fr',
6+
'csrf_protection' => true,
67
'form' => array(
78
'csrf_protection' => array(
8-
'enabled' => true,
99
'field_name' => '_csrf',
1010
),
1111
),

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/csrf.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
88

99
<framework:config>
10-
<framework:form>
11-
<framework:csrf-protection />
12-
</framework:form>
13-
10+
<framework:csrf-protection />
11+
<framework:form />
1412
<framework:session />
1513
</framework:config>
1614
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/csrf_disabled.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
88

99
<framework:config>
10-
<framework:form>
11-
<framework:csrf-protection enabled="false" />
12-
</framework:form>
10+
<framework:csrf-protection enabled="false" />
1311
</framework:config>
1412
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/csrf_needs_session.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
88

99
<framework:config>
10-
<framework:form>
11-
<framework:csrf-protection />
12-
</framework:form>
10+
<framework:csrf-protection />
1311
</framework:config>
1412
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/form_csrf_sets_field_name.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
88

99
<framework:config>
10-
<framework:form>
11-
<framework:csrf-protection field-name="_custom" />
12-
</framework:form>
10+
<framework:csrf-protection field-name="_custom" />
1311
<framework:session />
12+
<framework:form />
1413
</framework:config>
1514
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/form_csrf_under_form_sets_field_name.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
88

99
<framework:config>
10-
<framework:form>
11-
<framework:csrf-protection field-name="_custom_form" />
12-
</framework:form>
10+
<framework:csrf-protection field-name="_custom_form" />
11+
<framework:form />
1312
<framework:session />
1413
</framework:config>
1514
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
88

99
<framework:config secret="s3cr3t" ide="file%%link%%format" default-locale="fr" trusted-proxies="127.0.0.1, 10.0.0.1" http-method-override="false">
10+
<framework:csrf-protection />
1011
<framework:form>
11-
<framework:csrf-protection enabled="true" field-name="_csrf"/>
12+
<framework:csrf-protection field-name="_csrf"/>
1213
</framework:form>
1314
<framework:esi enabled="true" />
1415
<framework:profiler only-exceptions="true" enabled="false" />
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
framework:
22
secret: s3cr3t
3-
form:
4-
csrf_protection: true
3+
csrf_protection: ~
4+
form: ~
55
session: ~

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/csrf_disabled.yml

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
framework:
2-
form:
3-
csrf_protection: ~
2+
csrf_protection: ~

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/form_csrf_under_form_sets_field_name.yml

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
framework:
22
secret: s3cr3t
33
default_locale: fr
4+
csrf_protection: true
45
form:
56
csrf_protection:
67
field_name: _csrf

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ abstract class FrameworkExtensionTest extends TestCase
2424
{
2525
abstract protected function loadFromFile(ContainerBuilder $container, $file);
2626

27-
public function testCsrfProtection()
27+
public function testFormCsrfProtection()
2828
{
2929
$container = $this->createContainerFromFile('full');
3030

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/config/framework.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ framework:
22
secret: test
33
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
44
validation: { enabled: true, enable_annotations: true }
5-
form:
6-
csrf_protection:
7-
enabled: true
5+
csrf_protection: true
6+
form: true
87
test: ~
98
default_locale: en
109
session:

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/FirewallEntryPoint/config.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
framework:
2-
secret: test
3-
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
4-
validation: { enabled: true, enable_annotations: true }
5-
form:
6-
csrf_protection:
7-
enabled: true
2+
secret: test
3+
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
4+
validation: { enabled: true, enable_annotations: true }
5+
csrf_protection: true
6+
form: true
87
test: ~
98
default_locale: en
109
session:

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/config/framework.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
framework:
2-
secret: test
3-
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
4-
validation: { enabled: true, enable_annotations: true }
2+
secret: test
3+
router: { resource: "%kernel.root_dir%/%kernel.test_case%/routing.yml" }
4+
validation: { enabled: true, enable_annotations: true }
55
assets: ~
6-
form:
7-
csrf_protection:
8-
enabled: true
6+
csrf_protection: true
7+
form: true
98
test: ~
109
default_locale: en
1110
session:
12-
storage_id: session.storage.mock_file
11+
storage_id: session.storage.mock_file
1312
profiler: { only_exceptions: false }
1413

1514
services:

0 commit comments

Comments
 (0)