Skip to content

Build your personal knowledge base with Trilium Notes

fix(link-preview): store a favicon's lone PNG picture as a PNG

Elian Dorana59b41ed95f5
diff --git a/packages/trilium-core/src/routes/api/link_embed.spec.ts b/packages/trilium-core/src/routes/api/link_embed.spec.ts
index 85e4f3cf19..be44ab7298 100644
--- a/packages/trilium-core/src/routes/api/link_embed.spec.ts
+++ b/packages/trilium-core/src/routes/api/link_embed.spec.ts
@@ -929,6 +929,22 @@ describe("storeFavicon", () => {
expect(awaitImageWrite).toHaveBeenCalledWith("att1");
});
+ it("stores the PNG inside a one-picture icon as a PNG", async () => {
+ // Browsers re-encode a favicon into such a file, writing 0 (256) as its size whatever the
+ // PNG holds, and Chromium then draws nothing.
+ const png = Buffer.from(await new Jimp({ width: 48, height: 48, color: 0xff0000ff }).getBuffer("image/png"));
+ const directory = Buffer.alloc(22);
+ directory.writeUInt16LE(1, 2);
+ directory.writeUInt16LE(1, 4);
+ directory.writeUInt32LE(png.byteLength, 14);
+ directory.writeUInt32LE(directory.byteLength, 18);
+
+ const url = await storeFavicon("note1", "https://example.com/", Buffer.concat([ directory, png ]));
+
+ expect(url).toMatch(/\/image\/example\.com\.png$/);
+ expect(stored("favicon")?.buffer.equals(png)).toBe(true);
+ });
+
it("stores nothing for bytes that are not a picture or too large", async () => {
const huge = makeMultiSizeIco([ { edge: 256, bytes: 100 * 1024 } ]);
diff --git a/packages/trilium-core/src/routes/api/link_embed.ts b/packages/trilium-core/src/routes/api/link_embed.ts
index 96387bfa9c..7c750a331a 100644
--- a/packages/trilium-core/src/routes/api/link_embed.ts
+++ b/packages/trilium-core/src/routes/api/link_embed.ts
@@ -9,7 +9,7 @@ import type { Request } from "../../http_interface";
import { parse } from "node-html-parser";
import { ValidationError } from "../../errors.js";
-import { trimIcoToSmallestEntry } from "../../services/ico.js";
+import { pngInsideIco, trimIcoToSmallestEntry } from "../../services/ico.js";
import imageService from "../../services/image.js";
import { storePictureBytes } from "../../services/image_download.js";
import { type InspectedImage, inspectImage, UNKNOWN_FORMAT } from "../../services/image_inspect.js";
@@ -204,11 +204,13 @@ async function downloadFavicon(faviconUrl: string): Promise<DownloadedPicture |
}
/**
- * Keeps the part of a favicon a preview draws: the smallest entry of an icon directory, any other
- * picture as it is. Returns undefined if the bytes are not a picture or what is kept is too large.
+ * Keeps the part of a favicon a preview draws: the smallest entry of an icon directory, as a plain
+ * PNG when that entry is one, and any other picture as it is. Returns undefined if the bytes are
+ * not a picture or what is kept is too large.
*/
function asFavicon(bytes: Uint8Array): DownloadedPicture | undefined {
- const kept = trimIcoToSmallestEntry(bytes, FAVICON_TARGET_EDGE) ?? bytes;
+ const trimmed = trimIcoToSmallestEntry(bytes, FAVICON_TARGET_EDGE) ?? bytes;
+ const kept = pngInsideIco(trimmed) ?? trimmed;
// The limit applies to what is kept, so an icon directory with one large entry is refused.
return kept.byteLength <= MAX_FAVICON_SIZE ? asPicture(kept) : undefined;
diff --git a/packages/trilium-core/src/services/ico.spec.ts b/packages/trilium-core/src/services/ico.spec.ts
index bba6ccf084..a6785b8dec 100644
--- a/packages/trilium-core/src/services/ico.spec.ts
+++ b/packages/trilium-core/src/services/ico.spec.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
-import { trimIcoToSmallestEntry } from "./ico.js";
+import { pngInsideIco, trimIcoToSmallestEntry } from "./ico.js";
interface FakeEntry {
edge: number;
@@ -180,3 +180,21 @@ describe("trimIcoToSmallestEntry", () => {
expect(trimIcoToSmallestEntry(holdsNothing)).toBeNull();
});
});
+
+describe("pngInsideIco", () => {
+ it("lifts the PNG out of a one-picture icon, whatever size the directory claims", () => {
+ const png = Uint8Array.from([ ...PNG_SIGNATURE, 1, 2, 3 ]);
+
+ for (const edge of [ 48, 256 ]) {
+ expect(pngInsideIco(buildIco([ { edge, payload: png } ]))).toEqual(png);
+ }
+ });
+
+ it("answers nothing for a BMP picture, several pictures, or bytes that are not an icon", () => {
+ const png = Uint8Array.from([ ...PNG_SIGNATURE, 1, 2, 3 ]);
+
+ expect(pngInsideIco(buildIco([ { edge: 32, payload: payload(7) } ]))).toBeNull();
+ expect(pngInsideIco(buildIco([ { edge: 16, payload: png }, { edge: 32, payload: png } ]))).toBeNull();
+ expect(pngInsideIco(png)).toBeNull();
+ });
+});
diff --git a/packages/trilium-core/src/services/ico.ts b/packages/trilium-core/src/services/ico.ts
index 218d46e183..c046d03f46 100644
--- a/packages/trilium-core/src/services/ico.ts
+++ b/packages/trilium-core/src/services/ico.ts
@@ -22,6 +22,8 @@ const DIRECTORY_ENTRY_SIZE = 16;
const ICON_TYPE = 1;
/** Below this an icon is too small to draw a preview with, whatever else the file offers. */
const DEFAULT_MIN_EDGE = 16;
+/** The first eight bytes of every PNG file. */
+const PNG_SIGNATURE = [ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a ];
interface IconDirectoryEntry {
/** Longest edge in pixels. */
@@ -68,6 +70,19 @@ export function trimIcoToSmallestEntry(bytes: Uint8Array, minEdge = DEFAULT_MIN_
return trimmed;
}
+/**
+ * The PNG an icon file of one picture holds, or null when it holds several pictures, a BMP picture
+ * or is not an icon file. Browsers re-encode favicons into such files with a directory size of 256
+ * whatever the PNG measures, and Chromium sizes the image by the directory and draws nothing.
+ */
+export function pngInsideIco(bytes: Uint8Array): Uint8Array | null {
+ const entries = readIconDirectory(bytes);
+ const entry = entries?.length === 1 ? entries[0] : undefined;
+ const picture = entry ? bytes.subarray(entry.offset, entry.offset + entry.length) : null;
+
+ return picture && PNG_SIGNATURE.every((byte, index) => picture[index] === byte) ? picture : null;
+}
+
/** The pictures an icon file says it holds, or null if it does not read as one. */
function readIconDirectory(bytes: Uint8Array): IconDirectoryEntry[] | null {
if (bytes.byteLength < DIRECTORY_HEADER_SIZE) {