Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HtmlSanitizer] Fix node renderer handling of self-closing (void) elements #46274

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

Merged
merged 1 commit into from
May 6, 2022
Merged
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
14 changes: 12 additions & 2 deletions src/Symfony/Component/HtmlSanitizer/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 src/Symfony/Component/HtmlSanitizer/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