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