-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Check allowed mime types before uploading media #6968
Changes from all commits
d044de0
6386d73
8fed14f
5b8f17d
fcff0f0
889c592
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
/** | ||
* External Dependencies | ||
*/ | ||
import { compact, forEach, get, noop, startsWith } from 'lodash'; | ||
import { compact, forEach, get, includes, noop, startsWith } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* WordPress dependencies | ||
|
@@ -38,15 +43,41 @@ export function mediaUpload( { | |
filesSet[ idx ] = value; | ||
onFileChange( compact( filesSet ) ); | ||
}; | ||
|
||
// Allowed type specified by consumer | ||
const isAllowedType = ( fileType ) => startsWith( fileType, `${ allowedType }/` ); | ||
|
||
// Allowed types for the current WP_User | ||
const allowedMimeTypesForUser = get( window, [ '_wpMediaSettings', 'allowedMimeTypes' ] ); | ||
const isAllowedMimeTypeForUser = ( fileType ) => { | ||
return includes( allowedMimeTypesForUser, fileType ); | ||
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. If because of some reason allowedMimeTypesForUser is undefined I think we should allow the upload and not reject the upload with a mimeType error. So maybe we should add here a check to see if allowedMimeTypesForUser is undefined. 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. Yes, it's doing the check right here, should be good to go 👍 |
||
}; | ||
|
||
files.forEach( ( mediaFile, idx ) => { | ||
if ( ! isAllowedType( mediaFile.type ) ) { | ||
return; | ||
} | ||
|
||
// verify if user is allowed to upload this mime type | ||
if ( allowedMimeTypesForUser && ! isAllowedMimeTypeForUser( mediaFile.type ) ) { | ||
onError( { | ||
code: 'MIME_TYPE_NOT_ALLOWED_FOR_USER', | ||
message: __( 'Sorry, this file type is not permitted for security reasons.' ), | ||
file: mediaFile, | ||
} ); | ||
return; | ||
} | ||
|
||
// verify if file is greater than the maximum file upload size allowed for the site. | ||
if ( maxUploadFileSize && mediaFile.size > maxUploadFileSize ) { | ||
onError( { sizeAboveLimit: true, file: mediaFile } ); | ||
onError( { | ||
code: 'SIZE_ABOVE_LIMIT', | ||
message: sprintf( | ||
__( '%s exceeds the maximum upload size for this site.' ), | ||
mediaFile.name | ||
), | ||
file: mediaFile, | ||
} ); | ||
return; | ||
} | ||
|
||
|
@@ -69,7 +100,14 @@ export function mediaUpload( { | |
() => { | ||
// Reset to empty on failure. | ||
setAndUpdateFiles( idx, null ); | ||
onError( { generalError: true, file: mediaFile } ); | ||
onError( { | ||
code: 'GENERAL', | ||
message: sprintf( | ||
__( 'Error while uploading file %s to the media library.' ), | ||
mediaFile.name | ||
), | ||
file: mediaFile, | ||
} ); | ||
} | ||
); | ||
} ); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you considered adding an optional property allowedMimeTypes that defaults to get( window, [ '_wpMediaSettings', 'allowedMimeTypes' ] ); ? (similar to what we do with maxUploadFileSize) This would allow us to avoid the usage of global in the test case, and would make this function more generic without errors whose triggering can only be parameterized using a global variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided against it because although it would make testing simpler, I didn't think it made sense to expose
allowedMimeTypes
as an overridable property. It is explicitly for checking what WP says is allowed, and theallowedTypes
property should be used instead for consumer-side restrictions.But I do understand your point about testing, and maybe that is more important than exposing too many properties. What do you think? I probably don't have a full grasp of all the ramifications yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A use case may appear where we want to upload something but we want to verify if the upload is of a given mimeType. But I understand your point this is to verify if the mimeType is allowed by WordPress. If later we want to parameterize this it should verify the mimetype of the parameter and this one and only allow the upload if both mimetypes allow it.
I think we can keep this version.