upload: Fix incorrect handling of missing file extension in UploadStash

The problematic code:

  $extensions = explode( ' ', $magic->getExtensionsForType( $mimeType ) );
  if ( count( $extensions ) ) {
    $extension = $extensions[0];
  }

If $mimeType is not known to MediaWiki,
   $magic->getExtensionsForType($mimeType) will return null.
explode( ' ', null ) is [""]. (Thank you so much, PHP!)
This means $extensions is nonempty (it contains the empty string), so
$extension is set to ''.

Change-Id: Icf387a9c93cb7351c2f48c69f413e7ad2224ba6b
This commit is contained in:
Ori Livneh 2020-05-22 14:56:30 -04:00
parent 96ad0db6b4
commit 0a82600a27
2 changed files with 5 additions and 15 deletions

View file

@ -485,9 +485,9 @@ class UploadStash {
// If not, assume that it should be related to the MIME type of the original file.
$magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
$mimeType = $magic->guessMimeType( $path );
$extensions = explode( ' ', $magic->getExtensionsForType( $mimeType ) );
if ( count( $extensions ) ) {
$extension = $extensions[0];
$extensions = $magic->getExtensionsForType( $mimeType );
if ( $extensions !== null ) {
$extension = strtok( $extensions, ' ' );
}
}

View file

@ -40,21 +40,14 @@ class UploadStashTest extends MediaWikiTestCase {
];
}
/**
* @todo give this test a real name explaining what is being tested here
*/
public function testT31408() {
public function testExceptionNoFileExtension() {
$this->setMwGlobals( 'wgUser', self::$users['uploader']->getUser() );
$repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
$stash = new UploadStash( $repo );
// Throws exception caught by PHPUnit on failure
$this->expectExceptionMessage( wfMessage( 'uploadstash-no-extension' )->text() );
$file = $stash->stashFile( $this->tmpFile );
// We'll never reach this point if we hit T31408
$this->assertTrue( true, 'Unrecognized file without extension' );
$stash->removeFile( $file->getFileKey() );
}
public static function provideInvalidRequests() {
@ -99,9 +92,6 @@ class UploadStashTest extends MediaWikiTestCase {
$mockRepo = $this->getMockBuilder( FileRepo::class )
->disableOriginalConstructor()
->getMock();
$mockRepo->expects( $this->once() )
->method( 'storeTemp' )
->willReturn( $mockRepoStoreStatusResult );
$stash = new UploadStash( $mockRepo );
try {