8000 [Form] Fixed: CSRF protection did not run if token was missing · symfony/symfony@c623fcf · GitHub
[go: up one dir, main page]

Skip to content

Commit c623fcf

Browse files
committed
[Form] Fixed: CSRF protection did not run if token was missing
1 parent eb75ab1 commit c623fcf

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

src/Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ public function onBindClientData(FilterDataEvent $event)
6363
$form = $event->getForm();
6464
$data = $event->getData();
6565

66-
if ($form->isRoot() && $form->hasChildren() && isset($data[$this->fieldName])) {
67-
if (!$this->csrfProvider->isCsrfTokenValid($this->intention, $data[$this->fieldName])) {
66+
if ($form->isRoot() && $form->hasChildren()) {
67+
if (!isset($data[$this->fieldName]) || !$this->csrfProvider->isCsrfTokenValid($this->intention, $data[$this->fieldName])) {
6868
$form->addError(new FormError('The CSRF token is invalid. Please try to resubmit the form'));
6969
}
7070

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,32 @@ public function testValidateTokenOnBindIfRootAndChildren($valid)
171171
$this->assertSame($valid, $form->isValid());
172172
}
173173

174+
public function testFailIfRootAndChildrenAndTokenMissing()
175+
{
176+
$this->csrfProvider->expects($this->never())
177+
->method('isCsrfTokenValid');
178+
179+
$form = $this->factory
180+
->createBuilder('form', null, array(
181+
'csrf_field_name' => 'csrf',
182+
'csrf_provider' => $this->csrfProvider,
183+
'intention' => '%INTENTION%'
184+
))
185+
->add($this->factory->createNamedBuilder('form', 'child'))
186+
->getForm();
187+
188+
$form->bind(array(
189+
'child' => 'foobar',
190+
// token is missing
191+
));
192+
193+
// Remove token from data
194+
$this->assertSame(array('child' => 'foobar'), $form->getData());
195+
196+
// Validate accordingly
197+
$this->assertFalse($form->isValid());
198+
}
199+
174200
public function testDontValidateTokenIfChildrenButNoRoot()
175201
{
176202
$this->csrfProvider->expects($this->never())

0 commit comments

Comments
 (0)
0