File tree Expand file tree Collapse file tree 2 files changed +28
-2
lines changed
src/Symfony/Component/Form
Extension/Csrf/EventListener
Tests/Extension/Csrf/Type Expand file tree Collapse file tree 2 files changed +28
-2
lines changed Original file line number Diff line number Diff line change @@ -63,8 +63,8 @@ public function onBindClientData(FilterDataEvent $event)
63
63
$ form = $ event ->getForm ();
64
64
$ data = $ event ->getData ();
65
65
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 ])) {
68
68
$ form ->addError (new FormError ('The CSRF token is invalid. Please try to resubmit the form ' ));
69
69
}
70
70
Original file line number Diff line number Diff line change @@ -171,6 +171,32 @@ public function testValidateTokenOnBindIfRootAndChildren($valid)
171
171
$ this ->assertSame ($ valid , $ form ->isValid ());
172
172
}
173
173
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
+
174
200
public function testDontValidateTokenIfChildrenButNoRoot ()
175
201
{
176
202
$ this ->csrfProvider ->expects ($ this ->never ())
You can’t perform that action at this time.
0 commit comments