Skip to content

Build your personal knowledge base with Trilium Notes

client: fix response.ok guard, safe filename decode and clean MIME extension issues

Adorian Doran6fe72d335690
diff --git a/apps/client/src/services/image.spec.ts b/apps/client/src/services/image.spec.ts
index cff97d5c5f..98e5ff7d35 100644
--- a/apps/client/src/services/image.spec.ts
+++ b/apps/client/src/services/image.spec.ts
@@ -129,6 +129,14 @@ describe("getFileNameFromSrc", () => {
expect(getFileNameFromSrc("api/images/abc/photo.jpg", "image/jpeg")).toBe("photo.jpg");
expect(getFileNameFromSrc("api/images/abc/", "image/png")).toBe("image.png");
});
+
+ it("derives a clean extension from a compound MIME type", () => {
+ expect(getFileNameFromSrc("api/images/abc/diagram", "image/svg+xml")).toBe("diagram.svg");
+ });
+
+ it("falls back to the raw segment when it is a malformed URI", () => {
+ expect(getFileNameFromSrc("api/images/abc/%E0%A4%A")).toBe("%E0%A4%A");
+ });
});
describe("isImageCopySupported", () => {
@@ -177,4 +185,17 @@ describe("downloadImage", () => {
expect(downloadSpy).toHaveBeenCalledWith("api/notes/abc123/download");
downloadSpy.mockRestore();
});
+
+ it("reports an error when the fallback fetch returns a non-OK response", async () => {
+ vi.mocked(utils.isElectron).mockReturnValue(false);
+ vi.stubGlobal("logError", vi.fn());
+ vi.stubGlobal("fetch", vi.fn(async () => ({ ok: false, status: 404, statusText: "Not Found" })));
+ const showErrorSpy = vi.spyOn(toastModule, "showError").mockImplementation(() => {});
+
+ // A data: URL doesn't map to a note/attachment endpoint, so the blob fallback runs and fetches.
+ await downloadImage("data:image/png;base64,AAAA");
+
+ expect(showErrorSpy).toHaveBeenCalledTimes(1);
+ showErrorSpy.mockRestore();
+ });
});
diff --git a/apps/client/src/services/image.ts b/apps/client/src/services/image.ts
index 6a462d41dc..e084f5f055 100644
--- a/apps/client/src/services/image.ts
+++ b/apps/client/src/services/image.ts
@@ -23,7 +23,7 @@ export function isImageCopySupported() {
export async function copyImageToClipboard(src: string) {
try {
if (utils.isElectron()) {
- const blob = await (await fetch(src)).blob();
+ const blob = await fetchImageBlob(src);
const buffer = new Uint8Array(await blob.arrayBuffer());
window.electronApi?.clipboard.copyImageToClipboard(buffer);
} else {
@@ -56,7 +56,7 @@ export async function downloadImage(src: string) {
// Fallback for other sources (e.g. data: URLs): fetch into a blob and save via an object URL.
try {
- const blob = await (await fetch(src)).blob();
+ const blob = await fetchImageBlob(src);
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
@@ -87,6 +87,15 @@ export function getImageDownloadUrl(src: string) {
return null;
}
+/** Fetches the image as a blob, throwing on a non-OK response so error pages aren't copied/saved. */
+async function fetchImageBlob(src: string) {
+ const response = await fetch(src);
+ if (!response.ok) {
+ throw new Error(`Failed to fetch image: ${response.status} ${response.statusText}`);
+ }
+ return await response.blob();
+}
+
async function renderImageToPng(src: string) {
// Decode through an <img> element — the browser's full image decoder, which handles every
// format it can display — rather than createImageBitmap, which rejects some formats (e.g. SVG).
@@ -112,11 +121,17 @@ export function getFileNameFromSrc(src: string, mimeType?: string) {
// Image URLs look like `api/images/<noteId>/<title>` — use the last path segment as the name.
const path = src.split("?")[0];
const lastSegment = path.substring(path.lastIndexOf("/") + 1);
- let name = decodeURIComponent(lastSegment) || "image";
+ let name: string;
+ try {
+ name = decodeURIComponent(lastSegment) || "image";
+ } catch {
+ // A malformed %-escape in the segment would make decodeURIComponent throw.
+ name = lastSegment || "image";
+ }
// Ensure an extension, since a blob: URL carries none: derive it from the MIME type.
if (mimeType && !name.includes(".")) {
- const extension = mimeType.split("/")[1];
+ const extension = mimeType.split("/")[1]?.split("+")[0]; // e.g. "image/svg+xml" → "svg"
if (extension) {
name += `.${extension}`;
}