MediaWiki REL1_28
SanitizerTest.php
Go to the documentation of this file.
1<?php
2
8
9 protected function tearDown() {
11 parent::tearDown();
12 }
13
17 public function testDecodeNamedEntities() {
18 $this->assertEquals(
19 "\xc3\xa9cole",
20 Sanitizer::decodeCharReferences( '&eacute;cole' ),
21 'decode named entities'
22 );
23 }
24
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
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
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
63 public function testInvalidAmpersand() {
64 $this->assertEquals(
65 'a & b',
66 Sanitizer::decodeCharReferences( 'a & b' ),
67 'Invalid ampersand'
68 );
69 }
70
74 public function testInvalidEntities() {
75 $this->assertEquals(
76 '&foo;',
77 Sanitizer::decodeCharReferences( '&foo;' ),
78 'Invalid named entity'
79 );
80 }
81
85 public function testInvalidNumberedEntities() {
86 $this->assertEquals(
87 UtfNormal\Constants::UTF8_REPLACEMENT,
88 Sanitizer::decodeCharReferences( "&#88888888888888;" ),
89 'Invalid numbered entity'
90 );
91 }
92
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
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 [
121 [ 'data', $VERBATIM ],
122 [ 'mark', $VERBATIM ],
123 [ 'time', $VERBATIM ],
124 [ 'video', $ESCAPED ],
125 ];
126 }
127
129 return [
130 // former testSelfClosingTag
131 [
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 [
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 [
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 [
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
162 public function testRemoveHTMLtags( $input, $output, $msg = null ) {
163 MWTidy::setInstance( false );
164 $this->assertEquals( $output, Sanitizer::removeHTMLtags( $input ), $msg );
165 }
166
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 [
180 [ [ 'foo' => 'bar' ], 'foo=bar', 'Unquoted attribute' ],
181 [ [ 'foo' => 'bar' ], ' foo = bar ', 'Spaced attribute' ],
182 [ [ 'foo' => 'bar' ], 'foo="bar"', 'Double-quoted attribute' ],
183 [ [ 'foo' => 'bar' ], 'foo=\'bar\'', 'Single-quoted attribute' ],
184 [
185 [ 'foo' => 'bar', 'baz' => 'foo' ],
186 'foo=\'bar\' baz="foo"',
187 'Several attributes'
188 ],
189 [
190 [ 'foo' => 'bar', 'baz' => 'foo' ],
191 'foo=\'bar\' baz="foo"',
192 'Several attributes'
193 ],
194 [
195 [ 'foo' => 'bar', 'baz' => 'foo' ],
196 'foo=\'bar\' baz="foo"',
197 'Several attributes'
198 ],
199 [ [ ':foo' => 'bar' ], ':foo=\'bar\'', 'Leading :' ],
200 [ [ '_foo' => 'bar' ], '_foo=\'bar\'', 'Leading _' ],
201 [ [ 'foo' => 'bar' ], 'Foo=\'bar\'', 'Leading capital' ],
202 [ [ 'foo' => 'BAR' ], 'FOO=BAR', 'Attribute keys are normalized to lowercase' ],
203
204 # Invalid beginning
205 [ [], '-foo=bar', 'Leading - is forbidden' ],
206 [ [], '.foo=bar', 'Leading . is forbidden' ],
207 [ [ 'foo-bar' => 'bar' ], 'foo-bar=bar', 'A - is allowed inside the attribute' ],
208 [ [ 'foo-' => 'bar' ], 'foo-=bar', 'A - is allowed inside the attribute' ],
209 [ [ 'foo.bar' => 'baz' ], 'foo.bar=baz', 'A . is allowed inside the attribute' ],
210 [ [ 'foo.' => 'baz' ], 'foo.=baz', 'A . is allowed as last character' ],
211 [ [ '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 [ [ '1foo' => 'baz' ], '1foo=baz', 'Leading numbers are allowed' ],
216 [ [], 'foo$=baz', 'Symbols are not allowed' ],
217 [ [], 'foo@=baz', 'Symbols are not allowed' ],
218 [ [], 'foo~=baz', 'Symbols are not allowed' ],
219 [
220 [ 'foo' => '1[#^`*%w/(' ],
221 'foo=1[#^`*%w/(',
222 'All kind of characters are allowed as values'
223 ],
224 [
225 [ 'foo' => '1[#^`*%\'w/(' ],
226 'foo="1[#^`*%\'w/("',
227 'Double quotes are allowed if quoted by single quotes'
228 ],
229 [
230 [ 'foo' => '1[#^`*%"w/(' ],
231 'foo=\'1[#^`*%"w/(\'',
232 'Single quotes are allowed if quoted by double quotes'
233 ],
234 [ [ 'foo' => '&"' ], 'foo=&amp;&quot;', 'Special chars can be provided as entities' ],
235 [ [ 'foo' => '&foobar;' ], 'foo=&foobar;', 'Entity-like items are accepted' ],
236 ];
237 }
238
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() {
252 return [
253 [ 'clear="left"', 'br' ],
254 [ 'clear="all"', 'br' ],
255 [ 'width="100"', 'td' ],
256 [ 'nowrap="true"', 'td' ],
257 [ 'nowrap=""', 'td' ],
258 [ 'align="right"', 'td' ],
259 [ 'align="center"', 'table' ],
260 [ 'align="left"', 'tr' ],
261 [ 'align="center"', 'div' ],
262 [ 'align="left"', 'h1' ],
263 [ 'align="left"', 'p' ],
264 ];
265 }
266
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() {
280 return [
281 // Valid comments spanning entire input
282 [ '/**/', '/**/' ],
283 [ '/* comment */', '/* comment */' ],
284 // Weird stuff
285 [ ' ', '/****/' ],
286 [ ' ', '/* /* */' ],
287 [ 'display: block;', "display:/* foo */block;" ],
288 [ 'display: block;', "display:\\2f\\2a foo \\2a\\2f block;",
289 'Backslash-escaped comments must be stripped (bug 28450)' ],
290 [ '', '/* unfinished comment structure',
291 'Remove anything after a comment-start token' ],
292 [ '', "\\2f\\2a unifinished comment'",
293 'Remove anything after a backslash-escaped comment-start token' ],
294 [
295 '/* insecure input */',
296 'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader'
297 . '(src=\'asdf.png\',sizingMethod=\'scale\');'
298 ],
299 [
300 '/* insecure input */',
301 '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader'
302 . '(src=\'asdf.png\',sizingMethod=\'scale\')";'
303 ],
304 [ '/* insecure input */', 'width: expression(1+1);' ],
305 [ '/* insecure input */', 'background-image: image(asdf.png);' ],
306 [ '/* insecure input */', 'background-image: -webkit-image(asdf.png);' ],
307 [ '/* insecure input */', 'background-image: -moz-image(asdf.png);' ],
308 [ '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);' ],
309 [
310 '/* insecure input */',
311 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);'
312 ],
313 [
314 '/* insecure input */',
315 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);'
316 ],
317 [ '/* insecure input */', 'foo: attr( title, url );' ],
318 [ '/* insecure input */', 'foo: attr( title url );' ],
319 ];
320 }
321
326 public function testEscapeHtmlAllowEntities( $expected, $html ) {
327 $this->assertEquals(
328 $expected,
329 Sanitizer::escapeHtmlAllowEntities( $html )
330 );
331 }
332
333 public static function provideEscapeHtmlAllowEntities() {
334 return [
335 [ 'foo', 'foo' ],
336 [ 'a¡b', 'a&#161;b' ],
337 [ 'foo&#039;bar', "foo'bar" ],
338 [ '&lt;script&gt;foo&lt;/script&gt;', '<script>foo</script>' ],
339 ];
340 }
341
348 public function testEscapeIdReferenceList( $referenceList, $id1, $id2 ) {
349 $this->assertEquals(
350 Sanitizer::escapeIdReferenceList( $referenceList, 'noninitial' ),
351 Sanitizer::escapeId( $id1, 'noninitial' )
352 . ' '
353 . Sanitizer::escapeId( $id2, 'noninitial' )
354 );
355 }
356
357 public static function provideEscapeIdReferenceList() {
359 return [
360 [ 'foo bar', 'foo', 'bar' ],
361 [ '#1 #2', '#1', '#2' ],
362 [ '+1 +2', '+1', '+2' ],
363 ];
364 }
365}
static setInstance( $instance)
Set the driver to be used.
Definition MWTidy.php:153
static destroySingleton()
Destroy the current singleton instance.
Definition MWTidy.php:160
testDecodeMixedEntities()
Sanitizer::decodeCharReferences.
static provideHtml5Tags()
Provide HTML5 tags.
testInvalidEntities()
Sanitizer::decodeCharReferences.
testDecodeTagAttributes( $expected, $attributes, $message='')
provideTagAttributesToDecode Sanitizer::decodeTagAttributes
testRemoveHTMLtags( $input, $output, $msg=null)
dataRemoveHTMLtags Sanitizer::removeHTMLtags
static provideDeprecatedAttributes()
testInvalidAmpersand()
Sanitizer::decodeCharReferences.
testEscapeHtmlAllowEntities( $expected, $html)
provideEscapeHtmlAllowEntities Sanitizer::escapeHtmlAllowEntities
static provideEscapeHtmlAllowEntities()
testDecodeNamedEntities()
Sanitizer::decodeCharReferences.
testDecodeMixedComplexEntities()
Sanitizer::decodeCharReferences.
testInvalidNumberedEntities()
Sanitizer::decodeCharReferences.
testDeprecatedAttributesUnaltered( $inputAttr, $inputEl, $message='')
provideDeprecatedAttributes Sanitizer::fixTagAttributes
static provideCssCommentsFixtures()
static provideEscapeIdReferenceList()
static provideTagAttributesToDecode()
testDecodeNumericEntities()
Sanitizer::decodeCharReferences.
testEscapeIdReferenceList( $referenceList, $id1, $id2)
Test escapeIdReferenceList for consistency with escapeId.
testRemovehtmltagsOnHtml5Tags( $tag, $escaped)
Sanitizer::removeHTMLtags provideHtml5Tags.
testCssCommentsChecking( $expected, $css, $message='')
provideCssCommentsFixtures Sanitizer::checkCss
Unicode normalization routines for working with UTF-8 strings.
Definition UtfNormal.php:48
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object & $output
Definition hooks.txt:1102
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books $tag
Definition hooks.txt:1033
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:1957
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37