Skip to content
Closed
20 changes: 18 additions & 2 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3306,8 +3306,24 @@ function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) {
* and some media files are commonly named with the wrong extension (.mov instead of .mp4)
*/
if ( substr( $real_mime, 0, strcspn( $real_mime, '/' ) ) !== substr( $type, 0, strcspn( $type, '/' ) ) ) {
$type = false;
$ext = false;
/*
* Audio stored in an MP4/ISO-BMFF container (such as .m4a and .m4b, and
* some .mp3 files produced by mobile recorders) is frequently detected as
* video/mp4. Allow these known audio-in-video-container confusions rather
* than rejecting a legitimate audio file whose major types don't match.
* Also allow `video/3gpp`, as a RFC 3839 specification-compliant container
* for audio-only content.
*/
$cross_container_types = array(
'audio/mpeg' => array( 'video/mp4', 'video/3gpp' ),
);

if ( empty( $cross_container_types[ $type ] )
|| ! in_array( $real_mime, $cross_container_types[ $type ], true )
) {
$type = false;
$ext = false;
}
}
} elseif ( 'text/plain' === $real_mime ) {
// A few common file types are occasionally detected as text/plain; allow those.
Expand Down
Binary file not shown.
Binary file added tests/phpunit/data/uploads/audio-only-3gp4.m4a
Binary file not shown.
28 changes: 28 additions & 0 deletions tests/phpunit/tests/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1761,6 +1761,34 @@ public static function data_wp_check_filetype_and_ext() {
'proper_filename' => false,
),
),
/*
* Audio wrapped in an MP4/ISO-BMFF container (e.g. .m4a from a phone
* recorder) is detected by fileinfo as video/mp4, while the extension
* maps to audio/mpeg. The differing major types must not cause a rejection.
*/
array(
DIR_TESTDATA . '/uploads/audio-in-video-container.m4a',
'audio-in-video-container.m4a',
array(
'ext' => 'm4a',
'type' => 'audio/mpeg',
'proper_filename' => false,
),
),
/*
* Audio wrapped in an 3GPP TS 26.244 container (e.g. .m4a from a phone
* recorder) is detected by fileinfo as video/3gpp, while the extension
* maps to audio/mpeg. The differing major types must not cause a rejection.
*/
array(
DIR_TESTDATA . '/uploads/audio-only-3gp4.m4a',
'audio-only-3gp4.m4a',
array(
'ext' => 'm4a',
'type' => 'audio/mpeg',
'proper_filename' => false,
),
),
// Assorted text/* sample files
array(
DIR_TESTDATA . '/uploads/test.vtt',
Expand Down
Loading