Skip to content

Commit

Permalink
Merge pull request from GHSA-rrvc-c7xg-7cf3
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler authored Jun 6, 2024
1 parent 0eb960a commit 3f341b7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Controller/FormTokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function tokenAction(Request $request): Response
$content = $csrfToken;

if ($request->get('html')) {
$formName = htmlspecialchars($formName, ENT_QUOTES, 'UTF-8');

Check failure on line 38 in Controller/FormTokenController.php

View workflow job for this annotation

GitHub Actions / PHP Lint

Parameter #1 $string of function htmlspecialchars expects string, mixed given.
$csrfToken = htmlspecialchars($csrfToken, ENT_QUOTES, 'UTF-8');

$content = \sprintf(
'<input type="hidden" id="%s__token" name="%s[_token]" value="%s" />',
$formName,
Expand Down
55 changes: 55 additions & 0 deletions Tests/Functional/Controller/FormTokenControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\FormBundle\Tests\Functional\Controller;

use Sulu\Bundle\FormBundle\Controller\FormTokenController;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Csrf\CsrfToken;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;

class FormTokenControllerTest extends SuluTestCase
{
/**
* @var FormTokenController
*/
private formTokenController $formTokenController;

protected function setUp(): void
{
parent::setUp();
$csrfTokenManager = $this->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' => '<script>alert(1)</script>', 'html' => true]);
$response = $this->formTokenController->tokenAction($request);
$this->assertSame(200, $response->getStatusCode());
$this->assertStringContainsString('&lt;script&gt;alert(1)&lt;/script&gt;', $response->getContent());
$this->assertStringNotContainsString('<script>alert(1)</script>', $response->getContent());
}
}

0 comments on commit 3f341b7

Please sign in to comment.