Skip to content

Commit 04c3f25

Browse files
committed
bug symfony#9327 [2.2][Form] Changed FormTypeCsrfExtension to use the form's name as default intention (bschussek)
This PR was merged into the 2.2 branch. Discussion ---------- [2.2][Form] Changed FormTypeCsrfExtension to use the form's name as default intention | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Before, every form used the same "intention"/"csrf_token_id" value by default, namely "unknown". This PR fixes the default value to the form's name, which is at least different for forms with (a) explicit names and (b) different types, where the implicit name equals the type's name. Commits ------- b07c618 [Form] Changed FormTypeCsrfExtension to use the form's name as default intention
2 parents 0080399 + b07c618 commit 04c3f25

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

src/Symfony/Component/Form/Extension/Csrf/Type/FormTypeCsrfExtension.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4949

5050
$builder
5151
->setAttribute('csrf_factory', $builder->getFormFactory())
52-
->addEventSubscriber(new CsrfValidationListener($options['csrf_field_name'], $options['csrf_provider'], $options['intention']))
52+
->addEventSubscriber(new CsrfValidationListener(
53+
$options['csrf_field_name'],
54+
$options['csrf_provider'],
55+
$options['intention'] ?: $builder->getName()
56+
))
5357
;
5458
}
5559

@@ -64,7 +68,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
6468
{
6569
if ($options['csrf_protection'] && !$view->parent && $options['compound']) {
6670
$factory = $form->getConfig()->getAttribute('csrf_factory');
67-
$data = $options['csrf_provider']->generateCsrfToken($options['intention']);
71+
$data = $options['csrf_provider']->generateCsrfToken($options['intention'] ?: $form->getName());
6872

6973
$csrfForm = $factory->createNamed($options['csrf_field_name'], 'hidden', $data, array(
7074
'mapped' => false,
@@ -83,7 +87,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
8387
'csrf_protection' => $this->defaultEnabled,
8488
'csrf_field_name' => $this->defaultFieldName,
8589
'csrf_provider' => $this->defaultCsrfProvider,
86-
'intention' => 'unknown',
90+
'intention' => null,
8791
));
8892
}
8993

src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,24 @@ public function testGenerateCsrfToken()
129129
$this->assertEquals('token', $view['csrf']->vars['value']);
130130
}
131131

132+
public function testGenerateCsrfTokenUsesFormNameAsIntentionByDefault()
133+
{
134+
$this->csrfProvider->expects($this->once())
135+
->method('generateCsrfToken')
136+
->with('FORM_NAME')
137+
->will($this->returnValue('token'));
138+
139+
$view = $this->factory
140+
->createNamed('FORM_NAME', 'form', null, array(
141+
'csrf_field_name' => 'csrf',
142+
'csrf_provider' => $this->csrfProvider,
143+
'compound' => true,
144+
))
145+
->createView();
146+
147+
$this->assertEquals('token', $view['csrf']->vars['value']);
148+
}
149+
132150
public function provideBoolean()
133151
{
134152
return array(
@@ -169,6 +187,37 @@ public function testValidateTokenOnBindIfRootAndCompound($valid)
169187
$this->assertSame($valid, $form->isValid());
170188
}
171189

190+
/**
191+
* @dataProvider provideBoolean
192+
*/
193+
public function testValidateTokenOnBindIfRootAndCompoundUsesFormNameAsIntentionByDefault($valid)
194+
{
195+
$this->csrfProvider->expects($this->once())
196+
->method('isCsrfTokenValid')
197+
->with('FORM_NAME', 'token')
198+
->will($this->returnValue($valid));
199+
200+
$form = $this->factory
201+
->createNamedBuilder('FORM_NAME', 'form', null, array(
202+
'csrf_field_name' => 'csrf',
203+
'csrf_provider' => $this->csrfProvider,
204+
'compound' => true,
205+
))
206+
->add('child', 'text')
207+
->getForm();
208+
209+
$form->bind(array(
210+
'child' => 'foobar',
211+
'csrf' => 'token',
212+
));
213+
214+
// Remove token from data
215+
$this->assertSame(array('child' => 'foobar'), $form->getData());
216+
217+
// Validate accordingly
218+
$this->assertSame($valid, $form->isValid());
219+
}
220+
172221
public function testFailIfRootAndCompoundAndTokenMissing()
173222
{
174223
$this->csrfProvider->expects($this->never())

0 commit comments

Comments
 (0)