Skip to content

Commit

Permalink
Merge pull request from GHSA-jq4p-mq33-w375
Browse files Browse the repository at this point in the history
Html escape validation error messages
  • Loading branch information
weierophinney committed Jan 28, 2022
2 parents beee25d + d9131a2 commit 43005a3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
"laminas/laminas-view": "^2.14, required for using the laminas-form view helpers"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
"laminas": {
Expand Down
7 changes: 7 additions & 0 deletions src/View/Helper/FormElementErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use function array_merge;
use function array_walk_recursive;
use function count;
use function implode;
use function sprintf;

Expand Down Expand Up @@ -77,6 +78,12 @@ public function render(ElementInterface $element, array $attributes = []): strin
$attributes = ' ' . $attributes;
}

$count = count($messages);
$escaper = $this->getEscapeHtmlHelper();
for ($i = 0; $i < $count; $i += 1) {
$messages[$i] = $escaper($messages[$i]);
}

// Generate markup
$markup = sprintf($this->getMessageOpenFormat(), $attributes);
$markup .= implode($this->getMessageSeparatorString(), $messages);
Expand Down
18 changes: 18 additions & 0 deletions test/View/Helper/FormElementErrorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,22 @@ public function testCallingTheHelperToRenderInvokeCanReturnObject(): void
$helper = $this->helper;
$this->assertEquals($helper(), $helper);
}

public function testHtmlEscapingOfMessages(): void
{
$messages = [
[
'<span>First validator message</span>',
'<span>Second validator first message</span>',
'<span>Second validator second message</span>',
],
];
$element = new Element('foo');
$element->setMessages($messages);

$markup = $this->helper->render($element);

$this->assertStringNotContainsString('<span>', $markup);
$this->assertStringNotContainsString('</span>', $markup);
}
}

0 comments on commit 43005a3

Please sign in to comment.