Problem/Motivation
Currently, Drupal 9.3.13 CKEditor 5 does not support SVG Images to upload. SVGs are insecure to serve but the SVG image module sanitizes SVG files. Since the SVG image module sanitizes the file, it will need a way to communicate with the CKEditor5 image uploader, that it is safe to upload the file. Also CKEditor5 supports ".webp" files, but there is currently no way to mark them as uploadable, other than changing magic strings in core.
Proposed resolution
Demonstrate how to alter CKEditor5 plugin info to add custom file type to the image Upload plugin.
Add an alter hook to modify the list of allowed extensions for the image upload plugin.
Update the CKEditor5ImageController to call that alter hook.
MR to use
MR 5295 = 11.x
Remaining tasks
Write a patchUpdate issue summary- Review and feedback
- RTBC and maintainer feedback
- Commit
User interface changes
None but implementing the API will allow uploading and dragging and dropping of new types of images.
API changes
None.
Data model changes
None.
Release notes snippet
It's now possible to alter the ckeditor5_imageUpload plugin definition to allow uploads of additional file types, such as TIFF or SVG.
Original report by sandeepraib
Currently, Drupal 9.3.13 ckeditor 5 does not support SVG Images to upload.
| Comment | File | Size | Author |
|---|---|---|---|
| #74 | 3280279-74--allow-svg-image-upload.patch | 10.68 KB | victordcp |
Issue fork drupal-3280279
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
sandeepraib commentedComment #3
sandeepraib commentedComment #4
sandeepraib commentedI have created this patch. It enables to add SVG images through ckeditor 5.
Comment #5
wim leersThat's intentional: SVGs are insecure to serve.
Nothing in Drupal allows SVG uploads: not the image field type, not Media, not CKEditor 4, not user profile picture, not site logo, nothing. Because it is insecure.
At least today. See linked issues. So for now, there's nothing to do here.
Sorry 😞
Comment #6
sandeepraib commentedComment #7
sandeepraib commentedComment #8
claudiu.cristea@Wim Leers, that's correct but if we want to use a module such as https://www.drupal.org/project/issues/svg_image, which is sanitizing the file? What's the API that such a module is triggering to allow SVG upload without patching the core?
Comment #9
claudiu.cristeaAs far as I can see the CKE config can be update to support
svg+xmlby implementinghook_ckeditor5_plugin_info_alter(), even, for some reasons is not straight because there's no setter for "ckeditor5" definition. But how aboutCKEditor5ImageController::upload()? Could we, at least, pass the extensions as route options or defaults instead of hardcoding them? BTW, they are now 'gif png jpg jpeg', not even following CKE, which accepts also WebP.Comment #10
wim leersThey're matching what was allowed for CKEditor 4 — see
\Drupal\editor\Form\EditorImageDialog::buildForm().I like that! 😊 That'd make it easy for people Who Know What They're Doing like yourself to allow SVG easily. That's the reason you'd like it to work like this, right? 🤓
Comment #11
aleexgreen commentedHere's a rough draft of how to programmatically allow uploading any type of file to CKEditor5. If the svg_image module implemented this, it would solve @Claudiu.cristea's issue, but also add support for ".webp", ".avif", etc..., as suggested by @wim-leers. The issue title probably needs to be changed now?
This doesn't have any tests yet, I'm just looking for feedback on the general idea.
I did not base this on the previous patch, so I'm not including an interdiff, but if you would like, I can include one :). Feedback welcome.
Comment #12
smustgrave commentedCan the issue summary be updated with the proposed solution please. Easier for reviews when the default template is used.
Thanks!
Comment #13
aleexgreen commentedUpdated issue summary
Comment #14
smustgrave commentedApplied the patch #11 but don't appear able to upload an image via image upload button in ckeditor.
Also noticed test coverage was missing we will need some
Thanks.
Comment #15
aleexgreen commented@smustgrave will add test coverage soon, you'll need some code like this to upload new file types and to be running CKEditor 5:
See also comments #8, #9 and #10 for why this custom code is necessary
Comment #16
bruno.bicudoI applied #11 and tested with the suggested snippet (from @AleexGreen on #15). Notice i created a custom module named svg-test with the snippet, and also tested on a new, fresh Drupal 10 installation.
Patch applied with no issues and solution works great! Only tests left to make it RTBC i guess.
Evidence attached.
Thanks.
Comment #17
aleexgreen commentedLet's see if Test Bot likes this test.
Comment #18
aleexgreen commentedSorry, bad patch. Let's see if this one works.
Comment #19
aleexgreen commentedFixed some lints, now ready for review. Feedback welcome!
Comment #20
wim leerss/Modify/Modifies/
🤔 This is only modifying the Drupal-side validation logic.
Shouldn't we also update
?
See https://ckeditor.com/docs/ckeditor5/latest/api/module_image_imageconfig-....
Comment #21
aleexgreen commentedHere is a patch for problem #1.
@wim-leers for problem #2, we adjust ckeditor5_imageUpload.ckeditor5.config.image.upload.types in
ckeditor5_test_module_allowed_image_ckeditor5_plugin_info_alter()i.e.: hook_ckeditor5_plugin_info_alter()Comment #22
aleexgreen commentedAdded a draft Change record
Comment #23
smustgrave commentedCR reads well I think
Comment #24
claudiu.cristeaI think .webp should be officially supported, so it should be in the
$extensionslist? Setting to NR to weight on that.EDIT: Or should be treated in a followup?
Comment #25
smustgrave commentedPersonally I would say follow up. Think expanding the allowed list could be larger conversation vs around this new hook.
Comment #26
claudiu.cristeaOK, agree. Just keep in mind that WebP is officially adopted as image type by Drupal core, so we need to add the followup only for .webp. A future expansion of accepted types is out of scope for the followup.
Setting to NR to have the followup created and linked here.
Comment #27
mparker17Follow-up issue added: #3386605: [PP-1] Add CKEditor 5 support for WebP images
Comment #28
claudiu.cristeaThank you
Comment #29
wim leers#21.2: Right,
ckeditor5_test_module_allowed_image_ckeditor5_plugin_info_alter()only modifies the plugin definition. Andckeditor5_test_module_allowed_image_ckeditor5_image_controller_extensions_alter()modifies only the Drupal file upload validation logic.My bad!
But what's missing here is explicit docs in:
ckeditor5_imageUploadplugin definition that points out this connection (which was not necessary before because it was all hard-coded)ckeditor5.api.php, since this issue is increasing the API surface!It's critical that these two are in sync. And it's also critical to describe why in one place you need
svgand in the othersvg+xml.Comment #30
aleexgreen commentedI've made the documentation clear in both cases (filename extension vs image media type name) and the change record. Feedback is welcome :).
Comment #31
wim leersI'm sorry to be so annoying, but I still think the connection isn't quite clear enough 😅
This should have:
This should have:
Can we move this logic into a
protected function getAllowedExtensions()helper method? Then that provides us a point to point towards.Also, can we add an
@see ckeditor5_imageUpload in ckeditor5.ckeditor5.ymljust above the$extensionsvariable declaration, to make the connection clear?@see😅@see \Drupal\ckeditor5\Controller\CKEditor5ImageController::getAllowedExtensions()😊Comment #32
wim leersAs I wrote #31, I was thinking:
Then we wouldn't need
hook_ckeditor5_image_controller_extensions_alter()(aka the API addition!) at all. Which is much better: less API surface, and no need to manually keep the two in sync, so it also removes the need for documenting that tricky aspect, which is what I've been worried about in #29 and #31!And … sure enough, core has the necessary infra. AFAICT this pseudocode should work:
That'd make this issue trivial to land 😊
Comment #34
aleexgreen commented@wim-leers, after some manual testing, I figured out that symfony/mime expects to be passed the fully-qualified mime type with subtype and type ( i.e. the Template (second) column in this table) in order to look up the file extensions.
However, CKEditor5 expects to be passed the IANA image type name (i.e. the Name (first) column in the table)
Internally, CKEditor5 adds/removes the 'image/' string to the beginning of the IANA image type to convert between fully-qualified mime types and image types, but as far as I can tell this assumption isn't guaranteed by the IANA, so it may be risky for us to use it too. I'm unsure on how else to achieve this without patching CKEditor5 (i.e. so it always handles fully-qualified mime types) or symfony/mime (i.e. to make it aware of how IANA image type names map to fully-qualified mime types) .
Comment #35
wim leersThanks for your very thorough work here, @AleexGreen — it does not go unnoticed and is deeply appreciated! 🤩 👏
Given the very long list in the table you link, I do think that's a safe assumption to make. Especially because in December 2020, some new top-level types were added.
Any image file type that does not use the
imagetop-level type will not work correctly in many applications.All I had to do here was fix nits and copy/paste bits and pieces from
\Drupal\Tests\ckeditor5\FunctionalJavascript\ImageTest::addImage()to make the test coverage actually do what it claims 👍P.S.: looking forward to having our paths cross again here in the d.o issue queue! 😊
Comment #36
wim leersUpdated issue summary 👍
Comment #37
longwaveAdded a little bit of feedback but overall this looks good to me.
Comment #38
aleexgreen commentedI think this is ready for re-review
Comment #39
smustgrave commentedSeems all open threads have been addressed and already received reviews from @longwave and @Wim Leers
Comment #40
wim leersPosted one last nit, but it's really something that is already in HEAD.
This MR looks great — thanks @AleexGreen!
Comment #41
aleexgreen commentedLast thread resolved, thanks @wim-leers!
Comment #42
longwaveThis looks great!
However the MR needs rebasing against 11.x following #3221793: Move file upload validation from file.module to constraint validators which also added to the CKEditor5ImageController constructor. It might be easier to avoid changing all arguments to use constructor property promotion, there is an issue somewhere to do this in bulk at some point in the future.
Comment #43
wim leersRTBC++ 😊EDIT: cross-posted with @longwave and … shoot, he's right, even though @AleexGreen rebased on latest upstream. Root cause: this MR was created before the
11.xbranch existed 😬@longwave Could you please change the target branch to
11.x? Only committers can do that unfortunately 🫣Comment #44
longwaveMight also be worth waiting for #3388985: Make CKEditor5ImageController reuse FileUploadHandler?
Comment #45
longwaveDidn't realise that the linked issue above only went into 11.x (10.3.x) and so I'm not sure how to get this into 10.2.x without having to untangle it again in 10.3.x given both want to change the constructor :/
Maybe this just has to be 11.x/10.3.x only as well?
Comment #46
wim leers@longwave I didn't either! 🤪🙈
I think this issue/feature request is hard-blocking some sites (i.e. those sites that absolutely need SVG or webp images).
On the other hand, #3221793: Move file upload validation from file.module to constraint validators and #3388985: Make CKEditor5ImageController reuse FileUploadHandler are "only" changing implementation details: zero end user impact.
Conclusion: this issue should land in both
10.2.x(in its current MR form) and11.x(in a rebased form).Do you agree? If you do, then @AleexGreen can just open a second merge request that targets
11.x!(@AleexGreen: the policy is that everything must first land in
11.xbefore it lands in10.x.y— otherwise we risk making the future major less capable than the current major 😅)Comment #47
longwaveGiven #3388985: Make CKEditor5ImageController reuse FileUploadHandler is a standard refactoring task with no deadline and this is a major feature request blocking other sites, and they both want to touch the same controller, I think we should postpone that one on this, and try to get this in to 10.2.0 before next week's beta deadline.
Comment #49
wim leers💯
I opened a new MR for
11.xsince the current MR would need to go in as-is into10.2.xanywya.Since this is just applying the existing MR + resolving conflicts, I squashed all of the existing commits to a single one, applied that as a commit, and resolved the conflicts. (No point in resolving the same conflict in many subtly different ways dozens of times.)
Comment #50
wim leers@longwave Could you please change the target branch of the original MR back to
10.2.x? 🙏Comment #51
longwave#50 Done, but that still needs rebasing.
Comment #52
longwaveIn fact the 11.x MR applies cleanly to 10.2.x (as it should), so we don't need the older MR any more.
Comment #53
wim leersI'm confused, the
11.xMR must necessarily be different compared to10.2.xbecause #3221793: Move file upload validation from file.module to constraint validators only exists in11.x? 🤔AHHHHHHHHHHH! 🤣 Now I get it! #3221793 landed in
11.xat a time when10.2.xdid not exist yet! That explains all the confusion 😅Back to so that @AleexGreen can address @longwave's review on the MR — one new thing has come up due to the rebase 😅
Comment #54
aleexgreen commentedI can confirm that the code in the branch named
3280279-11applies to both 11.x (atb20ec5185c) and 10.2.x (atffd4be4dda)@longwave I think I understand what you were asking me to do, but I'm still pretty new to this and don't fully understand all the terminology yet.
Comment #55
smustgrave commentedHighlighted in the issue summary MR 5295 is the main one, this isn't always needed but when multiple MRs are on a ticket just helps
I see on MR 5295 though there are some test failures see https://git.drupalcode.org/issue/drupal-3280279/-/pipelines/55954
Believe what @longwave meant was for backwards compatibility, if contrib or custom modules could be extending this class the new parameter would instantly break their site.
I see on this class there's already an example of that converage. For this ticket though it will be 10.3.0 and required in 11. The CR should be checked to make sure it includes that the class now takes an additional parameter.
Comment #57
aleexgreen commentedFixed broken tests, changed the CR, and added the deprecation warning, ready for re-review :).
Comment #58
smustgrave commentedBelieve all feedback has been addressed. Not sure about 10.2 though if the tag for releases notes should be removed.
Comment #59
wim leersPer https://www.drupal.org/about/core/policies/core-release-cycles/schedule, the final release happens next week. https://www.drupal.org/project/drupal/releases/10.2.0-rc1 was tagged Dec 1. The 10.2 train has left the station, so this will board the next one 😊
Updated issue tag + CR.
Comment #60
pk_singhHi
I just used the below code & patch and this works for me. You need to turn off /on the insert image icon in CK Editor config.
Patch code
Comment #61
larowlanUpdated issue credits.
Changed the change record to remove references to SVG because we shouldn't be recommending that, used webp instead.
Went to commit it, but it's failing phpcs as follows.
Comment #62
larowlan#3388985: Make CKEditor5ImageController reuse FileUploadHandler is in too, so this needs a reroll
We should also remove the last hunk (Additional changes) from the change record and update https://www.drupal.org/node/3388990 instead
Comment #63
aleexgreen commentedComment #64
kim.pepperJust a couple of nits.
Comment #68
kim.pepperRebased on 11.x and resolved my own feedback. :-D
Comment #69
smustgrave commentedBelieve @larowlan did the change record updates he mentioned #62 can you confirm? I was looking at the history of the CR.
Changes here to the MR look good and all threads addressed. So +1 RTBC from me but per new approach for Needs Review queue going to leave in review for additional eyes.
Comment #70
smustgrave commentedConfirmed with @larowlan on slack he did make the updates.
Comment #71
quietone commentedI'm triaging RTBC issues. I read the IS, the comments, the CR and the MR. I didn't find any unanswered questions.
Everything about this issue was easy to review. Thank you to everyone for the easy to follow comment in the issue and in the MR. The CR reads well too. :-)
I pushed a small change to the description of the test module and left a question. The question does not block this so I will leave at RTBC. I will also make a comment in #needs-review-queue-initiative about the change to the module description field.
Leaving at RTBC.
Comment #72
larowlanJust one minor nit on the deprecation message, very close
Comment #74
victordcp commentedHey guys, just uploading a static patch from the MR #5295 for D10.2.x.
Comment #75
mparker17The nit was fixed, back to RTBC
Comment #76
alexpott@victordcp please don't change the issue version for a backport patch.
Comment #77
alexpottCommitted and pushed 8f95f51a84 to 11.x and e1e8d87df6 to 10.3.x. Thanks!
Comment #80
bramdriesenPublished the CR.