39 abstract class UploadBase {
43 protected $tempFileObj;
45 protected $mDesiredDestName, $mDestName, $mRemoveTempFile, $mSourceType;
46 protected $mTitle =
false, $mTitleError = 0;
47 protected $mFilteredName, $mFinalExtension;
48 protected $mLocalFile, $mStashFile, $mFileSize, $mFileProps;
49 protected $mBlackListedExtensions;
50 protected $mJavaDetected, $mSVGNSError;
52 protected static $safeXmlEncodings = [
72 const MIN_LENGTH_PARTNAME = 4;
73 const ILLEGAL_FILENAME = 5;
74 const OVERWRITE_EXISTING_FILE = 7; # Not
used anymore; handled
by verifyTitlePermissions()
75 const FILETYPE_MISSING = 8;
76 const FILETYPE_BADTYPE = 9;
77 const VERIFICATION_ERROR = 10;
78 const HOOK_ABORTED = 11;
79 const FILE_TOO_LARGE = 12;
80 const WINDOWS_NONASCII_FILENAME = 13;
81 const FILENAME_TOO_LONG = 14;
89 self::EMPTY_FILE =>
'empty-file',
90 self::FILE_TOO_LARGE =>
'file-too-large',
91 self::FILETYPE_MISSING =>
'filetype-missing',
92 self::FILETYPE_BADTYPE =>
'filetype-banned',
93 self::MIN_LENGTH_PARTNAME =>
'filename-tooshort',
94 self::ILLEGAL_FILENAME =>
'illegal-filename',
95 self::OVERWRITE_EXISTING_FILE =>
'overwrite',
96 self::VERIFICATION_ERROR =>
'verification-error',
97 self::HOOK_ABORTED =>
'hookaborted',
98 self::WINDOWS_NONASCII_FILENAME =>
'windows-nonascii-filename',
99 self::FILENAME_TOO_LONG =>
'filename-toolong',
101 if ( isset( $code_to_status[$error] ) ) {
102 return $code_to_status[$error];
105 return 'unknown-error';
113 public static function isEnabled() {
120 # Check php's file_uploads setting
132 public static function isAllowed(
$user ) {
133 foreach ( [
'upload',
'edit' ]
as $permission ) {
134 if ( !
$user->isAllowed( $permission ) ) {
148 public static function isThrottled(
$user ) {
149 return $user->pingLimiter(
'upload' );
153 private static $uploadHandlers = [
'Stash',
'File',
'Url' ];
162 public static function createFromRequest( &
$request,
$type =
null ) {
175 if ( is_null( $className ) ) {
176 $className =
'UploadFrom' .
$type;
177 wfDebug( __METHOD__ .
": class name: $className\n" );
178 if ( !in_array(
$type, self::$uploadHandlers ) ) {
184 if ( !call_user_func( [ $className,
'isEnabled' ] ) ) {
189 if ( !call_user_func( [ $className,
'isValidRequest' ],
$request ) ) {
206 public static function isValidRequest(
$request ) {
210 public function __construct() {
219 public function getSourceType() {
231 public function initializePathInfo(
$name, $tempPath, $fileSize, $removeTempFile =
false ) {
232 $this->mDesiredDestName =
$name;
234 throw new MWException( __METHOD__ .
" given storage path `$tempPath`." );
237 $this->setTempFile( $tempPath, $fileSize );
238 $this->mRemoveTempFile = $removeTempFile;
246 abstract public function initializeFromRequest( &
$request );
252 protected function setTempFile( $tempPath, $fileSize =
null ) {
253 $this->mTempPath = $tempPath;
254 $this->mFileSize = $fileSize ?:
null;
255 if ( strlen( $this->mTempPath ) && file_exists( $this->mTempPath ) ) {
256 $this->tempFileObj =
new TempFSFile( $this->mTempPath );
258 $this->mFileSize = filesize( $this->mTempPath );
261 $this->tempFileObj =
null;
269 public function fetchFile() {
277 public function isEmptyFile() {
278 return empty( $this->mFileSize );
285 public function getFileSize() {
286 return $this->mFileSize;
293 public function getTempFileSha1Base36() {
301 public function getRealPath( $srcPath ) {
303 if ( $repo->isVirtualUrl( $srcPath ) ) {
307 $tmpFile = $repo->getLocalCopy( $srcPath );
309 $tmpFile->bind( $this );
311 $path = $tmpFile ? $tmpFile->getPath() :
false;
323 public function verifyUpload() {
327 if ( $this->isEmptyFile() ) {
328 return [
'status' => self::EMPTY_FILE ];
334 $maxSize = self::getMaxUploadSize( $this->getSourceType() );
335 if ( $this->mFileSize > $maxSize ) {
337 'status' => self::FILE_TOO_LARGE,
347 $verification = $this->verifyFile();
348 if ( $verification !==
true ) {
350 'status' => self::VERIFICATION_ERROR,
351 'details' => $verification
358 $result = $this->validateName();
365 [ $this->mDestName, $this->mTempPath, &$error ],
'1.28' )
367 return [
'status' => self::HOOK_ABORTED,
'error' => $error ];
370 return [
'status' => self::OK ];
379 public function validateName() {
380 $nt = $this->getTitle();
381 if ( is_null( $nt ) ) {
382 $result = [
'status' => $this->mTitleError ];
383 if ( $this->mTitleError == self::ILLEGAL_FILENAME ) {
384 $result[
'filtered'] = $this->mFilteredName;
386 if ( $this->mTitleError == self::FILETYPE_BADTYPE ) {
387 $result[
'finalExt'] = $this->mFinalExtension;
388 if (
count( $this->mBlackListedExtensions ) ) {
389 $result[
'blacklistedExt'] = $this->mBlackListedExtensions;
395 $this->mDestName = $this->getLocalFile()->
getName();
409 protected function verifyMimeType(
$mime ) {
412 wfDebug(
"mime: <$mime> extension: <{$this->mFinalExtension}>\n" );
415 return [
'filetype-badmime',
$mime ];
418 # Check what Internet Explorer would detect
419 $fp = fopen( $this->mTempPath,
'rb' );
420 $chunk = fread( $fp, 256 );
424 $extMime = $magic->guessTypesForExtension( $this->mFinalExtension );
425 $ieTypes = $magic->getIEMimeTypes( $this->mTempPath, $chunk, $extMime );
426 foreach ( $ieTypes
as $ieType ) {
428 return [
'filetype-bad-ie-mime', $ieType ];
441 protected function verifyFile() {
444 $status = $this->verifyPartialFile();
450 $this->mFileProps = $mwProps->getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
451 $mime = $this->mFileProps[
'mime'];
454 # XXX: Missing extension will be caught by validateName() via getTitle()
455 if ( $this->mFinalExtension !=
'' && !$this->verifyExtension(
$mime, $this->mFinalExtension ) ) {
456 return [
'filetype-mime-mismatch', $this->mFinalExtension,
$mime ];
460 # check for htmlish code and javascript
462 if ( $this->mFinalExtension ==
'svg' ||
$mime ==
'image/svg+xml' ) {
463 $svgStatus = $this->detectScriptInSvg( $this->mTempPath,
false );
464 if ( $svgStatus !==
false ) {
472 $handlerStatus =
$handler->verifyUpload( $this->mTempPath );
473 if ( !$handlerStatus->isOK() ) {
474 $errors = $handlerStatus->getErrorsArray();
476 return reset( $errors );
482 if ( $error !==
true ) {
483 if ( !is_array( $error ) ) {
489 wfDebug( __METHOD__ .
": all clear; passing.\n" );
502 protected function verifyPartialFile() {
505 # getTitle() sets some internal parameters like $this->mFinalExtension
509 $this->mFileProps = $mwProps->getPropsFromPath( $this->mTempPath, $this->mFinalExtension );
511 # check MIME type, if desired
512 $mime = $this->mFileProps[
'file-mime'];
518 # check for htmlish code and javascript
520 if ( self::detectScript( $this->mTempPath,
$mime, $this->mFinalExtension ) ) {
521 return [
'uploadscripted' ];
523 if ( $this->mFinalExtension ==
'svg' ||
$mime ==
'image/svg+xml' ) {
524 $svgStatus = $this->detectScriptInSvg( $this->mTempPath,
true );
525 if ( $svgStatus !==
false ) {
531 # Check for Java applets, which if uploaded can bypass cross-site
534 $this->mJavaDetected =
false;
536 [ $this,
'zipEntryCallback' ] );
537 if ( !$zipStatus->isOK() ) {
538 $errors = $zipStatus->getErrorsArray();
539 $error = reset( $errors );
540 if ( $error[0] !==
'zip-wrong-format' ) {
544 if ( $this->mJavaDetected ) {
545 return [
'uploadjava' ];
549 # Scan the uploaded file for viruses
550 $virus = $this->detectVirus( $this->mTempPath );
552 return [
'uploadvirus', $virus ];
563 public function zipEntryCallback( $entry ) {
564 $names = [ $entry[
'name'] ];
571 $nullPos = strpos( $entry[
'name'],
"\000" );
572 if ( $nullPos !==
false ) {
573 $names[] = substr( $entry[
'name'], 0, $nullPos );
578 if ( preg_grep(
'!\.class/?$!', $names ) ) {
579 $this->mJavaDetected =
true;
592 public function verifyPermissions(
$user ) {
593 return $this->verifyTitlePermissions(
$user );
607 public function verifyTitlePermissions(
$user ) {
612 $nt = $this->getTitle();
613 if ( is_null( $nt ) ) {
616 $permErrors = $nt->getUserPermissionsErrors(
'edit',
$user );
617 $permErrorsUpload = $nt->getUserPermissionsErrors(
'upload',
$user );
618 if ( !$nt->exists() ) {
619 $permErrorsCreate = $nt->getUserPermissionsErrors(
'create',
$user );
621 $permErrorsCreate = [];
623 if ( $permErrors || $permErrorsUpload || $permErrorsCreate ) {
624 $permErrors = array_merge( $permErrors,
wfArrayDiff2( $permErrorsUpload, $permErrors ) );
625 $permErrors = array_merge( $permErrors,
wfArrayDiff2( $permErrorsCreate, $permErrors ) );
630 $overwriteError = $this->checkOverwrite(
$user );
631 if ( $overwriteError !==
true ) {
632 return [ $overwriteError ];
645 public function checkWarnings() {
648 $localFile = $this->getLocalFile();
649 $localFile->
load( File::READ_LATEST );
650 $filename = $localFile->
getName();
651 $hash = $this->getTempFileSha1Base36();
653 $badFileName = $this->checkBadFileName( $filename, $this->mDesiredDestName );
654 if ( $badFileName !==
null ) {
655 $warnings[
'badfilename'] = $badFileName;
658 $unwantedFileExtensionDetails = $this->checkUnwantedFileExtensions( $this->mFinalExtension );
659 if ( $unwantedFileExtensionDetails !==
null ) {
660 $warnings[
'filetype-unwanted-type'] = $unwantedFileExtensionDetails;
663 $fileSizeWarnings = $this->checkFileSize( $this->mFileSize );
664 if ( $fileSizeWarnings ) {
665 $warnings = array_merge( $warnings, $fileSizeWarnings );
668 $localFileExistsWarnings = $this->checkLocalFileExists( $localFile, $hash );
669 if ( $localFileExistsWarnings ) {
670 $warnings = array_merge( $warnings, $localFileExistsWarnings );
673 if ( $this->checkLocalFileWasDeleted( $localFile ) ) {
674 $warnings[
'was-deleted'] = $filename;
677 $dupes = $this->checkAgainstExistingDupes( $hash );
679 $warnings[
'duplicate'] = $dupes;
682 $archivedDupes = $this->checkAgainstArchiveDupes( $hash );
683 if ( $archivedDupes !==
null ) {
684 $warnings[
'duplicate-archive'] = $archivedDupes;
699 private function checkBadFileName( $filename, $desiredFileName ) {
700 $comparableName = str_replace(
' ',
'_', $desiredFileName );
703 if ( $desiredFileName != $filename && $comparableName != $filename ) {
718 private function checkUnwantedFileExtensions( $fileExtension ) {
723 if ( !$this->checkFileExtension( $fileExtension, $extensions ) ) {
726 $wgLang->commaList( $extensions ),
740 private function checkFileSize( $fileSize ) {
749 if ( $fileSize == 0 ) {
750 $warnings[
'empty-file'] =
true;
762 private function checkLocalFileExists(
LocalFile $localFile, $hash ) {
765 $exists = self::getExistsWarning( $localFile );
766 if ( $exists !==
false ) {
767 $warnings[
'exists'] = $exists;
770 if ( $hash === $localFile->
getSha1() ) {
771 $warnings[
'no-change'] = $localFile;
776 foreach ( $history
as $oldFile ) {
777 if ( $hash === $oldFile->getSha1() ) {
778 $warnings[
'duplicate-version'][] = $oldFile;
786 private function checkLocalFileWasDeleted(
LocalFile $localFile ) {
795 private function checkAgainstExistingDupes( $hash ) {
797 $title = $this->getTitle();
799 foreach ( $dupes
as $key => $dupe ) {
800 if (
$title->equals( $dupe->getTitle() ) ) {
801 unset( $dupes[$key] );
814 private function checkAgainstArchiveDupes( $hash ) {
816 if ( $archivedFile->getID() > 0 ) {
818 return $archivedFile->getName();
840 public function performUpload( $comment, $pageText, $watch,
$user, $tags = [] ) {
841 $this->getLocalFile()->load( File::READ_LATEST );
842 $props = $this->mFileProps;
845 Hooks::run(
'UploadVerifyUpload', [ $this,
$user, $props, $comment, $pageText, &$error ] );
847 if ( !is_array( $error ) ) {
850 return call_user_func_array(
'Status::newFatal', $error );
853 $status = $this->getLocalFile()->upload(
867 $this->getLocalFile()->getTitle(),
874 Hooks::run(
'UploadComplete', [ &$uploadBase ] );
876 $this->postProcessUpload();
887 public function postProcessUpload() {
896 public function getTitle() {
897 if ( $this->mTitle !==
false ) {
898 return $this->mTitle;
900 if ( !is_string( $this->mDesiredDestName ) ) {
901 $this->mTitleError = self::ILLEGAL_FILENAME;
902 $this->mTitle =
null;
904 return $this->mTitle;
911 $this->mFilteredName =
$title->getDBkey();
913 $this->mFilteredName = $this->mDesiredDestName;
916 # oi_archive_name is max 255 bytes, which include a timestamp and an
917 # exclamation mark, so restrict file name to 240 bytes.
918 if ( strlen( $this->mFilteredName ) > 240 ) {
919 $this->mTitleError = self::FILENAME_TOO_LONG;
920 $this->mTitle =
null;
922 return $this->mTitle;
933 if ( is_null( $nt ) ) {
934 $this->mTitleError = self::ILLEGAL_FILENAME;
935 $this->mTitle =
null;
937 return $this->mTitle;
939 $this->mFilteredName = $nt->getDBkey();
945 list( $partname,
$ext ) = $this->splitExtensions( $this->mFilteredName );
950 $this->mFinalExtension =
'';
952 # No extension, try guessing one
954 $mime = $magic->guessMimeType( $this->mTempPath );
955 if (
$mime !==
'unknown/unknown' ) {
956 # Get a space separated list of extensions
957 $extList = $magic->getExtensionsForType(
$mime );
959 # Set the extension to the canonical extension
960 $this->mFinalExtension = strtok( $extList,
' ' );
962 # Fix up the other variables
963 $this->mFilteredName .=
".{$this->mFinalExtension}";
965 $ext = [ $this->mFinalExtension ];
976 if ( $this->mFinalExtension ==
'' ) {
977 $this->mTitleError = self::FILETYPE_MISSING;
978 $this->mTitle =
null;
980 return $this->mTitle;
981 } elseif ( $blackListedExtensions ||
985 $this->mBlackListedExtensions = $blackListedExtensions;
986 $this->mTitleError = self::FILETYPE_BADTYPE;
987 $this->mTitle =
null;
989 return $this->mTitle;
993 if ( !preg_match(
'/^[\x0-\x7f]*$/', $nt->getText() )
996 $this->mTitleError = self::WINDOWS_NONASCII_FILENAME;
997 $this->mTitle =
null;
999 return $this->mTitle;
1002 # If there was more than one "extension", reassemble the base
1003 # filename to prevent bogus complaints about length
1006 for ( $i = 0; $i < $iterations; $i++ ) {
1007 $partname .=
'.' .
$ext[$i];
1011 if ( strlen( $partname ) < 1 ) {
1012 $this->mTitleError = self::MIN_LENGTH_PARTNAME;
1013 $this->mTitle =
null;
1015 return $this->mTitle;
1018 $this->mTitle = $nt;
1020 return $this->mTitle;
1028 public function getLocalFile() {
1029 if ( is_null( $this->mLocalFile ) ) {
1030 $nt = $this->getTitle();
1031 $this->mLocalFile = is_null( $nt ) ? null :
wfLocalFile( $nt );
1034 return $this->mLocalFile;
1040 public function getStashFile() {
1041 return $this->mStashFile;
1055 public function tryStashFile(
User $user, $isPartial =
false ) {
1056 if ( !$isPartial ) {
1057 $error = $this->runUploadStashFileHook(
$user );
1059 return call_user_func_array(
'Status::newFatal', $error );
1063 $file = $this->doStashFile(
$user );
1074 protected function runUploadStashFileHook(
User $user ) {
1075 $props = $this->mFileProps;
1079 if ( !is_array( $error ) ) {
1080 $error = [ $error ];
1105 public function stashFile(
User $user =
null ) {
1106 return $this->doStashFile(
$user );
1115 protected function doStashFile(
User $user =
null ) {
1117 $file = $stash->stashFile( $this->mTempPath, $this->getSourceType() );
1118 $this->mStashFile = $file;
1130 public function stashFileGetKey() {
1132 return $this->doStashFile()->getFileKey();
1141 public function stashSession() {
1143 return $this->doStashFile()->getFileKey();
1150 public function cleanupTempFile() {
1151 if ( $this->mRemoveTempFile && $this->tempFileObj ) {
1153 wfDebug( __METHOD__ .
": Marked temporary file '{$this->mTempPath}' for removal\n" );
1154 $this->tempFileObj->autocollect();
1158 public function getTempPath() {
1159 return $this->mTempPath;
1171 public static function splitExtensions( $filename ) {
1172 $bits = explode(
'.', $filename );
1173 $basename = array_shift( $bits );
1175 return [ $basename, $bits ];
1186 public static function checkFileExtension(
$ext, $list ) {
1187 return in_array( strtolower(
$ext ), $list );
1198 public static function checkFileExtensionList(
$ext, $list ) {
1199 return array_intersect( array_map(
'strtolower',
$ext ), $list );
1209 public static function verifyExtension(
$mime, $extension ) {
1213 if ( !$magic->isRecognizableExtension( $extension ) ) {
1214 wfDebug( __METHOD__ .
": passing file with unknown detected mime type; " .
1215 "unrecognized extension '$extension', can't verify\n" );
1219 wfDebug( __METHOD__ .
": rejecting file with unknown detected mime type; " .
1220 "recognized extension '$extension', so probably invalid file\n" );
1226 $match = $magic->isMatchingExtension( $extension,
$mime );
1228 if ( $match ===
null ) {
1229 if ( $magic->getTypesForExtension( $extension ) !== null ) {
1230 wfDebug( __METHOD__ .
": No extension known for $mime, but we know a mime for $extension\n" );
1234 wfDebug( __METHOD__ .
": no file extension known for mime type $mime, passing file\n" );
1238 } elseif ( $match ===
true ) {
1239 wfDebug( __METHOD__ .
": mime type $mime matches extension $extension, passing file\n" );
1245 .
": mime type $mime mismatches file extension $extension, rejecting file\n" );
1262 public static function detectScript( $file,
$mime, $extension ) {
1265 # ugly hack: for text files, always look at the entire file.
1266 # For binary field, just check the first K.
1268 if ( strpos(
$mime,
'text/' ) === 0 ) {
1269 $chunk = file_get_contents( $file );
1271 $fp = fopen( $file,
'rb' );
1272 $chunk = fread( $fp, 1024 );
1276 $chunk = strtolower( $chunk );
1282 # decode from UTF-16 if needed (could be used for obfuscation).
1283 if ( substr( $chunk, 0, 2 ) ==
"\xfe\xff" ) {
1285 } elseif ( substr( $chunk, 0, 2 ) ==
"\xff\xfe" ) {
1292 $chunk = iconv( $enc,
"ASCII//IGNORE", $chunk );
1295 $chunk = trim( $chunk );
1298 wfDebug( __METHOD__ .
": checking for embedded scripts and HTML stuff\n" );
1300 # check for HTML doctype
1301 if ( preg_match(
"/<!DOCTYPE *X?HTML/i", $chunk ) ) {
1307 if ( $extension ==
'svg' || strpos(
$mime,
'image/svg' ) === 0 ) {
1308 if ( self::checkXMLEncodingMissmatch( $file ) ) {
1332 '<html', # also
in safari
1335 '<script', # also
in safari
1343 foreach ( $tags
as $tag ) {
1344 if (
false !== strpos( $chunk, $tag ) ) {
1345 wfDebug( __METHOD__ .
": found something that may make it be mistaken for html: $tag\n" );
1355 # resolve entity-refs to look at attributes. may be harsh on big files... cache result?
1356 $chunk = Sanitizer::decodeCharReferences( $chunk );
1358 # look for script-types
1359 if ( preg_match(
'!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) ) {
1360 wfDebug( __METHOD__ .
": found script types\n" );
1365 # look for html-style script-urls
1366 if ( preg_match(
'!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
1367 wfDebug( __METHOD__ .
": found html-style script urls\n" );
1372 # look for css-style script-urls
1373 if ( preg_match(
'!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
1374 wfDebug( __METHOD__ .
": found css-style script urls\n" );
1379 wfDebug( __METHOD__ .
": no scripts found\n" );
1391 public static function checkXMLEncodingMissmatch( $file ) {
1394 $encodingRegex =
'!encoding[ \t\n\r]*=[ \t\n\r]*[\'"](.*?)[\'"]!si';
1396 if ( preg_match(
"!<\?xml\b(.*?)\?>!si", $contents,
$matches ) ) {
1397 if ( preg_match( $encodingRegex,
$matches[1], $encMatch )
1398 && !in_array( strtoupper( $encMatch[1] ), self::$safeXmlEncodings )
1400 wfDebug( __METHOD__ .
": Found unsafe XML encoding '{$encMatch[1]}'\n" );
1404 } elseif ( preg_match(
"!<\?xml\b!si", $contents ) ) {
1407 wfDebug( __METHOD__ .
": Unmatched XML declaration start\n" );
1410 } elseif ( substr( $contents, 0, 4 ) ==
"\x4C\x6F\xA7\x94" ) {
1412 wfDebug( __METHOD__ .
": EBCDIC Encoded XML\n" );
1419 $attemptEncodings = [
'UTF-16',
'UTF-16BE',
'UTF-32',
'UTF-32BE' ];
1420 foreach ( $attemptEncodings
as $encoding ) {
1421 MediaWiki\suppressWarnings();
1422 $str = iconv( $encoding,
'UTF-8', $contents );
1423 MediaWiki\restoreWarnings();
1424 if ( $str !=
'' && preg_match(
"!<\?xml\b(.*?)\?>!si", $str,
$matches ) ) {
1425 if ( preg_match( $encodingRegex,
$matches[1], $encMatch )
1426 && !in_array( strtoupper( $encMatch[1] ), self::$safeXmlEncodings )
1428 wfDebug( __METHOD__ .
": Found unsafe XML encoding '{$encMatch[1]}'\n" );
1432 } elseif ( $str !=
'' && preg_match(
"!<\?xml\b!si", $str ) ) {
1435 wfDebug( __METHOD__ .
": Unmatched XML declaration start\n" );
1449 protected function detectScriptInSvg( $filename, $partial ) {
1450 $this->mSVGNSError =
false;
1453 [ $this,
'checkSvgScriptCallback' ],
1456 'processing_instruction_handler' =>
'UploadBase::checkSvgPICallback',
1457 'external_dtd_handler' =>
'UploadBase::checkSvgExternalDTD',
1460 if ( $check->wellFormed !==
true ) {
1463 return $partial ?
false : [
'uploadinvalidxml' ];
1464 } elseif ( $check->filterMatch ) {
1465 if ( $this->mSVGNSError ) {
1466 return [
'uploadscriptednamespace', $this->mSVGNSError ];
1469 return $check->filterMatchType;
1481 public static function checkSvgPICallback( $target, $data ) {
1483 if ( preg_match(
'/xml-stylesheet/i', $target ) ) {
1484 return [
'upload-scripted-pi-callback' ];
1501 public static function checkSvgExternalDTD(
$type, $publicId, $systemId ) {
1505 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd',
1506 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd',
1507 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd',
1508 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd',
1510 'http://www.w3.org/TR/2001/PR-SVG-20010719/DTD/svg10.dtd',
1512 if (
$type !==
'PUBLIC'
1513 || !in_array( $systemId, $allowedDTDs )
1514 || strpos( $publicId,
"-//W3C//" ) !== 0
1516 return [
'upload-scripted-dtd' ];
1528 public function checkSvgScriptCallback( $element,
$attribs, $data =
null ) {
1529 list( $namespace, $strippedElement ) = $this->splitXmlNamespace( $element );
1533 static $validNamespaces = [
1536 'http://creativecommons.org/ns#',
1537 'http://inkscape.sourceforge.net/dtd/sodipodi-0.dtd',
1538 'http://ns.adobe.com/adobeillustrator/10.0/',
1539 'http://ns.adobe.com/adobesvgviewerextensions/3.0/',
1540 'http://ns.adobe.com/extensibility/1.0/',
1541 'http://ns.adobe.com/flows/1.0/',
1542 'http://ns.adobe.com/illustrator/1.0/',
1543 'http://ns.adobe.com/imagereplacement/1.0/',
1544 'http://ns.adobe.com/pdf/1.3/',
1545 'http://ns.adobe.com/photoshop/1.0/',
1546 'http://ns.adobe.com/saveforweb/1.0/',
1547 'http://ns.adobe.com/variables/1.0/',
1548 'http://ns.adobe.com/xap/1.0/',
1549 'http://ns.adobe.com/xap/1.0/g/',
1550 'http://ns.adobe.com/xap/1.0/g/img/',
1551 'http://ns.adobe.com/xap/1.0/mm/',
1552 'http://ns.adobe.com/xap/1.0/rights/',
1553 'http://ns.adobe.com/xap/1.0/stype/dimensions#',
1554 'http://ns.adobe.com/xap/1.0/stype/font#',
1555 'http://ns.adobe.com/xap/1.0/stype/manifestitem#',
1556 'http://ns.adobe.com/xap/1.0/stype/resourceevent#',
1557 'http://ns.adobe.com/xap/1.0/stype/resourceref#',
1558 'http://ns.adobe.com/xap/1.0/t/pg/',
1559 'http://purl.org/dc/elements/1.1/',
1560 'http://purl.org/dc/elements/1.1',
1561 'http://schemas.microsoft.com/visio/2003/svgextensions/',
1562 'http://sodipodi.sourceforge.net/dtd/sodipodi-0.dtd',
1563 'http://taptrix.com/inkpad/svg_extensions',
1564 'http://web.resource.org/cc/',
1565 'http://www.freesoftware.fsf.org/bkchem/cdml',
1566 'http://www.inkscape.org/namespaces/inkscape',
1567 'http://www.opengis.net/gml',
1568 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
1569 'http://www.w3.org/2000/svg',
1570 'http://www.w3.org/tr/rec-rdf-syntax/',
1571 'http://www.w3.org/2000/01/rdf-schema#',
1576 $isBuggyInkscape = preg_match(
'/^&(#38;)*ns_[a-z_]+;$/', $namespace );
1578 if ( !( $isBuggyInkscape || in_array( $namespace, $validNamespaces ) ) ) {
1579 wfDebug( __METHOD__ .
": Non-svg namespace '$namespace' in uploaded file.\n" );
1581 $this->mSVGNSError = $namespace;
1589 if ( $strippedElement ==
'script' ) {
1590 wfDebug( __METHOD__ .
": Found script element '$element' in uploaded file.\n" );
1592 return [
'uploaded-script-svg', $strippedElement ];
1595 # e.g., <svg xmlns="http://www.w3.org/2000/svg">
1596 # <handler xmlns:ev="http://www.w3.org/2001/xml-events" ev:event="load">alert(1)</handler> </svg>
1597 if ( $strippedElement ==
'handler' ) {
1598 wfDebug( __METHOD__ .
": Found scriptable element '$element' in uploaded file.\n" );
1600 return [
'uploaded-script-svg', $strippedElement ];
1603 # SVG reported in Feb '12 that used xml:stylesheet to generate javascript block
1604 if ( $strippedElement ==
'stylesheet' ) {
1605 wfDebug( __METHOD__ .
": Found scriptable element '$element' in uploaded file.\n" );
1607 return [
'uploaded-script-svg', $strippedElement ];
1610 # Block iframes, in case they pass the namespace check
1611 if ( $strippedElement ==
'iframe' ) {
1612 wfDebug( __METHOD__ .
": iframe in uploaded file.\n" );
1614 return [
'uploaded-script-svg', $strippedElement ];
1618 if ( $strippedElement ==
'style'
1619 && self::checkCssFragment( Sanitizer::normalizeCss( $data ) )
1621 wfDebug( __METHOD__ .
": hostile css in style element.\n" );
1622 return [
'uploaded-hostile-svg' ];
1626 $stripped = $this->stripXmlNamespace( $attrib );
1629 if ( substr( $stripped, 0, 2 ) ==
'on' ) {
1631 .
": Found event-handler attribute '$attrib'='$value' in uploaded file.\n" );
1633 return [
'uploaded-event-handler-on-svg', $attrib,
$value ];
1636 # Do not allow relative links, or unsafe url schemas.
1637 # For <a> tags, only data:, http: and https: and same-document
1638 # fragment links are allowed. For all other tags, only data:
1639 # and fragment are allowed.
1640 if ( $stripped ==
'href'
1642 && strpos(
$value,
'data:' ) !== 0
1643 && strpos(
$value,
'#' ) !== 0
1645 if ( !( $strippedElement ===
'a'
1646 && preg_match(
'!^https?://!i',
$value ) )
1648 wfDebug( __METHOD__ .
": Found href attribute <$strippedElement "
1649 .
"'$attrib'='$value' in uploaded file.\n" );
1651 return [
'uploaded-href-attribute-svg', $strippedElement, $attrib,
$value ];
1655 # only allow data: targets that should be safe. This prevents vectors like,
1656 # image/svg, text/xml, application/xml, and text/html, which can contain scripts
1657 if ( $stripped ==
'href' && strncasecmp(
'data:',
$value, 5 ) === 0 ) {
1660 $parameters =
'(?>;[a-zA-Z0-9\!#$&\'*+.^_`{|}~-]+=(?>[a-zA-Z0-9\!#$&\'*+.^_`{|}~-]+|"(?>[\0-\x0c\x0e-\x21\x23-\x5b\x5d-\x7f]+|\\\\[\0-\x7f])*"))*(?:;base64)?';
1663 if ( !preg_match(
"!^data:\s*image/(gif|jpeg|jpg|png)$parameters,!i",
$value ) ) {
1664 wfDebug( __METHOD__ .
": Found href to unwhitelisted data: uri "
1665 .
"\"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
1666 return [
'uploaded-href-unsafe-target-svg', $strippedElement, $attrib,
$value ];
1670 # Change href with animate from (http://html5sec.org/#137).
1671 if ( $stripped ===
'attributename'
1672 && $strippedElement ===
'animate'
1673 && $this->stripXmlNamespace(
$value ) ==
'href'
1675 wfDebug( __METHOD__ .
": Found animate that might be changing href using from "
1676 .
"\"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
1678 return [
'uploaded-animate-svg', $strippedElement, $attrib,
$value ];
1681 # use set/animate to add event-handler attribute to parent
1682 if ( ( $strippedElement ==
'set' || $strippedElement ==
'animate' )
1683 && $stripped ==
'attributename'
1684 && substr(
$value, 0, 2 ) ==
'on'
1686 wfDebug( __METHOD__ .
": Found svg setting event-handler attribute with "
1687 .
"\"<$strippedElement $stripped='$value'...\" in uploaded file.\n" );
1689 return [
'uploaded-setting-event-handler-svg', $strippedElement, $stripped,
$value ];
1692 # use set to add href attribute to parent element
1693 if ( $strippedElement ==
'set'
1694 && $stripped ==
'attributename'
1695 && strpos(
$value,
'href' ) !==
false
1697 wfDebug( __METHOD__ .
": Found svg setting href attribute '$value' in uploaded file.\n" );
1699 return [
'uploaded-setting-href-svg' ];
1702 # use set to add a remote / data / script target to an element
1703 if ( $strippedElement ==
'set'
1704 && $stripped ==
'to'
1705 && preg_match(
'!(http|https|data|script):!sim',
$value )
1707 wfDebug( __METHOD__ .
": Found svg setting attribute to '$value' in uploaded file.\n" );
1709 return [
'uploaded-wrong-setting-svg',
$value ];
1712 # use handler attribute with remote / data / script
1713 if ( $stripped ==
'handler' && preg_match(
'!(http|https|data|script):!sim',
$value ) ) {
1714 wfDebug( __METHOD__ .
": Found svg setting handler with remote/data/script "
1715 .
"'$attrib'='$value' in uploaded file.\n" );
1717 return [
'uploaded-setting-handler-svg', $attrib,
$value ];
1720 # use CSS styles to bring in remote code
1721 if ( $stripped ==
'style'
1722 && self::checkCssFragment( Sanitizer::normalizeCss(
$value ) )
1724 wfDebug( __METHOD__ .
": Found svg setting a style with "
1725 .
"remote url '$attrib'='$value' in uploaded file.\n" );
1726 return [
'uploaded-remote-url-svg', $attrib,
$value ];
1729 # Several attributes can include css, css character escaping isn't allowed
1730 $cssAttrs = [
'font',
'clip-path',
'fill',
'filter',
'marker',
1731 'marker-end',
'marker-mid',
'marker-start',
'mask',
'stroke' ];
1732 if ( in_array( $stripped, $cssAttrs )
1733 && self::checkCssFragment(
$value )
1735 wfDebug( __METHOD__ .
": Found svg setting a style with "
1736 .
"remote url '$attrib'='$value' in uploaded file.\n" );
1737 return [
'uploaded-remote-url-svg', $attrib,
$value ];
1740 # image filters can pull in url, which could be svg that executes scripts
1741 if ( $strippedElement ==
'image'
1742 && $stripped ==
'filter'
1743 && preg_match(
'!url\s*\(!sim',
$value )
1745 wfDebug( __METHOD__ .
": Found image filter with url: "
1746 .
"\"<$strippedElement $stripped='$value'...\" in uploaded file.\n" );
1748 return [
'uploaded-image-filter-svg', $strippedElement, $stripped,
$value ];
1762 private static function checkCssFragment(
$value ) {
1763 # Forbid external stylesheets, for both reliability and to protect viewer's privacy
1764 if ( stripos(
$value,
'@import' ) !==
false ) {
1768 # We allow @font-face to embed fonts with data: urls, so we snip the string
1769 # 'url' out so this case won't match when we check for urls below
1770 $pattern =
'!(@font-face\s*{[^}]*src:)url(\("data:;base64,)!im';
1773 # Check for remote and executable CSS. Unlike in Sanitizer::checkCss, the CSS
1774 # properties filter and accelerator don't seem to be useful for xss in SVG files.
1775 # Expression and -o-link don't seem to work either, but filtering them here in case.
1776 # Additionally, we catch remote urls like url("http:..., url('http:..., url(http:...,
1777 # but not local ones such as url("#..., url('#..., url(#....
1778 if ( preg_match(
'!expression
1780 | -o-link-source\s*:
1781 | -o-replace\s*:!imx',
$value ) ) {
1785 if ( preg_match_all(
1786 "!(\s*(url|image|image-set)\s*\(\s*[\"']?\s*[^#]+.*?\))!sim",
1791 # TODO: redo this in one regex. Until then, url("#whatever") matches the first
1793 if ( !preg_match(
"!\s*(url|image|image-set)\s*\(\s*(#|'#|\"#)!im", $match ) ) {
1799 if ( preg_match(
'/[\000-\010\013\016-\037\177]/',
$value ) ) {
1811 private static function splitXmlNamespace( $element ) {
1813 $parts = explode(
':', strtolower( $element ) );
1814 $name = array_pop( $parts );
1815 $ns = implode(
':', $parts );
1817 return [ $ns,
$name ];
1824 private function stripXmlNamespace(
$name ) {
1826 $parts = explode(
':', strtolower(
$name ) );
1828 return array_pop( $parts );
1841 public static function detectVirus( $file ) {
1845 wfDebug( __METHOD__ .
": virus scanner disabled\n" );
1851 wfDebug( __METHOD__ .
": unknown virus scanner: $wgAntivirus\n" );
1852 $wgOut->wrapWikiMsg(
"<div class=\"error\">\n$1\n</div>",
1855 return wfMessage(
'virus-unknownscanner' )->text() .
" $wgAntivirus";
1858 # look up scanner configuration
1864 if ( strpos(
$command,
"%f" ) ===
false ) {
1865 # simple pattern: append file to scan
1868 # complex pattern: replace "%f" with file to scan
1872 wfDebug( __METHOD__ .
": running virus scan: $command \n" );
1874 # execute virus scanner
1877 # NOTE: there's a 50 line workaround to make stderr redirection work on windows, too.
1878 # that does not seem to be worth the pain.
1879 # Ask me (Duesentrieb) about it if it's ever needed.
1882 # map exit code to AV_xxx constants.
1883 $mappedCode = $exitCode;
1884 if ( $exitCodeMap ) {
1885 if ( isset( $exitCodeMap[$exitCode] ) ) {
1886 $mappedCode = $exitCodeMap[$exitCode];
1887 } elseif ( isset( $exitCodeMap[
"*"] ) ) {
1888 $mappedCode = $exitCodeMap[
"*"];
1896 # scan failed (code was mapped to false by $exitCodeMap)
1897 wfDebug( __METHOD__ .
": failed to scan $file (code $exitCode).\n" );
1900 ?
wfMessage(
'virus-scanfailed', [ $exitCode ] )->text()
1903 # scan failed because filetype is unknown (probably imune)
1904 wfDebug( __METHOD__ .
": unsupported file type $file (code $exitCode).\n" );
1908 wfDebug( __METHOD__ .
": file passed virus scan.\n" );
1915 } elseif ( $msgPattern ) {
1917 if ( preg_match( $msgPattern, $output, $groups ) ) {
1919 $output = $groups[1];
1924 wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output \n" );
1938 private function checkOverwrite( $user ) {
1939 // First check whether the local file can be overwritten
1940 $file = $this->getLocalFile();
1941 $file->load( File::READ_LATEST );
1942 if ( $file->exists() ) {
1943 if ( !self::userCanReUpload( $user, $file ) ) {
1944 return [ 'fileexists-forbidden
', $file->getName() ];
1950 /* Check shared conflicts: if the local file does not exist, but
1951 * wfFindFile finds a file, it exists in a shared repository.
1953 $file = wfFindFile( $this->getTitle(), [ 'latest
' => true ] );
1954 if ( $file && !$user->isAllowed( 'reupload-shared
' ) ) {
1955 return [ 'fileexists-shared-forbidden
', $file->getName() ];
1968 public static function userCanReUpload( User $user, File $img ) {
1969 if ( $user->isAllowed( 'reupload
' ) ) {
1970 return true; // non-conditional
1971 } elseif ( !$user->isAllowed( 'reupload-
own' ) ) {
1975 if ( !( $img instanceof LocalFile ) ) {
1981 return $user->getId() == $img->getUser( 'id' );
1995 public static function getExistsWarning( $file ) {
1996 if ( $file->exists() ) {
1997 return [ 'warning
' => 'exists
', 'file' => $file ];
2000 if ( $file->getTitle()->getArticleID() ) {
2001 return [ 'warning
' => 'page-exists
', 'file' => $file ];
2004 if ( strpos( $file->getName(), '.
' ) == false ) {
2005 $partname = $file->getName();
2008 $n = strrpos( $file->getName(), '.
' );
2009 $extension = substr( $file->getName(), $n + 1 );
2010 $partname = substr( $file->getName(), 0, $n );
2012 $normalizedExtension = File::normalizeExtension( $extension );
2014 if ( $normalizedExtension != $extension ) {
2015 // We're
not using the normalized
form of the extension.
2023 if ( $file_lc->exists() ) {
2025 'warning' =>
'exists-normalized',
2027 'normalizedFile' => $file_lc
2034 "{$partname}.", 1 );
2035 if (
count( $similarFiles ) ) {
2037 'warning' =>
'exists-normalized',
2039 'normalizedFile' => $similarFiles[0],
2043 if ( self::isThumbName( $file->getName() ) ) {
2044 # Check for filenames like 50px- or 180px-, these are mostly thumbnails
2046 substr( $partname, strpos( $partname,
'-' ) + 1 ) .
'.' . $extension,
2050 if ( $file_thb->exists() ) {
2052 'warning' =>
'thumb',
2054 'thumbFile' => $file_thb
2059 'warning' =>
'thumb-name',
2061 'thumbFile' => $file_thb
2066 foreach ( self::getFilenamePrefixBlacklist()
as $prefix ) {
2067 if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix ) {
2069 'warning' =>
'bad-prefix',
2084 public static function isThumbName( $filename ) {
2085 $n = strrpos( $filename,
'.' );
2086 $partname = $n ? substr( $filename, 0, $n ) : $filename;
2089 substr( $partname, 3, 3 ) ==
'px-' ||
2090 substr( $partname, 2, 3 ) ==
'px-'
2092 preg_match(
"/[0-9]{2}/", substr( $partname, 0, 2 ) );
2100 public static function getFilenamePrefixBlacklist() {
2102 $message =
wfMessage(
'filename-prefix-blacklist' )->inContentLanguage();
2103 if ( !$message->isDisabled() ) {
2104 $lines = explode(
"\n", $message->plain() );
2107 $comment = substr( trim(
$line ), 0, 1 );
2108 if ( $comment ==
'#' || $comment ==
'' ) {
2112 $comment = strpos(
$line,
'#' );
2113 if ( $comment > 0 ) {
2116 $blacklist[] = trim(
$line );
2134 public function getImageInfo(
$result ) {
2135 $localFile = $this->getLocalFile();
2136 $stashFile = $this->getStashFile();
2154 public function convertVerifyErrorToStatus( $error ) {
2155 $code = $error[
'status'];
2156 unset(
$code[
'status'] );
2168 public static function getMaxUploadSize( $forType =
null ) {
2189 public static function getMaxPhpUploadSize() {
2191 ini_get(
'upload_max_filesize' ) ?: ini_get(
'hhvm.server.upload.upload_max_file_size' ),
2195 ini_get(
'post_max_size' ) ?: ini_get(
'hhvm.server.max_post_size' ),
2198 return min( $phpMaxFileSize, $phpMaxPostSize );
2210 public static function getSessionStatus(
User $user, $statusKey ) {
2211 $cache = MediaWikiServices::getInstance()->getMainObjectStash();
2212 $key =
$cache->makeKey(
'uploadstatus',
$user->getId() ?: md5(
$user->getName() ), $statusKey );
2214 return $cache->get( $key );
2227 public static function setSessionStatus(
User $user, $statusKey,
$value ) {
2228 $cache = MediaWikiServices::getInstance()->getMainObjectStash();
2229 $key =
$cache->makeKey(
'uploadstatus',
$user->getId() ?: md5(
$user->getName() ), $statusKey );
2231 if (
$value ===
false ) {