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 598620d

Browse filesBrowse files
committed
feature #28564 [Form] Added a cause on when a Csrf Error has occurred on CsrfValidationListener (gmponos)
This PR was squashed before being merged into the 4.2-dev branch (closes #28564). Discussion ---------- [Form] Added a cause on when a Csrf Error has occurred on CsrfValidationListener | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #28427 | License | MIT | Doc PR | symfony/symfony-docs The Csrf error that it is added as a `FormError` does not have a cause making it hard to filter it out. 1. I am not sure if this is a bug or a feature. 2. @xabbuh on the issue you said something about `CsrfValidationError`. I didn't quite get that. In the current PR you can see what I was thinking about. Let me know your thoughts and continue the discussion here. Commits ------- 162d0be [Form] Added a cause on when a Csrf Error has occurred on CsrfValidationListener
2 parents d1fd432 + 162d0be commit 598620d
Copy full SHA for 598620d

File tree

Expand file treeCollapse file tree

3 files changed

+7
-4
lines changed
Filter options
Expand file treeCollapse file tree

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.