Opened 3 years ago

Last modified 10 months ago

#11946 reopened defect (bug)

Ensure image MIME type matches extension

Reported by: Viper007Bond Owned by: Viper007Bond
Priority: normal Milestone: Future Release
Component: Upload Version: 3.0
Severity: minor Keywords: needs-refresh
Cc: ShaneF

Description

Take a bitmap (BMP) and rename it to .png. WordPress will say it's an image/png everywhere you look. This can cause issues if you're trying to manipulate it (thumbnail it, etc.).

We should either fix the extension or reject it.

Attachments (4)

11946.patch (1.4 KB) - added by Viper007Bond 3 years ago.
Potential solution
11946.2.patch (1.6 KB) - added by Viper007Bond 3 years ago.
Patch improvements, likely needs porting to wp_handle_sideload() too
11946.3.patch (1.7 KB) - added by Viper007Bond 3 years ago.
Minor code improvements
11946.4.patch (7.4 KB) - added by Viper007Bond 3 years ago.
Introduce new helper function and use it in all 3 upload functions

Download all attachments as: .zip

Change History (33)

  • Version set to 3.0

Potential solution

  • Summary changed from Ensure MIME type matches extension to Ensure image MIME type matches extension

Above patch fixes the extension. Tested and seems to work great.

  • Keywords has-patch needs-testing added
  • Milestone changed from Unassigned to Future Release

This patch requires GD and should therefore be wrapped in if ( function_exists('getimagesize') ) {. The patch is just a preliminary one though, so I won't bother updating it yet.

  • Cc ShaneF added
  • Keywords tested added; needs-testing removed

I took a "png" renamed it to "gif" (with patch). Uploaded. File was changed to the correct format, but in the editor it still says "File type: image/gif". Perhaps override the file type as well?

comment:6 follow-up: ↓ 7   ShaneF3 years ago

  • Milestone changed from Future Release to 2.9.2

This is a potential security problem. Recommending 2.9.2 release fix.

Patch improvements, likely needs porting to wp_handle_sideload() too

comment:7 in reply to: ↑ 6   Viper007Bond3 years ago

Replying to ShaneF:

This is a potential security problem. Recommending 2.9.2 release fix.

I can't think of a way to validate other extensions though, i.e. say a EXE uploaded as an AVI.

comment:8 follow-up: ↓ 9   hakre3 years ago

Don't we have an array already in there that is offering mime-types per extension? I think it's adviseable to use the same datasource here therein the patch as well instead offering duplicate data.

comment:9 in reply to: ↑ 8 ; follow-up: ↓ 10   Viper007Bond3 years ago

Replying to hakre:

Don't we have an array already in there that is offering mime-types per extension? I think it's adviseable to use the same datasource here therein the patch as well instead offering duplicate data.

That array (in get_allowed_mime_types()) is a list of allowed MIMEs that's filterable. It also has multiple extensions to one common MIME. Yes, I guess I could array_flip() it and then pop off the first extension, however I didn't think this was reliable enough due to the filter.

comment:10 in reply to: ↑ 9   Viper007Bond3 years ago

Replying to Viper007Bond:

That array (in get_allowed_mime_types()) is a list of allowed MIMEs that's filterable. It also has multiple extensions to one common MIME. Yes, I guess I could array_flip() it and then pop off the first extension, however I didn't think this was reliable enough due to the filter.

Actually, I take that back, it could work now that I think of it.

Well I did not say that it is fitting per-se, but worth to take a look into it. I mean why duplicate data here? But no idea if it is available in the location you would like to use it. We might need to introduce another filter here (or isn't there already one?!). Might make sense in the one or other way.

Now I remember why I just made a new array -- it's the list of image types that the function can identify. No point in using the other array.

Minor code improvements

  • Milestone changed from 2.9.3 to 3.0
  • Resolution set to fixed
  • Status changed from new to closed

(In [14400]) Ensure image MIME type matches extension for images. props Viper007Bond, fixes #11946.

  • Resolution fixed deleted
  • Status changed from closed to reopened

Should be moved to a helper function and added to the other upload functions (sideload and the bits one).

  • Keywords needs-patch added; has-patch tested removed
  • Priority changed from lowest to normal

Bumping the need for a patch up the importance radar

  • Owner set to Viper007Bond
  • Status changed from reopened to assigned

Introduce new helper function and use it in all 3 upload functions

  • Keywords has-patch needs-testing added; needs-patch removed
  • Status changed from assigned to accepted

11946.4.patch introduces wp_check_filetype_and_ext() which does what wp_check_filetype() does, but then goes on to attempt to validate the type and extension. If it determines that the extension doesn't match what the type truly is, then it'll return proper_filename as a part of it's returned array.

I've modified the standard upload function, the sideload function, and the bits function to make use of this new function.

I have only tested this using the standard upload process, not sideload or bits.

(In [14649]) Introduce wp_check_filetype_and_ext() to handle mime/ext image comparisons and corrections for upload and sideload. props Viper007Bond, see #11946.

  • Milestone changed from 3.0 to 3.1

Viper007Bond and I have thoroughly tested this for sideloads and uploads. Weary about bits, so going to handle that no sooner than 3.1.

3.1 now on shedule. maybe optionally support fileinfo?

  • Milestone changed from Awaiting Triage to 3.1

Just need to get the existing patch working for bits. Probably would be good for Joseph to take a look.

  • Keywords needs-patch added; has-patch needs-testing removed

Patch is broken.

  • Keywords needs-refresh added; needs-patch removed
  • Resolution set to fixed
  • Status changed from accepted to closed
  • Resolution fixed deleted
  • Status changed from closed to reopened

Last bit is needed.

  • Milestone changed from 3.1 to Future Release

Added a fix for wp_upload_bits at ticket #21292

Note: See TracTickets for help on using tickets.