ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

[Feature Request] Trusted Types support

Open tosmolka opened this issue 5 years ago • 5 comments

Trusted Types support

CKEditor 5 should support Trusted Types API so that it can be seamlessly integrated into applications that enforce Trusted Types for all DOM XSS Injection Sinks (such as innerHTML setters) via CSP directive require-trusted-types-for. Trusted Types are now fully supported in major browsers such as Chrome or Edge.

To support Trusted Types we should identify all instances where CKeditor 5 calls such methods and propose re-factoring. We need to be careful to keep supporting browsers without Trusted Types support. This is usually done by testing whether window.trustedTypes is defined and fall-back to current behavior if it's not.

Initial list to consider:

  • Here we could maybe avoid calling Injection Sink methods altogether

    • https://github.com/ckeditor/ckeditor5/blob/4c228fb0549507f01d1f1ba4f9862817ac589279/packages/ckeditor5-clipboard/src/dragdrop.js#L554
    • ~~https://github.com/ckeditor/ckeditor5/blob/8f0b1c306108b3e7f849b9ce5664ddb48ac2ab80/packages/ckeditor5-engine/src/view/filler.js#L59~~
    • https://github.com/ckeditor/ckeditor5/blob/99a56669b8cf4a5fb0d3e461aec8824596eed96a/packages/ckeditor5-minimap/src/utils.js#L219
    • https://github.com/ckeditor/ckeditor5/blob/bce8267e16fccb25448b4c68acc3bf54336aa087/packages/ckeditor5-paste-from-office/src/filters/space.js#L44
    • https://github.com/ckeditor/ckeditor5/blob/487d5444e72718725dbbf64b14af42dbd6b996a9/packages/ckeditor5-widget/src/widgettypearound/widgettypearound.js#L42
  • Here we could leverage TrustedTypePolicyFactory.emptyHTML

    • https://github.com/ckeditor/ckeditor5/blob/bce8267e16fccb25448b4c68acc3bf54336aa087/packages/ckeditor5-ui/src/icon/iconview.js#L106
  • Here we probably need to create unique TrustedTypesPolicy for CKEditor 5 and wrap content using createHTML

    • https://github.com/ckeditor/ckeditor5/blob/80f3cc5811df9473e9579c8fe3a41f6dcc7cc530/packages/ckeditor5-engine/src/view/domconverter.js#L287
    • https://github.com/ckeditor/ckeditor5/blob/80f3cc5811df9473e9579c8fe3a41f6dcc7cc530/packages/ckeditor5-engine/src/view/domconverter.js#L292
    • https://github.com/ckeditor/ckeditor5/blob/bce8267e16fccb25448b4c68acc3bf54336aa087/packages/ckeditor5-utils/src/dom/setdatainelement.js#L23
    • https://github.com/ckeditor/ckeditor5/blob/fdf2c02a6e82dac4301817d7ccde0e7938cec8a4/packages/ckeditor5-html-embed/src/htmlembedediting.js#L137
    • https://github.com/ckeditor/ckeditor5/blob/b097349f5063b4f9b4822419e2d5c6663dd48aa0/packages/ckeditor5-engine/src/dataprocessor/htmldataprocessor.js#L117
    • https://github.com/ckeditor/ckeditor5/blob/bce8267e16fccb25448b4c68acc3bf54336aa087/packages/ckeditor5-ui/src/icon/iconview.js#L98
    • https://github.com/ckeditor/ckeditor5/blob/39d5cb676c89045fe80fa4a5425fac3d08280c72/packages/ckeditor5-paste-from-office/src/filters/parse.js#L38
    • https://github.com/ckeditor/ckeditor5/blob/b097349f5063b4f9b4822419e2d5c6663dd48aa0/packages/ckeditor5-engine/src/dataprocessor/xmldataprocessor.js#L139
    • https://github.com/ckeditor/ckeditor5/blob/80f3cc5811df9473e9579c8fe3a41f6dcc7cc530/packages/ckeditor5-engine/src/view/domconverter.js#L292
    • https://github.com/ckeditor/ckeditor5/blob/99a56669b8cf4a5fb0d3e461aec8824596eed96a/packages/ckeditor5-minimap/src/minimapiframeview.js#L175

References

Similar implementations in other libraries:


If you'd like to see this feature implemented, add a 👍 reaction to this post.

tosmolka avatar Nov 15 '21 12:11 tosmolka

This is still an issue and MSFT had to patch various places that are still using unsafe parseFromString() as of CKEditor v47.6.0 as mentioned in this issue. Is there any plan to address this? https://github.com/search?q=repo%3Ackeditor%2Fckeditor5%20parseFromString&type=code

marco-ms avatar Mar 10 '26 15:03 marco-ms

Hi there, we're still experiencing issues with this.

itimie avatar Jun 10 '26 23:06 itimie

We had to create some patches to fix these issues which have been present for 5 years now, will you accept PRs to fix the issues?

itimie avatar Jun 15 '26 19:06 itimie

Hi all — thanks for the continued nudges, and apologies this has been open so long. We've picked this up and want to share concrete progress: we've built a working proof-of-concept for Trusted Types support and validated it under real CSP enforcement.

Approach

Rather than patching each call site individually, we've added a single centralized, feature-detected Trusted Types policy that every DOM injection sink routes through:

  • One named ckeditor5 policy, created lazily as a singleton and reused across editor instances.
  • Pass-through createHTML (the editor is the trusted producer of its own markup).
  • Graceful fallback: when window.trustedTypes is undefined, the helper returns the raw string and behavior is unchanged — so browsers without Trusted Types support are unaffected.

This mirrors the pattern suggested in this issue and used by Angular and VS Code. We then routed the sinks — innerHTML, DOMParser#parseFromString() (all types), Range#createContextualFragment(), and iframe document.write() — through the helper across the engine, UI, and feature packages.

Verified in the browser

Running a full-featured editor build under require-trusted-types-for 'script'; trusted-types ckeditor5; in Chrome, the editor loads, edits, and retrieves data with no Trusted Types violations originating from the editor — including the full data pipeline (parsing input, rendering, and reading data back out).

For integrators

Applications enforcing Trusted Types will need to allow-list the editor's policy in their CSP:

Content-Security-Policy: trusted-types ckeditor5; require-trusted-types-for 'script';

This will be covered in the documentation.

niegowski avatar Jul 13 '26 15:07 niegowski

Hi all, Trusted Types support has been released in CKEditor 5 v49.0.0. 🎉

You can find the complete setup guide in the documentation, including the CSP directives you need, the known limitations, and how to make your own plugins compatible: https://ckeditor.com/docs/ckeditor5/latest/getting-started/setup/csp.html#trusted-types

Thanks to everyone who reported this and for your patience!

jacek-cke avatar Oct 08 '26 09:10 jacek-cke