Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15731a9

Browse files
committed
merged branch jfcixmedia/2.1 (PR #5838)
This PR was squashed before being merged into the master branch (closes #5838). Commits ------- 201f3e6 [Form] Fixed cannot unset string offsets in CsrfValidationListener Discussion ---------- [Form] Fixed cannot unset string offsets in CsrfValidationListener Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - License of the code: MIT Documentation PR: - A php fatal error is happening when someone rewrite the entire form data for an object with a single input. ``` Fatal error: Cannot unset string offsets in vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php on line 72 ``` Example: ```html <form action="/app_dev.php/post/create" method="post" > <div id="posttype"> <div> <label for="posttype_name" class="required">Name</label> <input type="text" id="posttype_name" name="posttype[name]" required="required" maxlength="255" /> </div> <div> <label for="posttype_text" class="required">Text</label> <textarea id="posttype_text" name="posttype[text]" required="required"></textarea> </div> <input type="hidden" id="posttype__token" name="posttype[_token]" value="83a1617c694fbdea43c2527f1a55c7419ce82a42" /></div> <p> <button type="submit">Create</button> </p> </form> ``` If someone alters the html to add a simple input at the bottom of the form like this one: ```html <input type="text" id="posttype" name="posttype" value="test123" /> ``` The result will be a php fatal error. --------------------------------------------------------------------------- by bschussek at 2012-10-26T09:49:05Z Thank you for the pull request! Could you please reference the pull request in the test? ```php // #5838 public function testStringFormData() { ... ``` --------------------------------------------------------------------------- by jfcixmedia at 2012-10-26T10:21:29Z @bschussek Added, thanks.
2 parents bde2e26 + 201f3e6 commit 15731a9

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ public function preBind(FormEvent $event)
6868
$form->addError(new FormError('The CSRF token is invalid. Please try to resubmit the form.'));
6969
}
7070

71-
unset($data[$this->fieldName]);
71+
if (is_array($data)) {
72+
unset($data[$this->fieldName]);
73+
}
7274
}
7375

7476
$event->setData($data);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\Tests\Extension\Csrf\EventListener;
13+
14+
use Symfony\Component\Form\FormEvent;
15+
use Symfony\Component\Form\FormBuilder;
16+
use Symfony\Component\Form\Extension\Csrf\EventListener\CsrfValidationListener;
17+
18+
class CsrfValidationListenerTest extends \PHPUnit_Framework_TestCase
19+
{
20+
protected $dispatcher;
21+
protected $factory;
22+
protected $csrfProvider;
23+
24+
protected function setUp()
25+
{
26+
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
27+
$this->markTestSkipped('The "EventDispatcher" component is not available');
28+
}
29+
30+
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
31+
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
32+
$this->csrfProvider = $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
33+
$this->form = $this->getBuilder('post')
34+
->setDataMapper($this->getDataMapper())
35+
->getForm();
36+
}
37+
38+
protected function tearDown()
39+
{
40+
$this->dispatcher = null;
41+
$this->factory = null;
42+
$this->csrfProvider = null;
43+
$this->form = null;
44+
}
45+
46+
protected function getBuilder($name = 'name')
47+
{
48+
return new FormBuilder($name, null, $this->dispatcher, $this->factory, array('compound' => true));
49+
}
50+
51+
protected function getForm($name = 'name')
52+
{
53+
return $this->getBuilder($name)->getForm();
54+
}
55+
56+
protected function getDataMapper()
57+
{
58+
return $this->getMock('Symfony\Component\Form\DataMapperInterface');
59+
}
60+
61+
protected function getMockForm()
62+
{
63+
return $this->getMock('Symfony\Component\Form\Tests\FormInterface');
64+
}
65+
66+
// https://github.com/symfony/symfony/pull/5838
67+
public function testStringFormData()
68+
{
69+
$data = "XP4HUzmHPi";
70+
$event = new FormEvent($this->form, $data);
71+
72+
$validation = new CsrfValidationListener('csrf', $this->csrfProvider, 'unknown');
73+
$validation->preBind($event);
74+
75+
// Validate accordingly
76+
$this->assertSame($data, $event->getData());
77+
}
78+
}

0 commit comments

Comments
 (0)