diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index f31c53aa7dd4f..2f5f56d8bd8e9 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -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. diff --git a/tests/phpunit/data/uploads/audio-in-video-container.m4a b/tests/phpunit/data/uploads/audio-in-video-container.m4a new file mode 100644 index 0000000000000..1fc478842f51e Binary files /dev/null and b/tests/phpunit/data/uploads/audio-in-video-container.m4a differ diff --git a/tests/phpunit/data/uploads/audio-only-3gp4.m4a b/tests/phpunit/data/uploads/audio-only-3gp4.m4a new file mode 100644 index 0000000000000..f6a84f3b25f36 Binary files /dev/null and b/tests/phpunit/data/uploads/audio-only-3gp4.m4a differ diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 8ba133e930ad0..9e04db504de13 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -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',