Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(clipper): send URL-encoded data: images as base64 (closes #2621)
Elian Doran9129273fbbeb
diff --git a/.claude/skills/developing-web-clipper/SKILL.md b/.claude/skills/developing-web-clipper/SKILL.mdindex 66fb341010..894461ea86 100644--- a/.claude/skills/developing-web-clipper/SKILL.md+++ b/.claude/skills/developing-web-clipper/SKILL.md@@ -103,8 +103,12 @@ against older versions of the other.** Prefer changes that need nothing new from- **HTML built by concatenation escapes every page-derived value** with `escapeHtml()` — thesave-tabs list once put tab titles in raw. Tabs can also lack a `url` (still loading), and`new URL("")` throws, so `saveTabs()` filters them out.-- `fetchImage()` rejects a non-OK response and a non-image `Content-Type` — otherwise an error page- gets saved as the "image".+- `fetchImage()` rejects a non-OK response and a type that is not `image/<subtype>` — otherwise an+ error page gets saved as the "image".+- **`data:` images go through `fetchImage()` too.** The server decodes every `dataUrl` as base64,+ and a `data:image/svg+xml,…` or `;utf8,` URL is not; `fetch()` decodes either and `FileReader`+ re-encodes it. Node's `fetch()` types `data:image/;base64,…` as `image/` where browsers say+ `text/plain`, hence the subtype in the check.- The page is extracted with `defuddle`, installed from npm and pinned; Firefoxreview flags its `innerHTML` use, which the README explains to the reviewer.- `new Defuddle(document, { url })` takes the **live** document: it clones it itself, leavesdiff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.tsindex 8aae6e7e0e..d6b934e417 100644--- a/apps/web-clipper/entrypoints/background/index.spec.ts+++ b/apps/web-clipper/entrypoints/background/index.spec.ts@@ -64,8 +64,12 @@ const contextMenusRemoveAll = vi.fn(async () => {});const getContexts = vi.fn(async (): Promise<unknown[]> => []);const createDocument = vi.fn(async () => {});const runtimeSendMessage = vi.fn(async (_message: unknown): Promise<unknown> => PNG);-const fetchMock = vi.fn(- async (_url: string) => new Response(new Blob([ "img" ], { type: "image/png" })));+/** Answers a picture for a web address, and decodes a `data:` URL as the browser does. */+const browserFetch = globalThis.fetch;+const fetchMock = vi.fn(async (url: string) => (url.startsWith("data:")+ ? browserFetch(url)+ : new Response(new Blob([ "img" ], { type: "image/png" }))));+const SVG = "<svg xmlns='http://www.w3.org/2000/svg'></svg>";describe("background", () => {beforeEach(async () => {@@ -131,7 +135,7 @@ describe("background", () => {images: [{ imageId: "i2", src: "https://example.com/a.png" },{ imageId: "i3", src: "data:image/jpeg;base64,/9j/4AAQ" },- { imageId: "i4", src: "data:image/;base64,AAAA" }+ { imageId: "i4", src: `data:image/svg+xml,${encodeURIComponent(SVG)}` }]});@@ -154,8 +158,8 @@ describe("background", () => {},{imageId: "i4",- src: "data:image/;base64,AAAA",- dataUrl: "data:image/;base64,AAAA"+ src: "inline.svg",+ dataUrl: `data:image/svg+xml;base64,${btoa(SVG)}`}]});@@ -174,12 +178,13 @@ describe("background", () => {.mockResolvedValueOnce(new Response("<html>", { headers: { "Content-Type": "text/html" } }));tabMessageHandler = () => ({title: "Page",- content: `<img src="i1"><img src="i2"><img src="i3"><img src="i4">`,+ content: `<img src="i1"><img src="i2"><img src="i3"><img src="i4"><img src="i5">`,images: [{ imageId: "i1", src: "https://example.com/blocked$&$$.png" },{ imageId: "i2", src: "https://example.com/forbidden.png?a=1&b=\"2\"" },{ imageId: "i3", src: "https://example.com/page.html" },- { imageId: "i4", src: "https://example.com/ok.png" }+ { imageId: "i4", src: "https://example.com/ok.png" },+ { imageId: "i5", src: "data:image/;base64,AAAA" }]});@@ -189,16 +194,17 @@ describe("background", () => {title: "Page",content: `<img src="https://example.com/blocked$&$$.png">`+ `<img src="https://example.com/forbidden.png?a=1&b="2"">`- + `<img src="https://example.com/page.html"><img src="i4">`,+ + `<img src="https://example.com/page.html"><img src="i4">`+ + `<img src="data:image/;base64,AAAA">`,images: [ {imageId: "i4",src: "https://example.com/ok.png",dataUrl: expect.stringMatching(PNG_DATA_URL)} ]});- expect(console.error).toHaveBeenCalledTimes(3);+ expect(console.error).toHaveBeenCalledTimes(4);expect(await lastToast()).toMatchObject({- message: "Selection has been saved to Trilium, but 3 images could not be downloaded.",+ message: "Selection has been saved to Trilium, but 4 images could not be downloaded.",noteId: "saved"});});diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.tsindex b954df4712..666232d36c 100644--- a/apps/web-clipper/entrypoints/background/index.ts+++ b/apps/web-clipper/entrypoints/background/index.ts@@ -203,7 +203,7 @@ export default defineBackground(() => {}const blob = await resp.blob();- if (!blob.type.startsWith("image/")) {+ if (!/^image\/[\w.+-]+/.test(blob.type)) {throw new Error(`Not an image: '${blob.type}'`);}@@ -215,23 +215,24 @@ export default defineBackground(() => {return dataUrl;}- /** Inlines the image as a data URL, and returns whether it could be downloaded. */+ /**+ * Inlines the image as a base64 data URL, and returns whether it could be read. A `data:` URL+ * goes through `fetch()` too, which decodes a URL-encoded one; Trilium reads only base64.+ */async function postProcessImage(image: ClippedImage) {- if (image.src.startsWith("data:image/")) {- image.dataUrl = image.src;- const mimeSubtype = image.src.match(/data:image\/(.*?);/)?.[1];- if (!mimeSubtype) return true;- image.src = `inline.${mimeSubtype}`; // this should extract file type - png/jpg- return true;- }-try {image.dataUrl = await fetchImage(image.src);- return true;} catch (e) {- console.error(`Cannot fetch image from ${image.src}`, e);+ console.error(`Cannot fetch image from ${image.src.substring(0, 100)}`, e);return false;}++ if (image.src.startsWith("data:")) {+ // A `data:` URL names no file: Trilium names the attachment `inline.<type>`.+ const type = image.dataUrl.substring("data:image/".length).split(/[+;,]/)[0];+ image.src = `inline.${type}`;+ }+ return true;}/**