useClipboardHandler: Prevent file paste for users without media upload permissions#71607
useClipboardHandler: Prevent file paste for users without media upload permissions#71607
Conversation
|
Size Change: +21 B (0%) Total Size: 1.94 MB
ℹ️ View Unchanged
|
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
I don't think the clipboard handler needs to be aware of upload permissions; some blocks might perform other actions than upload. Usually, it's better to fix similar problems closer to the source; in this case, that would be |
I considered doing this, but this hook runs after the block transformation is performed and the block is mounted. So while we may be able to prevent media from being uploaded, we cannot prevent an image block from being created when media is pasted. Another approach might be to check whether the {
type: 'files',
isMatch( files ) {
const settings = select( blockEditorStore ).getSettings();
if ( ! settings.mediaUpload ) {
return false;
}
return files.every(
( file ) => file.type.indexOf( 'image/' ) === 0
);
},
transform( files ) {
// ...
},
},Does this approach make sense? |
198d932 to
8119235
Compare
Yes, that looks good. |
|
Flaky tests detected in 8119235. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/17761955421
|
What?
Closes #71581
This pull request updates the clipboard handler logic to prevent a critical error when users who can't upload media try to paste local files.
Why?
If the pasted event data contains files, the
useClipboardHandlertries to find block transformations withtype: filesand performs the transformation.The mounted block then tries to upload a file via
useUploadMediaFromBlobURL, but for users without access to the media library, the block breaks becausemediaUploadisundefined.How?
When pasting, I added an early return so that if
mediaUploadisundefined, the transformation itself won't be executed. There is already a similar early return in the processing when a file is "dropped".gutenberg/packages/block-editor/src/components/use-on-block-drop/index.js
Lines 172 to 174 in bc8fd77
If we want to allow block transformations to be performed, we might be able to modify the
useUploadMediaFromBlobURLhook instead.Testing Instructions
Screenshots or screencast
Before
60dc084d4b72e899a5ad527e53e010a0.mp4
After
5d95c841457034447c1f7b98fab51404.mp4