Opened 6 weeks ago
Last modified 3 weeks ago
#64798 new defect (bug)
REST API: Add dimension validation to sideload endpoint
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | 7.1 | Priority: | normal |
| Severity: | normal | Version: | |
| Component: | REST API | Keywords: | has-patch |
| Focuses: | Cc: |
Description (last modified by )
Summary
Backport for https://github.com/WordPress/wordpress-develop/pull/11100
Add image dimension validation to the wp/v2/media/<id>/sideload REST API endpoint to prevent uploading images with dimensions that don't match the target image size constraints.
Description
The REST API sideload endpoint (wp/v2/media/<id>/sideload) currently accepts uploaded images without validating that their dimensions are appropriate for the specified image_size. This means a client could sideload a 640x480 image as a thumbnail (which is registered as 150x150), producing incorrect metadata and potentially broken layouts.
This ticket adds a validate_image_dimensions() method to WP_REST_Attachments_Controller that validates uploaded image dimensions before processing, with size-specific rules:
Validation rules
originalsize: Uploaded dimensions must match the original attachment dimensions exactly.fullandscaledsizes: Only requires positive dimensions (no upper bound constraint).- Regular registered sizes (e.g.
thumbnail,medium,large): Dimensions must not exceed the registered size maximums, with a 1px tolerance for rounding differences. - Unknown sizes: Returns an error for unregistered image size names.
Implementation details
- Adds private method
validate_image_dimensions( int $width, int $height, string $image_size, int $attachment_id )toWP_REST_Attachments_Controller. - Moves the
wp_getimagesize()call earlier insideload_item()so dimensions are available for validation before metadata handling. - On validation failure, cleans up the uploaded file with
wp_delete_file()before returning the error. - Uses
wp_get_registered_image_subsizes()to look up size constraints for registered sizes.
Error codes
rest_upload_invalid_dimensions— Image has zero or negative dimensions.rest_upload_dimension_mismatch— Dimensions don't match expected constraints for the target size.rest_upload_unknown_size— The specifiedimage_sizeis not registered.
All errors return HTTP 400 status.
Change History (5)
This ticket was mentioned in PR #11100 on WordPress/wordpress-develop by @adamsilverstein.
6 weeks ago
#2
- Keywords has-patch added
## Summary
Builds on #11015. Adds dimension validation to the sideload endpoint.
validate_image_dimensions()private method toWP_REST_Attachments_Controllerwp/v2/media/<id>/sideloadendpointwp_getimagesize()call earlier insideload_item()to validate before metadata handling### Validation rules:
## Test plan
test_sideload_item_rejects_oversized_dimensions— uploads 640x480 image as thumbnail (150x150), expects 400 withrest_upload_dimension_mismatchtest_sideload_item_accepts_valid_dimensions— uploads 50x50 image as thumbnail, expects 200Corresponding Gutenberg PR: https://github.com/WordPress/gutenberg/pull/74903