Skip to content

Commit

Permalink
[HtmlSanitizer] Fix node renderer handling of self-closing (void) ele…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
omniError authored and nicolas-grekas committed May 6, 2022
1 parent 867cfda commit ad37531
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
14 changes: 12 additions & 2 deletions Tests/HtmlSanitizerAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,21 @@ public function provideSanitizeBody()
],
[
'<BODY BACKGROUND="javascript:alert(\'XSS\')">',
'<body />',
'<body></body>',
],
[
'<BGSOUND SRC="javascript:alert(\'XSS\');">',
'<bgsound />',
'<bgsound></bgsound>',
],
[
'<BR SIZE="&{alert(\'XSS\')}">',
'<br size="&amp;{alert(&#039;XSS&#039;)}" />',
],
[
'<BR></br>',
'<br /><br />',
],

[
'<OBJECT TYPE="text/x-scriptlet" DATA="http://xss.rocks/scriptlet.html"></OBJECT>',
'',
Expand Down Expand Up @@ -445,6 +450,11 @@ public function provideSanitizeBody()
'<i>Lorem ipsum</i>',
'<i>Lorem ipsum</i>',
],
[
'<i></i>',
'<i></i>',
],

[
'<li>Lorem ipsum</li>',
'<li>Lorem ipsum</li>',
Expand Down
21 changes: 20 additions & 1 deletion Visitor/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
*/
final class Node implements NodeInterface
{
// HTML5 elements which are self-closing
private const VOID_ELEMENTS = [
'area' => true,
'base' => true,
'br' => true,
'col' => true,
'embed' => true,
'hr' => true,
'img' => true,
'input' => true,
'keygen' => true,
'link' => true,
'meta' => true,
'param' => true,
'source' => true,
'track' => true,
'wbr' => true,
];

private NodeInterface $parent;
private string $tagName;
private array $attributes = [];
Expand Down Expand Up @@ -56,7 +75,7 @@ public function addChild(NodeInterface $node): void

public function render(): string
{
if (!$this->children) {
if (isset(self::VOID_ELEMENTS[$this->tagName])) {
return '<'.$this->tagName.$this->renderAttributes().' />';
}

Expand Down

0 comments on commit ad37531

Please sign in to comment.