FuCY's picture
Add files using upload-large-folder tool
cb29db0 verified
Raw History Blame Contribute Delete
6.17 kB
diff --git a/src/Core/XssSanitiser.php b/src/Core/XssSanitiser.php
index 5ffdb224b95..adfcfabb289 100644
--- a/src/Core/XssSanitiser.php
+++ b/src/Core/XssSanitiser.php
@@ -201,7 +201,9 @@ private function getStripAttributeContentsRegex(): string
$this->splitWithWhitespaceRegex('vbscript:'),
];
// Regex is "starts with any of these, with optional whitespace at the start, case insensitive"
- return '#^\s*(' . implode('|', $regexes) . ')#iu';
+ // \x08 is the backspace character, though it only causes vulnerabilities if it's at the start
+ // or end of a string
+ return '#^(\s|\x08)*(' . implode('|', $regexes) . ')(\s|\x08)*#iu';
}
private function splitWithWhitespaceRegex(string $string): string
diff --git a/tests/php/Core/XssSanitiserTest.php b/tests/php/Core/XssSanitiserTest.php
index 0d1ef959447..103e6eed496 100644
--- a/tests/php/Core/XssSanitiserTest.php
+++ b/tests/php/Core/XssSanitiserTest.php
@@ -9,6 +9,9 @@
class XssSanitiserTest extends SapphireTest
{
+ // This is the backspace character. It needs to be escaped in double-quotes.
+ private const CHAR_BACKSPACE = "\x08";
+
protected $usesDatabase = false;
public function provideSanitise(): array
@@ -73,6 +76,14 @@ public function provideSanitise(): array
'input' => '<a href="javascript:alert(\'ok\')">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</a>',
'expected' => '<a>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</a>',
],
+ [
+ 'input' => '<a href="' . XssSanitiserTest::CHAR_BACKSPACE . 'javascript:alert(\'ok\')">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</a>',
+ 'expected' => '<a>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</a>',
+ ],
+ [
+ 'input' => '<a href="javascript:alert(\'ok\')' . XssSanitiserTest::CHAR_BACKSPACE . '">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</a>',
+ 'expected' => '<a>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</a>',
+ ],
[
// Not exploitable XSS
'input' => '<<a href="javascript:evil"/>a href="javascript:evil"/>',
@@ -132,6 +143,10 @@ public function provideSanitise(): array
'input' => '<IMG SRC="javascript:alert(\'XSS\');">',
'expected' => '<img>',
],
+ [
+ 'input' => '<IMG SRC="' . XssSanitiserTest::CHAR_BACKSPACE . 'javascript:alert(\'XSS\');">',
+ 'expected' => '<img>',
+ ],
[
'input' => '<IMG SRC=javascript:alert(\'XSS\')>',
'expected' => '<img>',
@@ -206,6 +221,10 @@ public function provideSanitise(): array
'input' => '<BGSOUND SRC="javascript:alert(\'XSS\');">',
'expected' => '<bgsound></bgsound>',
],
+ [
+ 'input' => '<BGSOUND SRC="' . XssSanitiserTest::CHAR_BACKSPACE . 'javascript:alert(\'XSS\');">',
+ 'expected' => '<bgsound></bgsound>',
+ ],
[
// Not exploitable XSS
'input' => '<BR SIZE="&{alert(\'XSS\')}">',
diff --git a/tests/php/Forms/HTMLEditor/HTMLEditorSanitiserTest.php b/tests/php/Forms/HTMLEditor/HTMLEditorSanitiserTest.php
index ee3d9980212..ebeaef92dc7 100644
--- a/tests/php/Forms/HTMLEditor/HTMLEditorSanitiserTest.php
+++ b/tests/php/Forms/HTMLEditor/HTMLEditorSanitiserTest.php
@@ -10,6 +10,8 @@
class HTMLEditorSanitiserTest extends FunctionalTest
{
+ // This is the backspace character. It needs to be escaped in double-quotes.
+ private const CHAR_BACKSPACE = "\x08";
public function provideSanitise(): array
{
@@ -80,6 +82,18 @@ public function provideSanitise(): array
'<a>Test</a>',
'Javascript in the href attribute of a link is completely removed'
],
+ [
+ 'a[href|target|rel]',
+ '<a href="' . HTMLEditorSanitiserTest::CHAR_BACKSPACE . 'javascript:alert(0);">Test</a>',
+ '<a>Test</a>',
+ 'Javascript in the href attribute with leading backspace of a link is completely removed'
+ ],
+ [
+ 'a[href|target|rel]',
+ '<a href="javascript:alert(0);' . HTMLEditorSanitiserTest::CHAR_BACKSPACE . '">Test</a>',
+ '<a>Test</a>',
+ 'Javascript in the href attribute with backspace in middle of a link is completely removed'
+ ],
[
'a[href|target|rel]',
'<a href="' . implode("\n", str_split(' javascript:')) . '">Test</a>',
@@ -110,6 +124,12 @@ public function provideSanitise(): array
'<iframe></iframe>',
'Javascript with tab elements the src attribute of an iframe is completely removed'
],
+ [
+ 'iframe[src]',
+ '<iframe src="' . HTMLEditorSanitiserTest::CHAR_BACKSPACE . 'javascript:alert(0);"></iframe>',
+ '<iframe></iframe>',
+ 'Javascript in the src attribute of an iframe with a backspace is completely removed'
+ ],
[
'object[data]',
'<object data="OK"></object>',
@@ -128,6 +148,12 @@ public function provideSanitise(): array
'<object></object>',
'Object with dangerous javascript content in data attribute with quotes is completely removed'
],
+ [
+ 'object[data]',
+ '<object data="' . HTMLEditorSanitiserTest::CHAR_BACKSPACE . 'javascript:alert()">',
+ '<object></object>',
+ 'Object with dangerous javascript content in data attribute with backspace is completely removed'
+ ],
[
'object[data]',
'<object data="data:text/html;base64,PHNjcmlwdD5hbGVydChkb2N1bWVudC5sb2NhdGlvbik8L3NjcmlwdD4=">',