Skip to content

Commit

Permalink
Merge branch 'v0.26'
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Jul 10, 2019
2 parents 9444183 + c732970 commit 8fcb0e6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/Entities/Repos/EntityRepo.php
Expand Up @@ -760,13 +760,13 @@ protected function escapeScripts(string $html) : string
$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;
Expand Down
48 changes: 48 additions & 0 deletions tests/Entity/PageContentTest.php
Expand Up @@ -84,6 +84,31 @@ public function test_page_content_scripts_removed_by_default()
$pageView->assertSee('abc123abc123');
}

public function test_more_complex_content_script_escaping_scenarios()
{
$checks = [
"<p>Some script</p><script>alert('cat')</script>",
"<div><div><div><div><p>Some script</p><script>alert('cat')</script></div></div></div></div>",
"<p>Some script<script>alert('cat')</script></p>",
"<p>Some script <div><script>alert('cat')</script></div></p>",
"<p>Some script <script><div>alert('cat')</script></div></p>",
"<p>Some script <script><div>alert('cat')</script><script><div>alert('cat')</script></p><script><div>alert('cat')</script>",
];

$this->asEditor();
$page = Page::first();

foreach ($checks as $check) {
$page->html = $check;
$page->save();

$pageView = $this->get($page->getUrl());
$pageView->assertElementNotContains('.page-content', '<script>');
$pageView->assertElementNotContains('.page-content', '</script>');
}

}

public function test_page_inline_on_attributes_removed_by_default()
{
$this->asEditor();
Expand All @@ -97,6 +122,29 @@ public function test_page_inline_on_attributes_removed_by_default()
$pageView->assertSee('<p>Hello</p>');
}

public function test_more_complex_inline_on_attributes_escaping_scenarios()
{
$checks = [
'<p onclick="console.log(\'test\')">Hello</p>',
'<div>Lorem ipsum dolor sit amet.</div><p onclick="console.log(\'test\')">Hello</p>',
'<div>Lorem ipsum dolor sit amet.<p onclick="console.log(\'test\')">Hello</p></div>',
'<div><div><div><div>Lorem ipsum dolor sit amet.<p onclick="console.log(\'test\')">Hello</p></div></div></div></div>',
'<div onclick="console.log(\'test\')">Lorem ipsum dolor sit amet.</div><p onclick="console.log(\'test\')">Hello</p><div></div>',
];

$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();
Expand Down

0 comments on commit 8fcb0e6

Please sign in to comment.