Merge "Mention translatewiki.net on edits only, when edit a default message"
[lhc/web/wiklou.git] / tests / phpunit / includes / upload / UploadBaseTest.php
1 <?php
2
3 /**
4 * @group Upload
5 */
6 class UploadBaseTest extends MediaWikiTestCase {
7
8 /** @var UploadTestHandler */
9 protected $upload;
10
11 protected function setUp() {
12 parent::setUp();
13
14 $this->upload = new UploadTestHandler;
15
16 $this->setMwGlobals( 'wgHooks', array(
17 'InterwikiLoadPrefix' => array(
18 function ( $prefix, &$data ) {
19 return false;
20 }
21 ),
22 ) );
23 }
24
25 /**
26 * First checks the return code
27 * of UploadBase::getTitle() and then the actual returned title
28 *
29 * @dataProvider provideTestTitleValidation
30 * @covers UploadBase::getTitle
31 */
32 public function testTitleValidation( $srcFilename, $dstFilename, $code, $msg ) {
33 /* Check the result code */
34 $this->assertEquals( $code,
35 $this->upload->testTitleValidation( $srcFilename ),
36 "$msg code" );
37
38 /* If we expect a valid title, check the title itself. */
39 if ( $code == UploadBase::OK ) {
40 $this->assertEquals( $dstFilename,
41 $this->upload->getTitle()->getText(),
42 "$msg text" );
43 }
44 }
45
46 /**
47 * Test various forms of valid and invalid titles that can be supplied.
48 */
49 public static function provideTestTitleValidation() {
50 return array(
51 /* Test a valid title */
52 array( 'ValidTitle.jpg', 'ValidTitle.jpg', UploadBase::OK,
53 'upload valid title' ),
54 /* A title with a slash */
55 array( 'A/B.jpg', 'B.jpg', UploadBase::OK,
56 'upload title with slash' ),
57 /* A title with illegal char */
58 array( 'A:B.jpg', 'A-B.jpg', UploadBase::OK,
59 'upload title with colon' ),
60 /* Stripping leading File: prefix */
61 array( 'File:C.jpg', 'C.jpg', UploadBase::OK,
62 'upload title with File prefix' ),
63 /* Test illegal suggested title (r94601) */
64 array( '%281%29.JPG', null, UploadBase::ILLEGAL_FILENAME,
65 'illegal title for upload' ),
66 /* A title without extension */
67 array( 'A', null, UploadBase::FILETYPE_MISSING,
68 'upload title without extension' ),
69 /* A title with no basename */
70 array( '.jpg', null, UploadBase::MIN_LENGTH_PARTNAME,
71 'upload title without basename' ),
72 /* A title that is longer than 255 bytes */
73 array( str_repeat( 'a', 255 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
74 'upload title longer than 255 bytes' ),
75 /* A title that is longer than 240 bytes */
76 array( str_repeat( 'a', 240 ) . '.jpg', null, UploadBase::FILENAME_TOO_LONG,
77 'upload title longer than 240 bytes' ),
78 );
79 }
80
81 /**
82 * Test the upload verification functions
83 * @covers UploadBase::verifyUpload
84 */
85 public function testVerifyUpload() {
86 /* Setup with zero file size */
87 $this->upload->initializePathInfo( '', '', 0 );
88 $result = $this->upload->verifyUpload();
89 $this->assertEquals( UploadBase::EMPTY_FILE,
90 $result['status'],
91 'upload empty file' );
92 }
93
94 // Helper used to create an empty file of size $size.
95 private function createFileOfSize( $size ) {
96 $filename = tempnam( wfTempDir(), "mwuploadtest" );
97
98 $fh = fopen( $filename, 'w' );
99 ftruncate( $fh, $size );
100 fclose( $fh );
101
102 return $filename;
103 }
104
105 /**
106 * test uploading a 100 bytes file with $wgMaxUploadSize = 100
107 *
108 * This method should be abstracted so we can test different settings.
109 */
110 public function testMaxUploadSize() {
111 $this->setMwGlobals( array(
112 'wgMaxUploadSize' => 100,
113 'wgFileExtensions' => array(
114 'txt',
115 ),
116 ) );
117
118 $filename = $this->createFileOfSize( 100 );
119 $this->upload->initializePathInfo( basename( $filename ) . '.txt', $filename, 100 );
120 $result = $this->upload->verifyUpload();
121 unlink( $filename );
122
123 $this->assertEquals(
124 array( 'status' => UploadBase::OK ),
125 $result
126 );
127 }
128
129
130 /**
131 * @dataProvider provideCheckSvgScriptCallback
132 */
133 public function testCheckSvgScriptCallback( $svg, $wellFormed, $filterMatch, $message ) {
134 list( $formed, $match ) = $this->upload->checkSvgString( $svg );
135 $this->assertSame( $wellFormed, $formed, $message );
136 $this->assertSame( $filterMatch, $match, $message );
137 }
138
139 public static function provideCheckSvgScriptCallback() {
140 return array(
141 // html5sec SVG vectors
142 array(
143 '<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>',
144 true,
145 true,
146 'Script tag in svg (http://html5sec.org/#47)'
147 ),
148 array(
149 '<svg xmlns="http://www.w3.org/2000/svg"><g onload="javascript:alert(1)"></g></svg>',
150 true,
151 true,
152 'SVG with onload property (http://html5sec.org/#11)'
153 ),
154 array(
155 '<svg onload="javascript:alert(1)" xmlns="http://www.w3.org/2000/svg"></svg>',
156 true,
157 true,
158 'SVG with onload property (http://html5sec.org/#65)'
159 ),
160 array(
161 '<svg xmlns="http://www.w3.org/2000/svg"> <a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="javascript:alert(1)"><rect width="1000" height="1000" fill="white"/></a> </svg>',
162 true,
163 true,
164 'SVG with javascript xlink (http://html5sec.org/#87)'
165 ),
166 array(
167 '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <animation xlink:href="javascript:alert(1)"/> </svg>',
168 true,
169 true,
170 'SVG with Opera animation xlink (http://html5sec.org/#88 - a)'
171 ),
172 array(
173 '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <animation xlink:href="data:text/xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' onload=\'alert(1)\'%3E%3C/svg%3E"/> </svg>',
174 true,
175 true,
176 'SVG with Opera animation xlink (http://html5sec.org/#88 - b)'
177 ),
178 array(
179 '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <image xlink:href="data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' onload=\'alert(1)\'%3E%3C/svg%3E"/> </svg>',
180 true,
181 true,
182 'SVG with Opera image xlink (http://html5sec.org/#88 - c)'
183 ),
184 array(
185 '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <foreignObject xlink:href="javascript:alert(1)"/> </svg>',
186 true,
187 true,
188 'SVG with Opera foreignObject xlink (http://html5sec.org/#88 - d)'
189 ),
190 array(
191 '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <foreignObject xlink:href="data:text/xml,%3Cscript xmlns=\'http://www.w3.org/1999/xhtml\'%3Ealert(1)%3C/script%3E"/> </svg>',
192 true,
193 true,
194 'SVG with Opera foreignObject xlink (http://html5sec.org/#88 - e)'
195 ),
196 array(
197 '<svg xmlns="http://www.w3.org/2000/svg"> <set attributeName="onmouseover" to="alert(1)"/> </svg>',
198 true,
199 true,
200 'SVG with event handler set (http://html5sec.org/#89 - a)'
201 ),
202 array(
203 '<svg xmlns="http://www.w3.org/2000/svg"> <animate attributeName="onunload" to="alert(1)"/> </svg>',
204 true,
205 true,
206 'SVG with event handler animate (http://html5sec.org/#89 - a)'
207 ),
208 array(
209 '<svg xmlns="http://www.w3.org/2000/svg"> <handler xmlns:ev="http://www.w3.org/2001/xml-events" ev:event="load">alert(1)</handler> </svg>',
210 true,
211 true,
212 'SVG with element handler (http://html5sec.org/#94)'
213 ),
214 array(
215 '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <feImage> <set attributeName="xlink:href" to="data:image/svg+xml;charset=utf-8;base64, PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxzY3JpcHQ%2BYWxlcnQoMSk8L3NjcmlwdD48L3N2Zz4NCg%3D%3D"/> </feImage> </svg>',
216 true,
217 true,
218 'SVG with href to data: url (http://html5sec.org/#95)'
219 ),
220 array(
221 '<svg xmlns="http://www.w3.org/2000/svg" id="foo"> <x xmlns="http://www.w3.org/2001/xml-events" event="load" observer="foo" handler="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%0A%3Chandler%20xml%3Aid%3D%22bar%22%20type%3D%22application%2Fecmascript%22%3E alert(1) %3C%2Fhandler%3E%0A%3C%2Fsvg%3E%0A#bar"/> </svg>',
222 true,
223 true,
224 'SVG with Tiny handler (http://html5sec.org/#104)'
225 ),
226 array(
227 '<svg xmlns="http://www.w3.org/2000/svg"> <a id="x"><rect fill="white" width="1000" height="1000"/></a> <rect fill="white" style="clip-path:url(test3.svg#a);fill:url(#b);filter:url(#c);marker:url(#d);mask:url(#e);stroke:url(#f);"/> </svg>',
228 true,
229 true,
230 'SVG with new CSS styles properties (http://html5sec.org/#109)'
231 ),
232 array(
233 '<svg xmlns="http://www.w3.org/2000/svg"> <a id="x"><rect fill="white" width="1000" height="1000"/></a> <rect clip-path="url(test3.svg#a)" /> </svg>',
234 true,
235 true,
236 'SVG with new CSS styles properties as attributes'
237 ),
238 array(
239 '<svg xmlns="http://www.w3.org/2000/svg"> <a id="x"> <rect fill="white" width="1000" height="1000"/> </a> <rect fill="url(http://html5sec.org/test3.svg#a)" /> </svg>',
240 true,
241 true,
242 'SVG with new CSS styles properties as attributes (2)'
243 ),
244 array(
245 '<svg xmlns="http://www.w3.org/2000/svg"> <path d="M0,0" style="marker-start:url(test4.svg#a)"/> </svg>',
246 true,
247 true,
248 'SVG with path marker-start (http://html5sec.org/#110)'
249 ),
250 array(
251 '<?xml version="1.0"?> <?xml-stylesheet type="text/xml" href="#stylesheet"?> <!DOCTYPE doc [ <!ATTLIST xsl:stylesheet id ID #REQUIRED>]> <svg xmlns="http://www.w3.org/2000/svg"> <xsl:stylesheet id="stylesheet" version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <iframe xmlns="http://www.w3.org/1999/xhtml" src="javascript:alert(1)"></iframe> </xsl:template> </xsl:stylesheet> <circle fill="red" r="40"></circle> </svg>',
252 true,
253 true,
254 'SVG with embedded stylesheet (http://html5sec.org/#125)'
255 ),
256 array(
257 '<svg xmlns="http://www.w3.org/2000/svg" id="x"> <listener event="load" handler="#y" xmlns="http://www.w3.org/2001/xml-events" observer="x"/> <handler id="y">alert(1)</handler> </svg>',
258 true,
259 true,
260 'SVG with handler attribute (http://html5sec.org/#127)'
261 ),
262 array(
263 // Haven't found a browser that accepts this particular example, but we
264 // don't want to allow embeded svgs, ever
265 '<svg> <image style=\'filter:url("data:image/svg+xml;charset=utf-8;base64, PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxzY3JpcHQ/YWxlcnQoMSk8L3NjcmlwdD48L3N2Zz4NCg==")\' /> </svg>',
266 true,
267 true,
268 'SVG with image filter via style (http://html5sec.org/#129)'
269 ),
270 array(
271 // This doesn't seem possible without embedding the svg, but just in case
272 '<svg> <a xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="?"> <circle r="400"></circle> <animate attributeName="xlink:href" begin="0" from="javascript:alert(1)" to="" /> </a></svg>',
273 true,
274 true,
275 'SVG with animate from (http://html5sec.org/#137)'
276 ),
277
278 // Other hostile SVG's
279 array(
280 '<?xml version="1.0" encoding="UTF-8" standalone="no"?> <svg xmlns:xlink="http://www.w3.org/1999/xlink"> <image xlink:href="https://upload.wikimedia.org/wikipedia/commons/3/34/Bahnstrecke_Zeitz-Camburg_1930.png" /> </svg>',
281 true,
282 true,
283 'SVG with non-local image href (bug 65839)'
284 ),
285 array(
286 '<?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="/w/index.php?title=User:Jeeves/test.xsl&amp;action=raw&amp;format=xml" ?> <svg> <height>50</height> <width>100</width> </svg>',
287 true,
288 true,
289 'SVG with remote stylesheet (bug 57550)'
290 ),
291 array(
292 '<svg xmlns="http://www.w3.org/2000/svg" viewbox="-1 -1 15 15"> <rect y="0" height="13" width="12" stroke="#179" rx="1" fill="#2ac"/> <text x="1.5" y="11" font-family="courier" stroke="white" font-size="16"><![CDATA[B]]></text> <iframe xmlns="http://www.w3.org/1999/xhtml" srcdoc="&#x3C;&#x73;&#x63;&#x72;&#x69;&#x70;&#x74;&#x3E;&#x61;&#x6C;&#x65;&#x72;&#x74;&#x28;&#x27;&#x58;&#x53;&#x53;&#x45;&#x44;&#x20;&#x3D;&#x3E;&#x20;&#x44;&#x6F;&#x6D;&#x61;&#x69;&#x6E;&#x28;&#x27;&#x2B;&#x74;&#x6F;&#x70;&#x2E;&#x64;&#x6F;&#x63;&#x75;&#x6D;&#x65;&#x6E;&#x74;&#x2E;&#x64;&#x6F;&#x6D;&#x61;&#x69;&#x6E;&#x2B;&#x27;&#x29;&#x27;&#x29;&#x3B;&#x3C;&#x2F;&#x73;&#x63;&#x72;&#x69;&#x70;&#x74;&#x3E;"></iframe> </svg>',
293 true,
294 true,
295 'SVG with rembeded iframe (bug 60771)'
296 ),
297 array(
298 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="6 3 177 153" xmlns:xlink="http://www.w3.org/1999/xlink"> <style>@import url("https://fonts.googleapis.com/css?family=Bitter:700&amp;text=WebPlatform.org");</style> <g transform="translate(-.5,-.5)"> <text fill="#474747" x="95" y="150" text-anchor="middle" font-family="Bitter" font-size="20" font-weight="bold">WebPlatform.org</text> </g> </svg>',
299 true,
300 true,
301 'SVG with @import in style element (bug 69008)'
302 ),
303 array(
304 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="6 3 177 153" xmlns:xlink="http://www.w3.org/1999/xlink"> <style>@import url("https://fonts.googleapis.com/css?family=Bitter:700&amp;text=WebPlatform.org");<foo/></style> <g transform="translate(-.5,-.5)"> <text fill="#474747" x="95" y="150" text-anchor="middle" font-family="Bitter" font-size="20" font-weight="bold">WebPlatform.org</text> </g> </svg>',
305 true,
306 true,
307 'SVG with @import in style element and child element (bug 69008#c11)'
308 ),
309 array(
310 '<svg xmlns="http://www.w3.org/2000/svg"> <rect width="100" height="100" style="background-image:url(https://www.google.com/images/srpr/logo11w.png)"/> </svg>',
311 true,
312 true,
313 'SVG with remote background image (bug 69008)'
314 ),
315 array(
316 '<svg xmlns="http://www.w3.org/2000/svg"> <rect width="100" height="100" style="background-image:\55rl(https://www.google.com/images/srpr/logo11w.png)"/> </svg>',
317 true,
318 true,
319 'SVG with remote background image, encoded (bug 69008)'
320 ),
321 array(
322 '<svg xmlns="http://www.w3.org/2000/svg"> <style> #a { background-image:\55rl(\'https://www.google.com/images/srpr/logo11w.png\'); } </style> <rect width="100" height="100" id="a"/> </svg>',
323 true,
324 true,
325 'SVG with remote background image, in style element (bug 69008)'
326 ),
327 array(
328 // This currently doesn't seem to work in any browsers, but in case
329 // http://www.w3.org/TR/css3-images/ is implemented for SVG files
330 '<svg xmlns="http://www.w3.org/2000/svg"> <rect width="100" height="100" style="background-image:image(\'sprites.svg#xywh=40,0,20,20\')"/> </svg>',
331 true,
332 true,
333 'SVG with remote background image using image() (bug 69008)'
334 ),
335
336 // Test good, but strange files that we want to allow
337 array(
338 '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <g> <a xlink:href="http://en.wikipedia.org/wiki/Main_Page"> <path transform="translate(0,496)" id="path6706" d="m 112.09375,107.6875 -5.0625,3.625 -4.3125,5.03125 -0.46875,0.5 -4.09375,3.34375 -9.125,5.28125 -8.625,-3.375 z" style="fill:#cccccc;fill-opacity:1;stroke:#6e6e6e;stroke-width:0.69999999;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;display:inline" /> </a> </g> </svg>',
339 true,
340 false,
341 'SVG with <a> link to a remote site'
342 ),
343 array(
344 '<svg> <defs> <filter id="filter6226" x="-0.93243687" width="2.8648737" y="-0.24250539" height="1.4850108"> <feGaussianBlur stdDeviation="3.2344681" id="feGaussianBlur6228" /> </filter> <clipPath id="clipPath2436"> <path d="M 0,0 L 0,0 L 0,0 L 0,0 z" id="path2438" /> </clipPath> </defs> <g clip-path="url(#clipPath2436)" id="g2460"> <text id="text2466"> <tspan>12345</tspan> </text> </g> <path style="fill:#346733;fill-rule:evenodd;stroke:#000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:bevel;stroke-opacity:1;stroke-miterlimit:4;stroke-dasharray:1, 1;stroke-dashoffset:0;filter:url(\'#filter6226\');fill-opacity:1;opacity:0.79807692" d="M 236.82371,332.63732 C 236.92217,332.63732 z" id="path5618" /> </svg>',
345 true,
346 false,
347 'SVG with local urls, including filter: in style'
348 ),
349
350 );
351 }
352 }
353
354 class UploadTestHandler extends UploadBase {
355 public function initializeFromRequest( &$request ) {
356 }
357
358 public function testTitleValidation( $name ) {
359 $this->mTitle = false;
360 $this->mDesiredDestName = $name;
361 $this->mTitleError = UploadBase::OK;
362 $this->getTitle();
363
364 return $this->mTitleError;
365 }
366
367 /**
368 * Almost the same as UploadBase::detectScriptInSvg, except it's
369 * public, works on an xml string instead of filename, and returns
370 * the result instead of interpreting them.
371 */
372 public function checkSvgString( $svg ) {
373 $check = new XmlTypeCheck(
374 $svg,
375 array( $this, 'checkSvgScriptCallback' ),
376 false,
377 array( 'processing_instruction_handler' => 'UploadBase::checkSvgPICallback' )
378 );
379 return array( $check->wellFormed, $check->filterMatch );
380 }
381 }