Debian Codegraph @

tests/phpunit/includes/parser/SanitizerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Parser;
4
5 use InvalidArgumentException;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Parser\Sanitizer;
8 use MediaWikiIntegrationTestCase;
9 use UnexpectedValueException;
10 use Wikimedia\TestingAccessWrapper;
11
12 /**
13 * @group Sanitizer
14 */
15 class SanitizerTest extends MediaWikiIntegrationTestCase {
16
17 /**
18 * @covers \MediaWiki\Parser\Sanitizer::internalRemoveHTMLtags
19 * @dataProvider provideHtml5Tags
20 *
21 * @param string $tag Name of an HTML5 element (ie: 'video')
22 * @param bool $escaped Whether sanitizer let the tag in or escape it (ie: '&lt;video&gt;')
23 */
24 public function testInternalRemoveHtmlTagsOnHtml5Tags( $tag, $escaped ) {
25 if ( $escaped ) {
26 $this->assertEquals( "&lt;$tag&gt;",
27 Sanitizer::internalRemoveHtmlTags( "<$tag>" )
28 );
29 } else {
30 $this->assertEquals( "<$tag></$tag>\n",
31 Sanitizer::internalRemoveHtmlTags( "<$tag></$tag>\n" )
32 );
33 }
34 }
35
36 /**
37 * @covers \MediaWiki\Parser\Sanitizer::removeSomeTags
38 * @dataProvider provideHtml5Tags
39 *
40 * @param string $tag Name of an HTML5 element (ie: 'video')
41 * @param bool $escaped Whether sanitizer let the tag in or escape it (ie: '&lt;video&gt;')
42 */
43 public function testRemoveSomeTagsOnHtml5Tags( $tag, $escaped ) {
44 if ( $escaped ) {
45 $this->assertEquals( "&lt;$tag&gt;",
46 Sanitizer::removeSomeTags( "<$tag>" )
47 );
48 } else {
49 $this->assertEquals( "<$tag></$tag>\n",
50 Sanitizer::removeSomeTags( "<$tag></$tag>\n" )
51 );
52 $this->assertEquals( "<$tag></$tag>",
53 Sanitizer::removeSomeTags( "<$tag>" )
54 );
55 }
56 }
57
58 public static function provideHtml5Tags() {
59 $ESCAPED = true; # We want tag to be escaped
60 $VERBATIM = false; # We want to keep the tag
61 return [
62 [ 'data', $VERBATIM ],
63 [ 'mark', $VERBATIM ],
64 [ 'time', $VERBATIM ],
65 [ 'video', $ESCAPED ],
66 ];
67 }
68
69 public function dataRemoveHTMLtags() {
70 return [
71 // former testSelfClosingTag
72 [
73 '<div>Hello world</div />',
74 '<div>Hello world</div>',
75 'Self-closing closing div'
76 ],
77 // Make sure special nested HTML5 semantics are not broken
78 // https://html.spec.whatwg.org/multipage/semantics.html#the-kbd-element
79 [
80 '<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
81 '<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
82 'Nested <kbd>.'
83 ],
84 // https://html.spec.whatwg.org/multipage/semantics.html#the-sub-and-sup-elements
85 [
86 '<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
87 '<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
88 'Nested <var>.'
89 ],
90 // https://html.spec.whatwg.org/multipage/semantics.html#the-dfn-element
91 [
92 '<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
93 '<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
94 '<abbr> inside <dfn>',
95 ],
96 ];
97 }
98
99 /**
100 * @dataProvider dataRemoveHTMLtags
101 * @covers \MediaWiki\Parser\Sanitizer::internalRemoveHtmlTags
102 */
103 public function testInternalRemoveHTMLtags( $input, $output, $msg = null ) {
104 $this->assertEquals( $output, Sanitizer::internalRemoveHtmlTags( $input ), $msg );
105 }
106
107 /**
108 * @dataProvider dataRemoveHTMLtags
109 * @covers \MediaWiki\Parser\Sanitizer::removeSomeTags
110 */
111 public function testRemoveSomeTags( $input, $output, $msg = null ) {
112 $this->assertEquals( $output, Sanitizer::removeSomeTags( $input ), $msg );
113 }
114
115 /**
116 * @dataProvider provideDeprecatedAttributes
117 * @covers \MediaWiki\Parser\Sanitizer::fixTagAttributes
118 * @covers \MediaWiki\Parser\Sanitizer::validateTagAttributes
119 * @covers \MediaWiki\Parser\Sanitizer::validateAttributes
120 */
121 public function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message = '' ) {
122 $this->assertEquals( " $inputAttr",
123 Sanitizer::fixTagAttributes( $inputAttr, $inputEl ),
124 $message
125 );
126 }
127
128 public static function provideDeprecatedAttributes() {
129 /** [ <attribute>, <element>, [message] ] */
130 return [
131 [ 'clear="left"', 'br' ],
132 [ 'clear="all"', 'br' ],
133 [ 'width="100"', 'td' ],
134 [ 'nowrap="true"', 'td' ],
135 [ 'nowrap=""', 'td' ],
136 [ 'align="right"', 'td' ],
137 [ 'align="center"', 'table' ],
138 [ 'align="left"', 'tr' ],
139 [ 'align="center"', 'div' ],
140 [ 'align="left"', 'h1' ],
141 [ 'align="left"', 'p' ],
142 ];
143 }
144
145 /**
146 * @dataProvider provideValidateTagAttributes
147 * @covers \MediaWiki\Parser\Sanitizer::validateTagAttributes
148 * @covers \MediaWiki\Parser\Sanitizer::validateAttributes
149 */
150 public function testValidateTagAttributes( $element, $attribs, $expected ) {
151 $actual = Sanitizer::validateTagAttributes( $attribs, $element );
152 $this->assertArrayEquals( $expected, $actual, false, true );
153 }
154
155 public static function provideValidateTagAttributes() {
156 return [
157 [ 'math',
158 [ 'id' => 'foo bar', 'bogus' => 'stripped', 'data-foo' => 'bar' ],
159 [ 'id' => 'foo_bar', 'data-foo' => 'bar' ],
160 ],
161 [ 'meta',
162 [ 'id' => 'foo bar', 'itemprop' => 'foo', 'content' => 'bar' ],
163 [ 'itemprop' => 'foo', 'content' => 'bar' ],
164 ],
165 [ 'div',
166 [ 'role' => 'presentation', 'aria-hidden' => 'true' ],
167 [ 'role' => 'presentation', 'aria-hidden' => 'true' ],
168 ],
169 [ 'div',
170 [ 'role' => 'menuitem', 'aria-hidden' => 'false' ],
171 [ 'role' => 'menuitem', 'aria-hidden' => 'false' ],
172 ],
173 [ 'div',
174 [
175 'data-wikitext' => 'wikitext',
176 'DATA-WIKITEXT-2' => 'WIKITEXT-2',
177 'data-wikitext-ij' => 'wikitext-IJ',
178 'data-mw' => 'disallow impersonating parsoid',
179 'DATA-mw' => 'disallow impersonating PARSOID',
180 'data-mw-extension' => 'disallow impersonating extension',
181 'data-:namespaced' => 'disallow namespace',
182 'data- invalid' => 'disallow XSS',
183 'data-/invalid' => 'disallow XSS',
184 'data->invalid' => 'disallow XSS',
185 'data-=invalid' => 'disallow XSS',
186 ],
187 [
188 'data-wikitext' => 'wikitext',
189 'DATA-WIKITEXT-2' => 'WIKITEXT-2',
190 'data-wikitext-ij' => 'wikitext-IJ',
191 # other attributes removed
192 ]
193 ],
194 ];
195 }
196
197 /**
198 * @dataProvider provideAttributesAllowed
199 * @covers \MediaWiki\Parser\Sanitizer::attributesAllowedInternal
200 */
201 public function testAttributesAllowedInternal( $element, $attribs ) {
202 $sanitizer = TestingAccessWrapper::newFromClass( Sanitizer::class );
203 $actual = $sanitizer->attributesAllowedInternal( $element );
204 $this->assertArrayEquals( $attribs, array_keys( $actual ) );
205 }
206
207 public static function provideAttributesAllowed() {
208 /** [ <element>, [ <good attribute 1>, <good attribute 2>, ...] ] */
209 return [
210 [ 'math', [ 'class', 'style', 'id', 'title' ] ],
211 [ 'meta', [ 'itemprop', 'content' ] ],
212 [ 'link', [ 'itemprop', 'href', 'title' ] ],
213 ];
214 }
215
216 /**
217 * @dataProvider provideEscapeIdForStuff
218 *
219 * @covers \MediaWiki\Parser\Sanitizer::escapeIdForAttribute()
220 * @covers \MediaWiki\Parser\Sanitizer::escapeIdForLink()
221 * @covers \MediaWiki\Parser\Sanitizer::escapeIdForExternalInterwiki()
222 * @covers \MediaWiki\Parser\Sanitizer::escapeIdInternal()
223 * @covers \MediaWiki\Parser\Sanitizer::escapeIdInternalUrl()
224 *
225 * @param string $stuff
226 * @param string[] $config
227 * @param string $id
228 * @param string|false $expected
229 * @param int|null $mode
230 */
231 public function testEscapeIdForStuff( $stuff, array $config, $id, $expected, $mode = null ) {
232 $func = "Sanitizer::escapeIdFor{$stuff}";
233 $iwFlavor = array_pop( $config );
234 $this->overrideConfigValues( [
235 MainConfigNames::FragmentMode => $config,
236 MainConfigNames::ExternalInterwikiFragmentMode => $iwFlavor,
237 ] );
238 $escaped = $func( $id, $mode );
239 self::assertEquals( $expected, $escaped );
240 }
241
242 public static function provideEscapeIdForStuff() {
243 // Test inputs and outputs
244 $text = 'foo тест_#%!\'()[]:<>&&amp;&amp;amp;%F0';
245 $legacyEncoded = 'foo_.D1.82.D0.B5.D1.81.D1.82_.23.25.21.27.28.29.5B.5D:.3C.3E' .
246 '.26.26amp.3B.26amp.3Bamp.3B.25F0';
247 $html5EncodedId = 'foo_тест_#%!\'()[]:<>&&amp;&amp;amp;%F0';
248 $html5EncodedHref = 'foo_тест_#%!\'()[]:<>&&amp;&amp;amp;%25F0';
249
250 // Settings: last element is $wgExternalInterwikiFragmentMode, the rest is $wgFragmentMode
251 $legacy = [ 'legacy', 'legacy' ];
252 $legacyNew = [ 'legacy', 'html5', 'legacy' ];
253 $newLegacy = [ 'html5', 'legacy', 'legacy' ];
254 $new = [ 'html5', 'legacy' ];
255 $allNew = [ 'html5', 'html5' ];
256
257 return [
258 // Pure legacy: how MW worked before 2017
259 [ 'Attribute', $legacy, $text, $legacyEncoded, Sanitizer::ID_PRIMARY ],
260 [ 'Attribute', $legacy, $text, false, Sanitizer::ID_FALLBACK ],
261 [ 'Link', $legacy, $text, $legacyEncoded ],
262 [ 'ExternalInterwiki', $legacy, $text, $legacyEncoded ],
263
264 // Transition to a new world: legacy links with HTML5 fallback
265 [ 'Attribute', $legacyNew, $text, $legacyEncoded, Sanitizer::ID_PRIMARY ],
266 [ 'Attribute', $legacyNew, $text, $html5EncodedId, Sanitizer::ID_FALLBACK ],
267 [ 'Link', $legacyNew, $text, $legacyEncoded ],
268 [ 'ExternalInterwiki', $legacyNew, $text, $legacyEncoded ],
269
270 // New world: HTML5 links, legacy fallbacks
271 [ 'Attribute', $newLegacy, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ],
272 [ 'Attribute', $newLegacy, $text, $legacyEncoded, Sanitizer::ID_FALLBACK ],
273 [ 'Link', $newLegacy, $text, $html5EncodedHref ],
274 [ 'ExternalInterwiki', $newLegacy, $text, $legacyEncoded ],
275
276 // Distant future: no legacy fallbacks, but still linking to leagacy wikis
277 [ 'Attribute', $new, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ],
278 [ 'Attribute', $new, $text, false, Sanitizer::ID_FALLBACK ],
279 [ 'Link', $new, $text, $html5EncodedHref ],
280 [ 'ExternalInterwiki', $new, $text, $legacyEncoded ],
281
282 // Just before the heat death of universe: external interwikis are also HTML5 \m/
283 [ 'Attribute', $allNew, $text, $html5EncodedId, Sanitizer::ID_PRIMARY ],
284 [ 'Attribute', $allNew, $text, false, Sanitizer::ID_FALLBACK ],
285 [ 'Link', $allNew, $text, $html5EncodedHref ],
286 [ 'ExternalInterwiki', $allNew, $text, $html5EncodedHref ],
287
288 // Whitespace
289 [ 'attribute', $allNew, "foo bar", 'foo_bar', Sanitizer::ID_PRIMARY ],
290 [ 'attribute', $allNew, "foo\fbar", 'foo_bar', Sanitizer::ID_PRIMARY ],
291 [ 'attribute', $allNew, "foo\nbar", 'foo_bar', Sanitizer::ID_PRIMARY ],
292 [ 'attribute', $allNew, "foo\tbar", 'foo_bar', Sanitizer::ID_PRIMARY ],
293 [ 'attribute', $allNew, "foo\rbar", 'foo_bar', Sanitizer::ID_PRIMARY ],
294 ];
295 }
296
297 /**
298 * @covers \MediaWiki\Parser\Sanitizer::escapeIdInternal()
299 */
300 public function testInvalidFragmentThrows() {
301 $this->overrideConfigValue( MainConfigNames::FragmentMode, [ 'boom!' ] );
302 $this->expectException( InvalidArgumentException::class );
303 Sanitizer::escapeIdForAttribute( 'This should throw' );
304 }
305
306 /**
307 * @covers \MediaWiki\Parser\Sanitizer::escapeIdForAttribute()
308 */
309 public function testNoPrimaryFragmentModeThrows() {
310 $this->overrideConfigValue( MainConfigNames::FragmentMode, [ 666 => 'html5' ] );
311 $this->expectException( UnexpectedValueException::class );
312 Sanitizer::escapeIdForAttribute( 'This should throw' );
313 }
314
315 /**
316 * @covers \MediaWiki\Parser\Sanitizer::escapeIdForLink()
317 */
318 public function testNoPrimaryFragmentModeThrows2() {
319 $this->overrideConfigValue( MainConfigNames::FragmentMode, [ 666 => 'html5' ] );
320 $this->expectException( UnexpectedValueException::class );
321 Sanitizer::escapeIdForLink( 'This should throw' );
322 }
323
324 /**
325 * Test escapeIdReferenceListInternal for consistency with escapeIdForAttribute
326 *
327 * @dataProvider provideEscapeIdReferenceListInternal
328 * @covers \MediaWiki\Parser\Sanitizer::escapeIdReferenceListInternal
329 */
330 public function testEscapeIdReferenceListInternal( $referenceList, $id1, $id2 ) {
331 $sanitizer = TestingAccessWrapper::newFromClass( Sanitizer::class );
332 $actual = $sanitizer->escapeIdReferenceListInternal( $referenceList );
333
334 $this->assertEquals(
335 $actual,
336 Sanitizer::escapeIdForAttribute( $id1 )
337 . ' '
338 . Sanitizer::escapeIdForAttribute( $id2 )
339 );
340 }
341
342 public static function provideEscapeIdReferenceListInternal() {
343 /** [ <reference list>, <individual id 1>, <individual id 2> ] */
344 return [
345 [ 'foo bar', 'foo', 'bar' ],
346 [ '#1 #2', '#1', '#2' ],
347 [ '+1 +2', '+1', '+2' ],
348 ];
349 }
350
351 /**
352 * Test cleanUrl
353 *
354 * @dataProvider provideCleanUrl
355 * @covers \MediaWiki\Parser\Sanitizer::cleanUrl
356 */
357 public function testCleanUrl( string $input, string $output ) {
358 $this->assertEquals( $output, Sanitizer::cleanUrl( $input ) );
359 }
360
361 public static function provideCleanUrl() {
362 return [
363 [ 'http://www.example.com/file.txt', 'http://www.example.com/file.txt' ],
364 [
365 "https://www.exa\u{00AD}\u{200B}\u{2060}\u{FEFF}" .
366 "\u{034F}\u{180B}\u{180C}\u{180D}\u{200C}\u{200D}" .
367 "\u{FE00}\u{FE08}\u{FE0F}mple.com",
368 'https://www.example.com'
369 ],
370 ];
371 }
372
373 }