| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Api\ApiResult; |
| 25 |
use MediaWiki\Api\ApiUpload; |
| 26 |
use MediaWiki\Context\RequestContext; |
| 27 |
use MediaWiki\HookContainer\HookRunner; |
| 28 |
use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
| 29 |
use MediaWiki\Logger\LoggerFactory; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Message\Message; |
| 33 |
use MediaWiki\Parser\Sanitizer; |
| 34 |
use MediaWiki\Permissions\Authority; |
| 35 |
use MediaWiki\Permissions\PermissionStatus; |
| 36 |
use MediaWiki\Request\WebRequest; |
| 37 |
use MediaWiki\Shell\Shell; |
| 38 |
use MediaWiki\Status\Status; |
| 39 |
use MediaWiki\Title\Title; |
| 40 |
use MediaWiki\Upload\SVGCSSChecker; |
| 41 |
use MediaWiki\User\User; |
| 42 |
use MediaWiki\User\UserIdentity; |
| 43 |
use Wikimedia\AtEase\AtEase; |
| 44 |
use Wikimedia\FileBackend\FileBackend; |
| 45 |
use Wikimedia\FileBackend\FSFile\FSFile; |
| 46 |
use Wikimedia\FileBackend\FSFile\TempFSFile; |
| 47 |
use Wikimedia\Message\MessageParam; |
| 48 |
use Wikimedia\Mime\XmlTypeCheck; |
| 49 |
use Wikimedia\ObjectCache\BagOStuff; |
| 50 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
abstract class UploadBase { |
| 69 |
use ProtectedHookAccessorTrait; |
| 70 |
|
| 71 |
|
| 72 |
protected $mTempPath; |
| 73 |
|
| 74 |
protected $tempFileObj; |
| 75 |
|
| 76 |
protected $mDesiredDestName; |
| 77 |
|
| 78 |
protected $mDestName; |
| 79 |
|
| 80 |
protected $mRemoveTempFile; |
| 81 |
|
| 82 |
protected $mSourceType; |
| 83 |
|
| 84 |
protected $mTitle = false; |
| 85 |
|
| 86 |
protected $mTitleError = 0; |
| 87 |
|
| 88 |
protected $mFilteredName; |
| 89 |
|
| 90 |
protected $mFinalExtension; |
| 91 |
|
| 92 |
protected $mLocalFile; |
| 93 |
|
| 94 |
protected $mStashFile; |
| 95 |
|
| 96 |
protected $mFileSize; |
| 97 |
|
| 98 |
protected $mFileProps; |
| 99 |
|
| 100 |
protected $mBlackListedExtensions; |
| 101 |
|
| 102 |
protected $mJavaDetected; |
| 103 |
|
| 104 |
protected $mSVGNSError; |
| 105 |
|
| 106 |
private const SAFE_XML_ENCONDINGS = [ |
| 107 |
'UTF-8', |
| 108 |
'US-ASCII', |
| 109 |
'ISO-8859-1', |
| 110 |
'ISO-8859-2', |
| 111 |
'UTF-16', |
| 112 |
'UTF-32', |
| 113 |
'WINDOWS-1250', |
| 114 |
'WINDOWS-1251', |
| 115 |
'WINDOWS-1252', |
| 116 |
'WINDOWS-1253', |
| 117 |
'WINDOWS-1254', |
| 118 |
'WINDOWS-1255', |
| 119 |
'WINDOWS-1256', |
| 120 |
'WINDOWS-1257', |
| 121 |
'WINDOWS-1258', |
| 122 |
]; |
| 123 |
|
| 124 |
public const SUCCESS = 0; |
| 125 |
public const OK = 0; |
| 126 |
public const EMPTY_FILE = 3; |
| 127 |
public const MIN_LENGTH_PARTNAME = 4; |
| 128 |
public const ILLEGAL_FILENAME = 5; |
| 129 |
public const OVERWRITE_EXISTING_FILE = 7; |
| 130 |
public const FILETYPE_MISSING = 8; |
| 131 |
public const FILETYPE_BADTYPE = 9; |
| 132 |
public const VERIFICATION_ERROR = 10; |
| 133 |
public const HOOK_ABORTED = 11; |
| 134 |
public const FILE_TOO_LARGE = 12; |
| 135 |
public const WINDOWS_NONASCII_FILENAME = 13; |
| 136 |
public const FILENAME_TOO_LONG = 14; |
| 137 |
|
| 138 |
private const CODE_TO_STATUS = [ |
| 139 |
self::EMPTY_FILE => 'empty-file', |
| 140 |
self::FILE_TOO_LARGE => 'file-too-large', |
| 141 |
self::FILETYPE_MISSING => 'filetype-missing', |
| 142 |
self::FILETYPE_BADTYPE => 'filetype-banned', |
| 143 |
self::MIN_LENGTH_PARTNAME => 'filename-tooshort', |
| 144 |
self::ILLEGAL_FILENAME => 'illegal-filename', |
| 145 |
self::OVERWRITE_EXISTING_FILE => 'overwrite', |
| 146 |
self::VERIFICATION_ERROR => 'verification-error', |
| 147 |
self::HOOK_ABORTED => 'hookaborted', |
| 148 |
self::WINDOWS_NONASCII_FILENAME => 'windows-nonascii-filename', |
| 149 |
self::FILENAME_TOO_LONG => 'filename-toolong', |
| 150 |
]; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function getVerificationErrorCode( $error ) { |
| 157 |
return self::CODE_TO_STATUS[$error] ?? 'unknown-error'; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public static function isEnabled() { |
| 167 |
$enableUploads = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::EnableUploads ); |
| 168 |
|
| 169 |
return $enableUploads && wfIniGetBool( 'file_uploads' ); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public static function isAllowed( Authority $performer ) { |
| 181 |
foreach ( [ 'upload', 'edit' ] as $permission ) { |
| 182 |
if ( !$performer->isAllowed( $permission ) ) { |
| 183 |
return $permission; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public static function isThrottled( $user ) { |
| 200 |
wfDeprecated( __METHOD__, '1.41' ); |
| 201 |
return $user->pingLimiter( 'upload' ); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
private static $uploadHandlers = [ 'Stash', 'File', 'Url' ]; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public static function createFromRequest( &$request, $type = null ) { |
| 215 |
$type = $type ?: $request->getVal( 'wpSourceType', 'File' ); |
| 216 |
|
| 217 |
if ( !$type ) { |
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
$type = ucfirst( $type ); |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
$className = null; |
| 227 |
( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
| 228 |
|
| 229 |
->onUploadCreateFromRequest( $type, $className ); |
| 230 |
if ( $className === null ) { |
| 231 |
$className = 'UploadFrom' . $type; |
| 232 |
wfDebug( __METHOD__ . ": class name: $className" ); |
| 233 |
if ( !in_array( $type, self::$uploadHandlers ) ) { |
| 234 |
return null; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
if ( !$className::isEnabled() || !$className::isValidRequest( $request ) ) { |
| 239 |
return null; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
$handler = new $className; |
| 244 |
|
| 245 |
$handler->initializeFromRequest( $request ); |
| 246 |
|
| 247 |
return $handler; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public static function isValidRequest( $request ) { |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
public function getDesiredDestName() { |
| 264 |
return $this->mDesiredDestName; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function __construct() { |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public function getSourceType() { |
| 281 |
return null; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public function initializePathInfo( $name, $tempPath, $fileSize, $removeTempFile = false ) { |
| 291 |
$this->mDesiredDestName = $name; |
| 292 |
if ( FileBackend::isStoragePath( $tempPath ) ) { |
| 293 |
throw new InvalidArgumentException( __METHOD__ . " given storage path `$tempPath`." ); |
| 294 |
} |
| 295 |
|
| 296 |
$this->setTempFile( $tempPath, $fileSize ); |
| 297 |
$this->mRemoveTempFile = $removeTempFile; |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
abstract public function initializeFromRequest( &$request ); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
protected function setTempFile( $tempPath, $fileSize = null ) { |
| 312 |
$this->mTempPath = $tempPath ?? ''; |
| 313 |
$this->mFileSize = $fileSize ?: null; |
| 314 |
$this->mFileProps = null; |
| 315 |
if ( strlen( $this->mTempPath ) && file_exists( $this->mTempPath ) ) { |
| 316 |
$this->tempFileObj = new TempFSFile( $this->mTempPath ); |
| 317 |
if ( !$fileSize ) { |
| 318 |
$this->mFileSize = filesize( $this->mTempPath ); |
| 319 |
} |
| 320 |
} else { |
| 321 |
$this->tempFileObj = null; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public function fetchFile() { |
| 331 |
return Status::newGood(); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public function canFetchFile() { |
| 340 |
return Status::newGood(); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public function isEmptyFile() { |
| 348 |
return !$this->mFileSize; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
public function getFileSize() { |
| 356 |
return $this->mFileSize; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public function getTempFileSha1Base36() { |
| 365 |
|
| 366 |
if ( $this->mFileProps && is_string( $this->mFileProps['sha1'] ) ) { |
| 367 |
return $this->mFileProps['sha1']; |
| 368 |
} |
| 369 |
return FSFile::getSha1Base36FromPath( $this->mTempPath ); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
public function getRealPath( $srcPath ) { |
| 377 |
$repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo(); |
| 378 |
if ( FileRepo::isVirtualUrl( $srcPath ) ) { |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
$tmpFile = $repo->getLocalCopy( $srcPath ); |
| 383 |
if ( $tmpFile ) { |
| 384 |
$tmpFile->bind( $this ); |
| 385 |
} |
| 386 |
$path = $tmpFile ? $tmpFile->getPath() : false; |
| 387 |
} else { |
| 388 |
$path = $srcPath; |
| 389 |
} |
| 390 |
|
| 391 |
return $path; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
public function verifyUpload() { |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if ( $this->isEmptyFile() ) { |
| 416 |
return [ 'status' => self::EMPTY_FILE ]; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
$maxSize = self::getMaxUploadSize( $this->getSourceType() ); |
| 423 |
if ( $this->mFileSize > $maxSize ) { |
| 424 |
return [ |
| 425 |
'status' => self::FILE_TOO_LARGE, |
| 426 |
'max' => $maxSize, |
| 427 |
]; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
$verification = $this->verifyFile(); |
| 436 |
if ( $verification !== true ) { |
| 437 |
return [ |
| 438 |
'status' => self::VERIFICATION_ERROR, |
| 439 |
'details' => $verification |
| 440 |
]; |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
$result = $this->validateName(); |
| 447 |
if ( $result !== true ) { |
| 448 |
return $result; |
| 449 |
} |
| 450 |
|
| 451 |
return [ 'status' => self::OK ]; |
| 452 |
} |
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
public function validateName() { |
| 461 |
$nt = $this->getTitle(); |
| 462 |
if ( $nt === null ) { |
| 463 |
$result = [ 'status' => $this->mTitleError ]; |
| 464 |
if ( $this->mTitleError === self::ILLEGAL_FILENAME ) { |
| 465 |
$result['filtered'] = $this->mFilteredName; |
| 466 |
} |
| 467 |
if ( $this->mTitleError === self::FILETYPE_BADTYPE ) { |
| 468 |
$result['finalExt'] = $this->mFinalExtension; |
| 469 |
if ( count( $this->mBlackListedExtensions ) ) { |
| 470 |
$result['blacklistedExt'] = $this->mBlackListedExtensions; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
return $result; |
| 475 |
} |
| 476 |
$this->mDestName = $this->getLocalFile()->getName(); |
| 477 |
|
| 478 |
return true; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
protected function verifyMimeType( $mime ) { |
| 490 |
$verifyMimeType = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::VerifyMimeType ); |
| 491 |
if ( $verifyMimeType ) { |
| 492 |
wfDebug( "mime: <$mime> extension: <{$this->mFinalExtension}>" ); |
| 493 |
$mimeTypeExclusions = MediaWikiServices::getInstance()->getMainConfig() |
| 494 |
->get( MainConfigNames::MimeTypeExclusions ); |
| 495 |
if ( self::checkFileExtension( $mime, $mimeTypeExclusions ) ) { |
| 496 |
return [ 'filetype-badmime', $mime ]; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
return true; |
| 501 |
} |
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
protected function verifyFile() { |
| 509 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 510 |
$verifyMimeType = $config->get( MainConfigNames::VerifyMimeType ); |
| 511 |
$disableUploadScriptChecks = $config->get( MainConfigNames::DisableUploadScriptChecks ); |
| 512 |
$status = $this->verifyPartialFile(); |
| 513 |
if ( $status !== true ) { |
| 514 |
return $status; |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
if ( !is_array( $this->mFileProps ) ) { |
| 520 |
$mwProps = new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() ); |
| 521 |
$this->mFileProps = $mwProps->getPropsFromPath( $this->mTempPath, $this->mFinalExtension ); |
| 522 |
} |
| 523 |
$mime = $this->mFileProps['mime']; |
| 524 |
|
| 525 |
if ( $verifyMimeType ) { |
| 526 |
|
| 527 |
if ( (string)$this->mFinalExtension !== '' && |
| 528 |
!self::verifyExtension( $mime, $this->mFinalExtension ) |
| 529 |
) { |
| 530 |
return [ 'filetype-mime-mismatch', $this->mFinalExtension, $mime ]; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
if ( !$disableUploadScriptChecks ) { |
| 536 |
if ( $this->mFinalExtension === 'svg' || $mime === 'image/svg+xml' ) { |
| 537 |
$svgStatus = $this->detectScriptInSvg( $this->mTempPath, false ); |
| 538 |
if ( $svgStatus !== false ) { |
| 539 |
return $svgStatus; |
| 540 |
} |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
$handler = $mime !== null ? MediaHandler::getHandler( $mime ) : null; |
| 545 |
if ( $handler ) { |
| 546 |
$handlerStatus = $handler->verifyUpload( $this->mTempPath ); |
| 547 |
if ( !$handlerStatus->isOK() ) { |
| 548 |
$errors = $handlerStatus->getErrorsArray(); |
| 549 |
|
| 550 |
return reset( $errors ); |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
$error = true; |
| 555 |
$this->getHookRunner()->onUploadVerifyFile( $this, $mime, $error ); |
| 556 |
if ( $error !== true ) { |
| 557 |
if ( !is_array( $error ) ) { |
| 558 |
$error = [ $error ]; |
| 559 |
} |
| 560 |
return $error; |
| 561 |
} |
| 562 |
|
| 563 |
wfDebug( __METHOD__ . ": all clear; passing." ); |
| 564 |
|
| 565 |
return true; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
protected function verifyPartialFile() { |
| 578 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 579 |
$disableUploadScriptChecks = $config->get( MainConfigNames::DisableUploadScriptChecks ); |
| 580 |
|
| 581 |
$this->getTitle(); |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
if ( !is_array( $this->mFileProps ) ) { |
| 586 |
$mwProps = new MWFileProps( MediaWikiServices::getInstance()->getMimeAnalyzer() ); |
| 587 |
$this->mFileProps = $mwProps->getPropsFromPath( $this->mTempPath, $this->mFinalExtension ); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
$mime = $this->mFileProps['file-mime']; |
| 592 |
$status = $this->verifyMimeType( $mime ); |
| 593 |
if ( $status !== true ) { |
| 594 |
return $status; |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
if ( !$disableUploadScriptChecks ) { |
| 599 |
if ( self::detectScript( $this->mTempPath, $mime, $this->mFinalExtension ) ) { |
| 600 |
return [ 'uploadscripted' ]; |
| 601 |
} |
| 602 |
if ( $this->mFinalExtension === 'svg' || $mime === 'image/svg+xml' ) { |
| 603 |
$svgStatus = $this->detectScriptInSvg( $this->mTempPath, true ); |
| 604 |
if ( $svgStatus !== false ) { |
| 605 |
return $svgStatus; |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
$virus = self::detectVirus( $this->mTempPath ); |
| 612 |
if ( $virus ) { |
| 613 |
return [ 'uploadvirus', $virus ]; |
| 614 |
} |
| 615 |
|
| 616 |
return true; |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
public function zipEntryCallback( $entry ) { |
| 625 |
$names = [ $entry['name'] ]; |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
$nullPos = strpos( $entry['name'], "\000" ); |
| 633 |
if ( $nullPos !== false ) { |
| 634 |
$names[] = substr( $entry['name'], 0, $nullPos ); |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
if ( preg_grep( '!\.class/?$!', $names ) ) { |
| 640 |
$this->mJavaDetected = true; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
public function verifyPermissions( Authority $performer ) { |
| 654 |
return $this->verifyTitlePermissions( $performer ); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
public function verifyTitlePermissions( Authority $performer ) { |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
$nt = $this->getTitle(); |
| 674 |
if ( $nt === null ) { |
| 675 |
return true; |
| 676 |
} |
| 677 |
|
| 678 |
$status = PermissionStatus::newEmpty(); |
| 679 |
$performer->authorizeWrite( 'edit', $nt, $status ); |
| 680 |
$performer->authorizeWrite( 'upload', $nt, $status ); |
| 681 |
if ( !$status->isGood() ) { |
| 682 |
return $status->toLegacyErrorArray(); |
| 683 |
} |
| 684 |
|
| 685 |
$overwriteError = $this->checkOverwrite( $performer ); |
| 686 |
if ( $overwriteError !== true ) { |
| 687 |
return [ $overwriteError ]; |
| 688 |
} |
| 689 |
|
| 690 |
return true; |
| 691 |
} |
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
public function checkWarnings( $user = null ) { |
| 703 |
if ( $user === null ) { |
| 704 |
|
| 705 |
$user = RequestContext::getMain()->getUser(); |
| 706 |
} |
| 707 |
|
| 708 |
$warnings = []; |
| 709 |
|
| 710 |
$localFile = $this->getLocalFile(); |
| 711 |
$localFile->load( IDBAccessObject::READ_LATEST ); |
| 712 |
$filename = $localFile->getName(); |
| 713 |
$hash = $this->getTempFileSha1Base36(); |
| 714 |
|
| 715 |
$badFileName = $this->checkBadFileName( $filename, $this->mDesiredDestName ); |
| 716 |
if ( $badFileName !== null ) { |
| 717 |
$warnings['badfilename'] = $badFileName; |
| 718 |
} |
| 719 |
|
| 720 |
$unwantedFileExtensionDetails = $this->checkUnwantedFileExtensions( (string)$this->mFinalExtension ); |
| 721 |
if ( $unwantedFileExtensionDetails !== null ) { |
| 722 |
$warnings['filetype-unwanted-type'] = $unwantedFileExtensionDetails; |
| 723 |
} |
| 724 |
|
| 725 |
$fileSizeWarnings = $this->checkFileSize( $this->mFileSize ); |
| 726 |
if ( $fileSizeWarnings ) { |
| 727 |
$warnings = array_merge( $warnings, $fileSizeWarnings ); |
| 728 |
} |
| 729 |
|
| 730 |
$localFileExistsWarnings = $this->checkLocalFileExists( $localFile, $hash ); |
| 731 |
if ( $localFileExistsWarnings ) { |
| 732 |
$warnings = array_merge( $warnings, $localFileExistsWarnings ); |
| 733 |
} |
| 734 |
|
| 735 |
if ( $this->checkLocalFileWasDeleted( $localFile ) ) { |
| 736 |
$warnings['was-deleted'] = $filename; |
| 737 |
} |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
$ignoreLocalDupes = isset( $warnings['exists'] ); |
| 742 |
$dupes = $this->checkAgainstExistingDupes( $hash, $ignoreLocalDupes ); |
| 743 |
if ( $dupes ) { |
| 744 |
$warnings['duplicate'] = $dupes; |
| 745 |
} |
| 746 |
|
| 747 |
$archivedDupes = $this->checkAgainstArchiveDupes( $hash, $user ); |
| 748 |
if ( $archivedDupes !== null ) { |
| 749 |
$warnings['duplicate-archive'] = $archivedDupes; |
| 750 |
} |
| 751 |
|
| 752 |
return $warnings; |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
public static function makeWarningsSerializable( $warnings ) { |
| 768 |
array_walk_recursive( $warnings, static function ( &$param, $key ) { |
| 769 |
if ( $param instanceof File ) { |
| 770 |
$param = [ |
| 771 |
'fileName' => $param->getName(), |
| 772 |
'timestamp' => $param->getTimestamp() |
| 773 |
]; |
| 774 |
} elseif ( $param instanceof MessageParam ) { |
| 775 |
|
| 776 |
} elseif ( is_object( $param ) ) { |
| 777 |
throw new InvalidArgumentException( |
| 778 |
'UploadBase::makeWarningsSerializable: ' . |
| 779 |
'Unexpected object of class ' . get_class( $param ) ); |
| 780 |
} |
| 781 |
} ); |
| 782 |
return $warnings; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
public static function unserializeWarnings( $warnings ) { |
| 793 |
foreach ( $warnings as $key => $value ) { |
| 794 |
if ( is_array( $value ) ) { |
| 795 |
if ( isset( $value['fileName'] ) && isset( $value['timestamp'] ) ) { |
| 796 |
$warnings[$key] = MediaWikiServices::getInstance()->getRepoGroup()->findFile( |
| 797 |
$value['fileName'], |
| 798 |
[ 'time' => $value['timestamp'] ] |
| 799 |
); |
| 800 |
} else { |
| 801 |
$warnings[$key] = self::unserializeWarnings( $value ); |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
return $warnings; |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
private function checkBadFileName( $filename, $desiredFileName ) { |
| 818 |
$comparableName = str_replace( ' ', '_', $desiredFileName ); |
| 819 |
$comparableName = Title::capitalize( $comparableName, NS_FILE ); |
| 820 |
|
| 821 |
if ( $desiredFileName != $filename && $comparableName != $filename ) { |
| 822 |
return $filename; |
| 823 |
} |
| 824 |
|
| 825 |
return null; |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
private function checkUnwantedFileExtensions( $fileExtension ) { |
| 837 |
$checkFileExtensions = MediaWikiServices::getInstance()->getMainConfig() |
| 838 |
->get( MainConfigNames::CheckFileExtensions ); |
| 839 |
$fileExtensions = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::FileExtensions ); |
| 840 |
if ( $checkFileExtensions ) { |
| 841 |
$extensions = array_unique( $fileExtensions ); |
| 842 |
if ( !self::checkFileExtension( $fileExtension, $extensions ) ) { |
| 843 |
return [ |
| 844 |
$fileExtension, |
| 845 |
Message::listParam( $extensions, 'comma' ), |
| 846 |
count( $extensions ) |
| 847 |
]; |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
return null; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
private function checkFileSize( $fileSize ) { |
| 860 |
$uploadSizeWarning = MediaWikiServices::getInstance()->getMainConfig() |
| 861 |
->get( MainConfigNames::UploadSizeWarning ); |
| 862 |
|
| 863 |
$warnings = []; |
| 864 |
|
| 865 |
if ( $uploadSizeWarning && ( $fileSize > $uploadSizeWarning ) ) { |
| 866 |
$warnings['large-file'] = [ |
| 867 |
Message::sizeParam( $uploadSizeWarning ), |
| 868 |
Message::sizeParam( $fileSize ), |
| 869 |
]; |
| 870 |
} |
| 871 |
|
| 872 |
if ( $fileSize == 0 ) { |
| 873 |
$warnings['empty-file'] = true; |
| 874 |
} |
| 875 |
|
| 876 |
return $warnings; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
private function checkLocalFileExists( LocalFile $localFile, $hash ) { |
| 886 |
$warnings = []; |
| 887 |
|
| 888 |
$exists = self::getExistsWarning( $localFile ); |
| 889 |
if ( $exists !== false ) { |
| 890 |
$warnings['exists'] = $exists; |
| 891 |
|
| 892 |
|
| 893 |
if ( $hash !== false && $hash === $localFile->getSha1() ) { |
| 894 |
$warnings['no-change'] = $localFile; |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
$history = $localFile->getHistory(); |
| 899 |
foreach ( $history as $oldFile ) { |
| 900 |
if ( $hash === $oldFile->getSha1() ) { |
| 901 |
$warnings['duplicate-version'][] = $oldFile; |
| 902 |
} |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
return $warnings; |
| 907 |
} |
| 908 |
|
| 909 |
private function checkLocalFileWasDeleted( LocalFile $localFile ) { |
| 910 |
return $localFile->wasDeleted() && !$localFile->exists(); |
| 911 |
} |
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
private function checkAgainstExistingDupes( $hash, $ignoreLocalDupes ) { |
| 920 |
if ( $hash === false ) { |
| 921 |
return []; |
| 922 |
} |
| 923 |
$dupes = MediaWikiServices::getInstance()->getRepoGroup()->findBySha1( $hash ); |
| 924 |
$title = $this->getTitle(); |
| 925 |
foreach ( $dupes as $key => $dupe ) { |
| 926 |
if ( |
| 927 |
( $dupe instanceof LocalFile ) && |
| 928 |
$ignoreLocalDupes && |
| 929 |
$title->equals( $dupe->getTitle() ) |
| 930 |
) { |
| 931 |
unset( $dupes[$key] ); |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
return $dupes; |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
private function checkAgainstArchiveDupes( $hash, Authority $performer ) { |
| 946 |
if ( $hash === false ) { |
| 947 |
return null; |
| 948 |
} |
| 949 |
$archivedFile = new ArchivedFile( null, 0, '', $hash ); |
| 950 |
if ( $archivedFile->getID() > 0 ) { |
| 951 |
if ( $archivedFile->userCan( File::DELETED_FILE, $performer ) ) { |
| 952 |
return $archivedFile->getName(); |
| 953 |
} |
| 954 |
return ''; |
| 955 |
} |
| 956 |
|
| 957 |
return null; |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
public function performUpload( |
| 978 |
$comment, $pageText, $watch, $user, $tags = [], ?string $watchlistExpiry = null |
| 979 |
) { |
| 980 |
$this->getLocalFile()->load( IDBAccessObject::READ_LATEST ); |
| 981 |
$props = $this->mFileProps; |
| 982 |
|
| 983 |
$error = null; |
| 984 |
$this->getHookRunner()->onUploadVerifyUpload( $this, $user, $props, $comment, $pageText, $error ); |
| 985 |
if ( $error ) { |
| 986 |
if ( !is_array( $error ) ) { |
| 987 |
$error = [ $error ]; |
| 988 |
} |
| 989 |
return Status::newFatal( ...$error ); |
| 990 |
} |
| 991 |
|
| 992 |
$status = $this->getLocalFile()->upload( |
| 993 |
$this->mTempPath, |
| 994 |
$comment, |
| 995 |
$pageText !== false ? $pageText : '', |
| 996 |
File::DELETE_SOURCE, |
| 997 |
$props, |
| 998 |
false, |
| 999 |
$user, |
| 1000 |
$tags |
| 1001 |
); |
| 1002 |
|
| 1003 |
if ( $status->isGood() ) { |
| 1004 |
if ( $watch ) { |
| 1005 |
MediaWikiServices::getInstance()->getWatchlistManager()->addWatchIgnoringRights( |
| 1006 |
$user, |
| 1007 |
$this->getLocalFile()->getTitle(), |
| 1008 |
$watchlistExpiry |
| 1009 |
); |
| 1010 |
} |
| 1011 |
$this->getHookRunner()->onUploadComplete( $this ); |
| 1012 |
|
| 1013 |
$this->postProcessUpload(); |
| 1014 |
} |
| 1015 |
|
| 1016 |
return $status; |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
public function postProcessUpload() { |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
public function getTitle() { |
| 1035 |
if ( $this->mTitle !== false ) { |
| 1036 |
return $this->mTitle; |
| 1037 |
} |
| 1038 |
if ( !is_string( $this->mDesiredDestName ) ) { |
| 1039 |
$this->mTitleError = self::ILLEGAL_FILENAME; |
| 1040 |
$this->mTitle = null; |
| 1041 |
|
| 1042 |
return $this->mTitle; |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
$title = Title::newFromText( $this->mDesiredDestName ); |
| 1048 |
if ( $title && $title->getNamespace() === NS_FILE ) { |
| 1049 |
$this->mFilteredName = $title->getDBkey(); |
| 1050 |
} else { |
| 1051 |
$this->mFilteredName = $this->mDesiredDestName; |
| 1052 |
} |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
if ( strlen( $this->mFilteredName ) > 240 ) { |
| 1057 |
$this->mTitleError = self::FILENAME_TOO_LONG; |
| 1058 |
$this->mTitle = null; |
| 1059 |
|
| 1060 |
return $this->mTitle; |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
$this->mFilteredName = wfStripIllegalFilenameChars( $this->mFilteredName ); |
| 1069 |
|
| 1070 |
$nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName ); |
| 1071 |
if ( $nt === null ) { |
| 1072 |
$this->mTitleError = self::ILLEGAL_FILENAME; |
| 1073 |
$this->mTitle = null; |
| 1074 |
|
| 1075 |
return $this->mTitle; |
| 1076 |
} |
| 1077 |
$this->mFilteredName = $nt->getDBkey(); |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
[ $partname, $ext ] = self::splitExtensions( $this->mFilteredName ); |
| 1084 |
|
| 1085 |
if ( $ext !== [] ) { |
| 1086 |
$this->mFinalExtension = trim( end( $ext ) ); |
| 1087 |
} else { |
| 1088 |
$this->mFinalExtension = ''; |
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
if ( $this->mTempPath !== null ) { |
| 1094 |
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer(); |
| 1095 |
$mime = $magic->guessMimeType( $this->mTempPath ); |
| 1096 |
if ( $mime !== 'unknown/unknown' ) { |
| 1097 |
|
| 1098 |
$mimeExt = $magic->getExtensionFromMimeTypeOrNull( $mime ); |
| 1099 |
if ( $mimeExt !== null ) { |
| 1100 |
|
| 1101 |
$this->mFinalExtension = $mimeExt; |
| 1102 |
|
| 1103 |
|
| 1104 |
$this->mFilteredName .= ".{$this->mFinalExtension}"; |
| 1105 |
$nt = Title::makeTitleSafe( NS_FILE, $this->mFilteredName ); |
| 1106 |
$ext = [ $this->mFinalExtension ]; |
| 1107 |
} |
| 1108 |
} |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 1114 |
$checkFileExtensions = $config->get( MainConfigNames::CheckFileExtensions ); |
| 1115 |
$strictFileExtensions = $config->get( MainConfigNames::StrictFileExtensions ); |
| 1116 |
$fileExtensions = $config->get( MainConfigNames::FileExtensions ); |
| 1117 |
$prohibitedFileExtensions = $config->get( MainConfigNames::ProhibitedFileExtensions ); |
| 1118 |
|
| 1119 |
$badList = self::checkFileExtensionList( $ext, $prohibitedFileExtensions ); |
| 1120 |
|
| 1121 |
if ( $this->mFinalExtension == '' ) { |
| 1122 |
$this->mTitleError = self::FILETYPE_MISSING; |
| 1123 |
$this->mTitle = null; |
| 1124 |
|
| 1125 |
return $this->mTitle; |
| 1126 |
} |
| 1127 |
|
| 1128 |
if ( $badList || |
| 1129 |
( $checkFileExtensions && $strictFileExtensions && |
| 1130 |
!self::checkFileExtension( $this->mFinalExtension, $fileExtensions ) ) |
| 1131 |
) { |
| 1132 |
$this->mBlackListedExtensions = $badList; |
| 1133 |
$this->mTitleError = self::FILETYPE_BADTYPE; |
| 1134 |
$this->mTitle = null; |
| 1135 |
|
| 1136 |
return $this->mTitle; |
| 1137 |
} |
| 1138 |
|
| 1139 |
|
| 1140 |
if ( !preg_match( '/^[\x0-\x7f]*$/', $nt->getText() ) |
| 1141 |
&& !MediaWikiServices::getInstance()->getRepoGroup() |
| 1142 |
->getLocalRepo()->backendSupportsUnicodePaths() |
| 1143 |
) { |
| 1144 |
$this->mTitleError = self::WINDOWS_NONASCII_FILENAME; |
| 1145 |
$this->mTitle = null; |
| 1146 |
|
| 1147 |
return $this->mTitle; |
| 1148 |
} |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
if ( count( $ext ) > 1 ) { |
| 1153 |
$iterations = count( $ext ) - 1; |
| 1154 |
for ( $i = 0; $i < $iterations; $i++ ) { |
| 1155 |
$partname .= '.' . $ext[$i]; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
if ( strlen( $partname ) < 1 ) { |
| 1160 |
$this->mTitleError = self::MIN_LENGTH_PARTNAME; |
| 1161 |
$this->mTitle = null; |
| 1162 |
|
| 1163 |
return $this->mTitle; |
| 1164 |
} |
| 1165 |
|
| 1166 |
$this->mTitle = $nt; |
| 1167 |
|
| 1168 |
return $this->mTitle; |
| 1169 |
} |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
public function getLocalFile() { |
| 1178 |
if ( $this->mLocalFile === null ) { |
| 1179 |
$nt = $this->getTitle(); |
| 1180 |
$this->mLocalFile = $nt === null |
| 1181 |
? null |
| 1182 |
: MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()->newFile( $nt ); |
| 1183 |
} |
| 1184 |
|
| 1185 |
return $this->mLocalFile; |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
public function getStashFile() { |
| 1192 |
return $this->mStashFile; |
| 1193 |
} |
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
public function tryStashFile( User $user, $isPartial = false ) { |
| 1208 |
if ( !$isPartial ) { |
| 1209 |
$error = $this->runUploadStashFileHook( $user ); |
| 1210 |
if ( $error ) { |
| 1211 |
return Status::newFatal( ...$error ); |
| 1212 |
} |
| 1213 |
} |
| 1214 |
try { |
| 1215 |
$file = $this->doStashFile( $user ); |
| 1216 |
return Status::newGood( $file ); |
| 1217 |
} catch ( UploadStashException $e ) { |
| 1218 |
return Status::newFatal( 'uploadstash-exception', get_class( $e ), $e->getMessage() ); |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
protected function runUploadStashFileHook( User $user ) { |
| 1227 |
$props = $this->mFileProps; |
| 1228 |
$error = null; |
| 1229 |
$this->getHookRunner()->onUploadStashFile( $this, $user, $props, $error ); |
| 1230 |
if ( $error && !is_array( $error ) ) { |
| 1231 |
$error = [ $error ]; |
| 1232 |
} |
| 1233 |
return $error; |
| 1234 |
} |
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
|
| 1241 |
|
| 1242 |
|
| 1243 |
protected function doStashFile( ?User $user = null ) { |
| 1244 |
$stash = MediaWikiServices::getInstance()->getRepoGroup() |
| 1245 |
->getLocalRepo()->getUploadStash( $user ); |
| 1246 |
$file = $stash->stashFile( $this->mTempPath, $this->getSourceType(), $this->mFileProps ); |
| 1247 |
$this->mStashFile = $file; |
| 1248 |
|
| 1249 |
return $file; |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
public function cleanupTempFile() { |
| 1257 |
if ( $this->mRemoveTempFile && $this->tempFileObj ) { |
| 1258 |
|
| 1259 |
wfDebug( __METHOD__ . ": Marked temporary file '{$this->mTempPath}' for removal" ); |
| 1260 |
$this->tempFileObj->autocollect(); |
| 1261 |
} |
| 1262 |
} |
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
public function getTempPath() { |
| 1268 |
return $this->mTempPath; |
| 1269 |
} |
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
public static function splitExtensions( $filename ) { |
| 1281 |
$bits = explode( '.', $filename ); |
| 1282 |
$basename = array_shift( $bits ); |
| 1283 |
|
| 1284 |
return [ $basename, $bits ]; |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
public static function checkFileExtension( $ext, $list ) { |
| 1295 |
return in_array( strtolower( $ext ?? '' ), $list, true ); |
| 1296 |
} |
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
public static function checkFileExtensionList( $ext, $list ) { |
| 1307 |
return array_intersect( array_map( 'strtolower', $ext ), $list ); |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
public static function verifyExtension( $mime, $extension ) { |
| 1318 |
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer(); |
| 1319 |
|
| 1320 |
if ( !$mime || $mime === 'unknown' || $mime === 'unknown/unknown' ) { |
| 1321 |
if ( !$magic->isRecognizableExtension( $extension ) ) { |
| 1322 |
wfDebug( __METHOD__ . ": passing file with unknown detected mime type; " . |
| 1323 |
"unrecognized extension '$extension', can't verify" ); |
| 1324 |
|
| 1325 |
return true; |
| 1326 |
} |
| 1327 |
|
| 1328 |
wfDebug( __METHOD__ . ": rejecting file with unknown detected mime type; " . |
| 1329 |
"recognized extension '$extension', so probably invalid file" ); |
| 1330 |
return false; |
| 1331 |
} |
| 1332 |
|
| 1333 |
$match = $magic->isMatchingExtension( $extension, $mime ); |
| 1334 |
|
| 1335 |
if ( $match === null ) { |
| 1336 |
if ( $magic->getMimeTypesFromExtension( $extension ) !== [] ) { |
| 1337 |
wfDebug( __METHOD__ . ": No extension known for $mime, but we know a mime for $extension" ); |
| 1338 |
|
| 1339 |
return false; |
| 1340 |
} |
| 1341 |
|
| 1342 |
wfDebug( __METHOD__ . ": no file extension known for mime type $mime, passing file" ); |
| 1343 |
return true; |
| 1344 |
} |
| 1345 |
|
| 1346 |
if ( $match ) { |
| 1347 |
wfDebug( __METHOD__ . ": mime type $mime matches extension $extension, passing file" ); |
| 1348 |
|
| 1349 |
|
| 1350 |
return true; |
| 1351 |
} |
| 1352 |
|
| 1353 |
wfDebug( __METHOD__ |
| 1354 |
. ": mime type $mime mismatches file extension $extension, rejecting file" ); |
| 1355 |
|
| 1356 |
return false; |
| 1357 |
} |
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
public static function detectScript( $file, $mime, $extension ) { |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
if ( str_starts_with( $mime ?? '', 'text/' ) ) { |
| 1375 |
$chunk = file_get_contents( $file ); |
| 1376 |
} else { |
| 1377 |
$fp = fopen( $file, 'rb' ); |
| 1378 |
if ( !$fp ) { |
| 1379 |
return false; |
| 1380 |
} |
| 1381 |
$chunk = fread( $fp, 1024 ); |
| 1382 |
fclose( $fp ); |
| 1383 |
} |
| 1384 |
|
| 1385 |
$chunk = strtolower( $chunk ); |
| 1386 |
|
| 1387 |
if ( !$chunk ) { |
| 1388 |
return false; |
| 1389 |
} |
| 1390 |
|
| 1391 |
|
| 1392 |
if ( str_starts_with( $chunk, "\xfe\xff" ) ) { |
| 1393 |
$enc = 'UTF-16BE'; |
| 1394 |
} elseif ( str_starts_with( $chunk, "\xff\xfe" ) ) { |
| 1395 |
$enc = 'UTF-16LE'; |
| 1396 |
} else { |
| 1397 |
$enc = null; |
| 1398 |
} |
| 1399 |
|
| 1400 |
if ( $enc !== null ) { |
| 1401 |
AtEase::suppressWarnings(); |
| 1402 |
$chunk = iconv( $enc, "ASCII//IGNORE", $chunk ); |
| 1403 |
AtEase::restoreWarnings(); |
| 1404 |
} |
| 1405 |
|
| 1406 |
$chunk = trim( $chunk ); |
| 1407 |
|
| 1408 |
|
| 1409 |
wfDebug( __METHOD__ . ": checking for embedded scripts and HTML stuff" ); |
| 1410 |
|
| 1411 |
|
| 1412 |
if ( preg_match( "/<!DOCTYPE *X?HTML/i", $chunk ) ) { |
| 1413 |
return true; |
| 1414 |
} |
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
if ( $extension === 'svg' || str_starts_with( $mime ?? '', 'image/svg' ) ) { |
| 1419 |
if ( self::checkXMLEncodingMissmatch( $file ) ) { |
| 1420 |
return true; |
| 1421 |
} |
| 1422 |
} |
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
$tags = [ |
| 1431 |
'<body', |
| 1432 |
'<head', |
| 1433 |
'<html', |
| 1434 |
'<script', |
| 1435 |
]; |
| 1436 |
|
| 1437 |
foreach ( $tags as $tag ) { |
| 1438 |
if ( strpos( $chunk, $tag ) !== false ) { |
| 1439 |
wfDebug( __METHOD__ . ": found something that may make it be mistaken for html: $tag" ); |
| 1440 |
|
| 1441 |
return true; |
| 1442 |
} |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
$chunk = Sanitizer::decodeCharReferences( $chunk ); |
| 1451 |
|
| 1452 |
|
| 1453 |
if ( preg_match( '!type\s*=\s*[\'"]?\s*(?:\w*/)?(?:ecma|java)!im', $chunk ) ) { |
| 1454 |
wfDebug( __METHOD__ . ": found script types" ); |
| 1455 |
|
| 1456 |
return true; |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
if ( preg_match( '!(?:href|src|data)\s*=\s*[\'"]?\s*(?:ecma|java)script:!im', $chunk ) ) { |
| 1461 |
wfDebug( __METHOD__ . ": found html-style script urls" ); |
| 1462 |
|
| 1463 |
return true; |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
if ( preg_match( '!url\s*\(\s*[\'"]?\s*(?:ecma|java)script:!im', $chunk ) ) { |
| 1468 |
wfDebug( __METHOD__ . ": found css-style script urls" ); |
| 1469 |
|
| 1470 |
return true; |
| 1471 |
} |
| 1472 |
|
| 1473 |
wfDebug( __METHOD__ . ": no scripts found" ); |
| 1474 |
|
| 1475 |
return false; |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
public static function checkXMLEncodingMissmatch( $file ) { |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
$contents = file_get_contents( $file, false, null, 0, 4096 ); |
| 1491 |
$encodingRegex = '!encoding[ \t\n\r]*=[ \t\n\r]*[\'"](.*?)[\'"]!si'; |
| 1492 |
|
| 1493 |
if ( preg_match( "!<\?xml\b(.*?)\?>!si", $contents, $matches ) ) { |
| 1494 |
if ( preg_match( $encodingRegex, $matches[1], $encMatch ) |
| 1495 |
&& !in_array( strtoupper( $encMatch[1] ), self::SAFE_XML_ENCONDINGS ) |
| 1496 |
) { |
| 1497 |
wfDebug( __METHOD__ . ": Found unsafe XML encoding '{$encMatch[1]}'" ); |
| 1498 |
|
| 1499 |
return true; |
| 1500 |
} |
| 1501 |
} elseif ( preg_match( "!<\?xml\b!i", $contents ) ) { |
| 1502 |
|
| 1503 |
|
| 1504 |
wfDebug( __METHOD__ . ": Unmatched XML declaration start" ); |
| 1505 |
|
| 1506 |
return true; |
| 1507 |
} elseif ( str_starts_with( $contents, "\x4C\x6F\xA7\x94" ) ) { |
| 1508 |
|
| 1509 |
wfDebug( __METHOD__ . ": EBCDIC Encoded XML" ); |
| 1510 |
|
| 1511 |
return true; |
| 1512 |
} |
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
$attemptEncodings = [ 'UTF-16', 'UTF-16BE', 'UTF-32', 'UTF-32BE' ]; |
| 1517 |
foreach ( $attemptEncodings as $encoding ) { |
| 1518 |
AtEase::suppressWarnings(); |
| 1519 |
$str = iconv( $encoding, 'UTF-8', $contents ); |
| 1520 |
AtEase::restoreWarnings(); |
| 1521 |
if ( $str != '' && preg_match( "!<\?xml\b(.*?)\?>!si", $str, $matches ) ) { |
| 1522 |
if ( preg_match( $encodingRegex, $matches[1], $encMatch ) |
| 1523 |
&& !in_array( strtoupper( $encMatch[1] ), self::SAFE_XML_ENCONDINGS ) |
| 1524 |
) { |
| 1525 |
wfDebug( __METHOD__ . ": Found unsafe XML encoding '{$encMatch[1]}'" ); |
| 1526 |
|
| 1527 |
return true; |
| 1528 |
} |
| 1529 |
} elseif ( $str != '' && preg_match( "!<\?xml\b!i", $str ) ) { |
| 1530 |
|
| 1531 |
|
| 1532 |
wfDebug( __METHOD__ . ": Unmatched XML declaration start" ); |
| 1533 |
|
| 1534 |
return true; |
| 1535 |
} |
| 1536 |
} |
| 1537 |
|
| 1538 |
return false; |
| 1539 |
} |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
protected function detectScriptInSvg( $filename, $partial ) { |
| 1547 |
$this->mSVGNSError = false; |
| 1548 |
$check = new XmlTypeCheck( |
| 1549 |
$filename, |
| 1550 |
[ $this, 'checkSvgScriptCallback' ], |
| 1551 |
true, |
| 1552 |
[ |
| 1553 |
'processing_instruction_handler' => [ __CLASS__, 'checkSvgPICallback' ], |
| 1554 |
'external_dtd_handler' => [ __CLASS__, 'checkSvgExternalDTD' ], |
| 1555 |
] |
| 1556 |
); |
| 1557 |
if ( $check->wellFormed !== true ) { |
| 1558 |
|
| 1559 |
|
| 1560 |
return $partial ? false : [ 'uploadinvalidxml' ]; |
| 1561 |
} |
| 1562 |
|
| 1563 |
if ( $check->filterMatch ) { |
| 1564 |
if ( $this->mSVGNSError ) { |
| 1565 |
return [ 'uploadscriptednamespace', $this->mSVGNSError ]; |
| 1566 |
} |
| 1567 |
return $check->filterMatchType; |
| 1568 |
} |
| 1569 |
|
| 1570 |
return false; |
| 1571 |
} |
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
public static function checkSvgPICallback( $target, $data ) { |
| 1581 |
|
| 1582 |
if ( preg_match( '/xml-stylesheet/i', $target ) ) { |
| 1583 |
return [ 'upload-scripted-pi-callback' ]; |
| 1584 |
} |
| 1585 |
|
| 1586 |
return false; |
| 1587 |
} |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
public static function checkSvgExternalDTD( $type, $publicId, $systemId ) { |
| 1602 |
|
| 1603 |
|
| 1604 |
static $allowedDTDs = [ |
| 1605 |
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd', |
| 1606 |
'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd', |
| 1607 |
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd', |
| 1608 |
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-tiny.dtd', |
| 1609 |
|
| 1610 |
'http://www.w3.org/TR/2001/PR-SVG-20010719/DTD/svg10.dtd', |
| 1611 |
]; |
| 1612 |
if ( $type !== 'PUBLIC' |
| 1613 |
|| !in_array( $systemId, $allowedDTDs ) |
| 1614 |
|| !str_starts_with( $publicId, "-//W3C//" ) |
| 1615 |
) { |
| 1616 |
return [ 'upload-scripted-dtd' ]; |
| 1617 |
} |
| 1618 |
return false; |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
public function checkSvgScriptCallback( $element, $attribs, $data = null ) { |
| 1629 |
[ $namespace, $strippedElement ] = self::splitXmlNamespace( $element ); |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
static $validNamespaces = [ |
| 1634 |
'', |
| 1635 |
'adobe:ns:meta/', |
| 1636 |
'http://creativecommons.org/ns#', |
| 1637 |
'http://inkscape.sourceforge.net/dtd/sodipodi-0.dtd', |
| 1638 |
'http://ns.adobe.com/adobeillustrator/10.0/', |
| 1639 |
'http://ns.adobe.com/adobesvgviewerextensions/3.0/', |
| 1640 |
'http://ns.adobe.com/extensibility/1.0/', |
| 1641 |
'http://ns.adobe.com/flows/1.0/', |
| 1642 |
'http://ns.adobe.com/illustrator/1.0/', |
| 1643 |
'http://ns.adobe.com/imagereplacement/1.0/', |
| 1644 |
'http://ns.adobe.com/pdf/1.3/', |
| 1645 |
'http://ns.adobe.com/photoshop/1.0/', |
| 1646 |
'http://ns.adobe.com/saveforweb/1.0/', |
| 1647 |
'http://ns.adobe.com/variables/1.0/', |
| 1648 |
'http://ns.adobe.com/xap/1.0/', |
| 1649 |
'http://ns.adobe.com/xap/1.0/g/', |
| 1650 |
'http://ns.adobe.com/xap/1.0/g/img/', |
| 1651 |
'http://ns.adobe.com/xap/1.0/mm/', |
| 1652 |
'http://ns.adobe.com/xap/1.0/rights/', |
| 1653 |
'http://ns.adobe.com/xap/1.0/stype/dimensions#', |
| 1654 |
'http://ns.adobe.com/xap/1.0/stype/font#', |
| 1655 |
'http://ns.adobe.com/xap/1.0/stype/manifestitem#', |
| 1656 |
'http://ns.adobe.com/xap/1.0/stype/resourceevent#', |
| 1657 |
'http://ns.adobe.com/xap/1.0/stype/resourceref#', |
| 1658 |
'http://ns.adobe.com/xap/1.0/t/pg/', |
| 1659 |
'http://purl.org/dc/elements/1.1/', |
| 1660 |
'http://purl.org/dc/elements/1.1', |
| 1661 |
'http://schemas.microsoft.com/visio/2003/svgextensions/', |
| 1662 |
'http://sodipodi.sourceforge.net/dtd/sodipodi-0.dtd', |
| 1663 |
'http://taptrix.com/inkpad/svg_extensions', |
| 1664 |
'http://web.resource.org/cc/', |
| 1665 |
'http://www.freesoftware.fsf.org/bkchem/cdml', |
| 1666 |
'http://www.inkscape.org/namespaces/inkscape', |
| 1667 |
'http://www.opengis.net/gml', |
| 1668 |
'http://www.w3.org/1999/02/22-rdf-syntax-ns#', |
| 1669 |
'http://www.w3.org/2000/svg', |
| 1670 |
'http://www.w3.org/tr/rec-rdf-syntax/', |
| 1671 |
'http://www.w3.org/2000/01/rdf-schema#', |
| 1672 |
'http://www.w3.org/2000/02/svg/testsuite/description/', |
| 1673 |
]; |
| 1674 |
|
| 1675 |
|
| 1676 |
|
| 1677 |
$isBuggyInkscape = preg_match( '/^&(#38;)*ns_[a-z_]+;$/', $namespace ); |
| 1678 |
|
| 1679 |
if ( !( $isBuggyInkscape || in_array( $namespace, $validNamespaces ) ) ) { |
| 1680 |
wfDebug( __METHOD__ . ": Non-svg namespace '$namespace' in uploaded file." ); |
| 1681 |
|
| 1682 |
$this->mSVGNSError = $namespace; |
| 1683 |
|
| 1684 |
return true; |
| 1685 |
} |
| 1686 |
|
| 1687 |
|
| 1688 |
if ( $strippedElement === 'script' ) { |
| 1689 |
wfDebug( __METHOD__ . ": Found script element '$element' in uploaded file." ); |
| 1690 |
|
| 1691 |
return [ 'uploaded-script-svg', $strippedElement ]; |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
|
| 1696 |
if ( $strippedElement === 'handler' ) { |
| 1697 |
wfDebug( __METHOD__ . ": Found scriptable element '$element' in uploaded file." ); |
| 1698 |
|
| 1699 |
return [ 'uploaded-script-svg', $strippedElement ]; |
| 1700 |
} |
| 1701 |
|
| 1702 |
|
| 1703 |
if ( $strippedElement === 'stylesheet' ) { |
| 1704 |
wfDebug( __METHOD__ . ": Found scriptable element '$element' in uploaded file." ); |
| 1705 |
|
| 1706 |
return [ 'uploaded-script-svg', $strippedElement ]; |
| 1707 |
} |
| 1708 |
|
| 1709 |
|
| 1710 |
if ( $strippedElement === 'iframe' ) { |
| 1711 |
wfDebug( __METHOD__ . ": iframe in uploaded file." ); |
| 1712 |
|
| 1713 |
return [ 'uploaded-script-svg', $strippedElement ]; |
| 1714 |
} |
| 1715 |
|
| 1716 |
|
| 1717 |
if ( $strippedElement === 'style' |
| 1718 |
) { |
| 1719 |
$cssCheck = ( new SVGCSSChecker )->checkStyleTag( $data ); |
| 1720 |
if ( $cssCheck !== true ) { |
| 1721 |
wfDebug( __METHOD__ . ": hostile css in style element." ); |
| 1722 |
|
| 1723 |
return [ 'uploaded-hostile-svg', $cssCheck[0], $cssCheck[1], $cssCheck[2] ]; |
| 1724 |
} |
| 1725 |
} |
| 1726 |
|
| 1727 |
static $cssAttrs = [ 'font', 'clip-path', 'fill', 'filter', 'marker', |
| 1728 |
'marker-end', 'marker-mid', 'marker-start', 'mask', 'stroke', 'cursor' ]; |
| 1729 |
|
| 1730 |
foreach ( $attribs as $attrib => $value ) { |
| 1731 |
|
| 1732 |
[ $attributeNamespace, $stripped ] = self::splitXmlNamespace( $attrib ); |
| 1733 |
$value = strtolower( $value ); |
| 1734 |
|
| 1735 |
if ( !( |
| 1736 |
|
| 1737 |
$namespace === 'http://www.inkscape.org/namespaces/inkscape' && |
| 1738 |
$attributeNamespace === '' |
| 1739 |
) && str_starts_with( $stripped, 'on' ) |
| 1740 |
) { |
| 1741 |
wfDebug( __METHOD__ |
| 1742 |
. ": Found event-handler attribute '$attrib'='$value' in uploaded file." ); |
| 1743 |
|
| 1744 |
return [ 'uploaded-event-handler-on-svg', $attrib, $value ]; |
| 1745 |
} |
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
if ( |
| 1752 |
$stripped === 'href' |
| 1753 |
&& $value !== '' |
| 1754 |
&& !str_starts_with( $value, 'data:' ) |
| 1755 |
&& !str_starts_with( $value, '#' ) |
| 1756 |
&& !( $strippedElement === 'a' && preg_match( '!^https?://!i', $value ) ) |
| 1757 |
) { |
| 1758 |
wfDebug( __METHOD__ . ": Found href attribute <$strippedElement " |
| 1759 |
. "'$attrib'='$value' in uploaded file." ); |
| 1760 |
|
| 1761 |
return [ 'uploaded-href-attribute-svg', $strippedElement, $attrib, $value ]; |
| 1762 |
} |
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
if ( $stripped === 'href' && strncasecmp( 'data:', $value, 5 ) === 0 ) { |
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
$parameters = '(?>;[a-zA-Z0-9\!#$&\'*+.^_`{|}~-]+=(?>[a-zA-Z0-9\!#$&\'*+.^_`{|}~-]+|"(?>[\0-\x0c\x0e-\x21\x23-\x5b\x5d-\x7f]+|\\\\[\0-\x7f])*"))*(?:;base64)?'; |
| 1771 |
|
| 1772 |
if ( !preg_match( "!^data:\s*image/(gif|jpeg|jpg|png)$parameters,!i", $value ) ) { |
| 1773 |
wfDebug( __METHOD__ . ": Found href to allow listed data: uri " |
| 1774 |
. "\"<$strippedElement '$attrib'='$value'...\" in uploaded file." ); |
| 1775 |
return [ 'uploaded-href-unsafe-target-svg', $strippedElement, $attrib, $value ]; |
| 1776 |
} |
| 1777 |
} |
| 1778 |
|
| 1779 |
|
| 1780 |
if ( $stripped === 'attributename' |
| 1781 |
&& $strippedElement === 'animate' |
| 1782 |
&& $this->stripXmlNamespace( $value ) === 'href' |
| 1783 |
) { |
| 1784 |
wfDebug( __METHOD__ . ": Found animate that might be changing href using from " |
| 1785 |
. "\"<$strippedElement '$attrib'='$value'...\" in uploaded file." ); |
| 1786 |
|
| 1787 |
return [ 'uploaded-animate-svg', $strippedElement, $attrib, $value ]; |
| 1788 |
} |
| 1789 |
|
| 1790 |
|
| 1791 |
if ( ( $strippedElement === 'set' || $strippedElement === 'animate' ) |
| 1792 |
&& $stripped === 'attributename' |
| 1793 |
&& str_starts_with( $value, 'on' ) |
| 1794 |
) { |
| 1795 |
wfDebug( __METHOD__ . ": Found svg setting event-handler attribute with " |
| 1796 |
. "\"<$strippedElement $stripped='$value'...\" in uploaded file." ); |
| 1797 |
|
| 1798 |
return [ 'uploaded-setting-event-handler-svg', $strippedElement, $stripped, $value ]; |
| 1799 |
} |
| 1800 |
|
| 1801 |
|
| 1802 |
if ( $strippedElement === 'set' |
| 1803 |
&& $stripped === 'attributename' |
| 1804 |
&& str_contains( $value, 'href' ) |
| 1805 |
) { |
| 1806 |
wfDebug( __METHOD__ . ": Found svg setting href attribute '$value' in uploaded file." ); |
| 1807 |
|
| 1808 |
return [ 'uploaded-setting-href-svg' ]; |
| 1809 |
} |
| 1810 |
|
| 1811 |
|
| 1812 |
if ( $strippedElement === 'set' |
| 1813 |
&& $stripped === 'to' |
| 1814 |
&& preg_match( '!(http|https|data|script):!im', $value ) |
| 1815 |
) { |
| 1816 |
wfDebug( __METHOD__ . ": Found svg setting attribute to '$value' in uploaded file." ); |
| 1817 |
|
| 1818 |
return [ 'uploaded-wrong-setting-svg', $value ]; |
| 1819 |
} |
| 1820 |
|
| 1821 |
|
| 1822 |
if ( $stripped === 'handler' && preg_match( '!(http|https|data|script):!im', $value ) ) { |
| 1823 |
wfDebug( __METHOD__ . ": Found svg setting handler with remote/data/script " |
| 1824 |
. "'$attrib'='$value' in uploaded file." ); |
| 1825 |
|
| 1826 |
return [ 'uploaded-setting-handler-svg', $attrib, $value ]; |
| 1827 |
} |
| 1828 |
|
| 1829 |
|
| 1830 |
if ( $stripped === 'style' |
| 1831 |
&& ( new SVGCSSChecker )->checkStyleAttribute( $value ) !== true |
| 1832 |
) { |
| 1833 |
wfDebug( __METHOD__ . ": Found svg setting a style with " |
| 1834 |
. "remote url '$attrib'='$value' in uploaded file." ); |
| 1835 |
return [ 'uploaded-remote-url-svg', $attrib, $value ]; |
| 1836 |
} |
| 1837 |
|
| 1838 |
|
| 1839 |
if ( in_array( $stripped, $cssAttrs, true ) |
| 1840 |
&& ( new SVGCSSChecker )->checkPresentationalAttribute( $value ) !== true |
| 1841 |
) { |
| 1842 |
wfDebug( __METHOD__ . ": Found svg setting a style with " |
| 1843 |
. "remote url '$attrib'='$value' in uploaded file." ); |
| 1844 |
return [ 'uploaded-remote-url-svg', $attrib, $value ]; |
| 1845 |
} |
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
if ( $strippedElement === 'image' |
| 1851 |
&& $stripped === 'filter' |
| 1852 |
&& preg_match( '!url\s*\(\s*["\']?[^#]!im', $value ) |
| 1853 |
) { |
| 1854 |
wfDebug( __METHOD__ . ": Found image filter with url: " |
| 1855 |
. "\"<$strippedElement $stripped='$value'...\" in uploaded file." ); |
| 1856 |
|
| 1857 |
return [ 'uploaded-image-filter-svg', $strippedElement, $stripped, $value ]; |
| 1858 |
} |
| 1859 |
} |
| 1860 |
|
| 1861 |
return false; |
| 1862 |
} |
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
private static function splitXmlNamespace( $element ) { |
| 1870 |
|
| 1871 |
$parts = explode( ':', strtolower( $element ) ); |
| 1872 |
$name = array_pop( $parts ); |
| 1873 |
$ns = implode( ':', $parts ); |
| 1874 |
|
| 1875 |
return [ $ns, $name ]; |
| 1876 |
} |
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
|
| 1882 |
private function stripXmlNamespace( $element ) { |
| 1883 |
|
| 1884 |
return self::splitXmlNamespace( $element )[1]; |
| 1885 |
} |
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
public static function detectVirus( $file ) { |
| 1898 |
global $wgOut; |
| 1899 |
$mainConfig = MediaWikiServices::getInstance()->getMainConfig(); |
| 1900 |
$antivirus = $mainConfig->get( MainConfigNames::Antivirus ); |
| 1901 |
$antivirusSetup = $mainConfig->get( MainConfigNames::AntivirusSetup ); |
| 1902 |
$antivirusRequired = $mainConfig->get( MainConfigNames::AntivirusRequired ); |
| 1903 |
if ( !$antivirus ) { |
| 1904 |
wfDebug( __METHOD__ . ": virus scanner disabled" ); |
| 1905 |
|
| 1906 |
return null; |
| 1907 |
} |
| 1908 |
|
| 1909 |
if ( !$antivirusSetup[$antivirus] ) { |
| 1910 |
wfDebug( __METHOD__ . ": unknown virus scanner: {$antivirus}" ); |
| 1911 |
$wgOut->wrapWikiMsg( "<div class=\"error\">\n$1\n</div>", |
| 1912 |
[ 'virus-badscanner', $antivirus ] ); |
| 1913 |
|
| 1914 |
return wfMessage( 'virus-unknownscanner' )->text() . " {$antivirus}"; |
| 1915 |
} |
| 1916 |
|
| 1917 |
|
| 1918 |
$command = $antivirusSetup[$antivirus]['command']; |
| 1919 |
$exitCodeMap = $antivirusSetup[$antivirus]['codemap']; |
| 1920 |
$msgPattern = $antivirusSetup[$antivirus]['messagepattern'] ?? null; |
| 1921 |
|
| 1922 |
if ( !str_contains( $command, "%f" ) ) { |
| 1923 |
|
| 1924 |
$command .= " " . Shell::escape( $file ); |
| 1925 |
} else { |
| 1926 |
|
| 1927 |
$command = str_replace( "%f", Shell::escape( $file ), $command ); |
| 1928 |
} |
| 1929 |
|
| 1930 |
wfDebug( __METHOD__ . ": running virus scan: $command " ); |
| 1931 |
|
| 1932 |
|
| 1933 |
$exitCode = false; |
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
$output = wfShellExecWithStderr( $command, $exitCode ); |
| 1939 |
|
| 1940 |
|
| 1941 |
$mappedCode = $exitCode; |
| 1942 |
if ( $exitCodeMap ) { |
| 1943 |
if ( isset( $exitCodeMap[$exitCode] ) ) { |
| 1944 |
$mappedCode = $exitCodeMap[$exitCode]; |
| 1945 |
} elseif ( isset( $exitCodeMap["*"] ) ) { |
| 1946 |
$mappedCode = $exitCodeMap["*"]; |
| 1947 |
} |
| 1948 |
} |
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
if ( $mappedCode === AV_SCAN_FAILED ) { |
| 1953 |
|
| 1954 |
wfDebug( __METHOD__ . ": failed to scan $file (code $exitCode)." ); |
| 1955 |
|
| 1956 |
$output = $antivirusRequired |
| 1957 |
? wfMessage( 'virus-scanfailed', [ $exitCode ] )->text() |
| 1958 |
: null; |
| 1959 |
} elseif ( $mappedCode === AV_SCAN_ABORTED ) { |
| 1960 |
|
| 1961 |
wfDebug( __METHOD__ . ": unsupported file type $file (code $exitCode)." ); |
| 1962 |
$output = null; |
| 1963 |
} elseif ( $mappedCode === AV_NO_VIRUS ) { |
| 1964 |
|
| 1965 |
wfDebug( __METHOD__ . ": file passed virus scan." ); |
| 1966 |
$output = false; |
| 1967 |
} else { |
| 1968 |
$output = trim( $output ); |
| 1969 |
|
| 1970 |
if ( !$output ) { |
| 1971 |
$output = true; |
| 1972 |
} elseif ( $msgPattern ) { |
| 1973 |
$groups = []; |
| 1974 |
if ( preg_match( $msgPattern, $output, $groups ) && $groups[1] ) { |
| 1975 |
$output = $groups[1]; |
| 1976 |
} |
| 1977 |
} |
| 1978 |
|
| 1979 |
wfDebug( __METHOD__ . ": FOUND VIRUS! scanner feedback: $output" ); |
| 1980 |
} |
| 1981 |
|
| 1982 |
return $output; |
| 1983 |
} |
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
private function checkOverwrite( Authority $performer ) { |
| 1994 |
|
| 1995 |
$file = $this->getLocalFile(); |
| 1996 |
$file->load( IDBAccessObject::READ_LATEST ); |
| 1997 |
if ( $file->exists() ) { |
| 1998 |
if ( !self::userCanReUpload( $performer, $file ) ) { |
| 1999 |
return [ 'fileexists-forbidden', $file->getName() ]; |
| 2000 |
} |
| 2001 |
|
| 2002 |
return true; |
| 2003 |
} |
| 2004 |
|
| 2005 |
$services = MediaWikiServices::getInstance(); |
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
$file = $services->getRepoGroup()->findFile( $this->getTitle(), [ 'latest' => true ] ); |
| 2011 |
if ( $file && !$performer->isAllowed( 'reupload-shared' ) ) { |
| 2012 |
return [ 'fileexists-shared-forbidden', $file->getName() ]; |
| 2013 |
} |
| 2014 |
|
| 2015 |
return true; |
| 2016 |
} |
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
public static function userCanReUpload( Authority $performer, File $img ) { |
| 2026 |
if ( $performer->isAllowed( 'reupload' ) ) { |
| 2027 |
return true; |
| 2028 |
} |
| 2029 |
|
| 2030 |
if ( !$performer->isAllowed( 'reupload-own' ) ) { |
| 2031 |
return false; |
| 2032 |
} |
| 2033 |
|
| 2034 |
if ( !( $img instanceof LocalFile ) ) { |
| 2035 |
return false; |
| 2036 |
} |
| 2037 |
|
| 2038 |
return $performer->getUser()->equals( $img->getUploader( File::RAW ) ); |
| 2039 |
} |
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
public static function getExistsWarning( $file ) { |
| 2053 |
if ( $file->exists() ) { |
| 2054 |
return [ 'warning' => 'exists', 'file' => $file ]; |
| 2055 |
} |
| 2056 |
|
| 2057 |
if ( $file->getTitle()->getArticleID() ) { |
| 2058 |
return [ 'warning' => 'page-exists', 'file' => $file ]; |
| 2059 |
} |
| 2060 |
|
| 2061 |
$n = strrpos( $file->getName(), '.' ); |
| 2062 |
if ( $n > 0 ) { |
| 2063 |
$partname = substr( $file->getName(), 0, $n ); |
| 2064 |
$extension = substr( $file->getName(), $n + 1 ); |
| 2065 |
} else { |
| 2066 |
$partname = $file->getName(); |
| 2067 |
$extension = ''; |
| 2068 |
} |
| 2069 |
$normalizedExtension = File::normalizeExtension( $extension ); |
| 2070 |
$localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo(); |
| 2071 |
|
| 2072 |
if ( $normalizedExtension != $extension ) { |
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
|
| 2077 |
|
| 2078 |
$nt_lc = Title::makeTitle( NS_FILE, "{$partname}.{$normalizedExtension}" ); |
| 2079 |
$file_lc = $localRepo->newFile( $nt_lc ); |
| 2080 |
|
| 2081 |
if ( $file_lc->exists() ) { |
| 2082 |
return [ |
| 2083 |
'warning' => 'exists-normalized', |
| 2084 |
'file' => $file, |
| 2085 |
'normalizedFile' => $file_lc |
| 2086 |
]; |
| 2087 |
} |
| 2088 |
} |
| 2089 |
|
| 2090 |
|
| 2091 |
$similarFiles = $localRepo->findFilesByPrefix( "{$partname}.", 1 ); |
| 2092 |
if ( count( $similarFiles ) ) { |
| 2093 |
return [ |
| 2094 |
'warning' => 'exists-normalized', |
| 2095 |
'file' => $file, |
| 2096 |
'normalizedFile' => $similarFiles[0], |
| 2097 |
]; |
| 2098 |
} |
| 2099 |
|
| 2100 |
if ( self::isThumbName( $file->getName() ) ) { |
| 2101 |
|
| 2102 |
$nt_thb = Title::newFromText( |
| 2103 |
substr( $partname, strpos( $partname, '-' ) + 1 ) . '.' . $extension, |
| 2104 |
NS_FILE |
| 2105 |
); |
| 2106 |
$file_thb = $localRepo->newFile( $nt_thb ); |
| 2107 |
if ( $file_thb->exists() ) { |
| 2108 |
return [ |
| 2109 |
'warning' => 'thumb', |
| 2110 |
'file' => $file, |
| 2111 |
'thumbFile' => $file_thb |
| 2112 |
]; |
| 2113 |
} |
| 2114 |
|
| 2115 |
|
| 2116 |
return [ |
| 2117 |
'warning' => 'thumb-name', |
| 2118 |
'file' => $file, |
| 2119 |
'thumbFile' => $file_thb |
| 2120 |
]; |
| 2121 |
} |
| 2122 |
|
| 2123 |
foreach ( self::getFilenamePrefixBlacklist() as $prefix ) { |
| 2124 |
if ( str_starts_with( $partname, $prefix ) ) { |
| 2125 |
return [ |
| 2126 |
'warning' => 'bad-prefix', |
| 2127 |
'file' => $file, |
| 2128 |
'prefix' => $prefix |
| 2129 |
]; |
| 2130 |
} |
| 2131 |
} |
| 2132 |
|
| 2133 |
return false; |
| 2134 |
} |
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
|
| 2141 |
public static function isThumbName( $filename ) { |
| 2142 |
$n = strrpos( $filename, '.' ); |
| 2143 |
$partname = $n ? substr( $filename, 0, $n ) : $filename; |
| 2144 |
|
| 2145 |
return ( |
| 2146 |
substr( $partname, 3, 3 ) === 'px-' || |
| 2147 |
substr( $partname, 2, 3 ) === 'px-' |
| 2148 |
) && preg_match( "/[0-9]{2}/", substr( $partname, 0, 2 ) ); |
| 2149 |
} |
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
public static function getFilenamePrefixBlacklist() { |
| 2157 |
$list = []; |
| 2158 |
$message = wfMessage( 'filename-prefix-blacklist' )->inContentLanguage(); |
| 2159 |
if ( !$message->isDisabled() ) { |
| 2160 |
$lines = explode( "\n", $message->plain() ); |
| 2161 |
foreach ( $lines as $line ) { |
| 2162 |
|
| 2163 |
$comment = substr( trim( $line ), 0, 1 ); |
| 2164 |
if ( $comment === '#' || $comment == '' ) { |
| 2165 |
continue; |
| 2166 |
} |
| 2167 |
|
| 2168 |
$comment = strpos( $line, '#' ); |
| 2169 |
if ( $comment > 0 ) { |
| 2170 |
$line = substr( $line, 0, $comment - 1 ); |
| 2171 |
} |
| 2172 |
$list[] = trim( $line ); |
| 2173 |
} |
| 2174 |
} |
| 2175 |
|
| 2176 |
return $list; |
| 2177 |
} |
| 2178 |
|
| 2179 |
|
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
public function getImageInfo( $result = null ) { |
| 2189 |
$apiUpload = ApiUpload::getDummyInstance(); |
| 2190 |
return $apiUpload->getUploadImageInfo( $this ); |
| 2191 |
} |
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
|
| 2196 |
|
| 2197 |
public function convertVerifyErrorToStatus( $error ) { |
| 2198 |
$code = $error['status']; |
| 2199 |
unset( $code['status'] ); |
| 2200 |
|
| 2201 |
return Status::newFatal( $this->getVerificationErrorCode( $code ), $error ); |
| 2202 |
} |
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
public static function getMaxUploadSize( $forType = null ) { |
| 2212 |
$maxUploadSize = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::MaxUploadSize ); |
| 2213 |
|
| 2214 |
if ( is_array( $maxUploadSize ) ) { |
| 2215 |
if ( $forType !== null && isset( $maxUploadSize[$forType] ) ) { |
| 2216 |
return $maxUploadSize[$forType]; |
| 2217 |
} |
| 2218 |
return $maxUploadSize['*']; |
| 2219 |
} |
| 2220 |
return intval( $maxUploadSize ); |
| 2221 |
} |
| 2222 |
|
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
public static function getMaxPhpUploadSize() { |
| 2231 |
$phpMaxFileSize = wfShorthandToInteger( |
| 2232 |
ini_get( 'upload_max_filesize' ), |
| 2233 |
PHP_INT_MAX |
| 2234 |
); |
| 2235 |
$phpMaxPostSize = wfShorthandToInteger( |
| 2236 |
ini_get( 'post_max_size' ), |
| 2237 |
PHP_INT_MAX |
| 2238 |
) ?: PHP_INT_MAX; |
| 2239 |
return min( $phpMaxFileSize, $phpMaxPostSize ); |
| 2240 |
} |
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
|
| 2253 |
public static function getSessionStatus( UserIdentity $user, $statusKey ) { |
| 2254 |
$store = self::getUploadSessionStore(); |
| 2255 |
$key = self::getUploadSessionKey( $store, $user, $statusKey ); |
| 2256 |
|
| 2257 |
return $store->get( $key ); |
| 2258 |
} |
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
public static function setSessionStatus( UserIdentity $user, $statusKey, $value ) { |
| 2273 |
$store = self::getUploadSessionStore(); |
| 2274 |
$key = self::getUploadSessionKey( $store, $user, $statusKey ); |
| 2275 |
$logger = LoggerFactory::getInstance( 'upload' ); |
| 2276 |
|
| 2277 |
if ( is_array( $value ) && ( $value['result'] ?? '' ) === 'Failure' ) { |
| 2278 |
$logger->info( 'Upload session {key} for {user} set to failure {status} at {stage}', |
| 2279 |
[ |
| 2280 |
'result' => $value['result'] ?? '', |
| 2281 |
'stage' => $value['stage'] ?? 'unknown', |
| 2282 |
'user' => $user->getName(), |
| 2283 |
'status' => (string)( $value['status'] ?? '-' ), |
| 2284 |
'filekey' => $value['filekey'] ?? '', |
| 2285 |
'key' => $statusKey |
| 2286 |
] |
| 2287 |
); |
| 2288 |
} elseif ( is_array( $value ) ) { |
| 2289 |
$logger->debug( 'Upload session {key} for {user} changed {status} at {stage}', |
| 2290 |
[ |
| 2291 |
'result' => $value['result'] ?? '', |
| 2292 |
'stage' => $value['stage'] ?? 'unknown', |
| 2293 |
'user' => $user->getName(), |
| 2294 |
'status' => (string)( $value['status'] ?? '-' ), |
| 2295 |
'filekey' => $value['filekey'] ?? '', |
| 2296 |
'key' => $statusKey |
| 2297 |
] |
| 2298 |
); |
| 2299 |
} else { |
| 2300 |
$logger->debug( "Upload session {key} deleted for {user}", |
| 2301 |
[ |
| 2302 |
'value' => $value, |
| 2303 |
'key' => $statusKey, |
| 2304 |
'user' => $user->getName() |
| 2305 |
] |
| 2306 |
); |
| 2307 |
} |
| 2308 |
|
| 2309 |
if ( $value === false ) { |
| 2310 |
$store->delete( $key ); |
| 2311 |
} else { |
| 2312 |
$store->set( $key, $value, $store::TTL_DAY ); |
| 2313 |
} |
| 2314 |
} |
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
private static function getUploadSessionKey( BagOStuff $store, UserIdentity $user, $statusKey ) { |
| 2323 |
return $store->makeKey( |
| 2324 |
'uploadstatus', |
| 2325 |
$user->isRegistered() ? $user->getId() : md5( $user->getName() ), |
| 2326 |
$statusKey |
| 2327 |
); |
| 2328 |
} |
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
|
| 2333 |
private static function getUploadSessionStore() { |
| 2334 |
return MediaWikiServices::getInstance()->getMainObjectStash(); |
| 2335 |
} |
| 2336 |
} |