WordPress.org

Make WordPress Core

Opened 6 months ago

Closed 5 months ago

#40017 closed defect (bug) (fixed)

wp_get_image_mime() returns 'application/octet-stream' for non-image files.

Reported by: blobfolio Owned by: joemcgill
Milestone: 4.7.4 Priority: normal
Severity: normal Version: 4.7.1
Component: Media Keywords: has-patch has-unit-tests fixed-major
Focuses: Cc:

Description

In the newly-added function wp_get_image_mime(), the following line should be split into two parts:

<?php
$mime = image_type_to_mime_type( exif_imagetype( $file ) );

exif_imagetype() will return FALSE on failure.
image_type_to_mime_type() will cast FALSE as an integer, resulting in a response of "application/octet-stream" even for invalid files.

Some EXIF types are "application/octet-stream", so to differentiate between failures and merely strange formats, it would be better written like:

<?php
if ( is_callable( 'exif_imagetype' ) ) {
        if ( false !== $exif_type = exif_imagetype( $file ) ) {
                $mime = image_type_to_mime_type( $exif_type );
        } else {
                $mime = false;
        }
}

Attachments (4)

exif.diff (643 bytes) - added by blobfolio 6 months ago.
Implements fix.
exif.2.diff (1.5 KB) - added by blobfolio 5 months ago.
remove false !==, add unit test
exif.3.diff (1.5 KB) - added by blobfolio 5 months ago.
move unit test to function.php
exif.4.diff (2.1 KB) - added by blobfolio 5 months ago.
Added data provider for unit test

Download all attachments as: .zip

Change History (24)

@blobfolio
6 months ago

Implements fix.

#1 @blobfolio
6 months ago

  • Keywords has-patch dev-feedback added

Added a patch, tweaked a little bit to match the coding style of nearby lines.

This ticket was mentioned in Slack in #core-media by blobfolio. View the logs.


6 months ago

This ticket was mentioned in Slack in #core-media by blobfolio. View the logs.


5 months ago

#4 @joemcgill
5 months ago

  • Milestone changed from Awaiting Review to 4.7.4

#5 @swissspidy
5 months ago

  • Keywords needs-unit-tests added

The patch looks really straightforward, but some tests would be helpful to ensure we really fix the issue.

#6 @joemcgill
5 months ago

  • Keywords dev-feedback removed

Good catch @blobfolio. After chatting on Slack about this, it sounds like we don't need the strict checking on 'false' there and could simplify to ( ! $imagetype ). Whenever strict checking of bool values makes no difference, I tend to prefer the simpler style.

I also agree with @swissspidy about adding a simple PHPUnit test for this, if practical.

#7 @blobfolio
5 months ago

Thanks @joemcgill and @swissspidy. I'll get an updated patch up today.

@blobfolio
5 months ago

remove false !==, add unit test

#8 @blobfolio
5 months ago

Simple unit test, one image, one non-image. Both sources were already present in the test data.

$ phpunit tests/phpunit/tests/functions/wpGetImageMime.php 
Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests. To execute these, use --group ajax.
Not running ms-files tests. To execute these, use --group ms-files.
Not running external-http tests. To execute these, use --group external-http.
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 728 ms, Memory: 20.00Mb

OK (1 test, 2 assertions)

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


5 months ago

#10 @blobfolio
5 months ago

  • Keywords needs-unit-tests removed

@blobfolio
5 months ago

move unit test to function.php

#11 @blobfolio
5 months ago

  • Keywords has-unit-tests added

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


5 months ago

#13 @joemcgill
5 months ago

  • Owner set to joemcgill
  • Status changed from new to assigned

This ticket was mentioned in Slack in #core by swissspidy. View the logs.


5 months ago

#15 follow-up: @swissspidy
5 months ago

I'd probably split test_wp_get_image_mime() up into two separate tests with one assertion each.

This ticket was mentioned in Slack in #core-media by swissspidy. View the logs.


5 months ago

@blobfolio
5 months ago

Added data provider for unit test

#17 in reply to: ↑ 15 @blobfolio
5 months ago

Replying to swissspidy:

I'd probably split test_wp_get_image_mime() up into two separate tests with one assertion each.

I moved the assertions to a data provider function and added GIF, PNG, and misnamed JPEG to the batch. Should make it easier to extend going forward.

Speaking of extensions, image_type_to_mime_type() is another function that will return inconsistent values depending on the PHP version, so for the time being I've limited the valid extension tests to the main three (which shouldn't vary): JPG, GIF, PNG.

If/when #39963 gets pushed we can add alias-capable unit tests to match exif responses for formats like BMP (which could be either of image/bmp or image/x-ms-bmp).

#18 @joemcgill
5 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 40397:

Media: Improve handling of non-image files in wp_get_image_mime.

This prevents non-image fileypes from returning a mime type of
"application/octet-stream" when exif_imagetype() returns false.

Props blobfolio.
Fixes #40017.

#19 @joemcgill
5 months ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

#20 @swissspidy
5 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 40403:

Media: Improve handling of non-image files in wp_get_image_mime.

This prevents non-image fileypes from returning a mime type of "application/octet-stream" when exif_imagetype() returns false.

Props blobfolio.
Fixes #40017.

Merges [40397] to the 4.7 branch.

Note: See TracTickets for help on using tickets.