Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / tests / phpunit / includes / SanitizerTest.php
1 <?php
2
3 /**
4 * @todo Tests covering decodeCharReferences can be refactored into a single
5 * method and dataprovider.
6 */
7 class SanitizerTest extends MediaWikiTestCase {
8
9 protected function tearDown() {
10 MWTidy::destroySingleton();
11 parent::tearDown();
12 }
13
14 /**
15 * @covers Sanitizer::decodeCharReferences
16 */
17 public function testDecodeNamedEntities() {
18 $this->assertEquals(
19 "\xc3\xa9cole",
20 Sanitizer::decodeCharReferences( '&eacute;cole' ),
21 'decode named entities'
22 );
23 }
24
25 /**
26 * @covers Sanitizer::decodeCharReferences
27 */
28 public function testDecodeNumericEntities() {
29 $this->assertEquals(
30 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
31 Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&#233;cole!" ),
32 'decode numeric entities'
33 );
34 }
35
36 /**
37 * @covers Sanitizer::decodeCharReferences
38 */
39 public function testDecodeMixedEntities() {
40 $this->assertEquals(
41 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
42 Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&eacute;cole!" ),
43 'decode mixed numeric/named entities'
44 );
45 }
46
47 /**
48 * @covers Sanitizer::decodeCharReferences
49 */
50 public function testDecodeMixedComplexEntities() {
51 $this->assertEquals(
52 "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas &#x108;io dans l'&eacute;cole)",
53 Sanitizer::decodeCharReferences(
54 "&#x108;io bonas dans l'&eacute;cole! (mais pas &amp;#x108;io dans l'&#38;eacute;cole)"
55 ),
56 'decode mixed complex entities'
57 );
58 }
59
60 /**
61 * @covers Sanitizer::decodeCharReferences
62 */
63 public function testInvalidAmpersand() {
64 $this->assertEquals(
65 'a & b',
66 Sanitizer::decodeCharReferences( 'a & b' ),
67 'Invalid ampersand'
68 );
69 }
70
71 /**
72 * @covers Sanitizer::decodeCharReferences
73 */
74 public function testInvalidEntities() {
75 $this->assertEquals(
76 '&foo;',
77 Sanitizer::decodeCharReferences( '&foo;' ),
78 'Invalid named entity'
79 );
80 }
81
82 /**
83 * @covers Sanitizer::decodeCharReferences
84 */
85 public function testInvalidNumberedEntities() {
86 $this->assertEquals(
87 UtfNormal\Constants::UTF8_REPLACEMENT,
88 Sanitizer::decodeCharReferences( "&#88888888888888;" ),
89 'Invalid numbered entity'
90 );
91 }
92
93 /**
94 * @covers Sanitizer::removeHTMLtags
95 * @dataProvider provideHtml5Tags
96 *
97 * @param string $tag Name of an HTML5 element (ie: 'video')
98 * @param bool $escaped Whether sanitizer let the tag in or escape it (ie: '&lt;video&gt;')
99 */
100 public function testRemovehtmltagsOnHtml5Tags( $tag, $escaped ) {
101 MWTidy::setInstance( false );
102
103 if ( $escaped ) {
104 $this->assertEquals( "&lt;$tag&gt;",
105 Sanitizer::removeHTMLtags( "<$tag>" )
106 );
107 } else {
108 $this->assertEquals( "<$tag></$tag>\n",
109 Sanitizer::removeHTMLtags( "<$tag>" )
110 );
111 }
112 }
113
114 /**
115 * Provide HTML5 tags
116 */
117 public static function provideHtml5Tags() {
118 $ESCAPED = true; # We want tag to be escaped
119 $VERBATIM = false; # We want to keep the tag
120 return array(
121 array( 'data', $VERBATIM ),
122 array( 'mark', $VERBATIM ),
123 array( 'time', $VERBATIM ),
124 array( 'video', $ESCAPED ),
125 );
126 }
127
128 function dataRemoveHTMLtags() {
129 return array(
130 // former testSelfClosingTag
131 array(
132 '<div>Hello world</div />',
133 '<div>Hello world</div>',
134 'Self-closing closing div'
135 ),
136 // Make sure special nested HTML5 semantics are not broken
137 // http://www.whatwg.org/html/text-level-semantics.html#the-kbd-element
138 array(
139 '<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
140 '<kbd><kbd>Shift</kbd>+<kbd>F3</kbd></kbd>',
141 'Nested <kbd>.'
142 ),
143 // http://www.whatwg.org/html/text-level-semantics.html#the-sub-and-sup-elements
144 array(
145 '<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
146 '<var>x<sub><var>i</var></sub></var>, <var>y<sub><var>i</var></sub></var>',
147 'Nested <var>.'
148 ),
149 // http://www.whatwg.org/html/text-level-semantics.html#the-dfn-element
150 array(
151 '<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
152 '<dfn><abbr title="Garage Door Opener">GDO</abbr></dfn>',
153 '<abbr> inside <dfn>',
154 ),
155 );
156 }
157
158 /**
159 * @dataProvider dataRemoveHTMLtags
160 * @covers Sanitizer::removeHTMLtags
161 */
162 public function testRemoveHTMLtags( $input, $output, $msg = null ) {
163 MWTidy::setInstance( false );
164 $this->assertEquals( $output, Sanitizer::removeHTMLtags( $input ), $msg );
165 }
166
167 /**
168 * @dataProvider provideTagAttributesToDecode
169 * @covers Sanitizer::decodeTagAttributes
170 */
171 public function testDecodeTagAttributes( $expected, $attributes, $message = '' ) {
172 $this->assertEquals( $expected,
173 Sanitizer::decodeTagAttributes( $attributes ),
174 $message
175 );
176 }
177
178 public static function provideTagAttributesToDecode() {
179 return array(
180 array( array( 'foo' => 'bar' ), 'foo=bar', 'Unquoted attribute' ),
181 array( array( 'foo' => 'bar' ), ' foo = bar ', 'Spaced attribute' ),
182 array( array( 'foo' => 'bar' ), 'foo="bar"', 'Double-quoted attribute' ),
183 array( array( 'foo' => 'bar' ), 'foo=\'bar\'', 'Single-quoted attribute' ),
184 array(
185 array( 'foo' => 'bar', 'baz' => 'foo' ),
186 'foo=\'bar\' baz="foo"',
187 'Several attributes'
188 ),
189 array(
190 array( 'foo' => 'bar', 'baz' => 'foo' ),
191 'foo=\'bar\' baz="foo"',
192 'Several attributes'
193 ),
194 array(
195 array( 'foo' => 'bar', 'baz' => 'foo' ),
196 'foo=\'bar\' baz="foo"',
197 'Several attributes'
198 ),
199 array( array( ':foo' => 'bar' ), ':foo=\'bar\'', 'Leading :' ),
200 array( array( '_foo' => 'bar' ), '_foo=\'bar\'', 'Leading _' ),
201 array( array( 'foo' => 'bar' ), 'Foo=\'bar\'', 'Leading capital' ),
202 array( array( 'foo' => 'BAR' ), 'FOO=BAR', 'Attribute keys are normalized to lowercase' ),
203
204 # Invalid beginning
205 array( array(), '-foo=bar', 'Leading - is forbidden' ),
206 array( array(), '.foo=bar', 'Leading . is forbidden' ),
207 array( array( 'foo-bar' => 'bar' ), 'foo-bar=bar', 'A - is allowed inside the attribute' ),
208 array( array( 'foo-' => 'bar' ), 'foo-=bar', 'A - is allowed inside the attribute' ),
209 array( array( 'foo.bar' => 'baz' ), 'foo.bar=baz', 'A . is allowed inside the attribute' ),
210 array( array( 'foo.' => 'baz' ), 'foo.=baz', 'A . is allowed as last character' ),
211 array( array( 'foo6' => 'baz' ), 'foo6=baz', 'Numbers are allowed' ),
212
213 # This bit is more relaxed than XML rules, but some extensions use
214 # it, like ProofreadPage (see bug 27539)
215 array( array( '1foo' => 'baz' ), '1foo=baz', 'Leading numbers are allowed' ),
216 array( array(), 'foo$=baz', 'Symbols are not allowed' ),
217 array( array(), 'foo@=baz', 'Symbols are not allowed' ),
218 array( array(), 'foo~=baz', 'Symbols are not allowed' ),
219 array(
220 array( 'foo' => '1[#^`*%w/(' ),
221 'foo=1[#^`*%w/(',
222 'All kind of characters are allowed as values'
223 ),
224 array(
225 array( 'foo' => '1[#^`*%\'w/(' ),
226 'foo="1[#^`*%\'w/("',
227 'Double quotes are allowed if quoted by single quotes'
228 ),
229 array(
230 array( 'foo' => '1[#^`*%"w/(' ),
231 'foo=\'1[#^`*%"w/(\'',
232 'Single quotes are allowed if quoted by double quotes'
233 ),
234 array( array( 'foo' => '&"' ), 'foo=&amp;&quot;', 'Special chars can be provided as entities' ),
235 array( array( 'foo' => '&foobar;' ), 'foo=&foobar;', 'Entity-like items are accepted' ),
236 );
237 }
238
239 /**
240 * @dataProvider provideDeprecatedAttributes
241 * @covers Sanitizer::fixTagAttributes
242 */
243 public function testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message = '' ) {
244 $this->assertEquals( " $inputAttr",
245 Sanitizer::fixTagAttributes( $inputAttr, $inputEl ),
246 $message
247 );
248 }
249
250 public static function provideDeprecatedAttributes() {
251 /** array( <attribute>, <element>, [message] ) */
252 return array(
253 array( 'clear="left"', 'br' ),
254 array( 'clear="all"', 'br' ),
255 array( 'width="100"', 'td' ),
256 array( 'nowrap="true"', 'td' ),
257 array( 'nowrap=""', 'td' ),
258 array( 'align="right"', 'td' ),
259 array( 'align="center"', 'table' ),
260 array( 'align="left"', 'tr' ),
261 array( 'align="center"', 'div' ),
262 array( 'align="left"', 'h1' ),
263 array( 'align="left"', 'p' ),
264 );
265 }
266
267 /**
268 * @dataProvider provideCssCommentsFixtures
269 * @covers Sanitizer::checkCss
270 */
271 public function testCssCommentsChecking( $expected, $css, $message = '' ) {
272 $this->assertEquals( $expected,
273 Sanitizer::checkCss( $css ),
274 $message
275 );
276 }
277
278 public static function provideCssCommentsFixtures() {
279 /** array( <expected>, <css>, [message] ) */
280 return array(
281 // Valid comments spanning entire input
282 array( '/**/', '/**/' ),
283 array( '/* comment */', '/* comment */' ),
284 // Weird stuff
285 array( ' ', '/****/' ),
286 array( ' ', '/* /* */' ),
287 array( 'display: block;', "display:/* foo */block;" ),
288 array( 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;",
289 'Backslash-escaped comments must be stripped (bug 28450)' ),
290 array( '', '/* unfinished comment structure',
291 'Remove anything after a comment-start token' ),
292 array( '', "\\2f\\2a unifinished comment'",
293 'Remove anything after a backslash-escaped comment-start token' ),
294 array(
295 '/* insecure input */',
296 'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader'
297 . '(src=\'asdf.png\',sizingMethod=\'scale\');'
298 ),
299 array(
300 '/* insecure input */',
301 '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader'
302 . '(src=\'asdf.png\',sizingMethod=\'scale\')";'
303 ),
304 array( '/* insecure input */', 'width: expression(1+1);' ),
305 array( '/* insecure input */', 'background-image: image(asdf.png);' ),
306 array( '/* insecure input */', 'background-image: -webkit-image(asdf.png);' ),
307 array( '/* insecure input */', 'background-image: -moz-image(asdf.png);' ),
308 array( '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);' ),
309 array(
310 '/* insecure input */',
311 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);'
312 ),
313 array(
314 '/* insecure input */',
315 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);'
316 ),
317 );
318 }
319
320 /**
321 * Test for support or lack of support for specific attributes in the attribute whitelist.
322 */
323 public static function provideAttributeSupport() {
324 /** array( <attributes>, <expected>, <message> ) */
325 return array(
326 array(
327 'div',
328 ' role="presentation"',
329 ' role="presentation"',
330 'Support for WAI-ARIA\'s role="presentation".'
331 ),
332 array( 'div', ' role="main"', '', "Other WAI-ARIA roles are currently not supported." ),
333 );
334 }
335
336 /**
337 * @dataProvider provideAttributeSupport
338 * @covers Sanitizer::fixTagAttributes
339 */
340 public function testAttributeSupport( $tag, $attributes, $expected, $message ) {
341 $this->assertEquals( $expected,
342 Sanitizer::fixTagAttributes( $attributes, $tag ),
343 $message
344 );
345 }
346
347 /**
348 * @dataProvider provideEscapeHtmlAllowEntities
349 * @covers Sanitizer::escapeHtmlAllowEntities
350 */
351 public function testEscapeHtmlAllowEntities( $expected, $html ) {
352 $this->assertEquals(
353 $expected,
354 Sanitizer::escapeHtmlAllowEntities( $html )
355 );
356 }
357
358 public static function provideEscapeHtmlAllowEntities() {
359 return array(
360 array( 'foo', 'foo' ),
361 array( 'a¡b', 'a&#161;b' ),
362 array( 'foo&#039;bar', "foo'bar" ),
363 array( '&lt;script&gt;foo&lt;/script&gt;', '<script>foo</script>' ),
364 );
365 }
366 }