Skip to content

Commit

Permalink
[FrameworkBundle] Fail gracefully when forms use disabled CSRF
Browse files Browse the repository at this point in the history
  • Loading branch information
HeahDude committed Jul 19, 2022
1 parent 92c7e6e commit 5990182
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
}

if ($this->isConfigEnabled($container, $config['form']['csrf_protection'])) {
if (!$container->hasDefinition('security.csrf.token_generator')) {
throw new \LogicException('To use form CSRF protection `framework.csrf_protection` must be enabled.');
}

$loader->load('form_csrf.xml');

$container->setParameter('form.type_extension.csrf.enabled', true);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$container->loadFromExtension('framework', [
'csrf_protection' => false,
'form' => [
'csrf_protection' => true,
],
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
>
<framework:config>
<framework:csrf-protection enabled="false"/>
<framework:form enabled="true">
<framework:csrf-protection enabled="true"/>
</framework:form>
</framework:config>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
framework:
csrf_protection: false
form:
csrf_protection: true
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public function testFormCsrfProtection()
$this->assertEquals('%form.type_extension.csrf.field_name%', $def->getArgument(2));
}

public function testFormCsrfProtectionWithCsrfDisabled()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('To use form CSRF protection `framework.csrf_protection` must be enabled.');

$this->createContainerFromFile('form_csrf_disabled');
}

public function testPropertyAccessWithDefaultValue()
{
$container = $this->createContainerFromFile('full');
Expand Down

0 comments on commit 5990182

Please sign in to comment.