Skip to content

[Form] Add csrf_token_lazy option #54705

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function finishView(FormView $view, FormInterface $form, array $options):
if ($options['csrf_protection'] && !$view->parent && $options['compound']) {
$factory = $form->getConfig()->getFormFactory();
$tokenId = $options['csrf_token_id'] ?: ($form->getName() ?: $form->getConfig()->getType()->getInnerType()::class);
$data = (string) $options['csrf_token_manager']->getToken($tokenId);
$data = $options['csrf_token_lazy'] ? '' : (string) $options['csrf_token_manager']->getToken($tokenId);

$csrfForm = $factory->createNamed($options['csrf_field_name'], HiddenType::class, $data, [
'block_prefix' => 'csrf_token',
Expand All @@ -87,6 +87,7 @@ public function configureOptions(OptionsResolver $resolver): void
'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.',
'csrf_token_manager' => $this->defaultTokenManager,
'csrf_token_id' => null,
'csrf_token_lazy' => false,
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,26 @@ public function testGenerateCsrfToken()
$this->assertEquals('token', $view['csrf']->vars['value']);
}

public function testGenerateLazyCsrfToken()
{
$this->tokenManager->expects($this->once())
->method('getToken')
->with('TOKEN_ID')
->willReturn(new CsrfToken('TOKEN_ID', 'token'));

$view = $this->factory
->create('Symfony\Component\Form\Extension\Core\Type\FormType', null, [
'csrf_field_name' => 'csrf',
'csrf_token_manager' => $this->tokenManager,
'csrf_token_id' => 'TOKEN_ID',
'csrf_token_lazy' => true,
'compound' => true,
])
->createView();

$this->assertEquals('token', '');
}

public function testGenerateCsrfTokenUsesFormNameAsIntentionByDefault()
{
$this->tokenManager->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"csrf_message",
"csrf_protection",
"csrf_token_id",
"csrf_token_lazy",
"csrf_token_manager"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
choice_loader data_class allow_file_upload csrf_message
choice_name empty_data attr csrf_protection
choice_translation_domain error_bubbling attr_translation_parameters csrf_token_id
choice_translation_parameters invalid_message auto_initialize csrf_token_manager
choice_value trim block_name
choice_translation_parameters invalid_message auto_initialize csrf_token_lazy
choice_value trim block_name csrf_token_manager
choices block_prefix
duplicate_preferred_choices by_reference
expanded data
Expand Down
Loading