Skip to content

Commit

Permalink
fix: remove HTML event attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Apr 13, 2023
1 parent a583317 commit 5401ab7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 10 additions & 1 deletion phpmyfaq/src/phpMyFAQ/Helper/FaqHelper.php
Expand Up @@ -18,6 +18,7 @@
namespace phpMyFAQ\Helper;

use DOMDocument;
use DOMXPath;
use Exception;
use ParsedownExtra;
use phpMyFAQ\Category;
Expand Down Expand Up @@ -245,6 +246,14 @@ public function cleanUpContent(string $content): string
$scriptTags->item($i)->parentNode->removeChild($scriptTags->item($i));
}

return preg_replace(['/\r/', '/\n/'], '', $document->saveHTML());
$xpath = new DOMXPath($document);
$onAttributes = $xpath->query("//*/@*[starts-with(name(), 'on')]");
foreach ($onAttributes as $onAttribute) {
$onAttribute->ownerElement->removeAttributeNode($onAttribute);
}

$body = $xpath->query('body')->item(0);

return preg_replace(['/\r/', '/\n/'], '', $document->saveHTML($body));
}
}
4 changes: 2 additions & 2 deletions tests/phpMyFAQ/Helper/FaqHelperTest.php
Expand Up @@ -62,8 +62,8 @@ public function testCreateFaqUrl(): void

public function testCleanUpContent(): void
{
$content = '<p>Some text <script>alert("Hello, world!");</script></p>';
$expectedOutput = '<p>Some text </p>';
$content = '<p>Some text <script>alert("Hello, world!");</script><img src=foo onerror=alert(document.cookie)></p>';
$expectedOutput = '<p>Some text <img src="foo"></p>';

$actualOutput = $this->faqHelper->cleanUpContent($content);

Expand Down

0 comments on commit 5401ab7

Please sign in to comment.