wiki.techinc.nl/tests/phpunit/includes/MimeMagicTest.php
Brian Wolff 69905ce9c7 Fix mime detection of easily-confused-with text/plain formats
json, csv, and tsv are often detected as text/plain. However that's
not right. This patch causes MediaWiki to look at the file extension
of files detected as text/plain, and if the file extension is
for a "textual" type, use the mime type associated with that extension.

This change also changes the "does mime type match uploaded file
extension" check to use the mime based on the file contents
plus extension, as opposed to just the file contents. Various
documentation suggests this is more appropriate (e.g. line 807
of MimeMagic.php). In my opinion we should use just the file
contents when verifying file is not on blacklist, but use ext
when verifying file type matches extension, and for decided
what handler specific checks to run. Not the detect mime type
with extension doesn't override the detected mime type with
the extension, but only uses the extension if content based
detection is ambigious or not specific enough.

This patch should be reviewed by csteipp before merge for
any potential security implications.

Note: This is partially fixing a regression from 3846d10487,
where previously csv and json files were allowed to be uploaded,
and that change prevented them

Bug: 66036
Bug: 45424
Change-Id: Ib637fe6850a81b26f84dc8c00ab4772f3d3a1f34
2014-07-04 04:03:31 -03:00

39 lines
1.3 KiB
PHP

<?php
class MimeMagicTest extends MediaWikiTestCase {
/** @var MimeMagic */
private $mimeMagic;
function setUp() {
$this->mimeMagic = MimeMagic::singleton();
parent::setUp();
}
/**
* @dataProvider providerImproveTypeFromExtension
* @param $ext String File extension (no leading dot)
* @param $oldMime String Initially detected mime
* @param $expectedMime String Mime type after taking extension into account
*/
function testImproveTypeFromExtension( $ext, $oldMime, $expectedMime ) {
$actualMime = $this->mimeMagic->improveTypeFromExtension( $oldMime, $ext );
$this->assertEquals( $expectedMime, $actualMime );
}
function providerImproveTypeFromExtension() {
return array(
array( 'gif', 'image/gif', 'image/gif' ),
array( 'gif', 'unknown/unknown', 'unknown/unknown' ),
array( 'wrl', 'unknown/unknown', 'model/vrml' ),
array( 'txt', 'text/plain', 'text/plain' ),
array( 'csv', 'text/plain', 'text/csv' ),
array( 'tsv', 'text/plain', 'text/tab-separated-values' ),
array( 'json', 'text/plain', 'application/json' ),
array( 'foo', 'application/x-opc+zip', 'application/zip' ),
array( 'docx', 'application/x-opc+zip', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ),
array( 'djvu', 'image/x-djvu', 'image/vnd.djvu' ),
array( 'wav', 'audio/wav', 'audio/wav' ),
);
}
}