Skip to content

Commit fa1e994

Browse files
committed
Merge branch '2.4'
* 2.4: updated Composer suggested packages updated VERSION for 2.2.11 update CONTRIBUTORS for 2.2.11 updated CHANGELOG for 2.2.11 Fixed typo in phpdoc Default form.csrf_protection.enabled to csrf_protection.enabled Handled the scenario when no entity manager is passed with closure query builder. Enabled csrf_protection by default if form.csrf_protection is enabled [HttpKernel] made a small optimization to Bundle initialization minor optimalization at bundle initialization [EventDispatcher] tweaked README removed observer pattern, in favour of mediator [DoctrineBridge] normalized class names in the ORM type guesser Fix `extract` method to avoid recalculating count() for each iteration. [Debug] ensured that a fatal PHP error is actually fatal after being handled by our error handler use the correct class name to retrieve mapped class' metadata and repository [WebProfilerBundle] Fixed js escaping in time.html.twig
2 parents 7ea658c + e858abf commit fa1e994

File tree

7 files changed

+35
-16
lines changed

7 files changed

+35
-16
lines changed

DependencyInjection/Configuration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,12 @@ private function addFormSection(ArrayNodeDefinition $rootNode)
120120
->canBeEnabled()
121121
->children()
122122
->arrayNode('csrf_protection')
123-
->canBeDisabled()
123+
->treatFalseLike(array('enabled' => false))
124+
->treatTrueLike(array('enabled' => true))
125+
->treatNullLike(array('enabled' => true))
126+
->addDefaultsIfNotSet()
124127
->children()
128+
->booleanNode('enabled')->defaultNull()->end() // defaults to framework.csrf_protection.enabled
125129
->scalarNode('field_name')->defaultNull()->end()
126130
->end()
127131
->end()

DependencyInjection/FrameworkExtension.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,18 @@ public function load(array $configs, ContainerBuilder $container)
9393

9494
$loader->load('security.xml');
9595

96-
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
97-
9896
if ($this->isConfigEnabled($container, $config['form'])) {
9997
$this->formConfigEnabled = true;
10098
$this->registerFormConfiguration($config, $container, $loader);
10199
$config['validation']['enabled'] = true;
100+
101+
if ($this->isConfigEnabled($container, $config['form']['csrf_protection'])) {
102+
$config['csrf_protection']['enabled'] = true;
103+
}
102104
}
103105

106+
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
107+
104108
if (isset($config['templating'])) {
105109
$this->registerTemplatingConfiguration($config['templating'], $config['ide'], $container, $loader);
106110
}
@@ -158,11 +162,11 @@ public function load(array $configs, ContainerBuilder $container)
158162
private function registerFormConfiguration($config, ContainerBuilder $container, XmlFileLoader $loader)
159163
{
160164
$loader->load('form.xml');
161-
if ($this->isConfigEnabled($container, $config['form']['csrf_protection'])) {
162-
if (!$this->isConfigEnabled($container, $config['csrf_protection'])) {
163-
throw new \LogicException('CSRF protection needs to be enabled in order to use CSRF protection for forms.');
164-
}
165+
if (null === $config['form']['csrf_protection']['enabled']) {
166+
$config['form']['csrf_protection']['enabled'] = $config['csrf_protection']['enabled'];
167+
}
165168

169+
if ($this->isConfigEnabled($container, $config['form']['csrf_protection'])) {
166170
$loader->load('form_csrf.xml');
167171

168172
$container->setParameter('form.type_extension.csrf.enabled', true);

Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected static function getBundleDefaultConfig()
9696
'form' => array(
9797
'enabled' => false,
9898
'csrf_protection' => array(
99-
'enabled' => true,
99+
'enabled' => null, // defaults to csrf_protection.enabled
100100
'field_name' => null,
101101
),
102102
),

Tests/DependencyInjection/Fixtures/php/csrf.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<?php
22

33
$container->loadFromExtension('framework', array(
4+
'csrf_protection' => array(
5+
'enabled' => false,
6+
),
47
'form' => array(
58
'enabled' => true,
9+
'csrf_protection' => array(
10+
'enabled' => true,
11+
),
612
),
713
'session' => array(
814
'handler_id' => null,

Tests/DependencyInjection/Fixtures/xml/csrf.xml

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

99
<framework:config>
10-
<framework:form />
10+
<framework:csrf-protection enabled="false" />
11+
12+
<framework:form>
13+
<framework:csrf-protection />
14+
</framework:form>
15+
1116
<framework:session />
1217
</framework:config>
1318
</container>
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
framework:
2+
csrf_protection: false
23
secret: s3cr3t
3-
form: ~
4+
form:
5+
csrf_protection: true
46
session: ~
57
# CSRF is disabled by default
68
# csrf_protection: ~

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ public function testCsrfProtectionNeedsSessionToBeEnabled()
4141
$this->createContainerFromFile('csrf_needs_session');
4242
}
4343

44-
/**
45-
* @expectedException \LogicException
46-
* @expectedExceptionMessage CSRF protection needs to be enabled in order to use CSRF protection for forms.
47-
*/
48-
public function testCsrfProtectionForFormsNeedCsrfProtectionToBeEnabled()
44+
public function testCsrfProtectionForFormsEnablesCsrfProtectionAutomatically()
4945
{
50-
$this->createContainerFromFile('csrf');
46+
$container = $this->createContainerFromFile('csrf');
47+
48+
$this->assertTrue($container->hasDefinition('security.csrf.token_manager'));
5149
}
5250

5351
public function testSecureRandomIsAvailableIfCsrfIsDisabled()

0 commit comments

Comments
 (0)