Skip to content

Media REST API: Update sideload from url path to include an upload size check - #12670

Closed
andrewserong wants to merge 1 commit into
WordPress:trunkfrom
andrewserong:update/media-sideload-from-url-filesize-check
Closed

Media REST API: Update sideload from url path to include an upload size check#12670
andrewserong wants to merge 1 commit into
WordPress:trunkfrom
andrewserong:update/media-sideload-from-url-filesize-check

Conversation

@andrewserong

Copy link
Copy Markdown
Contributor

In WP 7.1, the /wp/v2/media REST API endpoint supports a url param to allow sideloading media via a url, without requiring the browser to first download an image and upload via a standard media upload POST request.

This PR adds a call to check_upload_size for this upload path, to create parity with the other upload paths (e.g. the multipart and raw-body upload paths).

To test manually that this doesn't regress the "upload to media library" feature, try adding an Image block to a post using an external url and click the "upload to media library button". It should work as on trunk. Here's what that button looks like:

image

And here's some test markup for you to try that out with:

<!-- wp:image {"sizeSlug":"large"} --><figureclass="wp-block-image size-large"><imgsrc="https://user-images.githubusercontent.com/1204802/100067796-fc3e8700-2e36-11eb-993b-6b80b4310b87.png" alt=""/></figure><!-- /wp:image -->

Trac ticket: https://core.trac.wordpress.org/ticket/65517

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Opus 4.8
Used for: Identifying the gap in upload paths; final implementation and tests were reviewed and edited by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

…heck for parity with the multipart and raw-body upload paths
@andrewserongandrewserong self-assigned this Jul 24, 2026
CopilotAI review requested due to automatic review settings July 24, 2026 00:22
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

CopilotAI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the REST /wp/v2/media “sideload from url” upload path to enforce multisite upload size/quota limits by invoking the same check_upload_size() logic used by other upload paths, and adds PHPUnit coverage to prevent regressions.

Changes:

  • Add a multisite upload-size/quota check to WP_REST_Attachments_Controller::create_item_from_url() before sideloading.
  • Ensure the temporary downloaded file is deleted when the new size/quota check fails.
  • Add multisite REST API tests asserting the correct 400 error codes for max-file-size and site quota exceedance on the url upload path.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

FileDescription
tests/phpunit/tests/rest-api/rest-attachments-controller.phpAdds multisite tests covering size/quota enforcement for URL-based media sideload.
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.phpEnforces multisite upload size/quota limits for the url sideload upload path and cleans up temp files on failure.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ramonjdramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the happy path (in the pr desc) and also by setting multisite upload and storage limits.

LGTM

😄

Image

😦

Image

☹️

Image

Also direct call using await wp.apiFetch

Image

Should we backport to Gutenberg?

@andrewserong

Copy link
Copy Markdown
ContributorAuthor

Should we backport to Gutenberg?

Indeed we should! Backport here: WordPress/gutenberg#80659

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props andrewserong, ramonopoly.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

pento pushed a commit that referenced this pull request Jul 24, 2026
… 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: #12670
Follow-up to [62659].
Props andrewserong, ramonopoly.
Fixes #65517.
git-svn-id: https://develop.svn.wordpress.org/trunk@62841 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 62841
GitHub commit: 58b28ff

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Jul 24, 2026
… 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: WordPress/wordpress-develop#12670
Follow-up to [62659].
Props andrewserong, ramonopoly.
Fixes #65517.
Built from https://develop.svn.wordpress.org/trunk@62841
git-svn-id: http://core.svn.wordpress.org/trunk@62121 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@andrewserong@ramonjd