php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78183 finfo_file shows wrong mime-type for .tga file
Submitted: 2019-06-19 15:51 UTC Modified: 2019-06-21 06:28 UTC
From: dev at jdoubleu dot de Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 7.2.19 OS: Linux
Private report: No CVE-ID: None
 [2019-06-19 15:51 UTC] dev at jdoubleu dot de
Description:
------------
The finfo_file() method does not return the correct mime-type for .tga files.

Test script:
---------------
ini_set('user_agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36');

$src = fopen('http://people.math.sc.edu/Burkardt/data/tga/earth.tga', 'r');
$dest = fopen('image.tga', 'w');
stream_copy_to_stream($src, $dest);

print(finfo_file(finfo_open(FILEINFO_MIME_TYPE), 'image.tga'));
// => image/x-tgaimage/x-tga

Expected result:
----------------
"image/x-tga"

Actual result:
--------------
"image/x-tgaimage/x-tga"

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-19 16:29 UTC] dev at jdoubleu dot de
I created a repository on github which tests this bug against PHP 7.2, 7.3 and 7.3RC1 inside docker containers: https://github.com/jdoubleu/php-bug-78183

The issue only affects PHP 7.2
 [2019-06-21 06:25 UTC] krakjoe@php.net
-Status: Open +Status: Feedback
 [2019-06-21 06:25 UTC] krakjoe@php.net
I cannot reproduce this error, in addition, our use of the underlying API (magic_stream) doesn't seem to allow us to cause this, the only thing I can think of is that maybe the magic database is somehow corrupted, but this seems highly unlikely.

Running the script as it was is not possible from my location, and so probably the same is true for others.

I suggest you upload the image to a globally accessible location, and suggest you expand the test script to include buffer (string) and stream (resource) usages.
 [2019-06-21 06:28 UTC] krakjoe@php.net
Ah, I still had this page open from yesterday, and saw your comment after I posted analysis.

I'll take another look ...
 [2019-06-21 13:10 UTC] dev at jdoubleu dot de
I have uploaded the images to Docker Hub: https://hub.docker.com/r/jdoubleu/php-bug-78183

Running the script in Travis CI also confirms this bug: https://travis-ci.com/jdoubleu/php-bug-78183/builds/116430262#L236
 [2019-06-22 20:38 UTC] dev at jdoubleu dot de
According to git the ext-fileinfo did not change between 7.2 and 7.3 (see https://github.com/php/php-src/compare/PHP-7.2/ext/fileinfo/fileinfo.c..PHP-7.3/).

Running the script under 7.1 returns "application/octet-stream". This might be related to https://bugs.php.net/bug.php?id=67516 (and others).

Libmagic was updated from version 531 (PHP7.2) to 533 (PHP7.3).
 [2019-06-27 14:35 UTC] dev at jdoubleu dot de
I found the cause for this issue:

It has something to do with the underlying libmagic (or file) library.

This commit (https://github.com/file/file/commit/bb421c437d406760fb9f70f49e2eb5c504528450#diff-5c8aa65000f916c2f4b5c29c808ea851) fixes the duplicate output for tga images.

It looks like this issue does not appear in version 5.32 or later anymore.
 [2019-06-28 13:17 UTC] dev at jdoubleu dot de
The following pull request has been associated:

Patch Name: Fix #78183: finfo_file shows wrong mime-type for .tga file
On GitHub:  https://github.com/php/php-src/pull/4326
Patch:      https://github.com/php/php-src/pull/4326.patch
 [2019-06-29 11:32 UTC] ab@php.net
Automatic comment on behalf of dev@jdoubleu.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=855bbc88c94bd9317379ab67e96a7bfce2f7c195
Log: Fix #78183: finfo_file shows wrong mime-type for .tga file
 [2019-06-29 11:32 UTC] ab@php.net
-Status: Feedback +Status: Closed
 [2019-06-29 11:32 UTC] ab@php.net
Automatic comment on behalf of dev@jdoubleu.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=6cdeedb8de469388e378bd2292b89b725905fd18
Log: Add tests from fix #78183 from PHP-7.2
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 06:01:24 2019 UTC