Skip to content

[Form] Changing CSRF token default name #60537

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
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CHANGELOG
=========

7.3
---

* feature #60537 [Form] Renaming the default CSRF token form field from `_token` to `_csrf_token` (ThomasLandauer)
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private function addFormSection(ArrayNodeDefinition $rootNode, callable $enableI
->children()
->scalarNode('enabled')->defaultNull()->end() // defaults to framework.csrf_protection.enabled
->scalarNode('token_id')->defaultNull()->end()
->scalarNode('field_name')->defaultValue('_token')->end()
->scalarNode('field_name')->defaultValue('_csrf_token')->end()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am still 👎 on this change as I don't think it's worth it potentially breaking applications just to change the name of this field.

If we really want to change the default name we should IMO follow the usual deprecation-first approach: Meaning here that not setting the value explicitly would trigger a deprecation which would then allow us to change the default in 8.0.

->arrayNode('field_attr')
->performNoDeepMerging()
->normalizeKeys(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ protected static function getBundleDefaultConfig()
'enabled' => !class_exists(FullStack::class),
'csrf_protection' => [
'enabled' => null, // defaults to csrf_protection.enabled
'field_name' => '_token',
'field_name' => '_csrf_token',
'field_attr' => ['data-controller' => 'csrf-protection'],
'token_id' => null,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
protected function loadTypeExtensions(): array
{
return [
new Type\FormTypeCsrfExtension($this->tokenManager, true, '_token', $this->translator, $this->translationDomain),
new Type\FormTypeCsrfExtension($this->tokenManager, true, '_csrf_token', $this->translator, $this->translationDomain),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FormTypeCsrfExtension extends AbstractTypeExtension
public function __construct(
private CsrfTokenManagerInterface $defaultTokenManager,
private bool $defaultEnabled = true,
private string $defaultFieldName = '_token',
private string $defaultFieldName = '_csrf_token',
private ?TranslatorInterface $translator = null,
private ?string $translationDomain = null,
private ?ServerParams $serverParams = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function testAddTaggedTypesToCsrfTypeExtension()

$container->register('form.registry', FormRegistry::class);
$container->register('form.type_extension.csrf', FormTypeCsrfExtension::class)
->setArguments([null, true, '_token', null, 'validator.translation_domain', null, [], null])
->setArguments([null, true, '_csrf_token', null, 'validator.translation_domain', null, [], null])
->setPublic(true);

$container->setDefinition('form.extension', $this->createExtensionDefinition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
/**
* Sets the key of the request that contains the actual token value that should be validated.
*/
public ?string $tokenKey = '_token',
public ?string $tokenKey = '_csrf_token',

/**
* Sets the available http methods that can be used to validate the token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class IsCsrfTokenValidAttributeListenerTest extends TestCase
{
public function testIsCsrfTokenValidCalledCorrectlyOnInvokableClass()
{
$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
$csrfTokenManager->expects($this->once())
Expand Down Expand Up @@ -68,7 +68,7 @@ public function testNothingHappensWithNoConfig()

public function testIsCsrfTokenValidCalledCorrectly()
{
$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
$csrfTokenManager->expects($this->once())
Expand All @@ -90,7 +90,7 @@ public function testIsCsrfTokenValidCalledCorrectly()

public function testIsCsrfTokenValidCalledCorrectlyInPayload()
{
$request = new Request(server: ['headers' => ['content-type' => 'application/json']], content: json_encode(['_token' => 'bar']));
$request = new Request(server: ['headers' => ['content-type' => 'application/json']], content: json_encode(['_csrf_token' => 'bar']));

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
$csrfTokenManager->expects($this->once())
Expand All @@ -112,7 +112,7 @@ public function testIsCsrfTokenValidCalledCorrectlyInPayload()

public function testIsCsrfTokenValidCalledCorrectlyWithCustomExpressionId()
{
$request = new Request(query: ['id' => '123'], request: ['_token' => 'bar']);
$request = new Request(query: ['id' => '123'], request: ['_csrf_token' => 'bar']);

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
$csrfTokenManager->expects($this->once())
Expand Down Expand Up @@ -165,7 +165,7 @@ public function testIsCsrfTokenValidCalledCorrectlyWithCustomTokenKey()

public function testIsCsrfTokenValidCalledCorrectlyWithInvalidTokenKey()
{
$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
$csrfTokenManager->expects($this->once())
Expand Down Expand Up @@ -209,7 +209,7 @@ public function testExceptionWhenInvalidToken()

public function testIsCsrfTokenValidCalledCorrectlyWithDeleteMethod()
{
$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);
$request->setMethod('DELETE');

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
Expand All @@ -232,7 +232,7 @@ public function testIsCsrfTokenValidCalledCorrectlyWithDeleteMethod()

public function testIsCsrfTokenValidIgnoredWithNonMatchingMethod()
{
$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);
$request->setMethod('POST');

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
Expand All @@ -254,7 +254,7 @@ public function testIsCsrfTokenValidIgnoredWithNonMatchingMethod()

public function testIsCsrfTokenValidCalledCorrectlyWithGetOrPostMethodWithGetMethod()
{
$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);
$request->setMethod('GET');

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
Expand All @@ -277,7 +277,7 @@ public function testIsCsrfTokenValidCalledCorrectlyWithGetOrPostMethodWithGetMet

public function testIsCsrfTokenValidNoIgnoredWithGetOrPostMethodWithPutMethod()
{
$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);
$request->setMethod('PUT');

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
Expand All @@ -301,7 +301,7 @@ public function testIsCsrfTokenValidCalledCorrectlyWithInvalidTokenKeyAndPostMet
{
$this->expectException(InvalidCsrfTokenException::class);

$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);
$request->setMethod('POST');

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
Expand All @@ -324,7 +324,7 @@ public function testIsCsrfTokenValidCalledCorrectlyWithInvalidTokenKeyAndPostMet

public function testIsCsrfTokenValidIgnoredWithInvalidTokenKeyAndUnavailableMethod()
{
$request = new Request(request: ['_token' => 'bar']);
$request = new Request(request: ['_csrf_token' => 'bar']);
$request->setMethod('PUT');

$csrfTokenManager = $this->createMock(CsrfTokenManagerInterface::class);
Expand Down
Loading