Skip to content

When a CSRF occures on a Form submit add a cause on the FormError object #28656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* deprecated the `$scale` argument of the `IntegerToLocalizedStringTransformer`
* added `Symfony\Component\Form\ClearableErrorsInterface`
* deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered
* added a cause when a CSRF error has occurred
* deprecated the `scale` option of the `IntegerType`

4.1.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,15 @@ public function preSubmit(FormEvent $event)
if ($form->isRoot() && $form->getConfig()->getOption('compound') && !$postRequestSizeExceeded) {
$data = $event->getData();

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

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

$form->addError(new FormError($errorMessage));
$form->addError(new FormError($errorMessage, $errorMessage, array(), null, $csrfToken));
}

if (\is_array($data)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,10 @@ public function testNoCsrfProtectionOnPrototype()

public function testsTranslateCustomErrorMessage()
{
$csrfToken = new CsrfToken('TOKEN_ID', 'token');
$this->tokenManager->expects($this->once())
->method('isTokenValid')
->with(new CsrfToken('TOKEN_ID', 'token'))
->with($csrfToken)
->will($this->returnValue(false));

$this->translator->expects($this->once())
Expand All @@ -390,7 +391,7 @@ public function testsTranslateCustomErrorMessage()
));

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

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