Preemptively add image-set to our sanitizer.
[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 $this->setMwGlobals( 'wgCleanupPresentationalAttributes', true );
9
10 AutoLoader::loadClass( 'Sanitizer' );
11 }
12
13 function testDecodeNamedEntities() {
14 $this->assertEquals(
15 "\xc3\xa9cole",
16 Sanitizer::decodeCharReferences( '&eacute;cole' ),
17 'decode named entities'
18 );
19 }
20
21 function testDecodeNumericEntities() {
22 $this->assertEquals(
23 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
24 Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&#233;cole!" ),
25 'decode numeric entities'
26 );
27 }
28
29 function testDecodeMixedEntities() {
30 $this->assertEquals(
31 "\xc4\x88io bonas dans l'\xc3\xa9cole!",
32 Sanitizer::decodeCharReferences( "&#x108;io bonas dans l'&eacute;cole!" ),
33 'decode mixed numeric/named entities'
34 );
35 }
36
37 function testDecodeMixedComplexEntities() {
38 $this->assertEquals(
39 "\xc4\x88io bonas dans l'\xc3\xa9cole! (mais pas &#x108;io dans l'&eacute;cole)",
40 Sanitizer::decodeCharReferences(
41 "&#x108;io bonas dans l'&eacute;cole! (mais pas &amp;#x108;io dans l'&#38;eacute;cole)"
42 ),
43 'decode mixed complex entities'
44 );
45 }
46
47 function testInvalidAmpersand() {
48 $this->assertEquals(
49 'a & b',
50 Sanitizer::decodeCharReferences( 'a & b' ),
51 'Invalid ampersand'
52 );
53 }
54
55 function testInvalidEntities() {
56 $this->assertEquals(
57 '&foo;',
58 Sanitizer::decodeCharReferences( '&foo;' ),
59 'Invalid named entity'
60 );
61 }
62
63 function testInvalidNumberedEntities() {
64 $this->assertEquals( UTF8_REPLACEMENT, Sanitizer::decodeCharReferences( "&#88888888888888;" ), 'Invalid numbered entity' );
65 }
66
67 function testSelfClosingTag() {
68 $GLOBALS['wgUseTidy'] = false;
69 $this->assertEquals(
70 '<div>Hello world</div>',
71 Sanitizer::removeHTMLtags( '<div>Hello world</div />' ),
72 'Self-closing closing div'
73 );
74 }
75
76 function testDecodeTagAttributes() {
77 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=bar' ), array( 'foo' => 'bar' ), 'Unquoted attribute' );
78 $this->assertEquals( Sanitizer::decodeTagAttributes( ' foo = bar ' ), array( 'foo' => 'bar' ), 'Spaced attribute' );
79 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo="bar"' ), array( 'foo' => 'bar' ), 'Double-quoted attribute' );
80 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'bar\'' ), array( 'foo' => 'bar' ), 'Single-quoted attribute' );
81 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
82
83 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
84 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'bar\' baz="foo"' ), array( 'foo' => 'bar', 'baz' => 'foo' ), 'Several attributes' );
85
86 $this->assertEquals( Sanitizer::decodeTagAttributes( ':foo=\'bar\'' ), array( ':foo' => 'bar' ), 'Leading :' );
87 $this->assertEquals( Sanitizer::decodeTagAttributes( '_foo=\'bar\'' ), array( '_foo' => 'bar' ), 'Leading _' );
88 $this->assertEquals( Sanitizer::decodeTagAttributes( 'Foo=\'bar\'' ), array( 'foo' => 'bar' ), 'Leading capital' );
89 $this->assertEquals( Sanitizer::decodeTagAttributes( 'FOO=BAR' ), array( 'foo' => 'BAR' ), 'Attribute keys are normalized to lowercase' );
90
91 # Invalid beginning
92 $this->assertEquals( Sanitizer::decodeTagAttributes( '-foo=bar' ), array(), 'Leading - is forbidden' );
93 $this->assertEquals( Sanitizer::decodeTagAttributes( '.foo=bar' ), array(), 'Leading . is forbidden' );
94 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo-bar=bar' ), array( 'foo-bar' => 'bar' ), 'A - is allowed inside the attribute' );
95 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo-=bar' ), array( 'foo-' => 'bar' ), 'A - is allowed inside the attribute' );
96
97 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo.bar=baz' ), array( 'foo.bar' => 'baz' ), 'A . is allowed inside the attribute' );
98 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo.=baz' ), array( 'foo.' => 'baz' ), 'A . is allowed as last character' );
99
100 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo6=baz' ), array( 'foo6' => 'baz' ), 'Numbers are allowed' );
101
102 # This bit is more relaxed than XML rules, but some extensions use it, like ProofreadPage (see bug 27539)
103 $this->assertEquals( Sanitizer::decodeTagAttributes( '1foo=baz' ), array( '1foo' => 'baz' ), 'Leading numbers are allowed' );
104
105 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo$=baz' ), array(), 'Symbols are not allowed' );
106 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo@=baz' ), array(), 'Symbols are not allowed' );
107 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo~=baz' ), array(), 'Symbols are not allowed' );
108
109
110 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=1[#^`*%w/(' ), array( 'foo' => '1[#^`*%w/(' ), 'All kind of characters are allowed as values' );
111 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo="1[#^`*%\'w/("' ), array( 'foo' => '1[#^`*%\'w/(' ), 'Double quotes are allowed if quoted by single quotes' );
112 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=\'1[#^`*%"w/(\'' ), array( 'foo' => '1[#^`*%"w/(' ), 'Single quotes are allowed if quoted by double quotes' );
113 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=&amp;&quot;' ), array( 'foo' => '&"' ), 'Special chars can be provided as entities' );
114 $this->assertEquals( Sanitizer::decodeTagAttributes( 'foo=&foobar;' ), array( 'foo' => '&foobar;' ), 'Entity-like items are accepted' );
115 }
116
117 /**
118 * @dataProvider provideDeprecatedAttributes
119 */
120 function testDeprecatedAttributes( $input, $tag, $expected, $message = null ) {
121 $this->assertEquals( $expected, Sanitizer::fixTagAttributes( $input, $tag ), $message );
122 }
123
124 function testDeprecatedAttributesDisabled() {
125 global $wgCleanupPresentationalAttributes;
126
127 $wgCleanupPresentationalAttributes = false;
128
129 $this->assertEquals( ' clear="left"', Sanitizer::fixTagAttributes( 'clear="left"', 'br' ), 'Deprecated attributes are not converted to styles when enabled.' );
130 }
131
132 public static function provideDeprecatedAttributes() {
133 return array(
134 array( 'clear="left"', 'br', ' style="clear: left;"', 'Deprecated attributes are converted to styles when enabled.' ),
135 array( 'clear="all"', 'br', ' style="clear: both;"', 'clear=all is converted to clear: both; not clear: all;' ),
136 array( 'CLEAR="ALL"', 'br', ' style="clear: both;"', 'clear=ALL is not treated differently from clear=all' ),
137 array( 'width="100"', 'td', ' style="width: 100px;"', 'Numeric sizes use pixels instead of numbers.' ),
138 array( 'width="100%"', 'td', ' style="width: 100%;"', 'Units are allowed in sizes.' ),
139 array( 'WIDTH="100%"', 'td', ' style="width: 100%;"', 'Uppercase WIDTH is treated as lowercase width.' ),
140 array( 'WiDTh="100%"', 'td', ' style="width: 100%;"', 'Mixed case does not break WiDTh.' ),
141 array( 'nowrap="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute is output as white-space: nowrap; not something else.' ),
142 array( 'nowrap=""', 'td', ' style="white-space: nowrap;"', 'nowrap="" is considered true, not false' ),
143 array( 'NOWRAP="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute works when uppercase.' ),
144 array( 'NoWrAp="true"', 'td', ' style="white-space: nowrap;"', 'nowrap attribute works when mixed-case.' ),
145 array( 'align="right"', 'td', ' style="text-align: right;"' , 'align on table cells gets converted to text-align' ),
146 array( 'align="center"', 'td', ' style="text-align: center;"' , 'align on table cells gets converted to text-align' ),
147 array( 'align="left"' , 'div', ' style="text-align: left;"' , 'align=(left|right) on div elements gets converted to text-align' ),
148 array( 'align="center"', 'div', ' style="text-align: center;"', 'align="center" on div elements gets converted to text-align' ),
149 array( 'align="left"' , 'p', ' style="text-align: left;"' , 'align on p elements gets converted to text-align' ),
150 array( 'align="left"' , 'h1', ' style="text-align: left;"' , 'align on h1 elements gets converted to text-align' ),
151 array( 'align="left"' , 'h1', ' style="text-align: left;"' , 'align on h1 elements gets converted to text-align' ),
152 array( 'align="left"' , 'caption',' style="text-align: left;"','align on caption elements gets converted to text-align' ),
153 array( 'align="left"' , 'tfoot',' style="text-align: left;"' , 'align on tfoot elements gets converted to text-align' ),
154 array( 'align="left"' , 'tbody',' style="text-align: left;"' , 'align on tbody elements gets converted to text-align' ),
155
156 # <tr>
157 array( 'align="right"' , 'tr', ' style="text-align: right;"' , 'align on table row get converted to text-align' ),
158 array( 'align="center"', 'tr', ' style="text-align: center;"', 'align on table row get converted to text-align' ),
159 array( 'align="left"' , 'tr', ' style="text-align: left;"' , 'align on table row get converted to text-align' ),
160
161 #table
162 array( 'align="left"' , 'table', ' style="float: left;"' , 'align on table converted to float' ),
163 array( 'align="center"', 'table', ' style="margin-left: auto; margin-right: auto;"', 'align center on table converted to margins' ),
164 array( 'align="right"' , 'table', ' style="float: right;"' , 'align on table converted to float' ),
165 );
166 }
167
168 /**
169 * @dataProvider provideCssCommentsFixtures
170 */
171 function testCssCommentsChecking( $expected, $css, $message = '' ) {
172 $this->assertEquals(
173 $expected,
174 Sanitizer::checkCss( $css ),
175 $message
176 );
177 }
178
179 public static function provideCssCommentsFixtures() {
180 /** array( <expected>, <css>, [message] ) */
181 return array(
182 array( ' ', '/**/' ),
183 array( ' ', '/****/' ),
184 array( ' ', '/* comment */' ),
185 array( ' ', "\\2f\\2a foo \\2a\\2f",
186 'Backslash-escaped comments must be stripped (bug 28450)' ),
187 array( '', '/* unfinished comment structure',
188 'Remove anything after a comment-start token' ),
189 array( '', "\\2f\\2a unifinished comment'",
190 'Remove anything after a backslash-escaped comment-start token' ),
191 array( '/* insecure input */', 'filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'asdf.png\',sizingMethod=\'scale\');'),
192 array( '/* insecure input */', '-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'asdf.png\',sizingMethod=\'scale\')";'),
193 array( '/* insecure input */', 'width: expression(1+1);'),
194 array( '/* insecure input */', 'background-image: image(asdf.png);'),
195 array( '/* insecure input */', 'background-image: -webkit-image(asdf.png);'),
196 array( '/* insecure input */', 'background-image: -moz-image(asdf.png);'),
197 array( '/* insecure input */', 'background-image: image-set("asdf.png" 1x, "asdf.png" 2x);'),
198 array( '/* insecure input */', 'background-image: -webkit-image-set("asdf.png" 1x, "asdf.png" 2x);'),
199 array( '/* insecure input */', 'background-image: -moz-image-set("asdf.png" 1x, "asdf.png" 2x);'),
200 );
201 }
202 }
203