Skip to content

[Security] Validating a CSRF token in the test environment doesn't work #41757

Closed
@YaFou

Description

@YaFou

Symfony version(s) affected: 5.3

Description

Generating a CSRF token in a test and check if it is valid doesn't work.

Test

$client = self::createClient();
$token = self::getContainer()->get('security.csrf.token_manager')->getToken('id')->getValue();
$client->request('POST', '/test', ['token' => $token]);
self::assertSame('valid', $client->getResponse()->getContent());

Controller

/**
 * @Route("/test")
 */
public function __invoke(Request $request): Response
{
    $tokenValid = $this->isCsrfTokenValid('id', $request->request->get('token'));

    return new Response($tokenValid ? 'valid' : 'invalid');
}

How to reproduce

Reproducer

Possible Solution

I will search one and open a PR if needed

Additional context

Is related to #41046

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions