diff --git a/Controller/FormTokenController.php b/Controller/FormTokenController.php index 14a6fe60..630279b8 100644 --- a/Controller/FormTokenController.php +++ b/Controller/FormTokenController.php @@ -35,6 +35,9 @@ public function tokenAction(Request $request): Response $content = $csrfToken; if ($request->get('html')) { + $formName = htmlspecialchars($formName, ENT_QUOTES, 'UTF-8'); + $csrfToken = htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8'); + $content = \sprintf( '', $formName, diff --git a/Tests/Functional/Controller/FormTokenControllerTest.php b/Tests/Functional/Controller/FormTokenControllerTest.php new file mode 100644 index 00000000..50c004b2 --- /dev/null +++ b/Tests/Functional/Controller/FormTokenControllerTest.php @@ -0,0 +1,55 @@ +createMock(CsrfTokenManagerInterface::class); + $csrfToken = $this->createMock(CsrfToken::class); + $csrfToken->method('getValue')->willReturn('testToken'); + $csrfTokenManager->method('getToken')->willReturn($csrfToken); + $this->formTokenController = new FormTokenController($csrfTokenManager); + } + + public function testTokenAction(): void + { + $request = new Request([], [], ['form' => 'testForm', 'html' => true]); + $response = $this->formTokenController->tokenAction($request); + $this->assertSame(200, $response->getStatusCode()); + $this->assertStringContainsString('testForm', $response->getContent()); + } + + public function testTokenActionWithScript(): void + { + $request = new Request([], [], ['form' => '', 'html' => true]); + $response = $this->formTokenController->tokenAction($request); + $this->assertSame(200, $response->getStatusCode()); + $this->assertStringContainsString('<script>alert(1)</script>', $response->getContent()); + $this->assertStringNotContainsString('', $response->getContent()); + } +}