-
Notifications
You must be signed in to change notification settings - Fork 3.6k
REST API: Bound the size of media sideloaded from a URL #12825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
adamsilverstein
wants to merge
4
commits into
WordPress:trunk
from
adamsilverstein:fix/65517-sideload-size-limit
+134
−0
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4a954db
REST API: Bound the size of media sideloaded from a URL.
adamsilverstein e47b7bb
Update src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-c…
adamsilverstein 4f98e3d
Merge branch 'trunk' into fix/65517-sideload-size-limit
adamsilverstein 84227f5
Merge branch 'trunk' into fix/65517-sideload-size-limit
adamsilverstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -630,12 +630,41 @@ protected function create_item_from_url( $request ) { | |
| ); | ||
| } | ||
|
|
||
| /* | ||
| * Cap the download at the same size the site would accept as a direct | ||
| * upload. check_upload_size() only applies on multisite, so without a | ||
| * ceiling here a single site has no limit at all on this path: the | ||
| * `upload_max_filesize` and `post_max_size` directives bound a request | ||
| * body, not a fetch the server makes itself. | ||
| * | ||
| * When `wp_max_upload_size` returns 0, no ceiling is applied. | ||
| */ | ||
| $max_size = (int) wp_max_upload_size(); | ||
|
|
||
| /* | ||
| * Download the remote file with WordPress's HTTP API, which validates | ||
| * the host and blocks requests to private or local addresses. This is | ||
| * the same primitive core's media_sideload_image() relies on. | ||
| * | ||
| * `limit_response_size` stops the transfer once the limit is passed, | ||
| * so an oversized remote file is never written to disk in full. One | ||
| * byte over the ceiling is enough to fail the size check below. | ||
| */ | ||
| $limit_response_size = static function ( $args ) use ( $max_size ) { | ||
| $args['limit_response_size'] = $max_size + 1; | ||
| return $args; | ||
| }; | ||
|
|
||
| if ( $max_size > 0 ) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here is where we skip setting the limit. |
||
| add_filter( 'http_request_args', $limit_response_size ); | ||
| } | ||
|
|
||
| $tmp_file = download_url( $url ); | ||
|
|
||
| if ( $max_size > 0 ) { | ||
| remove_filter( 'http_request_args', $limit_response_size ); | ||
| } | ||
|
|
||
| if ( is_wp_error( $tmp_file ) ) { | ||
| return $tmp_file; | ||
| } | ||
|
|
@@ -653,6 +682,19 @@ protected function create_item_from_url( $request ) { | |
| return $size_check; | ||
| } | ||
|
|
||
| if ( $max_size > 0 && wp_filesize( $tmp_file ) > $max_size ) { | ||
| if ( file_exists( $tmp_file ) ) { | ||
| wp_delete_file( $tmp_file ); | ||
| } | ||
|
|
||
| return new WP_Error( | ||
| 'rest_upload_file_too_big', | ||
| /* translators: %s: Maximum allowed file size in kilobytes. */ | ||
| sprintf( __( 'This file is too big. Files must be less than %s KB in size.' ), number_format( $max_size / KB_IN_BYTES ) ), | ||
| array( 'status' => 400 ) | ||
| ); | ||
| } | ||
|
|
||
| $attachment_id = media_handle_sideload( $file_array, $post_id ); | ||
|
|
||
| if ( is_wp_error( $attachment_id ) ) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.