Make WordPress Core

Changeset 44443 for branches/5.0


Ignore:
Timestamp:
01/07/2019 10:22:03 PM (8 years ago)
Author:
joemcgill
Message:

Upload: Fix upload failures of common text file types.

This adds some special case handling in 'wp_check_filetype_and_ext()' that prevents some common file types from being blocked based on mismatched MIME checks, which were made more strict in WordPress 5.0.1.

Merges [44438], [44439], [44441], and [44442] to the 4.9 branch.

Props Kloon, birgire, tellyworth, joemcgill.
See #45615.

Location:
branches/5.0
Files:
3 edited
5 copied

Legend:

Unmodified
Added
Removed
  • branches/5.0

  • branches/5.0/src/wp-includes/functions.php

    r44407 r44443  
    23742374                         * and some media files are commonly named with the wrong extension (.mov instead of .mp4)
    23752375                         */
    2376 
    23772376                        if ( substr( $real_mime, 0, strcspn( $real_mime, '/' ) ) !== substr( $type, 0, strcspn( $type, '/' ) ) ) {
     2377                                $type = $ext = false;
     2378                        }
     2379                } elseif ( 'text/plain' === $real_mime ) {
     2380                        // A few common file types are occasionally detected as text/plain; allow those.
     2381                        if ( ! in_array(
     2382                                $type,
     2383                                array(
     2384                                        'text/plain',
     2385                                        'text/csv',
     2386                                        'text/richtext',
     2387                                        'text/tsv',
     2388                                        'text/vtt',
     2389                                )
     2390                        )
     2391                        ) {
     2392                                $type = $ext = false;
     2393                        }
     2394                } elseif ( 'text/rtf' === $real_mime ) {
     2395                        // Special casing for RTF files.
     2396                        if ( ! in_array(
     2397                                $type,
     2398                                array(
     2399                                        'text/rtf',
     2400                                        'text/plain',
     2401                                        'application/rtf',
     2402                                )
     2403                        )
     2404                        ) {
    23782405                                $type = $ext = false;
    23792406                        }
  • branches/5.0/tests/phpunit/tests/functions.php

    r44407 r44443  
    10731073
    10741074        /**
    1075          * Data profider for test_wp_get_image_mime();
     1075         * Data provider for test_wp_get_image_mime();
    10761076         */
    10771077        public function _wp_get_image_mime() {
     
    11791179                                ),
    11801180                        ),
     1181                        // Non-image file not allowed even if it's named like one.
     1182                        array(
     1183                                DIR_TESTDATA . '/export/crazy-cdata.xml',
     1184                                'crazy-cdata.jpg',
     1185                                array(
     1186                                        'ext'             => false,
     1187                                        'type'            => false,
     1188                                        'proper_filename' => false,
     1189                                ),
     1190                        ),
     1191                        // Non-image file not allowed if it's named like something else.
     1192                        array(
     1193                                DIR_TESTDATA . '/export/crazy-cdata.xml',
     1194                                'crazy-cdata.doc',
     1195                                array(
     1196                                        'ext'             => false,
     1197                                        'type'            => false,
     1198                                        'proper_filename' => false,
     1199                                ),
     1200                        ),
    11811201                );
    11821202
    11831203                // Test a few additional file types on single sites.
    11841204                if ( ! is_multisite() ) {
    1185                         $data = array_merge( $data, array(
    1186                                 // Standard non-image file.
     1205                        $data = array_merge(
     1206                                $data,
    11871207                                array(
    1188                                         DIR_TESTDATA . '/formatting/big5.txt',
    1189                                         'big5.txt',
     1208                                        // Standard non-image file.
    11901209                                        array(
    1191                                                 'ext' => 'txt',
    1192                                                 'type' => 'text/plain',
    1193                                                 'proper_filename' => false,
     1210                                                DIR_TESTDATA . '/formatting/big5.txt',
     1211                                                'big5.txt',
     1212                                                array(
     1213                                                        'ext' => 'txt',
     1214                                                        'type' => 'text/plain',
     1215                                                        'proper_filename' => false,
     1216                                                ),
    11941217                                        ),
    1195                                 ),
    1196                                 // Non-image file with wrong sub-type.
    1197                                 array(
    1198                                         DIR_TESTDATA . '/uploads/pages-to-word.docx',
    1199                                         'pages-to-word.docx',
     1218                                        // Non-image file with wrong sub-type.
    12001219                                        array(
    1201                                                 'ext' => 'docx',
    1202                                                 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
    1203                                                 'proper_filename' => false,
     1220                                                DIR_TESTDATA . '/uploads/pages-to-word.docx',
     1221                                                'pages-to-word.docx',
     1222                                                array(
     1223                                                        'ext' => 'docx',
     1224                                                        'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
     1225                                                        'proper_filename' => false,
     1226                                                ),
    12041227                                        ),
    1205                                 ),
    1206                                 // FLAC file.
    1207                                 array(
    1208                                         DIR_TESTDATA . '/uploads/small-audio.flac',
    1209                                         'small-audio.flac',
     1228                                        // FLAC file.
    12101229                                        array(
    1211                                                 'ext' => 'flac',
    1212                                                 'type' => 'audio/flac',
    1213                                                 'proper_filename' => false,
     1230                                                DIR_TESTDATA . '/uploads/small-audio.flac',
     1231                                                'small-audio.flac',
     1232                                                array(
     1233                                                        'ext' => 'flac',
     1234                                                        'type' => 'audio/flac',
     1235                                                        'proper_filename' => false,
     1236                                                ),
    12141237                                        ),
    1215                                 ),
    1216                         ) );
     1238                                        // Assorted text/* sample files
     1239                                        array(
     1240                                                DIR_TESTDATA . '/uploads/test.vtt',
     1241                                                'test.vtt',
     1242                                                array(
     1243                                                        'ext'             => 'vtt',
     1244                                                        'type'            => 'text/vtt',
     1245                                                        'proper_filename' => false,
     1246                                                ),
     1247                                        ),
     1248                                        array(
     1249                                                DIR_TESTDATA . '/uploads/test.csv',
     1250                                                'test.csv',
     1251                                                array(
     1252                                                        'ext'             => 'csv',
     1253                                                        'type'            => 'text/csv',
     1254                                                        'proper_filename' => false,
     1255                                                ),
     1256                                        ),
     1257                                        // RTF files.
     1258                                        array(
     1259                                                DIR_TESTDATA . '/uploads/test.rtf',
     1260                                                'test.rtf',
     1261                                                array(
     1262                                                        'ext'             => 'rtf',
     1263                                                        'type'            => 'application/rtf',
     1264                                                        'proper_filename' => false,
     1265                                                ),
     1266                                        ),
     1267                                )
     1268                        );
    12171269                }
    12181270
Note: See TracChangeset for help on using the changeset viewer.