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(desktop): pin origin and navigation checks to the exact trilium-app://app host
Elian Doran72319a25adf2
diff --git a/apps/desktop/src/protocol.spec.ts b/apps/desktop/src/protocol.spec.tsindex cfaebb7222..af9e38f90e 100644--- a/apps/desktop/src/protocol.spec.ts+++ b/apps/desktop/src/protocol.spec.ts@@ -459,12 +459,16 @@ describe("trilium-app protocol dispatcher", () => {}// Rule 1: foreign origins are rejected regardless of method,- // including the opaque "null" origin of sandboxed frames.+ // including the opaque "null" origin of sandboxed frames and+ // other hosts under our own scheme — `trilium-app://app` is the+ // only origin the app ever loads.expect(isDispatchOriginAllowed("GET", "https://evil.example")).toBe(false);expect(isDispatchOriginAllowed("POST", "https://evil.example")).toBe(false);expect(isDispatchOriginAllowed("POST", "null")).toBe(false);+ expect(isDispatchOriginAllowed("POST", "trilium-app://evil")).toBe(false);+ expect(isDispatchOriginAllowed("POST", "trilium-app://app.evil.example")).toBe(false);- // Our own scheme passes for both reads and writes.+ // The app's own exact origin passes for both reads and writes.expect(isDispatchOriginAllowed("GET", "trilium-app://app")).toBe(true);expect(isDispatchOriginAllowed("POST", "trilium-app://app")).toBe(true);});diff --git a/apps/desktop/src/protocol.ts b/apps/desktop/src/protocol.tsindex fbcd2a292f..5b19501238 100644--- a/apps/desktop/src/protocol.ts+++ b/apps/desktop/src/protocol.ts@@ -49,8 +49,9 @@ export function registerTriliumAppScheme() {export function setupTriliumAppProtocol(app: Application) {electron.app.whenReady().then(() => {electron.protocol.handle("trilium-app", async (request) => {- if (!isDispatchOriginAllowed(request.method, request.headers.get("origin"))) {- console.error(`[trilium-app] blocked ${request.method} ${request.url} from origin '${request.headers.get("origin")}'`);+ const origin = request.headers.get("origin");+ if (!isDispatchOriginAllowed(request.method, origin)) {+ console.error(`[trilium-app] blocked ${request.method} ${request.url} from origin '${origin}'`);return new Response("Forbidden", { status: 403 });}try {@@ -76,8 +77,10 @@ export function setupTriliumAppProtocol(app: Application) {* frame whose Origin header (when present) is trustworthy.** Policy:- * 1. `Origin` present but not `trilium-app://...` → reject. Catches foreign- * http(s) frames and the opaque `"null"` origin of sandboxed frames.+ * 1. `Origin` present but not exactly `trilium-app://app` (the sole origin+ * the app ever loads — see the loadURL call sites) → reject. Catches+ * foreign http(s) frames, the opaque `"null"` origin of sandboxed frames,+ * and any other host under our own scheme.* 2. No `Origin` on a state-changing method → reject. Chromium always* attaches Origin to cross-origin and non-GET/HEAD requests, so a write* without one is anomalous by construction.@@ -89,7 +92,7 @@ export function setupTriliumAppProtocol(app: Application) {*/export function isDispatchOriginAllowed(method: string, origin: string | null): boolean {if (origin !== null) {- return origin.startsWith("trilium-app://");+ return origin === "trilium-app://app";}return method === "GET" || method === "HEAD";}diff --git a/apps/desktop/src/services/web_contents_security.spec.ts b/apps/desktop/src/services/web_contents_security.spec.tsindex 7971321142..d0a9d8908a 100644--- a/apps/desktop/src/services/web_contents_security.spec.ts+++ b/apps/desktop/src/services/web_contents_security.spec.ts@@ -408,6 +408,8 @@ describe("navigation guard", () => {expect(isNavigationAllowed("https://evil.example/page")).toBe(false);// Internal host but non-root path is blocked.expect(isNavigationAllowed("http://localhost/somewhere")).toBe(false);+ // Our scheme but not the app host — only `trilium-app://app` is ever served.+ expect(isNavigationAllowed("trilium-app://evil/")).toBe(false);// URL with no hostname falls back to "" and is blocked.expect(isNavigationAllowed("javascript:void(0)")).toBe(false);});diff --git a/apps/desktop/src/services/web_contents_security.ts b/apps/desktop/src/services/web_contents_security.tsindex 4bf24a95f7..81324c2662 100644--- a/apps/desktop/src/services/web_contents_security.ts+++ b/apps/desktop/src/services/web_contents_security.ts@@ -148,15 +148,15 @@ export function isPermissionAllowed(kind: SessionKind, permission: string): bool/*** Decides whether a renderer-initiated navigation (link click, drag & drop,* meta refresh) may proceed. Only the app shell itself is allowed: the- * `trilium-app://` protocol or localhost, and only at the root path —- * internal redirects from the setup and migration pages land there. Anything- * deeper is in-page SPA routing (which `will-navigate` does not fire for) or- * hostile.+ * `trilium-app://app` origin (the sole host ever served — see the loadURL+ * call sites) or localhost, and only at the root path — internal redirects+ * from the setup and migration pages land there. Anything deeper is in-page+ * SPA routing (which `will-navigate` does not fire for) or hostile.*/export function isNavigationAllowed(targetUrl: string): boolean {const parsedUrl = url.parse(targetUrl);- const isInternal = parsedUrl.protocol === "trilium-app:"+ const isInternal = (parsedUrl.protocol === "trilium-app:" && parsedUrl.hostname === "app")|| ["localhost", "127.0.0.1"].includes(parsedUrl.hostname || "");return isInternal && (!parsedUrl.path || parsedUrl.path === "/" || parsedUrl.path === "/?");}