Make WordPress Core

Changeset 43995


Ignore:
Timestamp:
12/12/2018 11:13:21 PM (7 years ago)
Author:
jeremyfelt
Message:

Media: Improve verification of MIME file types.

Merges [43988] to the 4.4 branch.

Location:
branches/4.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

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

    r43398 r43995  
    22432243            }
    22442244        }
    2245     } elseif ( function_exists( 'finfo_file' ) ) {
    2246         // Use finfo_file if available to validate non-image files.
     2245    }
     2246
     2247    // Validate files that didn't get validated during previous checks.
     2248    if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) {
    22472249        $finfo = finfo_open( FILEINFO_MIME_TYPE );
    22482250        $real_mime = finfo_file( $finfo, $file );
    22492251        finfo_close( $finfo );
    22502252
    2251         // If the extension does not match the file's real type, return false.
    2252         if ( $real_mime !== $type ) {
     2253        // fileinfo often misidentifies obscure files as one of these types
     2254        $nonspecific_types = array(
     2255            'application/octet-stream',
     2256            'application/encrypted',
     2257            'application/CDFV2-encrypted',
     2258            'application/zip',
     2259        );
     2260
     2261        /*
     2262         * If $real_mime doesn't match the content type we're expecting from the file's extension,
     2263         * we need to do some additional vetting. Media types and those listed in $nonspecific_types are
     2264         * allowed some leeway, but anything else must exactly match the real content type.
     2265         */
     2266        if ( in_array( $real_mime, $nonspecific_types, true ) ) {
     2267            // File is a non-specific binary type. That's ok if it's a type that generally tends to be binary.
     2268            if ( !in_array( substr( $type, 0, strcspn( $type, '/' ) ), array( 'application', 'video', 'audio' ) ) ) {
     2269                $type = $ext = false;
     2270            }
     2271        } elseif ( 0 === strpos( $real_mime, 'video/' ) || 0 === strpos( $real_mime, 'audio/' ) ) {
     2272            /*
     2273             * For these types, only the major type must match the real value.
     2274             * This means that common mismatches are forgiven: application/vnd.apple.numbers is often misidentified as application/zip,
     2275             * and some media files are commonly named with the wrong extension (.mov instead of .mp4)
     2276             */
     2277
     2278            if ( substr( $real_mime, 0, strcspn( $real_mime, '/' ) ) !== substr( $type, 0, strcspn( $type, '/' ) ) ) {
     2279                $type = $ext = false;
     2280            }
     2281        } else {
     2282            if ( $type !== $real_mime ) {
     2283                /*
     2284                 * Everything else including image/* and application/*:
     2285                 * If the real content type doesn't match the file extension, assume it's dangerous.
     2286                 */
     2287                $type = $ext = false;
     2288            }
     2289
     2290        }
     2291    }
     2292
     2293    // The mime type must be allowed
     2294    if ( $type ) {
     2295        $allowed = get_allowed_mime_types();
     2296
     2297        if ( ! in_array( $type, $allowed ) ) {
    22532298            $type = $ext = false;
    22542299        }
Note: See TracChangeset for help on using the changeset viewer.