Make WordPress Core

Changeset 62841


Ignore:
Timestamp:
07/24/2026 02:42:23 AM (less than one hour ago)
Author:
andrewserong
Message:

REST API: Enforce multisite upload limits when sideloading media from a URL.

The attachments controller's URL-based creation path, create_item_from_url(), passed the downloaded file to media_handle_sideload() without running check_upload_size(). Unlike the multipart and raw-body upload paths, it did not enforce the multisite maximum file size or the site's upload space quota.

Run check_upload_size() on the downloaded file before sideloading it, for parity with the other upload paths, and remove the temporary file when the check fails.

Developed in: https://github.com/WordPress/wordpress-develop/pull/12670

Follow-up to [62659].

Props andrewserong, ramonopoly.

Fixes #65517.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r62820 r62841  
    641641                        'tmp_name' => $tmp_file,
    642642                );
     643
     644                $size_check = self::check_upload_size( $file_array );
     645                if ( is_wp_error( $size_check ) ) {
     646                        if ( file_exists( $tmp_file ) ) {
     647                                wp_delete_file( $tmp_file );
     648                        }
     649                        return $size_check;
     650                }
    643651
    644652                $attachment_id = media_handle_sideload( $file_array, $post_id );
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r62820 r62841  
    53325332
    53335333        /**
     5334         * Verifies that the URL sideload path enforces the multisite maximum file
     5335         * size, for parity with the multipart and raw-body upload paths.
     5336         *
     5337         * @ticket 65517
     5338         * @group multisite
     5339         * @group ms-required
     5340         *
     5341         * @covers WP_REST_Attachments_Controller::create_item_from_url
     5342         * @covers WP_REST_Attachments_Controller::check_upload_size
     5343         */
     5344        public function test_create_item_from_url_exceeds_multisite_max_filesize() {
     5345                $this->enable_client_side_media_processing();
     5346
     5347                wp_set_current_user( self::$superadmin_id );
     5348                update_site_option( 'fileupload_maxk', 1 );
     5349                update_site_option( 'upload_space_check_disabled', false );
     5350
     5351                // Ensure ample space is available so the file-size limit is what rejects it.
     5352                add_filter( 'pre_get_space_used', '__return_zero' );
     5353                add_filter( 'pre_http_request', array( $this, 'mock_image_download' ), 10, 3 );
     5354
     5355                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     5356                $request->set_param( 'url', 'https://example.com/too-big.jpg' );
     5357                $request->set_param( 'generate_sub_sizes', false );
     5358
     5359                $response = rest_get_server()->dispatch( $request );
     5360
     5361                remove_filter( 'pre_http_request', array( $this, 'mock_image_download' ), 10 );
     5362
     5363                $this->assertErrorResponse( 'rest_upload_file_too_big', $response, 400 );
     5364        }
     5365
     5366        /**
     5367         * Verifies that the URL sideload path enforces the multisite site upload
     5368         * space quota, for parity with the multipart and raw-body upload paths.
     5369         *
     5370         * @ticket 65517
     5371         * @group multisite
     5372         * @group ms-required
     5373         *
     5374         * @covers WP_REST_Attachments_Controller::create_item_from_url
     5375         * @covers WP_REST_Attachments_Controller::check_upload_size
     5376         */
     5377        public function test_create_item_from_url_exceeds_multisite_site_upload_space() {
     5378                $this->enable_client_side_media_processing();
     5379
     5380                wp_set_current_user( self::$superadmin_id );
     5381                add_filter( 'get_space_allowed', '__return_zero' );
     5382                update_site_option( 'upload_space_check_disabled', false );
     5383
     5384                add_filter( 'pre_http_request', array( $this, 'mock_image_download' ), 10, 3 );
     5385
     5386                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     5387                $request->set_param( 'url', 'https://example.com/no-space.jpg' );
     5388                $request->set_param( 'generate_sub_sizes', false );
     5389
     5390                $response = rest_get_server()->dispatch( $request );
     5391
     5392                remove_filter( 'pre_http_request', array( $this, 'mock_image_download' ), 10 );
     5393
     5394                $this->assertErrorResponse( 'rest_upload_limited_space', $response, 400 );
     5395        }
     5396
     5397        /**
    53345398         * Verifies that a URL with no usable path bails with a 400 before any
    53355399         * download is attempted, rather than handing an empty filename to the
Note: See TracChangeset for help on using the changeset viewer.