Make WordPress Core

Opened 5 weeks ago

Last modified 4 weeks ago

#63294 new defect (bug)

Got this error when send image via REST API on WP 6.8

Reported by: astraport's profile Astraport Owned by:
Milestone: Awaiting Review Priority: normal
Severity: critical Version: 6.8
Component: REST API Keywords: reporter-feedback
Focuses: Cc:

Description

{"code":"rest_upload_image_type_not_supported","message":"The web server cannot generate responsive image sizes for this image. Convert it to JPEG or PNG before uploading.","data":{"status":400}}

Attachments (1)

2025-04-17_14h52_23.png (32.4 KB) - added by QuietNoise 5 weeks ago.
The same image uploaded through the Media library gets through without problems but uploading that image through Upload function will fail

Download all attachments as: .zip

Change History (9)

#1 follow-up: @mukesh27
5 weeks ago

  • Component changed from XML-RPC to REST API

Hi there!

Could you please share image details and rest API call if possible?

#2 in reply to: ↑ 1 @Astraport
5 weeks ago

Replying to mukesh27:

Hi there!

Could you please share image details and rest API call if possible?

Hi

Any image, it doesn't matter. But if I rename the image extension to JPEG instead of JPG and change "Content-Type: image/jpeg" in the request instead of "Content-Type: image/jpg", then everything works and image uploads successfully.

#3 follow-up: @oglekler
5 weeks ago

  • Keywords reporter-feedback added; needs-patch removed

Hi @Astraport if your images have MIME types that are not aligned with the image format or are outside the limits, they will not be accepted. This is normal behaviour and should be expected, you don't want some virus pretended to be an image and such things are happening.

To see what types are allowed, you can check wp_get_mime_types() function.

Please clarify what MIME types and formats you have and where they come from to move forward — how it has happened in the first place, usually people don't have such issues at all. Possibly you need to check with the image source.

This ticket was mentioned in Slack in #core by jorbin. View the logs.


5 weeks ago

#5 @johnbillion
5 weeks ago

Additionally if you visit the Tools -> Site Health menu then switch to the Info tab and open the Media Handling section, you can see which mime types are supported by your server.

@QuietNoise
5 weeks ago

The same image uploaded through the Media library gets through without problems but uploading that image through Upload function will fail

#6 in reply to: ↑ 3 @QuietNoise
5 weeks ago

Replying to oglekler:

Hi @Astraport if your images have MIME types that are not aligned with the image format or are outside the limits, they will not be accepted. This is normal behaviour and should be expected, you don't want some virus pretended to be an image and such things are happening.

To see what types are allowed, you can check wp_get_mime_types() function.

Please clarify what MIME types and formats you have and where they come from to move forward — how it has happened in the first place, usually people don't have such issues at all. Possibly you need to check with the image source.

I believe there is more to that. The same image uploaded through the Media library gets through without problems but uploading that image through Upload function will fail with the error mentioned by the OP.
Similarly drag and dropping image into media upload box in Editor will fail with same error.
Attached image above for reference what I'm talking about.

Also this works without issues in older versions i.e. 6.7.1 and 6.7.2. The problem was introduced in 6.8 seems.

Edit:
I nailed it down to new code introduced in function create_item_permissions_check() in version 6.8.

Further down the code it appears that wp_image_editor_supports() returns false if image editing lirary is not enabled on server.

I believe this is not a correct approach since whether file can be edited or not should not determine whether file can be uploaded or not. Not all servers have support for Imagick or GD and these should not be punished if admins are not planning to use image editing feature in a first place.

Workaround
Together with the new code mentioned above a new filter was introduced in WP 6.8: wp_prevent_unsupported_mime_type_uploads
It can be used to disable this new behaviour.

Nuclear option would be:

<?php
add_filter( 'wp_prevent_unsupported_mime_type_uploads', '__return_false' );

More gradual option would be to expand the function to check what mime type is passed and return false only for images or whatever other file type affected in your project.
The filter function passes mime type string. So, it could be something like this:

<?php
add_filter( 'wp_prevent_unsupported_mime_type_uploads', function($current, $mime) {
        return str_starts_with( $mime, 'image/' ) ? false : $current;
}, 10, 2);
Last edited 5 weeks ago by QuietNoise (previous) (diff)

#7 @nsladen
5 weeks ago

+1 for this. I use the REST API to upload SVG images to the media library and this 100% broke after upgrading my site to 6.8.

The suggested workaround above works for me and SVG upload works again. Thanks @QuietNoise!

add_filter( 'wp_prevent_unsupported_mime_type_uploads', function( $prevent_upload, $mime_type ) {
    if ( 'image/svg+xml' === $mime_type ) {
        return false;
    }
    return $prevent_upload;

}, 10, 2 );
Last edited 5 weeks ago by nsladen (previous) (diff)

#8 @paperboy123
4 weeks ago

+1 for this. Also used @QuietNoise solution since i need SVG upload (via REST-API) for my page builder. It appears that this change was not really thought through.

Note: See TracTickets for help on using tickets.