Debian Codegraph @

tests/phpunit/unit/includes/parser/SanitizerUnitTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Parser;
4
5 use MediaWiki\Parser\Sanitizer;
6 use MediaWikiUnitTestCase;
7 use UtfNormal\Constants;
8
9 /**
10 * @group Sanitizer
11 */
12 class SanitizerUnitTest extends MediaWikiUnitTestCase {
13
14 /**
15 * @dataProvider provideDecodeCharReferences
16 * @covers \MediaWiki\Parser\Sanitizer::decodeCharReferences
17 */
18 public function testDecodeCharReferences( string $expected, string $input ) {
19 $this->assertSame( $expected, Sanitizer::decodeCharReferences( $input ) );
20 }
21
22 public static function provideDecodeCharReferences() {
23 return [
24 'decode named entities' => [
25 "\u{00E9}cole",
26 '&eacute;cole',
27 ],
28 'decode numeric entities' => [
29 "\u{0108}io bonas dans l'\u{00E9}cole!",
30 "&#x108;io bonas dans l'&#233;cole!",
31 ],
32 'decode mixed numeric/named entities' => [
33 "\u{0108}io bonas dans l'\u{00E9}cole!",
34 "&#x108;io bonas dans l'&eacute;cole!",
35 ],
36 'decode mixed complex entities' => [
37 "\u{0108}io bonas dans l'\u{00E9}cole! (mais pas &#x108;io dans l'&eacute;cole)",
38 "&#x108;io bonas dans l'&eacute;cole! (mais pas &amp;#x108;io dans l'&#38;eacute;cole)",
39 ],
40 'Invalid ampersand' => [
41 'a & b',
42 'a & b',
43 ],
44 'Invalid named entity' => [
45 '&foo;',
46 '&foo;',
47 ],
48 'Invalid numbered entity (decimal)' => [
49 Constants::UTF8_REPLACEMENT,
50 "&#888888888888888888;",
51 ],
52 'Invalid numbered entity (hex)' => [
53 Constants::UTF8_REPLACEMENT,
54 "&#x88888888888888888;",
55 ],
56 // These cases are also "very large" numbers, but they will
57 // truncate down to ASCII. So be careful.
58 'Invalid numbered entity w/ valid truncation (decimal)' => [
59 Constants::UTF8_REPLACEMENT,
60 "&#18446744073709551681;",
61 ],
62 'Invalid numbered entity w/ valid truncation (hex)' => [
63 Constants::UTF8_REPLACEMENT,
64 "&#x10000000000000041;",
65 ],
66 ];
67 }
68
69 /**
70 * @dataProvider provideTagAttributesToDecode
71 * @covers \MediaWiki\Parser\Sanitizer::decodeTagAttributes
72 */
73 public function testDecodeTagAttributes( $expected, $attributes, $message = '' ) {
74 $this->assertSame( $expected,
75 Sanitizer::decodeTagAttributes( $attributes ),
76 $message
77 );
78 }
79
80 public static function provideTagAttributesToDecode() {
81 return [
82 [ [ 'foo' => 'bar' ], 'foo=bar', 'Unquoted attribute' ],
83 [ [ 'עברית' => 'bar' ], 'עברית=bar', 'Non-Latin attribute' ],
84 [ [ '६' => 'bar' ], '६=bar', 'Devanagari number' ],
85 [ [ '搭𨋢' => 'bar' ], '搭𨋢=bar', 'Non-BMP character' ],
86 [ [], 'ńgh=bar', 'Combining accent is not allowed' ],
87 [ [ 'foo' => 'bar' ], ' foo = bar ', 'Spaced attribute' ],
88 [ [ 'foo' => 'bar' ], 'foo="bar"', 'Double-quoted attribute' ],
89 [ [ 'foo' => 'bar' ], 'foo=\'bar\'', 'Single-quoted attribute' ],
90 [
91 [ 'foo' => 'bar', 'baz' => 'foo' ],
92 'foo=\'bar\' baz="foo"',
93 'Several attributes'
94 ],
95 [
96 [ 'foo' => 'bar', 'baz' => 'foo' ],
97 'foo=\'bar\' baz="foo"',
98 'Several attributes'
99 ],
100 [
101 [ 'foo' => 'bar', 'baz' => 'foo' ],
102 'foo=\'bar\' baz="foo"',
103 'Several attributes'
104 ],
105 [ [ ':foo' => 'bar' ], ':foo=\'bar\'', 'Leading :' ],
106 [ [ '_foo' => 'bar' ], '_foo=\'bar\'', 'Leading _' ],
107 [ [ 'foo' => 'bar' ], 'Foo=\'bar\'', 'Leading capital' ],
108 [ [ 'foo' => 'BAR' ], 'FOO=BAR', 'Attribute keys are normalized to lowercase' ],
109
110 # Invalid beginning
111 [ [], '-foo=bar', 'Leading - is forbidden' ],
112 [ [], '.foo=bar', 'Leading . is forbidden' ],
113 [ [ 'foo-bar' => 'bar' ], 'foo-bar=bar', 'A - is allowed inside the attribute' ],
114 [ [ 'foo-' => 'bar' ], 'foo-=bar', 'A - is allowed inside the attribute' ],
115 [ [ 'foo.bar' => 'baz' ], 'foo.bar=baz', 'A . is allowed inside the attribute' ],
116 [ [ 'foo.' => 'baz' ], 'foo.=baz', 'A . is allowed as last character' ],
117 [ [ 'foo6' => 'baz' ], 'foo6=baz', 'Numbers are allowed' ],
118
119 # This bit is more relaxed than XML rules, but some extensions use
120 # it, like ProofreadPage (see T29539)
121 [ [ '1foo' => 'baz' ], '1foo=baz', 'Leading numbers are allowed' ],
122 [ [], 'foo$=baz', 'Symbols are not allowed' ],
123 [ [], 'foo@=baz', 'Symbols are not allowed' ],
124 [ [], 'foo~=baz', 'Symbols are not allowed' ],
125 [
126 [ 'foo' => '1[#^`*%w/(' ],
127 'foo=1[#^`*%w/(',
128 'All kind of characters are allowed as values'
129 ],
130 [
131 [ 'foo' => '1[#^`*%\'w/(' ],
132 'foo="1[#^`*%\'w/("',
133 'Double quotes are allowed if quoted by single quotes'
134 ],
135 [
136 [ 'foo' => '1[#^`*%"w/(' ],
137 'foo=\'1[#^`*%"w/(\'',
138 'Single quotes are allowed if quoted by double quotes'
139 ],
140 [ [ 'foo' => '&"' ], 'foo=&amp;&quot;', 'Special chars can be provided as entities' ],
141 [ [ 'foo' => '&foobar;' ], 'foo=&foobar;', 'Entity-like items are accepted' ],
142 ];
143 }
144
145 /**
146 * @dataProvider provideCssCommentsFixtures
147 * @covers \MediaWiki\Parser\Sanitizer::checkCss
148 */
149 public function testCssCommentsChecking( $expected, $css, $message = '' ) {
150 $this->assertSame( $expected,
151 Sanitizer::checkCss( $css ),
152 $message
153 );
154 }
155
156 public static function provideCssCommentsFixtures() {
157 /** [ <expected>, <css>, [message] ] */
158 return [
159 // Valid comments spanning entire input
160 [ '/**/', '/**/' ],
161 [ '/* comment */', '/* comment */' ],
162 // Weird stuff
163 [ ' ', '/****/' ],
164 [ ' ', '/* /* */' ],
165 [ 'display: block;', "display:/* foo */block;" ],
166 [ 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;",
167 'Backslash-escaped comments must be stripped (T30450)' ],
168 [ '', '/* unfinished comment structure',
169 'Remove anything after a comment-start token' ],
170 [ '', "\\2f\\2a unifinished comment'",
171 'Remove anything after a backslash-escaped comment-start token' ],
172 [ '/* insecure input */', 'width: expression(1+1);' ],
173 [ '/* insecure input */', 'background-image: image(asdf.png);' ],
174 [ '/* insecure input */', 'background-image: -webkit-image(asdf.png);' ],
175 [ '/* insecure input */', 'background-image: -moz-image(asdf.png);' ],
176 [ '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);' ],
177 [
178 '/* insecure input */',
179 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);'
180 ],
181 [
182 '/* insecure input */',
183 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);'
184 ],
185 [ '/* insecure input */', 'foo: attr( title, url );' ],
186 [ '/* insecure input */', 'foo: attr( title url );' ],
187 ];
188 }
189
190 /**
191 * @dataProvider provideEscapeHtmlAllowEntities
192 * @covers \MediaWiki\Parser\Sanitizer::escapeHtmlAllowEntities
193 */
194 public function testEscapeHtmlAllowEntities( $expected, $html ) {
195 $this->assertSame(
196 $expected,
197 Sanitizer::escapeHtmlAllowEntities( $html )
198 );
199 }
200
201 public static function provideEscapeHtmlAllowEntities() {
202 return [
203 [ 'foo', 'foo' ],
204 [ 'a¡b', 'a&#161;b' ],
205 [ 'foo&#039;bar', "foo'bar" ],
206 [ '&lt;script&gt;foo&lt;/script&gt;', '<script>foo</script>' ],
207 [ '&#x338;', "\u{0338}" ],
208 [ '&#x338;', '&#x338;' ],
209 ];
210 }
211
212 /**
213 * @dataProvider provideIsReservedDataAttribute
214 * @covers \MediaWiki\Parser\Sanitizer::isReservedDataAttribute
215 */
216 public function testIsReservedDataAttribute( $attr, $expected ) {
217 $this->assertSame( $expected, Sanitizer::isReservedDataAttribute( $attr ) );
218 }
219
220 public static function provideIsReservedDataAttribute() {
221 return [
222 [ 'foo', false ],
223 [ 'data', false ],
224 [ 'data-foo', false ],
225 [ 'data-mw', true ],
226 [ 'data-ooui', true ],
227 [ 'data-parsoid', true ],
228 [ 'data-mw-foo', true ],
229 [ 'data-ooui-foo', true ],
230 // could be false but this is how it's implemented currently
231 [ 'data-mwfoo', true ],
232 ];
233 }
234
235 /**
236 * @dataProvider provideStripAllTags
237 *
238 * @covers \MediaWiki\Parser\Sanitizer::stripAllTags()
239 * @covers \MediaWiki\Parser\RemexStripTagHandler
240 *
241 * @param string $input
242 * @param string $expected
243 */
244 public function testStripAllTags( $input, $expected ) {
245 $this->assertSame( $expected, Sanitizer::stripAllTags( $input ) );
246 }
247
248 public static function provideStripAllTags() {
249 return [
250 [ '<p>Foo</p>', 'Foo' ],
251 [ '<p id="one">Foo</p><p id="two">Bar</p>', 'Foo Bar' ],
252 [ "<p>Foo</p>\n<p>Bar</p>", 'Foo Bar' ],
253 [ '<p>Hello &lt;strong&gt; wor&#x6c;&#100; caf&eacute;</p>', 'Hello <strong> world café' ],
254 [
255 '<p><small data-foo=\'bar"&lt;baz>quux\'><a href="./Foo">Bar</a></small> Whee!</p>',
256 'Bar Whee!'
257 ],
258 [ '1<span class="<?php">2</span>3', '123' ],
259 [ '1<span class="<?">2</span>3', '123' ],
260 [ '<th>1</th><td>2</td>', '1 2' ],
261 [ '<style>.hello { display: block; }</style>', '' ],
262 [ 'Foo<style>p { color: red; }</style>Bar', 'FooBar' ],
263 [ '<script>var test = true;</script>', '' ],
264 ];
265 }
266
267 }