-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Image: sideload external images on the server when uploading to the library #79409
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
226429e
Image: sideload external images on the server when uploading to the l…
adamsilverstein 81dd959
Image: add tests for external URL sideload
adamsilverstein c0ef781
Update CHANGELOG references to the upstream PR
adamsilverstein dfb6a9c
Image: require upload capability before sideloading an external URL
adamsilverstein 9f5d1be
Image: validate sideload filename and set Location header
adamsilverstein 5d59c87
Merge branch 'trunk' into fix/external-image-server-sideload
adamsilverstein 6153573
Add backport changelog link to core PR 12268
adamsilverstein e0f4961
Merge branch 'trunk' into fix/external-image-server-sideload
adamsilverstein 7d6c3e0
Merge remote-tracking branch 'origin/trunk' into fix/external-image-s…
adamsilverstein d0617fe
Merge branch 'trunk' into fix/external-image-server-sideload
adamsilverstein 7ae78e6
Tests: Guard ReflectionMethod::setAccessible() for PHP 8.5
adamsilverstein ccae925
Image: register mediaSideloadFromUrl as a private block editor setting
adamsilverstein 3c2213e
Merge remote-tracking branch 'origin/trunk' into fix/external-image-s…
adamsilverstein bbeaf2e
Image: Fire rest_after_insert_attachment on the URL sideload path.
adamsilverstein 300cf54
Image: Validate the sideload url against SSRF.
adamsilverstein e488275
Update lib/media/class-gutenberg-rest-attachments-controller.php
adamsilverstein c38f8d4
Re-apply schema validation in the url validate_callback
adamsilverstein 1739120
Merge remote-tracking branch 'origin/trunk' into fix/external-image-s…
adamsilverstein a969a3a
Merge branch 'trunk' into fix/external-image-server-sideload
adamsilverstein 81d7825
Apply suggestions from code review
adamsilverstein 6242718
Reject non-image URL extensions before downloading a sideload
adamsilverstein ebb6f65
Generate sub-sizes by default when sideloading external images
adamsilverstein d2ffa0b
Merge branch 'trunk' into fix/external-image-server-sideload
adamsilverstein 9abd293
Add e2e coverage for button-gated external image upload
adamsilverstein 15012f9
Merge remote-tracking branch 'origin/fix/external-image-server-sidelo…
adamsilverstein 1570c0c
Specify value types in get_endpoint_args_for_item_schema() return doc…
adamsilverstein 72e385e
Merge branch 'trunk' into fix/external-image-server-sideload
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/12268 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/79409 |
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 |
|---|---|---|
|
|
@@ -268,7 +268,7 @@ public function create_item_permissions_check( $request ) { | |
| * @param string $method Optional. HTTP method of the request. The arguments for `CREATABLE` requests are | ||
| * checked for required values and may fall-back to a given default, this is not done | ||
| * on `EDITABLE` requests. Default WP_REST_Server::CREATABLE. | ||
| * @return array Endpoint arguments. | ||
| * @return array<string, array<string, mixed>> Endpoint arguments keyed by argument name. | ||
| */ | ||
| public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) { | ||
| $args = rest_get_endpoint_args_for_schema( $this->get_item_schema(), $method ); | ||
|
|
@@ -284,6 +284,39 @@ public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CRE | |
| 'default' => true, | ||
| 'description' => __( 'Whether to convert image formats.', 'gutenberg' ), | ||
| ); | ||
| $args['url'] = array( | ||
| 'type' => 'string', | ||
| 'format' => 'uri', | ||
| 'description' => __( 'URL of an external image to sideload into the media library, instead of uploading a file.', 'gutenberg' ), | ||
| 'sanitize_callback' => 'sanitize_url', | ||
| 'validate_callback' => static function ( $url, WP_REST_Request $request, string $param ) { | ||
| /* | ||
| * A custom validate_callback replaces the default | ||
| * rest_validate_request_arg(), so re-apply it first to keep | ||
| * the schema checks (string type, uri format) enforced. | ||
| */ | ||
| $valid = rest_validate_request_arg( $url, $request, $param ); | ||
| if ( is_wp_error( $valid ) ) { | ||
| return $valid; | ||
| } | ||
|
adamsilverstein marked this conversation as resolved.
|
||
| /** @var non-empty-string $url */ | ||
|
|
||
| /* | ||
| * Reject URLs that are not safe to request server-side. wp_http_validate_url() | ||
| * enforces an HTTP(S) scheme and blocks private, local, and otherwise | ||
| * disallowed hosts, guarding the sideload against SSRF. | ||
| */ | ||
| if ( false === wp_http_validate_url( $url ) ) { | ||
| return new WP_Error( | ||
| 'rest_invalid_url', | ||
| __( 'Invalid URL. Provide a valid, publicly reachable HTTP or HTTPS image URL.', 'gutenberg' ), | ||
| array( 'status' => 400 ) | ||
| ); | ||
| } | ||
|
|
||
| return true; | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| return $args; | ||
|
|
@@ -553,7 +586,17 @@ public function create_item( $request ) { | |
| add_filter( 'image_editor_output_format', '__return_empty_array', 100 ); | ||
| } | ||
|
|
||
| $response = parent::create_item( $request ); | ||
| /* | ||
| * When a URL is supplied instead of an uploaded file, sideload the | ||
| * remote image on the server. This avoids a cross-origin browser fetch, | ||
| * which fails under cross-origin isolation. The sub-size and scaling | ||
| * filters applied above still govern whether derivatives are generated. | ||
| */ | ||
| if ( ! empty( $request['url'] ) ) { | ||
| $response = $this->create_item_from_url( $request ); | ||
| } else { | ||
| $response = parent::create_item( $request ); | ||
| } | ||
|
Comment on lines
+589
to
+599
Member
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. Also makes sense to me. Great idea. My first thought was, " |
||
|
|
||
| remove_filter( 'intermediate_image_sizes_advanced', '__return_empty_array', 100 ); | ||
| remove_filter( 'fallback_intermediate_image_sizes', '__return_empty_array', 100 ); | ||
|
|
@@ -593,6 +636,106 @@ public function create_item( $request ) { | |
| return $response; | ||
| } | ||
|
|
||
| /** | ||
| * Sideloads an external image from a URL into the media library. | ||
| * | ||
| * Downloads the remote file on the server, avoiding a cross-origin browser | ||
| * fetch that fails under cross-origin isolation. Whether sub-sizes are | ||
| * generated is governed by the filters applied in create_item(). | ||
| * | ||
| * @param WP_REST_Request $request Full details about the request. | ||
| * @return WP_REST_Response|WP_Error Response object on success, WP_Error object on failure. | ||
| */ | ||
| private function create_item_from_url( WP_REST_Request $request ) { | ||
| // Sideloading downloads and stores a file, so require the upload capability. | ||
| if ( ! current_user_can( 'upload_files' ) ) { | ||
| return new WP_Error( | ||
| 'rest_cannot_create', | ||
| __( 'Sorry, you are not allowed to upload media on this site.', 'gutenberg' ), | ||
| array( 'status' => rest_authorization_required_code() ) | ||
| ); | ||
| } | ||
|
|
||
| require_once ABSPATH . 'wp-admin/includes/file.php'; | ||
| require_once ABSPATH . 'wp-admin/includes/media.php'; | ||
| require_once ABSPATH . 'wp-admin/includes/image.php'; | ||
|
|
||
| $url = $request['url']; | ||
| $post_id = ! empty( $request['post'] ) ? (int) $request['post'] : 0; | ||
|
|
||
| // Derive the filename from the URL path before downloading anything. | ||
| $url_path = wp_parse_url( $url, PHP_URL_PATH ); | ||
| $filename = $url_path ? wp_basename( $url_path ) : ''; | ||
| if ( '' === $filename ) { | ||
| return new WP_Error( | ||
| 'rest_invalid_url', | ||
| __( 'Could not determine a filename from the provided URL.', 'gutenberg' ), | ||
| array( 'status' => 400 ) | ||
| ); | ||
| } | ||
|
|
||
| /* | ||
| * Only download URLs whose extension maps to an allowed image MIME type. | ||
| * The sideload handler would reject other types anyway (via | ||
| * wp_check_filetype_and_ext()), but checking first avoids downloading | ||
| * files that can never be accepted, such as PHP scripts. | ||
| */ | ||
| $filetype = wp_check_filetype( $filename ); | ||
| if ( ! $filetype['type'] || ! str_starts_with( $filetype['type'], 'image/' ) ) { | ||
| return new WP_Error( | ||
| 'rest_invalid_url', | ||
| __( 'The provided URL does not point to a supported image file.', 'gutenberg' ), | ||
| array( 'status' => 400 ) | ||
| ); | ||
| } | ||
|
|
||
| /* | ||
| * 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. | ||
| */ | ||
| $tmp_file = download_url( $url ); | ||
| if ( is_wp_error( $tmp_file ) ) { | ||
| return $tmp_file; | ||
| } | ||
|
|
||
| $file_array = array( | ||
| 'name' => $filename, | ||
| 'tmp_name' => $tmp_file, | ||
| ); | ||
|
|
||
| $attachment_id = media_handle_sideload( $file_array, $post_id ); | ||
|
|
||
| if ( is_wp_error( $attachment_id ) ) { | ||
| /* | ||
| * media_handle_sideload() deletes the temp file on success; remove | ||
| * it explicitly when the sideload fails. | ||
| */ | ||
| if ( file_exists( $tmp_file ) ) { | ||
| wp_delete_file( $tmp_file ); | ||
| } | ||
| return $attachment_id; | ||
| } | ||
|
|
||
| $attachment = get_post( $attachment_id ); | ||
|
|
||
| $request->set_param( 'context', 'edit' ); | ||
|
|
||
| /* | ||
| * media_handle_sideload() fires the standard insert hooks (including | ||
| * wp_after_insert_post), but not the REST-specific action, so fire it | ||
| * here for parity with the uploaded-file path in create_item(). | ||
| */ | ||
| /** This action is documented in wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php */ | ||
| do_action( 'rest_after_insert_attachment', $attachment, $request, true ); | ||
|
|
||
| $response = $this->prepare_item_for_response( $attachment, $request ); | ||
| $response->set_status( 201 ); | ||
| $response->header( 'Location', rest_url( rest_get_route_for_post( $attachment_id ) ) ); | ||
|
|
||
| return $response; | ||
| } | ||
|
|
||
| /** | ||
| * Finalizes an attachment after client-side media processing. | ||
| * | ||
|
|
||
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
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
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
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
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.
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.