Skip to content

[FrameworkBundle] Don't auto-register form/csrf when the corresponding components are not installed #58937

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
Nov 20, 2024
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 @@ -211,7 +211,7 @@
->addDefaultsIfNotSet()
->fixXmlConfig('stateless_token_id')
->children()
// defaults to framework.csrf_protection.stateless_token_ids || framework.session.enabled && !class_exists(FullStack::class) && interface_exists(CsrfTokenManagerInterface::class)
// defaults to (framework.csrf_protection.stateless_token_ids || framework.session.enabled) && !class_exists(FullStack::class) && interface_exists(CsrfTokenManagerInterface::class)
->scalarNode('enabled')->defaultNull()->end()
->arrayNode('stateless_token_ids')
->scalarPrototype()->end()
Expand All @@ -237,9 +237,13 @@
->children()
->arrayNode('form')
->info('Form configuration')
->{$enableIfStandalone('symfony/form', Form::class)}()
->treatFalseLike(['enabled' => false])
->treatTrueLike(['enabled' => true])
->treatNullLike(['enabled' => true])
->addDefaultsIfNotSet()
->children()
->scalarNode('enabled')->defaultNull()->end() // defaults to !class_exists(FullStack::class) && class_exists(Form::class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why changing this one ? enabling form based on the presence of the form component was right here. This is what $enableIfStandalone is doing. and the canBeEnabled and canBeDisabled methods of symfony/config have an additional behavior: setting any other setting implicitly enables it (losing that behavior is the reason why you had to update tests)

Copy link
Member Author

@nicolas-grekas nicolas-grekas Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of https://github.com/symfony/recipes/pull/1337/files, which requires being able to set some defaults without enabling forms while symfony/form isn't installed
alternatives are way too complex (recipes relying on many components being installed is not possible)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why setting this in the FrameworkBundle recipe instead of the recipe of components, as done for other component-specific config ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, this is a BC break for people using the fullstack framework package (which is discouraged, but might still be done) as it means the form component would not be enabled anymore if they only have other settings set.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in favor of a specific csrf recipe as well. We do this for other optional framework config options as well (e.g. messenger, mailer)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check symfony/recipes#1361

->arrayNode('csrf_protection')

Check failure on line 246 in src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php:246:27: UndefinedInterfaceMethod: Method Symfony\Component\Config\Definition\Builder\NodeParentInterface::arrayNode does not exist (see https://psalm.dev/181)

Check failure on line 246 in src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php:246:27: UndefinedInterfaceMethod: Method Symfony\Component\Config\Definition\Builder\NodeParentInterface::arrayNode does not exist (see https://psalm.dev/181)
->treatFalseLike(['enabled' => false])
->treatTrueLike(['enabled' => true])
->treatNullLike(['enabled' => true])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,19 @@ public function load(array $configs, ContainerBuilder $container): void
$this->readConfigEnabled('profiler', $container, $config['profiler']);
$this->readConfigEnabled('workflows', $container, $config['workflows']);

// csrf depends on session or stateless token ids being registered
if (null === $config['csrf_protection']['enabled']) {
$this->writeConfigEnabled('csrf_protection', ($config['csrf_protection']['stateless_token_ids'] || $this->readConfigEnabled('session', $container, $config['session'])) && !class_exists(FullStack::class) && ContainerBuilder::willBeAvailable('symfony/security-csrf', CsrfTokenManagerInterface::class, ['symfony/framework-bundle']), $config['csrf_protection']);
}

if (null === $config['form']['enabled']) {
$this->writeConfigEnabled('form', !class_exists(FullStack::class) && ContainerBuilder::willBeAvailable('symfony/form', Form::class, ['symfony/framework-bundle']), $config['form']);
}

if (null === $config['form']['csrf_protection']['enabled']) {
$this->writeConfigEnabled('form.csrf_protection', $config['csrf_protection']['enabled'], $config['form']['csrf_protection']);
}

// A translator must always be registered (as support is included by
// default in the Form and Validator component). If disabled, an identity
// translator will be used and everything will still work as expected.
Expand Down Expand Up @@ -466,10 +479,6 @@ public function load(array $configs, ContainerBuilder $container): void
$container->removeDefinition('test.session.listener');
}

// csrf depends on session being registered
if (null === $config['csrf_protection']['enabled']) {
$this->writeConfigEnabled('csrf_protection', $config['csrf_protection']['stateless_token_ids'] || $this->readConfigEnabled('session', $container, $config['session']) && !class_exists(FullStack::class) && ContainerBuilder::willBeAvailable('symfony/security-csrf', CsrfTokenManagerInterface::class, ['symfony/framework-bundle']), $config['csrf_protection']);
}
$this->registerSecurityCsrfConfiguration($config['csrf_protection'], $container, $loader);

// form depends on csrf being registered
Expand Down Expand Up @@ -754,10 +763,6 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont
{
$loader->load('form.php');

if (null === $config['form']['csrf_protection']['enabled']) {
$this->writeConfigEnabled('form.csrf_protection', $config['csrf_protection']['enabled'], $config['form']['csrf_protection']);
}

if ($this->readConfigEnabled('form.csrf_protection', $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.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'annotations' => false,
'csrf_protection' => false,
'form' => [
'enabled' => true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we change tests to make this PR pass, this won't affect end user's project: the auto-enable will still rely on the required packages being installed. Here, we have to be explicit because FullStack::class is found.

'csrf_protection' => true,
],
'http_method_override' => false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'handle_all_throwables' => true,
'php_errors' => ['log' => true],
'form' => [
'enabled' => true,
'csrf_protection' => [
'enabled' => false,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
'enabled_locales' => ['fr', 'en'],
'csrf_protection' => true,
'form' => [
'enabled' => true,
'csrf_protection' => [
'field_name' => '_csrf',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<framework:enabled-locale>fr</framework:enabled-locale>
<framework:enabled-locale>en</framework:enabled-locale>
<framework:csrf-protection />
<framework:form>
<framework:form enabled="true">
<framework:csrf-protection field-name="_csrf"/>
</framework:form>
<framework:esi enabled="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ framework:
annotations: false
csrf_protection: false
form:
enabled: true
csrf_protection: true
http_method_override: false
handle_all_throwables: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ framework:
php_errors:
log: true
form:
enabled: true
csrf_protection:
enabled: false
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ framework:
enabled_locales: ['fr', 'en']
csrf_protection: true
form:
enabled: true
csrf_protection:
field_name: _csrf
http_method_override: false
Expand Down
Loading