Make WordPress Core


Ignore:
Timestamp:
03/25/2025 02:04:47 PM (13 months ago)
Author:
adamsilverstein
Message:

Media: prevent uploading image types the server doesn’t support.

Normalize behavior between uploading in the media library and uploading directly to the block editor. Now, when uploading an image with a mime type the server does not support (either in the media library or the block editor), the user will see an error message “This image cannot be processed by the web server. Convert it to JPEG or PNG before uploading”.

Alos, add a new filter wp_prevent_unsupported_mime_type_uploads which determines whether the server should prevent uploads for image types it doesn't support. The default value is true and the filter also receives the uploaded image mime type.

Props: joomskys, adamsilverstein, azaozz, swissspidy, joemcgill, flixos90, audrasjb. 

Fixes #61167

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r60068 r60084  
    2929
    3030    /**
     31     * @var string The path to the AVIF test image.
     32     */
     33    private static $test_avif_file;
     34
     35    /**
    3136     * @var array The recorded posts query clauses.
    3237     */
     
    7378            unlink( self::$test_file2 );
    7479        }
     80        if ( file_exists( self::$test_avif_file ) ) {
     81            unlink( self::$test_avif_file );
     82        }
    7583
    7684        self::delete_user( self::$editor_id );
     
    100108        if ( ! file_exists( self::$test_file2 ) ) {
    101109            copy( $orig_file2, self::$test_file2 );
     110        }
     111
     112        $orig_avif_file       = DIR_TESTDATA . '/images/avif-lossy.avif';
     113        self::$test_avif_file = get_temp_dir() . 'avif-lossy.avif';
     114        if ( ! file_exists( self::$test_avif_file ) ) {
     115            copy( $orig_avif_file, self::$test_avif_file );
    102116        }
    103117
     
    25422556        );
    25432557    }
     2558
     2559    /**
     2560     * Test that uploading unsupported image types throws a `rest_upload_image_type_not_supported` error.
     2561     *
     2562     * @ticket 61167
     2563     */
     2564    public function test_upload_unsupported_image_type() {
     2565
     2566        // Only run this test when the editor doesn't support AVIF.
     2567        if ( wp_image_editor_supports( array( 'AVIF' ) ) ) {
     2568            $this->markTestSkipped( 'The image editor suppports AVIF.' );
     2569        }
     2570
     2571        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     2572
     2573        wp_set_current_user( self::$author_id );
     2574        $request->set_header( 'Content-Type', 'image/avif' );
     2575        $request->set_header( 'Content-Disposition', 'attachment; filename=avif-lossy.avif' );
     2576        $request->set_body( file_get_contents( self::$test_avif_file ) );
     2577        $response = rest_get_server()->dispatch( $request );
     2578
     2579        $this->assertErrorResponse( 'rest_upload_image_type_not_supported', $response, 400 );
     2580    }
     2581
     2582    /**
     2583     * Test that the `wp_prevent_unsupported_image_uploads` filter enables uploading of unsupported image types.
     2584     *
     2585     * @ticket 61167
     2586     */
     2587    public function test_upload_unsupported_image_type_with_filter() {
     2588
     2589        // Only run this test when the editor doesn't support AVIF.
     2590        if ( wp_image_editor_supports( array( 'AVIF' ) ) ) {
     2591            $this->markTestSkipped( 'The image editor suppports AVIF.' );
     2592        }
     2593
     2594        add_filter( 'wp_prevent_unsupported_image_uploads', '__return_false' );
     2595
     2596        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     2597
     2598        wp_set_current_user( self::$author_id );
     2599        $request->set_header( 'Content-Type', 'image/avif' );
     2600        $request->set_header( 'Content-Disposition', 'attachment; filename=avif-lossy.avif' );
     2601        $request->set_body( file_get_contents( self::$test_avif_file ) );
     2602        $response = rest_get_server()->dispatch( $request );
     2603
     2604        $this->assertSame( 201, $response->get_status() );
     2605    }
    25442606}
Note: See TracChangeset for help on using the changeset viewer.