Skip to content

Build your personal knowledge base with Trilium Notes

fix(editor): prefer the clipboard file over another app's blob: URL

Elian Doranf3e1fc957275
diff --git a/packages/ckeditor5/src/plugins/clipboard_bare_image.spec.ts b/packages/ckeditor5/src/plugins/clipboard_bare_image.spec.ts
index 7b3d0915b7..0bb366aca9 100644
--- a/packages/ckeditor5/src/plugins/clipboard_bare_image.spec.ts
+++ b/packages/ckeditor5/src/plugins/clipboard_bare_image.spec.ts
@@ -13,6 +13,15 @@ const SLACK_HTML = `<html>
</body>
</html>`;
+/**
+ * What Element Desktop puts on the clipboard: a blob URL minted inside its own renderer, which
+ * resolves to nothing anywhere else.
+ */
+const ELEMENT_HTML = `<img src="blob:vector://vector/91743433-1e35-4caf-bf27-a23a1b234e1d" alt="image.png"/>`;
+
+/** A blob URL minted by this document — what an upload still in flight leaves behind. */
+const OWN_BLOB_HTML = `<img src="blob:${window.location.origin}/91743433-1e35-4caf-bf27-a23a1b234e1d">`;
+
/** A mixed selection — text, a mention and a picture — which carries no file to fall back on. */
const GOOGLE_CHAT_HTML = `<html>
<body>
@@ -67,11 +76,16 @@ describe("ClipboardBareImage", () => {
it("leaves images the editor can already resolve to the normal paste path", () => {
// Self-contained bytes: nothing to gain by preferring the file.
expect(isBareImageHtml(`<img src="data:image/png;base64,AAAA">`)).toBe(false);
- expect(isBareImageHtml(`<img src="blob:http://localhost/abc">`)).toBe(false);
+ // A blob URL of our own still resolves — it names an object this document holds.
+ expect(isBareImageHtml(OWN_BLOB_HTML)).toBe(false);
// Copied out of Trilium, where the reference beside the payload is the whole point.
expect(isBareImageHtml(`<img src="data:image/png;base64,AAAA" ${TRILIUM_SRC_ATTRIBUTE}="api/images/n1/p.png">`)).toBe(false);
});
+ it("accepts another application's blob URL, which resolves nowhere but there", () => {
+ expect(isBareImageHtml(ELEMENT_HTML)).toBe(true);
+ });
+
it("accepts an image with no src at all, since the file is then the only thing to go on", () => {
expect(isBareImageHtml(`<img alt="no source">`)).toBe(true);
});
@@ -154,6 +168,15 @@ describe("ClipboardBareImage", () => {
expect(reachedRestOfPipeline).not.toHaveBeenCalled();
});
+ it("uploads the clipboard file when the HTML names another application's blob URL", () => {
+ const file = imageFile();
+
+ const { reachedRestOfPipeline } = paste(ELEMENT_HTML, [file]);
+
+ expect(createLoader).toHaveBeenCalledWith(file);
+ expect(reachedRestOfPipeline).not.toHaveBeenCalled();
+ });
+
it("drops the image where the paste landed rather than where the caret was", () => {
// A real paste always names its target ranges — a drop lands wherever it was released,
// and even a keyboard paste carries the ranges — so this is the ordinary path.
diff --git a/packages/ckeditor5/src/plugins/clipboard_bare_image.ts b/packages/ckeditor5/src/plugins/clipboard_bare_image.ts
index 239bea016a..bd8b45a147 100644
--- a/packages/ckeditor5/src/plugins/clipboard_bare_image.ts
+++ b/packages/ckeditor5/src/plugins/clipboard_bare_image.ts
@@ -125,14 +125,18 @@ export function isBareImageHtml(html: string): boolean {
return false;
}
- // An image the editor can already resolve is left to the normal path: a `data:`/`blob:` payload
- // carries its own bytes, and Trilium's own marker means this came from a Trilium copy, where
- // the reference beside it is the point (see the ClipboardImageEmbed plugin).
+ // An image the editor can already resolve is left to the normal path: Trilium's own marker means
+ // this came from a Trilium copy, where the reference beside the payload is the point (see the
+ // ClipboardImageEmbed plugin).
if (image.hasAttribute(TRILIUM_SRC_ATTRIBUTE)) {
return false;
}
const src = image.getAttribute("src") ?? "";
- return !src.startsWith("data:") && !src.startsWith("blob:");
+ // A `data:` URI carries its own bytes. A `blob:` one only resolves inside the document that
+ // minted it: ours is fine, but another application's — Element Desktop copies its pictures as
+ // `blob:vector://vector/…` — is dead the moment it leaves that renderer, which makes it exactly
+ // the kind of unreachable source this plugin exists to replace.
+ return !src.startsWith("data:") && !src.startsWith(`blob:${window.location.origin}/`);
}