Skip to content

Commit 5740179

Browse files
Merge branch '4.0'
* 4.0: [appveyor] set memory_limit=-1 [Console] Keep the modified exception handler [Console] Fix restoring exception handler [Router] Skip anonymous classes when loading annotated routes allow dashes in cwd pathname when running the tests Fixed Request::__toString ignoring cookies Make sure we only build once and have one time the prefix when importing routes [Security] Fix fatal error on non string username [FrameworkBundle] Automatically enable the CSRF if component *+ session* are loaded
2 parents 2d0aba3 + fa68ee9 commit 5740179

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

DependencyInjection/Configuration.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Form\Form;
2222
use Symfony\Component\Lock\Lock;
2323
use Symfony\Component\Lock\Store\SemaphoreStore;
24+
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
2425
use Symfony\Component\Serializer\Serializer;
2526
use Symfony\Component\Translation\Translator;
2627
use Symfony\Component\Validator\Validation;
@@ -109,7 +110,14 @@ private function addCsrfSection(ArrayNodeDefinition $rootNode)
109110
$rootNode
110111
->children()
111112
->arrayNode('csrf_protection')
112-
->canBeEnabled()
113+
->treatFalseLike(array('enabled' => false))
114+
->treatTrueLike(array('enabled' => true))
115+
->treatNullLike(array('enabled' => true))
116+
->addDefaultsIfNotSet()
117+
->children()
118+
// defaults to framework.session.enabled && !class_exists(FullStack::class) && interface_exists(CsrfTokenManagerInterface::class)
119+
->booleanNode('enabled')->defaultNull()->end()
120+
->end()
113121
->end()
114122
->end()
115123
;

DependencyInjection/FrameworkExtension.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1818
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1919
use Symfony\Bundle\FrameworkBundle\Routing\AnnotatedRouteControllerLoader;
20+
use Symfony\Bundle\FullStack;
2021
use Symfony\Component\Cache\Adapter\AbstractAdapter;
2122
use Symfony\Component\Cache\Adapter\AdapterInterface;
2223
use Symfony\Component\Cache\Adapter\ArrayAdapter;
@@ -65,6 +66,7 @@
6566
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
6667
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
6768
use Symfony\Component\Security\Core\Security;
69+
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
6870
use Symfony\Component\Serializer\Encoder\DecoderInterface;
6971
use Symfony\Component\Serializer\Encoder\EncoderInterface;
7072
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
@@ -193,6 +195,11 @@ public function load(array $configs, ContainerBuilder $container)
193195
$this->registerRequestConfiguration($config['request'], $container, $loader);
194196
}
195197

198+
if (null === $config['csrf_protection']['enabled']) {
199+
$config['csrf_protection']['enabled'] = $this->sessionConfigEnabled && !class_exists(FullStack::class) && interface_exists(CsrfTokenManagerInterface::class);
200+
}
201+
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
202+
196203
if ($this->isConfigEnabled($container, $config['form'])) {
197204
if (!class_exists('Symfony\Component\Form\Form')) {
198205
throw new LogicException('Form support cannot be enabled as the Form component is not installed.');
@@ -213,8 +220,6 @@ public function load(array $configs, ContainerBuilder $container)
213220
$container->removeDefinition('console.command.form_debug');
214221
}
215222

216-
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);
217-
218223
if ($this->isConfigEnabled($container, $config['assets'])) {
219224
if (!class_exists('Symfony\Component\Asset\Package')) {
220225
throw new LogicException('Asset support cannot be enabled as the Asset component is not installed.');

0 commit comments

Comments
 (0)