Make WordPress Core

#58349 closed defect (bug) (fixed)

TypeError in wp_check_filetype_and_ext hook: bool values into $mimes argument can be passed

Reported by: platonkristinin's profile platonkristinin Owned by: johnbillion's profile johnbillion
Milestone: 6.3 Priority: normal
Severity: normal Version:
Component: Media Keywords: good-first-bug has-patch
Focuses: Cc:

Description

https://developer.wordpress.org/reference/hooks/wp_check_filetype_and_ext/ API doc states that $mimes argument is keyed array:

 $mimes string[]
    Array of mime types keyed by their file extension regex.

But by fact into this argument can be passed a bool data type. When I add my custom hook

<?php
function my_check_filetype_and_ext(array $wp_check_filetype_and_ext, string $file, string $filename, array $mimes, string|false $real_mime): array {
}

add_filter('wp_check_filetype_and_ext', [$this, 'my_check_svg_filetype_and_ext'], accepted_args: 5);

then I get following error:

PHP Fatal error: Uncaught TypeError: Argument #4 ($mimes) must be of type array, bool given

So there is a bug in documentation or in hook implementation. I'm leaning towards the second, because mimes always should be array and there are no reasons to pass boolean values.

Workaround is allow false values for $mimes:

array|false $mimes

Attachments (1)

58349.patch (705 bytes) - added by pkbhatt 11 months ago.
Added patch for the bug

Download all attachments as: .zip

Change History (6)

#1 @johnbillion
11 months ago

  • Component changed from General to Media
  • Keywords needs-patch good-first-bug added
  • Milestone changed from Awaiting Review to 6.3

Thanks for the report. It does indeed seem that false can get passed to the $mimes parameter, which is incorrect. This parameter should be an array of strings or null.

Ref: https://github.com/WordPress/wordpress-develop/blob/261ab3fa5de7b1972dd52e938cdb4abd8d9014df/src/wp-admin/includes/file.php#L931 . $mimes has a fallback of false here when it should be null: https://github.com/WordPress/wordpress-develop/blob/261ab3fa5de7b1972dd52e938cdb4abd8d9014df/src/wp-admin/includes/file.php#L893.

@pkbhatt
11 months ago

Added patch for the bug

#2 @pkbhatt
11 months ago

  • Keywords has-patch added; needs-patch good-first-bug removed

Fixed the issue and added patch

#3 @pkbhatt
11 months ago

  • Keywords good-first-bug added

#4 @johnbillion
11 months ago

  • Owner set to johnbillion
  • Status changed from new to reviewing

#5 @johnbillion
11 months ago

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

In 55872:

Media: Correct the fallback value passed to the $mimes parameter of wp_check_filetype_and_ext() from _wp_handle_upload(), and update corresponding documentation.

Previously when the mimes element was not specified in the overrides array passed to _wp_handle_upload() it resulted in boolean false being passed to this parameter, which is incorrect. The fallback value should be null.

Props platonkristinin, pkbhatt

Fixes #58349

Note: See TracTickets for help on using tickets.