Make WordPress Core

Ticket #39550: 39550.5.diff

File 39550.5.diff, 6.3 KB (added by iandunn, 8 years ago)

Adds unit test for comment:95

  • src/wp-includes/functions.php

    diff --git src/wp-includes/functions.php src/wp-includes/functions.php
    index 865c0d2..f2b7809 100644
    function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { 
    22692269                return compact( 'ext', 'type', 'proper_filename' );
    22702270        }
    22712271
     2272        $real_mime = false;
     2273
    22722274        // Validate image types.
    22732275        if ( $type && 0 === strpos( $type, 'image/' ) ) {
    22742276
    22752277                // Attempt to figure out what type of image it actually is
    22762278                $real_mime = wp_get_image_mime( $file );
    22772279
    2278                 if ( ! $real_mime ) {
    2279                         $type = $ext = false;
    2280                 } elseif ( $real_mime != $type ) {
     2280                if ( $real_mime && $real_mime != $type ) {
    22812281                        /**
    22822282                         * Filters the list mapping image mime types to their respective extensions.
    22832283                         *
    function wp_check_filetype_and_ext( $file, $filename, $mimes = null ) { 
    23082308                                $ext = $wp_filetype['ext'];
    23092309                                $type = $wp_filetype['type'];
    23102310                        } else {
    2311                                 $type = $ext = false;
     2311                                // Reset MIME and try again with fi
     2312                                $real_mime = false;
    23122313                        }
    23132314                }
    2314         } elseif ( function_exists( 'finfo_file' ) ) {
    2315                 // Use finfo_file if available to validate non-image files.
     2315        }
     2316
     2317        // Validate files that didn't get validated during previous checks.
     2318        if ( $type && ! $real_mime && extension_loaded( 'fileinfo' ) ) {
    23162319                $finfo = finfo_open( FILEINFO_MIME_TYPE );
    23172320                $real_mime = finfo_file( $finfo, $file );
    23182321                finfo_close( $finfo );
    23192322
    2320                 // If the extension does not match the file's real type, return false.
    2321                 if ( $real_mime !== $type ) {
    2322                         $type = $ext = false;
     2323                // If the real mime doesn't match, do some extra vetting.
     2324                if ( $real_mime && ( $real_mime !== $type ) && ( 0 === strpos( $real_mime, 'application' ) ) ) {
     2325                        $allowed = get_allowed_mime_types();
     2326
     2327                        if ( ! in_array( $real_mime, $allowed ) ) {
     2328                                $type = $ext = false;
     2329                        }
    23232330                }
    23242331        }
    23252332
  • tests/phpunit/tests/functions.php

    diff --git tests/phpunit/tests/functions.php tests/phpunit/tests/functions.php
    index 6a9e35c..cf1a0d3 100644
    class Tests_Functions extends WP_UnitTestCase { 
    914914                $unique_uuids = array_unique( $uuids );
    915915                $this->assertEquals( $uuids, $unique_uuids );
    916916        }
     917
     918        /**
     919         * @ticket 39550
     920         * @dataProvider _wp_check_filetype_and_ext_data
     921         */
     922        function test_wp_check_filetype_and_ext( $file, $filename, $expected ) {
     923                if ( ! extension_loaded( 'fileinfo' ) ) {
     924                        $this->markTestSkipped( 'The fileinfo PHP extension is not loaded.' );
     925                }
     926
     927                $this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
     928        }
     929
     930        /**
     931         * @ticket 39550
     932         */
     933        function test_wp_check_filetype_and_ext_with_filtered_svg() {
     934                $file = DIR_TESTDATA . '/uploads/video-play.svg';
     935                $filename = 'video-play.svg';
     936
     937                $expected = array(
     938                        'ext' => 'svg',
     939                        'type' => 'image/svg+xml',
     940                        'proper_filename' => false,
     941                );
     942
     943                add_filter( 'upload_mimes', array( $this, '_filter_mime_types_svg' ) );
     944                $this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
     945
     946                // Cleanup.
     947                remove_filter( 'upload_mimes', array( $this, '_test_add_mime_types_svg' ) );
     948        }
     949
     950        /**
     951         * @ticket 39550
     952         */
     953        function test_wp_check_filetype_and_ext_with_filtered_woff() {
     954                $file = DIR_TESTDATA . '/uploads/dashicons.woff';
     955                $filename = 'dashicons.woff';
     956
     957                $expected = array(
     958                        'ext' => 'woff',
     959                        'type' => 'application/font-woff',
     960                        'proper_filename' => false,
     961                );
     962
     963                add_filter( 'upload_mimes', array( $this, '_filter_mime_types_woff' ) );
     964                $this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
     965
     966                // Cleanup.
     967                remove_filter( 'upload_mimes', array( $this, '_test_add_mime_types_woff' ) );
     968        }
     969
     970        /**
     971         * @ticket 39550
     972         */
     973        function test_wp_check_filetype_and_ext_with_filtered_ttml() {
     974                $file = DIR_TESTDATA . '/uploads/wp-4.7-subtitles.ttml';
     975                $filename = 'wp-4.7-subtitles.ttml';
     976
     977                $expected = array(
     978                        'ext' => 'ttml',
     979                        'type' => 'application/ttml+xml',
     980                        'proper_filename' => false,
     981                );
     982
     983                add_filter( 'upload_mimes', array( $this, '_filter_mime_types_ttml' ) );
     984                $this->assertEquals( $expected, wp_check_filetype_and_ext( $file, $filename ) );
     985
     986                // Cleanup.
     987                remove_filter( 'upload_mimes', array( $this, '_test_add_mime_types_ttml' ) );
     988        }
     989
     990        public function _filter_mime_types_svg( $mimes ) {
     991                $mimes['svg'] = 'image/svg+xml';
     992                return $mimes;
     993        }
     994
     995        public function _filter_mime_types_woff( $mimes ) {
     996                $mimes['woff'] = 'application/font-woff';
     997                return $mimes;
     998        }
     999
     1000        public function _filter_mime_types_ttml( $mimes ) {
     1001                $mimes['ttml'] = 'application/ttml+xml';
     1002                return $mimes;
     1003        }
     1004
     1005        public function _wp_check_filetype_and_ext_data() {
     1006                return array(
     1007                        // Standard image.
     1008                        array(
     1009                                DIR_TESTDATA . '/images/canola.jpg',
     1010                                'canola.jpg',
     1011                                array(
     1012                                        'ext' => 'jpg',
     1013                                        'type' => 'image/jpeg',
     1014                                        'proper_filename' => false,
     1015                                ),
     1016                        ),
     1017                        // Image with wrong extension.
     1018                        array(
     1019                                DIR_TESTDATA . '/images/test-image-mime-jpg.png',
     1020                                'test-image-mime-jpg.png',
     1021                                array(
     1022                                        'ext' => 'jpg',
     1023                                        'type' => 'image/jpeg',
     1024                                        'proper_filename' => 'test-image-mime-jpg.jpg',
     1025                                ),
     1026                        ),
     1027                        // Image without extension.
     1028                        array(
     1029                                DIR_TESTDATA . '/images/test-image-no-extension',
     1030                                'test-image-no-extension',
     1031                                array(
     1032                                        'ext' => false,
     1033                                        'type' => false,
     1034                                        'proper_filename' => false,
     1035                                ),
     1036                        ),
     1037                        // Valid non-image file with an image extension.
     1038                        array(
     1039                                DIR_TESTDATA . '/formatting/big5.txt',
     1040                                'big5.jpg',
     1041                                array(
     1042                                        'ext' => 'jpg',
     1043                                        'type' => 'image/jpeg',
     1044                                        'proper_filename' => false,
     1045                                ),
     1046                        ),
     1047                        // Standard non-image file.
     1048                        array(
     1049                                DIR_TESTDATA . '/formatting/big5.txt',
     1050                                'big5.txt',
     1051                                array(
     1052                                        'ext' => 'txt',
     1053                                        'type' => 'text/plain',
     1054                                        'proper_filename' => false,
     1055                                ),
     1056                        ),
     1057                        // Non-image file with wrong sub-type.
     1058                        array(
     1059                                DIR_TESTDATA . '/uploads/pages-to-word.docx',
     1060                                'pages-to-word.docx',
     1061                                array(
     1062                                        'ext' => 'docx',
     1063                                        'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
     1064                                        'proper_filename' => false,
     1065                                ),
     1066                        ),
     1067                        // Non-image file not allowed.
     1068                        array(
     1069                                DIR_TESTDATA . '/export/crazy-cdata.xml',
     1070                                'crazy-cdata.xml',
     1071                                array(
     1072                                        'ext' => false,
     1073                                        'type' => false,
     1074                                        'proper_filename' => false,
     1075                                ),
     1076                        ),
     1077                );
     1078        }
    9171079}