Skip to content

[TwigExtra] Escape potential html in test attributes #251

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -43,7 +43,9 @@ function (array $attributes): array {
$result = [];

foreach ($attributes as $name => $value) {
$result[sprintf('data-test-%s', $name)] = (string) $value;
$escapedValue = htmlspecialchars((string) $value, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8');
Copy link
Member

Choose a reason for hiding this comment

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

My main opinion is that it'd be better to escape this in Twig itself to use escaper strategy. but maybe it'd be overkilled.

Copy link
Member Author

Choose a reason for hiding this comment

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

This won't be escaped in Twig because we're adding ['is_safe' => ['html']]. This tells Twig's escaper that the content is safe HTML, so it won't apply automatic escaping

Copy link
Member Author

@mpysiak mpysiak Apr 25, 2025

Choose a reason for hiding this comment

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

other option is to add in every twig

{{ sylius_test_html_attribute('test-button', value|escape) }}

Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if there are a lot test html that have this value. A lot of them are just empty data test attributes


$result[sprintf('data-test-%s', $name)] = $escapedValue;
}

return ['attr' => $result];
Expand All @@ -59,7 +61,9 @@ function (array $attributes): array {
public function getTestFormAttribute(string $name, ?string $value = null): array
{
if (str_starts_with($this->environment, 'test') || $this->isDebugEnabled) {
return ['attr' => ['data-test-' . $name => (string) $value]];
$escapedValue = htmlspecialchars((string) $value, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8');

return ['attr' => ['data-test-' . $name => $escapedValue]];
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public function getFunctions(): array
'sylius_test_html_attribute',
function (string $name, ?string $value = null): string {
if (str_starts_with($this->environment, 'test') || $this->isDebugEnabled) {
return sprintf('data-test-%s="%s"', $name, (string) $value);
$escapedValue = htmlspecialchars((string) $value, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8');

return sprintf('data-test-%s="%s"', $name, $escapedValue);
}

return '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,17 @@ public function testItsTestFormAttributesTwigFunctionIsSafeForHtml(): void

$this->assertEquals(['html'], $twigFunction->getSafe(new Node()));
}

public function testItsTestFormAttributeEscapesHtmlSpecialCharacters(): void
{
$twigFunction = (new TestFormAttributeExtension('test', false))->getFunctions()[0];
$callable = $twigFunction->getCallable();

$this->assertIsCallable($callable);

$input = '\'" onmouseover="alert(1)';
$expected = ['attr' => ['data-test-comment' => ''" onmouseover="alert(1)']];

$this->assertEquals($expected, ($callable)('comment', $input));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,17 @@ public function testItsTwigFunctionIsSafeForHtml(): void

$this->assertEquals(['html'], $twigFunction->getSafe(new Node()));
}

public function testItsTwigFunctionEscapesHtmlSpecialChars(): void
{
$twigFunction = (new TestHtmlAttributeExtension('test', false))->getFunctions()[0];
$callable = $twigFunction->getCallable();

$this->assertIsCallable($callable);

$input = '"><img src=x onerror=alert(1)';
$expectedEscaped = 'data-test-comment="&quot;&gt;&lt;img src=x onerror=alert(1)"';

$this->assertEquals($expectedEscaped, ($callable)('comment', $input));
}
}