From c732970f6e19f14a1107b7429cbf387bcb7848b3 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Wed, 10 Jul 2019 20:17:22 +0100 Subject: [PATCH] Hardened page content script escaping Increased range of tests to cover. Fixes #1531 --- app/Entities/Repos/EntityRepo.php | 4 +-- tests/Entity/PageContentTest.php | 48 +++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/app/Entities/Repos/EntityRepo.php b/app/Entities/Repos/EntityRepo.php index 4edd61723..aad9a1205 100644 --- a/app/Entities/Repos/EntityRepo.php +++ b/app/Entities/Repos/EntityRepo.php @@ -760,13 +760,13 @@ class EntityRepo $xPath = new DOMXPath($doc); // Remove standard script tags - $scriptElems = $xPath->query('//body//*//script'); + $scriptElems = $xPath->query('//script'); foreach ($scriptElems as $scriptElem) { $scriptElem->parentNode->removeChild($scriptElem); } // Remove 'on*' attributes - $onAttributes = $xPath->query('//body//*/@*[starts-with(name(), \'on\')]'); + $onAttributes = $xPath->query('//@*[starts-with(name(), \'on\')]'); foreach ($onAttributes as $attr) { /** @var \DOMAttr $attr*/ $attrName = $attr->nodeName; diff --git a/tests/Entity/PageContentTest.php b/tests/Entity/PageContentTest.php index 6201cf5d7..c80b5f1d9 100644 --- a/tests/Entity/PageContentTest.php +++ b/tests/Entity/PageContentTest.php @@ -84,6 +84,31 @@ class PageContentTest extends TestCase $pageView->assertSee('abc123abc123'); } + public function test_more_complex_content_script_escaping_scenarios() + { + $checks = [ + "

Some script

", + "

Some script

", + "

Some script

", + "

Some script

", + "

Some script

", + "

Some script

", + ]; + + $this->asEditor(); + $page = Page::first(); + + foreach ($checks as $check) { + $page->html = $check; + $page->save(); + + $pageView = $this->get($page->getUrl()); + $pageView->assertElementNotContains('.page-content', ''); + } + + } + public function test_page_inline_on_attributes_removed_by_default() { $this->asEditor(); @@ -97,6 +122,29 @@ class PageContentTest extends TestCase $pageView->assertSee('

Hello

'); } + public function test_more_complex_inline_on_attributes_escaping_scenarios() + { + $checks = [ + '

Hello

', + '
Lorem ipsum dolor sit amet.

Hello

', + '
Lorem ipsum dolor sit amet.

Hello

', + '
Lorem ipsum dolor sit amet.

Hello

', + '
Lorem ipsum dolor sit amet.

Hello

', + ]; + + $this->asEditor(); + $page = Page::first(); + + foreach ($checks as $check) { + $page->html = $check; + $page->save(); + + $pageView = $this->get($page->getUrl()); + $pageView->assertElementNotContains('.page-content', 'onclick'); + } + + } + public function test_page_content_scripts_show_when_configured() { $this->asEditor();