@@ -349,7 +349,21 @@ function mainUploadForm( $msg )
349349 </td></tr></table></form> \n" );
350350 }
351351
352+ /**
353+ * Returns false if the file is of a known type but can't be recognized,
354+ * indicating a corrupt file.
355+ * Returns true otherwise; unknown file types are not checked if given
356+ * with an unrecognized extension.
357+ *
358+ * @param string $tmpfile Pathname to the temporary upload file
359+ * @param string $extension The filename extension that the file is to be served with
360+ * @return bool
361+ */
352362 function verify ( $ tmpfile , $ extension ) {
363+ if ( $ this ->triggersIEbug ( $ tmpfile ) ) {
364+ return false ;
365+ }
366+
353367 $ fname = 'SpecialUpload::verify ' ;
354368 $ mergeExtensions = array (
355369 'jpg ' => 'jpeg ' ,
@@ -418,5 +432,30 @@ function verify( $tmpfile, $extension ) {
418432 wfDebug ( "$ fname: all clear; passing. \n" );
419433 return true ;
420434 }
435+
436+ /**
437+ * Internet Explorer for Windows performs some really stupid file type
438+ * autodetection which can cause it to interpret valid image files as HTML
439+ * and potentially execute JavaScript, creating a cross-site scripting
440+ * attack vectors.
441+ *
442+ * Returns true if IE is likely to mistake the given file for HTML.
443+ *
444+ * @param string $filename
445+ * @return bool
446+ */
447+ function triggersIEbug ( $ filename ) {
448+ $ file = fopen ( $ filename , 'rb ' );
449+ $ chunk = strtolower ( fread ( $ file , 200 ) );
450+ fclose ( $ file );
451+
452+ $ tags = array ( '<html ' , '<head ' , '<body ' , '<script ' );
453+ foreach ( $ tags as $ tag ) {
454+ if ( false !== strpos ( $ chunk , $ tag ) ) {
455+ return true ;
456+ }
457+ }
458+ return false ;
459+ }
421460}
422461?>
0 commit comments