Skip to content

REST API: Add dimension validation to sideload endpoint#11100

Open
adamsilverstein wants to merge 7 commits intoWordPress:trunkfrom
adamsilverstein:add-dimension-validation-to-sideload
Open

REST API: Add dimension validation to sideload endpoint#11100
adamsilverstein wants to merge 7 commits intoWordPress:trunkfrom
adamsilverstein:add-dimension-validation-to-sideload

Conversation

@adamsilverstein
Copy link
Member

Summary

Builds on #11015. Adds dimension validation to the sideload endpoint.

  • Adds validate_image_dimensions() private method to WP_REST_Attachments_Controller
  • Validates uploaded image dimensions against expected size constraints in the wp/v2/media/<id>/sideload endpoint
  • Moves wp_getimagesize() call earlier in sideload_item() to validate before metadata handling

Validation rules:

  • 'original' size: must match original attachment dimensions exactly
  • 'full' and 'scaled' sizes: requires positive dimensions only
  • Regular sizes: dimensions must not exceed registered size maximums (with 1px tolerance for rounding differences)

Test plan

  • test_sideload_item_rejects_oversized_dimensions — uploads 640x480 image as thumbnail (150x150), expects 400 with rest_upload_dimension_mismatch
  • test_sideload_item_accepts_valid_dimensions — uploads 50x50 image as thumbnail, expects 200

Corresponding Gutenberg PR: WordPress/gutenberg#74903

🤖 Generated with Claude Code

adamsilverstein and others added 7 commits February 23, 2026 18:08
When client-side media processing handles big image scaling,
the client creates a -scaled version and sideloads it back.
The sideload route's image_size enum was missing 'scaled',
causing 400 validation errors.

This adds 'scaled' to the enum, adds handling in sideload_item()
to record the original file and update the attachment to point
to the scaled version, and updates the unique filename filter
regex to recognize the -scaled suffix.
Add 'scaled' to the image_size enum in wp-api-generated.js to match the
PHP route registration change, fixing the git diff --exit-code CI check.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add tests for the new 'scaled' image_size enum value in the sideload
endpoint: verifying metadata updates, authentication requirements,
route schema, and unique filename handling for the -scaled suffix.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
get_attached_file() can return false when no file is
attached. Add a guard to return a WP_Error before calling
wp_basename() with a falsy value.
The sideload route uses edit_media_item_permissions_check
which returns rest_cannot_edit_image, not rest_forbidden.
Validates uploaded image dimensions against expected size constraints
in the wp/v2/media/<id>/sideload endpoint. This prevents users from
uploading incorrectly-sized images for a specified image size.

Validation rules:
- 'original' size: must match original attachment dimensions exactly.
- 'full' and 'scaled' sizes: requires positive dimensions only.
- Regular sizes: dimensions must not exceed registered size maximums
  (with 1px tolerance for rounding differences).

Also adds two new test cases for dimension validation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions
Copy link

github-actions bot commented Mar 1, 2026

Trac Ticket Missing

This pull request is missing a link to a Trac ticket. For a contribution to be considered, there must be a corresponding ticket in Trac.

To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. More information about contributing to WordPress on GitHub can be found in the Core Handbook.

@github-actions
Copy link

github-actions bot commented Mar 1, 2026

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 adamsilverstein.

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

@github-actions
Copy link

github-actions bot commented Mar 1, 2026

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.

Comment on lines +1994 to +2000
/* translators: 1: Expected width, 2: expected height, 3: actual width, 4: actual height. */
__( 'Uploaded image dimensions (%3$dx%4$d) do not match original image dimensions (%1$dx%2$d).' ),
$expected_width,
$expected_height,
$width,
$height
),
Copy link

Choose a reason for hiding this comment

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

Any deeper reasoning why you chose to switch order in the printf template over the order of how they are in the sprintf()here?

In my eyes it would be simpler to have them (by default) in the same order as they are displayed, as this would make things easier to read and reduce cognitive load.

Comment on lines +2008 to +2018
// 'full' size (PDF thumbnails) and 'scaled': dimensions must be positive.
if ( 'full' === $image_size || 'scaled' === $image_size ) {
if ( $width <= 0 || $height <= 0 ) {
return new WP_Error(
'rest_upload_invalid_dimensions',
__( 'Uploaded image must have positive dimensions.' ),
array( 'status' => 400 )
);
}
return true;
}
Copy link

Choose a reason for hiding this comment

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

Is there any valid case where negative dimensions are actually allowed?

I see that empty could be allowable for SVGs, but I can't see any case where a negative integer is a valid dimension.

Comment on lines +2035 to +2041
// Dimensions must be positive.
if ( $width <= 0 || $height <= 0 ) {
return new WP_Error(
'rest_upload_invalid_dimensions',
__( 'Uploaded image must have positive dimensions.' ),
array( 'status' => 400 )
);
Copy link

Choose a reason for hiding this comment

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

see above, you're checking this here, if it is a valid case for having 2 checks, maybe this could be refactored into a function that performs the check, so you don't repeat yourself?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants