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;
679 $ignoreLocalDupes = isset( $warnings[
'exists '] );
680 $dupes = $this->checkAgainstExistingDupes( $hash, $ignoreLocalDupes );
682 $warnings[
'duplicate'] = $dupes;
685 $archivedDupes = $this->checkAgainstArchiveDupes( $hash );
686 if ( $archivedDupes !==
null ) {
687 $warnings[
'duplicate-archive'] = $archivedDupes;
702 private function checkBadFileName( $filename, $desiredFileName ) {
703 $comparableName = str_replace(
' ',
'_', $desiredFileName );
706 if ( $desiredFileName != $filename && $comparableName != $filename ) {
721 private function checkUnwantedFileExtensions( $fileExtension ) {
726 if ( !$this->checkFileExtension( $fileExtension, $extensions ) ) {
729 $wgLang->commaList( $extensions ),
743 private function checkFileSize( $fileSize ) {
752 if ( $fileSize == 0 ) {
753 $warnings[
'empty-file'] =
true;
765 private function checkLocalFileExists(
LocalFile $localFile, $hash ) {
768 $exists = self::getExistsWarning( $localFile );
769 if ( $exists !==
false ) {
770 $warnings[
'exists'] = $exists;
773 if ( $hash === $localFile->
getSha1() ) {
774 $warnings[
'no-change'] = $localFile;
779 foreach ( $history
as $oldFile ) {
780 if ( $hash === $oldFile->getSha1() ) {
781 $warnings[
'duplicate-version'][] = $oldFile;
789 private function checkLocalFileWasDeleted(
LocalFile $localFile ) {
799 private function checkAgainstExistingDupes( $hash, $ignoreLocalDupes ) {
801 $title = $this->getTitle();
802 foreach ( $dupes
as $key => $dupe ) {
806 $title->equals( $dupe->getTitle() )
808 unset( $dupes[$key] );
821 private function checkAgainstArchiveDupes( $hash ) {
823 if ( $archivedFile->getID() > 0 ) {
825 return $archivedFile->getName();
847 public function performUpload( $comment, $pageText, $watch,
$user, $tags = [] ) {
848 $this->getLocalFile()->load( File::READ_LATEST );
849 $props = $this->mFileProps;
852 Hooks::run(
'UploadVerifyUpload', [ $this,
$user, $props, $comment, $pageText, &$error ] );
854 if ( !is_array( $error ) ) {
860 $status = $this->getLocalFile()->upload(
874 $this->getLocalFile()->getTitle(),
881 Hooks::run(
'UploadComplete', [ &$uploadBase ] );
883 $this->postProcessUpload();
894 public function postProcessUpload() {
903 public function getTitle() {
904 if ( $this->mTitle !==
false ) {
905 return $this->mTitle;
907 if ( !is_string( $this->mDesiredDestName ) ) {
908 $this->mTitleError = self::ILLEGAL_FILENAME;
909 $this->mTitle =
null;
911 return $this->mTitle;
918 $this->mFilteredName =
$title->getDBkey();
920 $this->mFilteredName = $this->mDesiredDestName;
923 # oi_archive_name is max 255 bytes, which include a timestamp and an
924 # exclamation mark, so restrict file name to 240 bytes.
925 if ( strlen( $this->mFilteredName ) > 240 ) {
926 $this->mTitleError = self::FILENAME_TOO_LONG;
927 $this->mTitle =
null;
929 return $this->mTitle;
940 if ( is_null( $nt ) ) {
941 $this->mTitleError = self::ILLEGAL_FILENAME;
942 $this->mTitle =
null;
944 return $this->mTitle;
946 $this->mFilteredName = $nt->getDBkey();
952 list( $partname,
$ext ) = $this->splitExtensions( $this->mFilteredName );
957 $this->mFinalExtension =
'';
959 # No extension, try guessing one
961 $mime = $magic->guessMimeType( $this->mTempPath );
962 if (
$mime !==
'unknown/unknown' ) {
963 # Get a space separated list of extensions
964 $extList = $magic->getExtensionsForType(
$mime );
966 # Set the extension to the canonical extension
967 $this->mFinalExtension = strtok( $extList,
' ' );
969 # Fix up the other variables
970 $this->mFilteredName .=
".{$this->mFinalExtension}";
972 $ext = [ $this->mFinalExtension ];
983 if ( $this->mFinalExtension ==
'' ) {
984 $this->mTitleError = self::FILETYPE_MISSING;
985 $this->mTitle =
null;
987 return $this->mTitle;
988 } elseif ( $blackListedExtensions ||
992 $this->mBlackListedExtensions = $blackListedExtensions;
993 $this->mTitleError = self::FILETYPE_BADTYPE;
994 $this->mTitle =
null;
996 return $this->mTitle;
1000 if ( !preg_match(
'/^[\x0-\x7f]*$/', $nt->getText() )
1003 $this->mTitleError = self::WINDOWS_NONASCII_FILENAME;
1004 $this->mTitle =
null;
1006 return $this->mTitle;
1009 # If there was more than one "extension", reassemble the base
1010 # filename to prevent bogus complaints about length
1013 for ( $i = 0; $i < $iterations; $i++ ) {
1014 $partname .=
'.' .
$ext[$i];
1018 if ( strlen( $partname ) < 1 ) {
1019 $this->mTitleError = self::MIN_LENGTH_PARTNAME;
1020 $this->mTitle =
null;
1022 return $this->mTitle;
1025 $this->mTitle = $nt;
1027 return $this->mTitle;
1035 public function getLocalFile() {
1036 if ( is_null( $this->mLocalFile ) ) {
1037 $nt = $this->getTitle();
1038 $this->mLocalFile = is_null( $nt ) ? null :
wfLocalFile( $nt );
1041 return $this->mLocalFile;
1047 public function getStashFile() {
1048 return $this->mStashFile;
1062 public function tryStashFile(
User $user, $isPartial =
false ) {
1063 if ( !$isPartial ) {
1064 $error = $this->runUploadStashFileHook(
$user );
1070 $file = $this->doStashFile(
$user );
1081 protected function runUploadStashFileHook(
User $user ) {
1082 $props = $this->mFileProps;
1086 if ( !is_array( $error ) ) {
1087 $error = [ $error ];
1112 public function stashFile(
User $user =
null ) {
1113 return $this->doStashFile(
$user );
1122 protected function doStashFile(
User $user =
null ) {
1124 $file = $stash->stashFile( $this->mTempPath, $this->getSourceType() );
1125 $this->mStashFile = $file;
1137 public function stashFileGetKey() {
1139 return $this->doStashFile()->getFileKey();
1148 public function stashSession() {
1150 return $this->doStashFile()->getFileKey();
1157 public function cleanupTempFile() {
1158 if ( $this->mRemoveTempFile && $this->tempFileObj ) {
1160 wfDebug( __METHOD__ .
": Marked temporary file '{$this->mTempPath}' for removal\n" );
1161 $this->tempFileObj->autocollect();
1165 public function getTempPath() {
1166 return $this->mTempPath;
1178 public static function splitExtensions( $filename ) {
1179 $bits = explode(
'.', $filename );
1180 $basename = array_shift( $bits );
1182 return [ $basename, $bits ];
1193 public static function checkFileExtension(
$ext, $list ) {
1194 return in_array( strtolower(
$ext ), $list );
1205 public static function checkFileExtensionList(
$ext, $list ) {
1206 return array_intersect( array_map(
'strtolower',
$ext ), $list );
1216 public static function verifyExtension(
$mime, $extension ) {
1220 if ( !$magic->isRecognizableExtension( $extension ) ) {
1221 wfDebug( __METHOD__ .
": passing file with unknown detected mime type; " .
1222 "unrecognized extension '$extension', can't verify\n" );
1226 wfDebug( __METHOD__ .
": rejecting file with unknown detected mime type; " .
1227 "recognized extension '$extension', so probably invalid file\n" );
1233 $match = $magic->isMatchingExtension( $extension,
$mime );
1235 if ( $match ===
null ) {
1236 if ( $magic->getTypesForExtension( $extension ) !== null ) {
1237 wfDebug( __METHOD__ .
": No extension known for $mime, but we know a mime for $extension\n" );
1241 wfDebug( __METHOD__ .
": no file extension known for mime type $mime, passing file\n" );
1245 } elseif ( $match ===
true ) {
1246 wfDebug( __METHOD__ .
": mime type $mime matches extension $extension, passing file\n" );
1252 .
": mime type $mime mismatches file extension $extension, rejecting file\n" );
1269 public static function detectScript( $file,
$mime, $extension ) {
1272 # ugly hack: for text files, always look at the entire file.
1273 # For binary field, just check the first K.
1275 if ( strpos(
$mime,
'text/' ) === 0 ) {
1276 $chunk = file_get_contents( $file );
1278 $fp = fopen( $file,
'rb' );
1279 $chunk = fread( $fp, 1024 );
1283 $chunk = strtolower( $chunk );
1289 # decode from UTF-16 if needed (could be used for obfuscation).
1290 if ( substr( $chunk, 0, 2 ) ==
"\xfe\xff" ) {
1292 } elseif ( substr( $chunk, 0, 2 ) ==
"\xff\xfe" ) {
1299 $chunk = iconv( $enc,
"ASCII//IGNORE", $chunk );
1302 $chunk = trim( $chunk );
1305 wfDebug( __METHOD__ .
": checking for embedded scripts and HTML stuff\n" );
1307 # check for HTML doctype
1308 if ( preg_match(
"/<!DOCTYPE *X?HTML/i", $chunk ) ) {
1314 if ( $extension ==
'svg' || strpos(
$mime,
'image/svg' ) === 0 ) {
1315 if ( self::checkXMLEncodingMissmatch( $file ) ) {
1339 '<html', # also
in safari
1342 '<script', # also
in safari
1350 foreach ( $tags
as $tag ) {
1351 if (
false !== strpos( $chunk, $tag ) ) {
1352 wfDebug( __METHOD__ .
": found something that may make it be mistaken for html: $tag\n" );
1362 # resolve entity-refs to look at attributes. may be harsh on big files... cache result?
1363 $chunk = Sanitizer::decodeCharReferences( $chunk );
1365 # look for script-types
1366 if ( preg_match(
'!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!sim', $chunk ) ) {
1367 wfDebug( __METHOD__ .
": found script types\n" );
1372 # look for html-style script-urls
1373 if ( preg_match(
'!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
1374 wfDebug( __METHOD__ .
": found html-style script urls\n" );
1379 # look for css-style script-urls
1380 if ( preg_match(
'!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!sim', $chunk ) ) {
1381 wfDebug( __METHOD__ .
": found css-style script urls\n" );
1386 wfDebug( __METHOD__ .
": no scripts found\n" );
1398 public static function checkXMLEncodingMissmatch( $file ) {
1401 $encodingRegex =
'!encoding[ \t\n\r]*=[ \t\n\r]*[\'"](.*?)[\'"]!si';
1403 if ( preg_match(
"!<\?xml\b(.*?)\?>!si", $contents,
$matches ) ) {
1404 if ( preg_match( $encodingRegex,
$matches[1], $encMatch )
1405 && !in_array( strtoupper( $encMatch[1] ), self::$safeXmlEncodings )
1407 wfDebug( __METHOD__ .
": Found unsafe XML encoding '{$encMatch[1]}'\n" );
1411 } elseif ( preg_match(
"!<\?xml\b!si", $contents ) ) {
1414 wfDebug( __METHOD__ .
": Unmatched XML declaration start\n" );
1417 } elseif ( substr( $contents, 0, 4 ) ==
"\x4C\x6F\xA7\x94" ) {
1419 wfDebug( __METHOD__ .
": EBCDIC Encoded XML\n" );
1426 $attemptEncodings = [
'UTF-16',
'UTF-16BE',
'UTF-32',
'UTF-32BE' ];
1427 foreach ( $attemptEncodings
as $encoding ) {
1428 Wikimedia\suppressWarnings();
1429 $str = iconv( $encoding,
'UTF-8', $contents );
1430 Wikimedia\restoreWarnings();
1431 if ( $str !=
'' && preg_match(
"!<\?xml\b(.*?)\?>!si", $str,
$matches ) ) {
1432 if ( preg_match( $encodingRegex,
$matches[1], $encMatch )
1433 && !in_array( strtoupper( $encMatch[1] ), self::$safeXmlEncodings )
1435 wfDebug( __METHOD__ .
": Found unsafe XML encoding '{$encMatch[1]}'\n" );
1439 } elseif ( $str !=
'' && preg_match(
"!<\?xml\b!si", $str ) ) {
1442 wfDebug( __METHOD__ .
": Unmatched XML declaration start\n" );
1456 protected function detectScriptInSvg( $filename, $partial ) {
1457 $this->mSVGNSError =
false;
1460 [ $this,
'checkSvgScriptCallback' ],
1463 'processing_instruction_handler' =>
'UploadBase::checkSvgPICallback',
1464 'external_dtd_handler' =>
'UploadBase::checkSvgExternalDTD',
1467 if ( $check->wellFormed !==
true ) {
1470 return $partial ?
false : [
'uploadinvalidxml' ];
1471 } elseif ( $check->filterMatch ) {
1472 if ( $this->mSVGNSError ) {
1473 return [
'uploadscriptednamespace', $this->mSVGNSError ];
1476 return $check->filterMatchType;
1488 public static function checkSvgPICallback( $target, $data ) {
1490 if ( preg_match(
'/xml-stylesheet/i', $target ) ) {
1491 return [
'upload-scripted-pi-callback' ];
1508 public static function checkSvgExternalDTD(
$type, $publicId, $systemId ) {
1512 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd',
1513 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd',
1514 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd',
1515 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd',
1517 'http://www.w3.org/TR/2001/PR-SVG-20010719/DTD/svg10.dtd',
1519 if (
$type !==
'PUBLIC'
1520 || !in_array( $systemId, $allowedDTDs )
1521 || strpos( $publicId,
"-//W3C//" ) !== 0
1523 return [
'upload-scripted-dtd' ];
1535 public function checkSvgScriptCallback( $element,
$attribs, $data =
null ) {
1536 list( $namespace, $strippedElement ) = $this->splitXmlNamespace( $element );
1540 static $validNamespaces = [
1543 'http://creativecommons.org/ns#',
1544 'http://inkscape.sourceforge.net/dtd/sodipodi-0.dtd',
1545 'http://ns.adobe.com/adobeillustrator/10.0/',
1546 'http://ns.adobe.com/adobesvgviewerextensions/3.0/',
1547 'http://ns.adobe.com/extensibility/1.0/',
1548 'http://ns.adobe.com/flows/1.0/',
1549 'http://ns.adobe.com/illustrator/1.0/',
1550 'http://ns.adobe.com/imagereplacement/1.0/',
1551 'http://ns.adobe.com/pdf/1.3/',
1552 'http://ns.adobe.com/photoshop/1.0/',
1553 'http://ns.adobe.com/saveforweb/1.0/',
1554 'http://ns.adobe.com/variables/1.0/',
1555 'http://ns.adobe.com/xap/1.0/',
1556 'http://ns.adobe.com/xap/1.0/g/',
1557 'http://ns.adobe.com/xap/1.0/g/img/',
1558 'http://ns.adobe.com/xap/1.0/mm/',
1559 'http://ns.adobe.com/xap/1.0/rights/',
1560 'http://ns.adobe.com/xap/1.0/stype/dimensions#',
1561 'http://ns.adobe.com/xap/1.0/stype/font#',
1562 'http://ns.adobe.com/xap/1.0/stype/manifestitem#',
1563 'http://ns.adobe.com/xap/1.0/stype/resourceevent#',
1564 'http://ns.adobe.com/xap/1.0/stype/resourceref#',
1565 'http://ns.adobe.com/xap/1.0/t/pg/',
1566 'http://purl.org/dc/elements/1.1/',
1567 'http://purl.org/dc/elements/1.1',
1568 'http://schemas.microsoft.com/visio/2003/svgextensions/',
1569 'http://sodipodi.sourceforge.net/dtd/sodipodi-0.dtd',
1570 'http://taptrix.com/inkpad/svg_extensions',
1571 'http://web.resource.org/cc/',
1572 'http://www.freesoftware.fsf.org/bkchem/cdml',
1573 'http://www.inkscape.org/namespaces/inkscape',
1574 'http://www.opengis.net/gml',
1575 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
1576 'http://www.w3.org/2000/svg',
1577 'http://www.w3.org/tr/rec-rdf-syntax/',
1578 'http://www.w3.org/2000/01/rdf-schema#',
1583 $isBuggyInkscape = preg_match(
'/^&(#38;)*ns_[a-z_]+;$/', $namespace );
1585 if ( !( $isBuggyInkscape || in_array( $namespace, $validNamespaces ) ) ) {
1586 wfDebug( __METHOD__ .
": Non-svg namespace '$namespace' in uploaded file.\n" );
1588 $this->mSVGNSError = $namespace;
1596 if ( $strippedElement ==
'script' ) {
1597 wfDebug( __METHOD__ .
": Found script element '$element' in uploaded file.\n" );
1599 return [
'uploaded-script-svg', $strippedElement ];
1602 # e.g., <svg xmlns="http://www.w3.org/2000/svg">
1603 # <handler xmlns:ev="http://www.w3.org/2001/xml-events" ev:event="load">alert(1)</handler> </svg>
1604 if ( $strippedElement ==
'handler' ) {
1605 wfDebug( __METHOD__ .
": Found scriptable element '$element' in uploaded file.\n" );
1607 return [
'uploaded-script-svg', $strippedElement ];
1610 # SVG reported in Feb '12 that used xml:stylesheet to generate javascript block
1611 if ( $strippedElement ==
'stylesheet' ) {
1612 wfDebug( __METHOD__ .
": Found scriptable element '$element' in uploaded file.\n" );
1614 return [
'uploaded-script-svg', $strippedElement ];
1617 # Block iframes, in case they pass the namespace check
1618 if ( $strippedElement ==
'iframe' ) {
1619 wfDebug( __METHOD__ .
": iframe in uploaded file.\n" );
1621 return [
'uploaded-script-svg', $strippedElement ];
1625 if ( $strippedElement ==
'style'
1626 && self::checkCssFragment( Sanitizer::normalizeCss( $data ) )
1628 wfDebug( __METHOD__ .
": hostile css in style element.\n" );
1629 return [
'uploaded-hostile-svg' ];
1633 $stripped = $this->stripXmlNamespace( $attrib );
1636 if ( substr( $stripped, 0, 2 ) ==
'on' ) {
1638 .
": Found event-handler attribute '$attrib'='$value' in uploaded file.\n" );
1640 return [
'uploaded-event-handler-on-svg', $attrib,
$value ];
1643 # Do not allow relative links, or unsafe url schemas.
1644 # For <a> tags, only data:, http: and https: and same-document
1645 # fragment links are allowed. For all other tags, only data:
1646 # and fragment are allowed.
1647 if ( $stripped ==
'href'
1649 && strpos(
$value,
'data:' ) !== 0
1650 && strpos(
$value,
'#' ) !== 0
1652 if ( !( $strippedElement ===
'a'
1653 && preg_match(
'!^https?://!i',
$value ) )
1655 wfDebug( __METHOD__ .
": Found href attribute <$strippedElement "
1656 .
"'$attrib'='$value' in uploaded file.\n" );
1658 return [
'uploaded-href-attribute-svg', $strippedElement, $attrib,
$value ];
1662 # only allow data: targets that should be safe. This prevents vectors like,
1663 # image/svg, text/xml, application/xml, and text/html, which can contain scripts
1664 if ( $stripped ==
'href' && strncasecmp(
'data:',
$value, 5 ) === 0 ) {
1667 $parameters =
'(?>;[a-zA-Z0-9\!#$&\'*+.^_`{|}~-]+=(?>[a-zA-Z0-9\!#$&\'*+.^_`{|}~-]+|"(?>[\0-\x0c\x0e-\x21\x23-\x5b\x5d-\x7f]+|\\\\[\0-\x7f])*"))*(?:;base64)?';
1669 if ( !preg_match(
"!^data:\s*image/(gif|jpeg|jpg|png)$parameters,!i",
$value ) ) {
1670 wfDebug( __METHOD__ .
": Found href to unwhitelisted data: uri "
1671 .
"\"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
1672 return [
'uploaded-href-unsafe-target-svg', $strippedElement, $attrib,
$value ];
1676 # Change href with animate from (http://html5sec.org/#137).
1677 if ( $stripped ===
'attributename'
1678 && $strippedElement ===
'animate'
1679 && $this->stripXmlNamespace(
$value ) ==
'href'
1681 wfDebug( __METHOD__ .
": Found animate that might be changing href using from "
1682 .
"\"<$strippedElement '$attrib'='$value'...\" in uploaded file.\n" );
1684 return [
'uploaded-animate-svg', $strippedElement, $attrib,
$value ];
1687 # use set/animate to add event-handler attribute to parent
1688 if ( ( $strippedElement ==
'set' || $strippedElement ==
'animate' )
1689 && $stripped ==
'attributename'
1690 && substr(
$value, 0, 2 ) ==
'on'
1692 wfDebug( __METHOD__ .
": Found svg setting event-handler attribute with "
1693 .
"\"<$strippedElement $stripped='$value'...\" in uploaded file.\n" );
1695 return [
'uploaded-setting-event-handler-svg', $strippedElement, $stripped,
$value ];
1698 # use set to add href attribute to parent element
1699 if ( $strippedElement ==
'set'
1700 && $stripped ==
'attributename'
1701 && strpos(
$value,
'href' ) !==
false
1703 wfDebug( __METHOD__ .
": Found svg setting href attribute '$value' in uploaded file.\n" );
1705 return [
'uploaded-setting-href-svg' ];
1708 # use set to add a remote / data / script target to an element
1709 if ( $strippedElement ==
'set'
1710 && $stripped ==
'to'
1711 && preg_match(
'!(http|https|data|script):!sim',
$value )
1713 wfDebug( __METHOD__ .
": Found svg setting attribute to '$value' in uploaded file.\n" );
1715 return [
'uploaded-wrong-setting-svg',
$value ];
1718 # use handler attribute with remote / data / script
1719 if ( $stripped ==
'handler' && preg_match(
'!(http|https|data|script):!sim',
$value ) ) {
1720 wfDebug( __METHOD__ .
": Found svg setting handler with remote/data/script "
1721 .
"'$attrib'='$value' in uploaded file.\n" );
1723 return [
'uploaded-setting-handler-svg', $attrib,
$value ];
1726 # use CSS styles to bring in remote code
1727 if ( $stripped ==
'style'
1728 && self::checkCssFragment( Sanitizer::normalizeCss(
$value ) )
1730 wfDebug( __METHOD__ .
": Found svg setting a style with "
1731 .
"remote url '$attrib'='$value' in uploaded file.\n" );
1732 return [
'uploaded-remote-url-svg', $attrib,
$value ];
1735 # Several attributes can include css, css character escaping isn't allowed
1736 $cssAttrs = [
'font',
'clip-path',
'fill',
'filter',
'marker',
1737 'marker-end',
'marker-mid',
'marker-start',
'mask',
'stroke' ];
1738 if ( in_array( $stripped, $cssAttrs )
1739 && self::checkCssFragment(
$value )
1741 wfDebug( __METHOD__ .
": Found svg setting a style with "
1742 .
"remote url '$attrib'='$value' in uploaded file.\n" );
1743 return [
'uploaded-remote-url-svg', $attrib,
$value ];
1746 # image filters can pull in url, which could be svg that executes scripts
1747 if ( $strippedElement ==
'image'
1748 && $stripped ==
'filter'
1749 && preg_match(
'!url\s*\(!sim',
$value )
1751 wfDebug( __METHOD__ .
": Found image filter with url: "
1752 .
"\"<$strippedElement $stripped='$value'...\" in uploaded file.\n" );
1754 return [
'uploaded-image-filter-svg', $strippedElement, $stripped,
$value ];
1768 private static function checkCssFragment(
$value ) {
1769 # Forbid external stylesheets, for both reliability and to protect viewer's privacy
1770 if ( stripos(
$value,
'@import' ) !==
false ) {
1774 # We allow @font-face to embed fonts with data: urls, so we snip the string
1775 # 'url' out so this case won't match when we check for urls below
1776 $pattern =
'!(@font-face\s*{[^}]*src:)url(\("data:;base64,)!im';
1779 # Check for remote and executable CSS. Unlike in Sanitizer::checkCss, the CSS
1780 # properties filter and accelerator don't seem to be useful for xss in SVG files.
1781 # Expression and -o-link don't seem to work either, but filtering them here in case.
1782 # Additionally, we catch remote urls like url("http:..., url('http:..., url(http:...,
1783 # but not local ones such as url("#..., url('#..., url(#....
1784 if ( preg_match(
'!expression
1786 | -o-link-source\s*:
1787 | -o-replace\s*:!imx',
$value ) ) {
1791 if ( preg_match_all(
1792 "!(\s*(url|image|image-set)\s*\(\s*[\"']?\s*[^#]+.*?\))!sim",
1797 # TODO: redo this in one regex. Until then, url("#whatever") matches the first
1799 if ( !preg_match(
"!\s*(url|image|image-set)\s*\(\s*(#|'#|\"#)!im", $match ) ) {
1805 if ( preg_match(
'/[\000-\010\013\016-\037\177]/',
$value ) ) {
1817 private static function splitXmlNamespace( $element ) {
1819 $parts = explode(
':', strtolower( $element ) );
1820 $name = array_pop( $parts );
1821 $ns = implode(
':', $parts );
1823 return [ $ns,
$name ];
1830 private function stripXmlNamespace(
$name ) {
1832 $parts = explode(
':', strtolower(
$name ) );
1834 return array_pop( $parts );
1847 public static function detectVirus( $file ) {
1851 wfDebug( __METHOD__ .
": virus scanner disabled\n" );
1857 wfDebug( __METHOD__ .
": unknown virus scanner: $wgAntivirus\n" );
1858 $wgOut->wrapWikiMsg(
"<div class=\"error\">\n$1\n</div>",
1861 return wfMessage(
'virus-unknownscanner' )->text() .
" $wgAntivirus";
1864 # look up scanner configuration
1869 if ( strpos(
$command,
"%f" ) ===
false ) {
1870 # simple pattern: append file to scan
1873 # complex pattern: replace "%f" with file to scan
1877 wfDebug( __METHOD__ .
": running virus scan: $command \n" );
1879 # execute virus scanner
1882 # NOTE: there's a 50 line workaround to make stderr redirection work on windows, too.
1883 # that does not seem to be worth the pain.
1884 # Ask me (Duesentrieb) about it if it's ever needed.
1887 # map exit code to AV_xxx constants.
1888 $mappedCode = $exitCode;
1889 if ( $exitCodeMap ) {
1890 if ( isset( $exitCodeMap[$exitCode] ) ) {
1891 $mappedCode = $exitCodeMap[$exitCode];
1892 } elseif ( isset( $exitCodeMap[
"*"] ) ) {
1893 $mappedCode = $exitCodeMap[
"*"];
1901 # scan failed (code was mapped to false by $exitCodeMap)
1902 wfDebug( __METHOD__ .
": failed to scan $file (code $exitCode).\n" );
1905 ?
wfMessage(
'virus-scanfailed', [ $exitCode ] )->text()
1908 # scan failed because filetype is unknown (probably imune)
1909 wfDebug( __METHOD__ .
": unsupported file type $file (code $exitCode).\n" );
1913 wfDebug( __METHOD__ .
": file passed virus scan.\n" );
1920 } elseif ( $msgPattern ) {
1922 if ( preg_match( $msgPattern, $output, $groups ) ) {
1924 $output = $groups[1];
1929 wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output \n" );
1943 private function checkOverwrite( $user ) {
1944 // First check whether the local file can be overwritten
1945 $file = $this->getLocalFile();
1946 $file->load( File::READ_LATEST );
1947 if ( $file->exists() ) {
1948 if ( !self::userCanReUpload( $user, $file ) ) {
1949 return [ 'fileexists-forbidden
', $file->getName() ];
1955 /* Check shared conflicts: if the local file does not exist, but
1956 * wfFindFile finds a file, it exists in a shared repository.
1958 $file = wfFindFile( $this->getTitle(), [ 'latest
' => true ] );
1959 if ( $file && !$user->isAllowed( 'reupload-shared
' ) ) {
1960 return [ 'fileexists-shared-forbidden
', $file->getName() ];
1973 public static function userCanReUpload( User $user, File $img ) {
1974 if ( $user->isAllowed( 'reupload
' ) ) {
1975 return true; // non-conditional
1976 } elseif ( !$user->isAllowed( 'reupload-
own' ) ) {
1980 if ( !( $img instanceof LocalFile ) ) {
1986 return $user->getId() == $img->getUser( 'id' );
2000 public static function getExistsWarning( $file ) {
2001 if ( $file->exists() ) {
2002 return [ 'warning
' => 'exists
', 'file' => $file ];
2005 if ( $file->getTitle()->getArticleID() ) {
2006 return [ 'warning
' => 'page-exists
', 'file' => $file ];
2009 if ( strpos( $file->getName(), '.
' ) == false ) {
2010 $partname = $file->getName();
2013 $n = strrpos( $file->getName(), '.
' );
2014 $extension = substr( $file->getName(), $n + 1 );
2015 $partname = substr( $file->getName(), 0, $n );
2017 $normalizedExtension = File::normalizeExtension( $extension );
2019 if ( $normalizedExtension != $extension ) {
2020 // We're
not using the normalized
form of the extension.
2028 if ( $file_lc->exists() ) {
2030 'warning' =>
'exists-normalized',
2032 'normalizedFile' => $file_lc
2039 "{$partname}.", 1 );
2040 if (
count( $similarFiles ) ) {
2042 'warning' =>
'exists-normalized',
2044 'normalizedFile' => $similarFiles[0],
2048 if ( self::isThumbName( $file->getName() ) ) {
2049 # Check for filenames like 50px- or 180px-, these are mostly thumbnails
2051 substr( $partname, strpos( $partname,
'-' ) + 1 ) .
'.' . $extension,
2055 if ( $file_thb->exists() ) {
2057 'warning' =>
'thumb',
2059 'thumbFile' => $file_thb
2064 'warning' =>
'thumb-name',
2066 'thumbFile' => $file_thb
2071 foreach ( self::getFilenamePrefixBlacklist()
as $prefix ) {
2072 if ( substr( $partname, 0, strlen( $prefix ) ) == $prefix ) {
2074 'warning' =>
'bad-prefix',
2089 public static function isThumbName( $filename ) {
2090 $n = strrpos( $filename,
'.' );
2091 $partname = $n ? substr( $filename, 0, $n ) : $filename;
2094 substr( $partname, 3, 3 ) ==
'px-' ||
2095 substr( $partname, 2, 3 ) ==
'px-'
2097 preg_match(
"/[0-9]{2}/", substr( $partname, 0, 2 ) );
2105 public static function getFilenamePrefixBlacklist() {
2107 $message =
wfMessage(
'filename-prefix-blacklist' )->inContentLanguage();
2108 if ( !$message->isDisabled() ) {
2109 $lines = explode(
"\n", $message->plain() );
2112 $comment = substr( trim(
$line ), 0, 1 );
2113 if ( $comment ==
'#' || $comment ==
'' ) {
2117 $comment = strpos(
$line,
'#' );
2118 if ( $comment > 0 ) {
2121 $blacklist[] = trim(
$line );
2139 public function getImageInfo(
$result ) {
2140 $localFile = $this->getLocalFile();
2141 $stashFile = $this->getStashFile();
2159 public function convertVerifyErrorToStatus( $error ) {
2160 $code = $error[
'status'];
2161 unset(
$code[
'status'] );
2173 public static function getMaxUploadSize( $forType =
null ) {
2194 public static function getMaxPhpUploadSize() {
2196 ini_get(
'upload_max_filesize' ) ?: ini_get(
'hhvm.server.upload.upload_max_file_size' ),
2200 ini_get(
'post_max_size' ) ?: ini_get(
'hhvm.server.max_post_size' ),
2203 return min( $phpMaxFileSize, $phpMaxPostSize );
2215 public static function getSessionStatus(
User $user, $statusKey ) {
2216 $cache = MediaWikiServices::getInstance()->getMainObjectStash();
2217 $key =
$cache->makeKey(
'uploadstatus',
$user->getId() ?: md5(
$user->getName() ), $statusKey );
2219 return $cache->get( $key );
2232 public static function setSessionStatus(
User $user, $statusKey,
$value ) {
2233 $cache = MediaWikiServices::getInstance()->getMainObjectStash();
2234 $key =
$cache->makeKey(
'uploadstatus',
$user->getId() ?: md5(
$user->getName() ), $statusKey );
2236 if (
$value ===
false ) {