Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e54e94c

Browse filesBrowse files
committed
When a CSRF occures on a Form submit add a cause on the FormError object
1 parent d1fd432 commit e54e94c
Copy full SHA for e54e94c

File tree

3 files changed

+7
-4
lines changed
Filter options

3 files changed

+7
-4
lines changed

‎src/Symfony/Component/Form/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* deprecated the `$scale` argument of the `IntegerToLocalizedStringTransformer`
88
* added `Symfony\Component\Form\ClearableErrorsInterface`
99
* deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered
10+
* added a cause when a CSRF error has occurred
1011
* deprecated the `scale` option of the `IntegerType`
1112

1213
4.1.0

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Csrf/EventListener/CsrfValidationListener.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ public function preSubmit(FormEvent $event)
5959
if ($form->isRoot() && $form->getConfig()->getOption('compound') && !$postRequestSizeExceeded) {
6060
$data = $event->getData();
6161

62-
if (!isset($data[$this->fieldName]) || !$this->tokenManager->isTokenValid(new CsrfToken($this->tokenId, $data[$this->fieldName]))) {
62+
$csrfToken = new CsrfToken($this->tokenId, $data[$this->fieldName] ?? null);
63+
if (!isset($data[$this->fieldName]) || !$this->tokenManager->isTokenValid($csrfToken)) {
6364
$errorMessage = $this->errorMessage;
6465

6566
if (null !== $this->translator) {
6667
$errorMessage = $this->translator->trans($errorMessage, array(), $this->translationDomain);
6768
}
6869

69-
$form->addError(new FormError($errorMessage));
70+
$form->addError(new FormError($errorMessage, $errorMessage, array(), null, $csrfToken));
7071
}
7172

7273
if (\is_array($data)) {

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Csrf/Type/FormTypeCsrfExtensionTest.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,9 +365,10 @@ public function testNoCsrfProtectionOnPrototype()
365365

366366
public function testsTranslateCustomErrorMessage()
367367
{
368+
$csrfToken = new CsrfToken('TOKEN_ID', 'token');
368369
$this->tokenManager->expects($this->once())
369370
->method('isTokenValid')
370-
->with(new CsrfToken('TOKEN_ID', 'token'))
371+
->with($csrfToken)
371372
->will($this->returnValue(false));
372373

373374
$this->translator->expects($this->once())
@@ -390,7 +391,7 @@ public function testsTranslateCustomErrorMessage()
390391
));
391392

392393
$errors = $form->getErrors();
393-
$expected = new FormError('[trans]Foobar[/trans]');
394+
$expected = new FormError('[trans]Foobar[/trans]', null, array(), null, $csrfToken);
394395
$expected->setOrigin($form);
395396

396397
$this->assertGreaterThan(0, \count($errors));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.