Make WordPress Core


Ignore:
Timestamp:
12/12/2018 11:16:07 PM (8 years ago)
Author:
jeremyfelt
Message:

Media: Improve verification of MIME file types.

Merges [43988] to the 4.3 branch.

Location:
branches/4.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.3

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

    r43399 r43996  
    21682168                        }
    21692169                }
    2170         } elseif ( function_exists( 'finfo_file' ) ) {
    2171                 // Use finfo_file if available to validate non-image files.
     2170        }
     2171
     2172        // Validate files that didn't get validated during previous checks.
     2173        if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) {
    21722174                $finfo = finfo_open( FILEINFO_MIME_TYPE );
    21732175                $real_mime = finfo_file( $finfo, $file );
    21742176                finfo_close( $finfo );
    21752177
    2176                 // If the extension does not match the file's real type, return false.
    2177                 if ( $real_mime !== $type ) {
     2178                // fileinfo often misidentifies obscure files as one of these types
     2179                $nonspecific_types = array(
     2180                        'application/octet-stream',
     2181                        'application/encrypted',
     2182                        'application/CDFV2-encrypted',
     2183                        'application/zip',
     2184                );
     2185
     2186                /*
     2187                 * If $real_mime doesn't match the content type we're expecting from the file's extension,
     2188                 * we need to do some additional vetting. Media types and those listed in $nonspecific_types are
     2189                 * allowed some leeway, but anything else must exactly match the real content type.
     2190                 */
     2191                if ( in_array( $real_mime, $nonspecific_types, true ) ) {
     2192                        // File is a non-specific binary type. That's ok if it's a type that generally tends to be binary.
     2193                        if ( !in_array( substr( $type, 0, strcspn( $type, '/' ) ), array( 'application', 'video', 'audio' ) ) ) {
     2194                                $type = $ext = false;
     2195                        }
     2196                } elseif ( 0 === strpos( $real_mime, 'video/' ) || 0 === strpos( $real_mime, 'audio/' ) ) {
     2197                        /*
     2198                         * For these types, only the major type must match the real value.
     2199                         * This means that common mismatches are forgiven: application/vnd.apple.numbers is often misidentified as application/zip,
     2200                         * and some media files are commonly named with the wrong extension (.mov instead of .mp4)
     2201                         */
     2202
     2203                        if ( substr( $real_mime, 0, strcspn( $real_mime, '/' ) ) !== substr( $type, 0, strcspn( $type, '/' ) ) ) {
     2204                                $type = $ext = false;
     2205                        }
     2206                } else {
     2207                        if ( $type !== $real_mime ) {
     2208                                /*
     2209                                 * Everything else including image/* and application/*:
     2210                                 * If the real content type doesn't match the file extension, assume it's dangerous.
     2211                                 */
     2212                                $type = $ext = false;
     2213                        }
     2214
     2215                }
     2216        }
     2217
     2218        // The mime type must be allowed
     2219        if ( $type ) {
     2220                $allowed = get_allowed_mime_types();
     2221
     2222                if ( ! in_array( $type, $allowed ) ) {
    21782223                        $type = $ext = false;
    21792224                }
Note: See TracChangeset for help on using the changeset viewer.