| 1 |
<?php |
| 2 |
|
| 3 |
namespace Test\Parsoid\Core; |
| 4 |
|
| 5 |
use Wikimedia\Parsoid\Core\Sanitizer; |
| 6 |
|
| 7 |
/** |
| 8 |
* @coversDefaultClass \Wikimedia\Parsoid\Core\Sanitizer |
| 9 |
*/ |
| 10 |
class SanitizerUnitTest extends \PHPUnit\Framework\TestCase { |
| 11 |
private const UTF8_REPLACEMENT = "\xef\xbf\xbd"; |
| 12 |
|
| 13 |
/** |
| 14 |
* @dataProvider provideDecodeCharReferences |
| 15 |
* @covers ::decodeCharReferences |
| 16 |
*/ |
| 17 |
public function testDecodeCharReferences( string $expected, string $input ) { |
| 18 |
$this->assertSame( $expected, Sanitizer::decodeCharReferences( $input ) ); |
| 19 |
} |
| 20 |
|
| 21 |
public function provideDecodeCharReferences() { |
| 22 |
return [ |
| 23 |
'decode named entities' => [ |
| 24 |
"\u{00E9}cole", |
| 25 |
'école', |
| 26 |
], |
| 27 |
'decode numeric entities' => [ |
| 28 |
"\u{0108}io bonas dans l'\u{00E9}cole!", |
| 29 |
"Ĉio bonas dans l'école!", |
| 30 |
], |
| 31 |
'decode mixed numeric/named entities' => [ |
| 32 |
"\u{0108}io bonas dans l'\u{00E9}cole!", |
| 33 |
"Ĉio bonas dans l'école!", |
| 34 |
], |
| 35 |
'decode mixed complex entities' => [ |
| 36 |
"\u{0108}io bonas dans l'\u{00E9}cole! (mais pas Ĉio dans l'école)", |
| 37 |
"Ĉio bonas dans l'école! (mais pas &#x108;io dans l'&eacute;cole)", |
| 38 |
], |
| 39 |
'Invalid ampersand' => [ |
| 40 |
'a & b', |
| 41 |
'a & b', |
| 42 |
], |
| 43 |
'Invalid named entity' => [ |
| 44 |
'&foo;', |
| 45 |
'&foo;', |
| 46 |
], |
| 47 |
'Invalid numbered entity (decimal)' => [ |
| 48 |
self::UTF8_REPLACEMENT, |
| 49 |
"�", |
| 50 |
], |
| 51 |
'Invalid numbered entity (hex)' => [ |
| 52 |
self::UTF8_REPLACEMENT, |
| 53 |
"�", |
| 54 |
], |
| 55 |
// These cases are also "very large" numbers, but they will |
| 56 |
// truncate down to ASCII. So be careful. |
| 57 |
'Invalid numbered entity w/ valid truncation (decimal)' => [ |
| 58 |
self::UTF8_REPLACEMENT, |
| 59 |
"�", |
| 60 |
], |
| 61 |
'Invalid numbered entity w/ valid truncation (hex)' => [ |
| 62 |
self::UTF8_REPLACEMENT, |
| 63 |
"�", |
| 64 |
], |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
// testDecodeTagAttributes from upstream omitted. |
| 69 |
|
| 70 |
/** |
| 71 |
* @dataProvider provideCssCommentsFixtures |
| 72 |
* @covers ::checkCss |
| 73 |
*/ |
| 74 |
public function testCssCommentsChecking( $expected, $css, $message = '' ) { |
| 75 |
$this->assertSame( $expected, |
| 76 |
Sanitizer::checkCss( $css ), |
| 77 |
$message |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
public static function provideCssCommentsFixtures() { |
| 82 |
/** [ <expected>, <css>, [message] ] */ |
| 83 |
return [ |
| 84 |
// Valid comments spanning entire input |
| 85 |
[ '/**/', '/**/' ], |
| 86 |
[ '/* comment */', '/* comment */' ], |
| 87 |
// Weird stuff |
| 88 |
[ ' ', '/****/' ], |
| 89 |
[ ' ', '/* /* */' ], |
| 90 |
[ 'display: block;', "display:/* foo */block;" ], |
| 91 |
[ 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;", |
| 92 |
'Backslash-escaped comments must be stripped (T30450)' ], |
| 93 |
[ '', '/* unfinished comment structure', |
| 94 |
'Remove anything after a comment-start token' ], |
| 95 |
[ '', "\\2f\\2a unifinished comment'", |
| 96 |
'Remove anything after a backslash-escaped comment-start token' ], |
| 97 |
[ '/* insecure input */', 'width: expression(1+1);' ], |
| 98 |
[ '/* insecure input */', 'background-image: image(asdf.png);' ], |
| 99 |
[ '/* insecure input */', 'background-image: -webkit-image(asdf.png);' ], |
| 100 |
[ '/* insecure input */', 'background-image: -moz-image(asdf.png);' ], |
| 101 |
[ '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);' ], |
| 102 |
[ |
| 103 |
'/* insecure input */', |
| 104 |
'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);' |
| 105 |
], |
| 106 |
[ |
| 107 |
'/* insecure input */', |
| 108 |
'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);' |
| 109 |
], |
| 110 |
[ '/* insecure input */', 'foo: attr( title, url );' ], |
| 111 |
[ '/* insecure input */', 'foo: attr( title url );' ], |
| 112 |
]; |
| 113 |
} |
| 114 |
|
| 115 |
// testEscapeHtmlAllowEntities from upstream omitted. |
| 116 |
|
| 117 |
/** |
| 118 |
* @dataProvider provideIsReservedDataAttribute |
| 119 |
* @covers ::isReservedDataAttribute |
| 120 |
*/ |
| 121 |
public function testIsReservedDataAttribute( $attr, $expected ) { |
| 122 |
$this->assertSame( $expected, Sanitizer::isReservedDataAttribute( $attr ) ); |
| 123 |
} |
| 124 |
|
| 125 |
public static function provideIsReservedDataAttribute() { |
| 126 |
return [ |
| 127 |
[ 'foo', false ], |
| 128 |
[ 'data', false ], |
| 129 |
[ 'data-foo', false ], |
| 130 |
// [ 'data-mw', true ], // PARSOID-SPECIFIC BEHAVIOR CHANGE |
| 131 |
[ 'data-ooui', true ], |
| 132 |
// [ 'data-parsoid', true ], // PARSOID-SPECIFIC BEHAVIOR CHANGE |
| 133 |
// [ 'data-mw-foo', true ], // PARSOID-SPECIFIC BEHAVIOR CHANGE |
| 134 |
[ 'data-ooui-foo', true ], |
| 135 |
// [ 'data-mwfoo', true ], // PARSOID-SPECIFIC BEHAVIOR CHANGE |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
// testStripAllTags from upstream omitted. |
| 140 |
} |