Skip to content

[FrameworkBundle] Fail gracefully when forms use disabled CSRF #46960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy