Skip to content

Commit 9a8ec64

Browse files
committed
Merge branch 'fix-csrf-default-2.3' into fix-csrf-default-2.4
Conflicts: src/Symfony/Component/Form/Extension/Csrf/Type/FormTypeCsrfExtension.php
2 parents bae3024 + baa2356 commit 9a8ec64

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8686
->addEventSubscriber(new CsrfValidationListener(
8787
$options['csrf_field_name'],
8888
$options['csrf_token_manager'],
89-
$options['csrf_token_id'] ?: $builder->getName(),
89+
$options['csrf_token_id'] ?: ($builder->getName() ?: get_class($builder->getType()->getInnerType())),
9090
$options['csrf_message'],
9191
$this->translator,
9292
$this->translationDomain
@@ -105,7 +105,8 @@ public function finishView(FormView $view, FormInterface $form, array $options)
105105
{
106106
if ($options['csrf_protection'] && !$view->parent && $options['compound']) {
107107
$factory = $form->getConfig()->getFormFactory();
108-
$data = (string) $options['csrf_token_manager']->getToken($options['csrf_token_id'] ?: $form->getName());
108+
$tokenId = $options['csrf_token_id'] ?: ($form->getName() ?: get_class($form->getConfig()->getType()->getInnerType()));
109+
$data = (string) $options['csrf_token_manager']->getToken($tokenId);
109110

110111
$csrfForm = $factory->createNamed($options['csrf_field_name'], 'hidden', $data, array(
111112
'mapped' => false,

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ public function testGenerateCsrfTokenUsesFormNameAsIntentionByDefault()
159159
$this->assertEquals('token', $view['csrf']->vars['value']);
160160
}
161161

162+
public function testGenerateCsrfTokenUsesTypeClassAsIntentionIfEmptyFormName()
163+
{
164+
$this->tokenManager->expects($this->once())
165+
->method('getToken')
166+
->with('Symfony\Component\Form\Extension\Core\Type\FormType')
167+
->will($this->returnValue('token'));
168+
169+
$view = $this->factory
170+
->createNamed('', 'form', null, array(
171+
'csrf_field_name' => 'csrf',
172+
'csrf_token_manager' => $this->tokenManager,
173+
'compound' => true,
174+
))
175+
->createView();
176+
177+
$this->assertEquals('token', $view['csrf']->vars['value']);
178+
}
179+
162180
public function provideBoolean()
163181
{
164182
return array(
@@ -202,7 +220,7 @@ public function testValidateTokenOnSubmitIfRootAndCompound($valid)
202220
/**
203221
* @dataProvider provideBoolean
204222
*/
205-
public function testValidateTokenOnBindIfRootAndCompoundUsesFormNameAsIntentionByDefault($valid)
223+
public function testValidateTokenOnSubmitIfRootAndCompoundUsesFormNameAsIntentionByDefault($valid)
206224
{
207225
$this->tokenManager->expects($this->once())
208226
->method('isTokenValid')
@@ -230,6 +248,37 @@ public function testValidateTokenOnBindIfRootAndCompoundUsesFormNameAsIntentionB
230248
$this->assertSame($valid, $form->isValid());
231249
}
232250

251+
/**
252+
* @dataProvider provideBoolean
253+
*/
254+
public function testValidateTokenOnSubmitIfRootAndCompoundUsesTypeClassAsIntentionIfEmptyFormName($valid)
255+
{
256+
$this->tokenManager->expects($this->once())
257+
->method('isTokenValid')
258+
->with(new CsrfToken('Symfony\Component\Form\Extension\Core\Type\FormType', 'token'))
259+
->will($this->returnValue($valid));
260+
261+
$form = $this->factory
262+
->createNamedBuilder('', 'form', null, array(
263+
'csrf_field_name' => 'csrf',
264+
'csrf_token_manager' => $this->tokenManager,
265+
'compound' => true,
266+
))
267+
->add('child', 'text')
268+
->getForm();
269+
270+
$form->submit(array(
271+
'child' => 'foobar',
272+
'csrf' => 'token',
273+
));
274+
275+
// Remove token from data
276+
$this->assertSame(array('child' => 'foobar'), $form->getData());
277+
278+
// Validate accordingly
279+
$this->assertSame($valid, $form->isValid());
280+
}
281+
233282
public function testFailIfRootAndCompoundAndTokenMissing()
234283
{
235284
$this->tokenManager->expects($this->never())

0 commit comments

Comments
 (0)