WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.3k stars 4.11k forks source link

Media upload component does not restrict uploaded files by allowedTypes prop. #25130

Open ThatStevensGuy opened 3 years ago

ThatStevensGuy commented 3 years ago

Describe the bug I am using a duplicate of the Post Featured Image component for a Newsletter attachment which saves to its own post meta.

I noticed that the MediaUpload component will restrict files in the Media Library to the allowed types set in the allowedTypes prop.

However it will still allow any file to be uploaded to the Upload files tab, and then selected.

To fix this I had to add my own mime detection to the onSelect action, which shouldn't really be necessary. Either the Select button should remain disabled after the wrong file type has been uploaded, or the file upload should not be allowed to enter the media library with an error notice.

To reproduce Add MediaUpload to a component, restrict mime types in allowedTypes, then head to the Upload files tab and upload an unsupported file type. It will not appear in the Media library tab, however you can select the file and add it without further mime type detection in your onSelect.

Expected behavior It should not allow the upload of unsupported file types, or not allow selection of the unsupported file.

Editor version (please complete the following information):

silvanarnet commented 3 years ago

I have the same problem with Wordpress 5.6

Mamaduka commented 3 years ago

I was able to reproduce this with WP 5.7.2 and Gutenberg trunk.

getsource commented 3 years ago

Related: #16297

getsource commented 2 years ago

Related: #18593

getsource commented 2 years ago

Related: https://core.trac.wordpress.org/ticket/39625

andreaslindahl commented 1 year ago

I'm still having this problem in 6.0.2 with the additional bug that allowedTypes isn't respected when you select a file from the media gallery either.

I only want to allow selection of images, but even with allowedTypes set to ["image"] I can still select, for example, videos

Mamaduka commented 1 year ago

Hi, @andreaslindahl

Can you share an example code from your plugin?

Core blocks use the same prop/settings, and I cannot reproduce the issue there.

andreaslindahl commented 1 year ago

I'm just doing this:

<MediaUpload
    onSelect={onSelectMedia}
    value={mediaId}
    allowedTypes={["image"]}
    render={({open}) => (
        <Button
            variant="secondary"
            icon={ mediaId ? 'edit' : 'plus' }
            onClick={open}
            > { mediaId ? 'Change media' : 'Choose media' }</Button>
      )}
/>
adamsilverstein commented 1 year ago

I took a look at this during contributor day, dropping a few notes here:

adamsilverstein commented 1 month ago

Here are some example screenshots of using Chrome and trying to upload a test HEIC format image to the editor and then to the media library (which prevents the upload entirely). These came up in the context of https://core.trac.wordpress.org/ticket/53645.

Editor upload: upload heic in editor

Media library upload (of same file): upload in media library

Mamaduka commented 1 month ago

@adamsilverstein, related to HEIC issue - #60244.