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
link previews: prevent link preview URLs getting logged
Adorian Doranee57c72f472f
diff --git a/apps/server/src/routes/api/link_embed.ts b/apps/server/src/routes/api/link_embed.tsindex 2a80938cb4..5c28e3f328 100644--- a/apps/server/src/routes/api/link_embed.ts+++ b/apps/server/src/routes/api/link_embed.ts@@ -1,4 +1,4 @@-import { extractYouTubeVideoId, type LinkEmbedMetadata, redactUrlForLog } from "@triliumnext/commons";+import { extractYouTubeVideoId, type LinkEmbedMetadata } from "@triliumnext/commons";import { getLog, ValidationError } from "@triliumnext/core";import type { Request } from "express";import isSvg from "is-svg";@@ -163,7 +163,10 @@ async function downloadImageAsDataUri(imageUrl: string, minSourceDimension = 0):// content type is checked so that an error page served in place of the image (an undecodable// response that is not an image at all) is dropped rather than embedded. A caller that// requires a minimum size gets nothing: undecodable means unverifiable.- getLog().info(`Could not decode link preview image ${redactUrlForLog(imageUrl)}: ${e}`);+ // The URL is deliberately left out of the log: it is the user's private browsing, and a+ // pasted link can carry a one-time token in its path or query. The timestamp is enough to+ // match a log line against the paste that caused it.+ getLog().info(`Could not decode a link preview image: ${e}`);return isSmallEnoughToKeepVerbatim && contentType.startsWith("image/") && !minSourceDimension? toDataUri(contentType, buffer): undefined;@@ -385,7 +388,8 @@ async function getMetadata(req: Request) {embedType: "opengraph"} satisfies LinkEmbedMetadata;} catch (e: unknown) {- getLog().info(`Failed to fetch metadata for ${redactUrlForLog(url)}: ${e}`);+ // No URL here either — see the note above the image-decode log.+ getLog().info(`Failed to fetch link preview metadata: ${e}`);return unresolvedMetadata(validatedUrl);}}diff --git a/packages/ckeditor5/src/plugins/linkembed.spec.ts b/packages/ckeditor5/src/plugins/linkembed.spec.tsindex c1f2ef4f83..aa7696b09c 100644--- a/packages/ckeditor5/src/plugins/linkembed.spec.ts+++ b/packages/ckeditor5/src/plugins/linkembed.spec.ts@@ -455,7 +455,10 @@ describe("LinkEmbed", () => {expect(findElement(editor, "linkEmbed")).toBeUndefined();// The link AutoLink created is still there, as plain linked text.expect(editor.getData()).toContain(`<a href="${url}">${url}</a>`);- expect(warn).toHaveBeenCalledWith(expect.stringContaining(url));+ // The warning says what happened but never names the URL: a console gets screenshotted and+ // pasted into bug reports, and a pasted link can carry a one-time token.+ expect(warn).toHaveBeenCalledWith(expect.stringContaining("Link preview dropped"));+ expect(warn).not.toHaveBeenCalledWith(expect.stringContaining(url));warn.mockRestore();});diff --git a/packages/ckeditor5/src/plugins/linkembed.ts b/packages/ckeditor5/src/plugins/linkembed.tsindex 225df97666..223b3513de 100644--- a/packages/ckeditor5/src/plugins/linkembed.ts+++ b/packages/ckeditor5/src/plugins/linkembed.ts@@ -1,4 +1,4 @@-import { type BlockChildLike, chooseLinkPreviewKind, isUrlAloneInBlock, redactUrlForLog } from '@triliumnext/commons';+import { type BlockChildLike, chooseLinkPreviewKind, isUrlAloneInBlock } from '@triliumnext/commons';import { ButtonView, Command, Plugin, toWidget, viewToModelPositionOutsideModelElement, Widget, type Observable } from 'ckeditor5';import linkEmbedIcon from '../icons/link-embed.svg?raw';import { preventCKEditorHandling } from './widget_utils.js';@@ -455,7 +455,9 @@ class AutoLinkToMention extends Plugin {// plain link AutoLink already created. A preview built from a hostname placeholder would// show strictly less than the URL it replaced.if (metadata.unresolved) {- console.warn(`Link preview dropped for ${redactUrlForLog(url)}: no metadata could be read from the page (kept as a plain link).`);+ // The URL is left out on purpose — a browser console gets screenshotted and pasted+ // into bug reports too, and the link is right there in the note anyway.+ console.warn('Link preview dropped: no metadata could be read from the page (the link was kept as a plain link).');return;}diff --git a/packages/commons/src/lib/link_embed.spec.ts b/packages/commons/src/lib/link_embed.spec.tsindex 430bb108ad..3bf8bd3c74 100644--- a/packages/commons/src/lib/link_embed.spec.ts+++ b/packages/commons/src/lib/link_embed.spec.ts@@ -5,7 +5,6 @@ import {chooseLinkPreviewKind,extractYouTubeVideoId,isUrlAloneInBlock,- redactUrlForLog,YOUTUBE_REGEX} from "./link_embed.js";@@ -47,24 +46,6 @@ describe("YOUTUBE_REGEX", () => {});});-describe("redactUrlForLog", () => {- it("drops the query and fragment, where the secrets live", () => {- // A password-reset token, a signed S3 signature, a doc-sharing key — none of it belongs in a- // log file. The trailing marker records that parameters were there.- expect(redactUrlForLog("https://example.com/reset?token=s3cr3t")).toBe("https://example.com/reset?…");- expect(redactUrlForLog("https://example.com/doc#key=s3cr3t")).toBe("https://example.com/doc?…");- });-- it("keeps a URL that has nothing to hide intact", () => {- expect(redactUrlForLog("https://example.com/blog/post")).toBe("https://example.com/blog/post");- expect(redactUrlForLog("http://localhost:8080/x")).toBe("http://localhost:8080/x");- });-- it("does not throw on an unparseable URL", () => {- expect(redactUrlForLog("not-a-url")).toBe("<unparseable URL>");- });-});-describe("isUrlAloneInBlock", () => {const url = "https://youtu.be/dQw4w9WgXcQ";const text = (data: string): BlockChildLike => ({ isText: true, data });diff --git a/packages/commons/src/lib/link_embed.ts b/packages/commons/src/lib/link_embed.tsindex d74fb47095..f9da1063ca 100644--- a/packages/commons/src/lib/link_embed.ts+++ b/packages/commons/src/lib/link_embed.ts@@ -5,26 +5,6 @@ export function extractYouTubeVideoId(url: string): string | null {return match ? match[1] : null;}-/**- * A URL trimmed to origin + path for logging.- *- * The query and fragment are where the secrets live — a password-reset token, a signed S3- * signature, a document-sharing key — and a log line is exactly the wrong place for them: logs get- * rotated, backed up, shipped to aggregators and pasted into bug reports. The `?…` marks that- * parameters were present, which is all a reader of the log needs to know.- */-export function redactUrlForLog(url: string): string {- let parsed: URL;- try {- parsed = new URL(url);- } catch {- return "<unparseable URL>";- }-- const withoutParams = `${parsed.origin}${parsed.pathname}`;- return parsed.search || parsed.hash ? `${withoutParams}?…` : withoutParams;-}-export interface LinkEmbedMetadata {url: string;title?: string;