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): drop the vestigial localhost navigation allowance
Elian Doran169bf0f8579b
diff --git a/apps/desktop/src/services/web_contents_security.spec.ts b/apps/desktop/src/services/web_contents_security.spec.tsindex af83517a9f..facb83f145 100644--- a/apps/desktop/src/services/web_contents_security.spec.ts+++ b/apps/desktop/src/services/web_contents_security.spec.ts@@ -506,18 +506,22 @@ describe("navigation guard", () => {setupWebContentsSecurity();});- it("only allows internal root navigation (setup and migration redirects)", () => {+ it("only allows the app shell at its root path", () => {expect(isNavigationAllowed("trilium-app://app/")).toBe(true);- // Internal host with the root "/?" path is allowed.+ // Root "/?" path is allowed.expect(isNavigationAllowed("trilium-app://app/?")).toBe(true);- expect(isNavigationAllowed("http://localhost/")).toBe(true);- expect(isNavigationAllowed("http://127.0.0.1/")).toBe(true);- expect(isNavigationAllowed("https://evil.example/page")).toBe(false);- // Internal host but non-root path is blocked.- expect(isNavigationAllowed("http://localhost/somewhere")).toBe(false);+ // App shell but non-root path is blocked (in-page SPA routing / hostile).+ expect(isNavigationAllowed("trilium-app://app/somewhere")).toBe(false);// Our scheme but not the app host — only `trilium-app://app` is ever served.expect(isNavigationAllowed("trilium-app://evil/")).toBe(false);+ expect(isNavigationAllowed("https://evil.example/page")).toBe(false);+ // localhost is no longer a trusted host: the renderer is served only+ // from trilium-app://app, so a link to a local listener must not+ // navigate the privileged window.+ expect(isNavigationAllowed("http://localhost/")).toBe(false);+ expect(isNavigationAllowed("http://127.0.0.1/")).toBe(false);+ expect(isNavigationAllowed("http://127.0.0.1:9090/")).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 10a20aa5aa..7e7e8b3af9 100644--- a/apps/desktop/src/services/web_contents_security.ts+++ b/apps/desktop/src/services/web_contents_security.ts@@ -184,16 +184,20 @@ export function isPermissionAllowedForOrigin(kind: SessionKind, permission: stri* Decides whether a renderer-initiated navigation (link click, drag & drop,* meta refresh) may proceed. Only the app shell itself is allowed: the* `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.+ * call sites), and only at the root path. Anything deeper is in-page SPA+ * routing (which `will-navigate` does not fire for) or hostile.+ *+ * `localhost` was previously allowed too, from the era when the desktop+ * renderer was served over `http://127.0.0.1:<port>`. The custom-protocol+ * migration made the shell load exclusively from `trilium-app://app/`, so the+ * carve-out is now dead — and dangerous: it let a crafted link navigate the+ * privileged window (which keeps the preload bridge) to any local listener.*/export function isNavigationAllowed(targetUrl: string): boolean {const parsedUrl = url.parse(targetUrl);- const isInternal = (parsedUrl.protocol === `${TRILIUM_APP_SCHEME}:` && parsedUrl.hostname === TRILIUM_APP_HOST)- || ["localhost", "127.0.0.1"].includes(parsedUrl.hostname || "");- return isInternal && (!parsedUrl.path || parsedUrl.path === "/" || parsedUrl.path === "/?");+ const isAppShell = parsedUrl.protocol === `${TRILIUM_APP_SCHEME}:` && parsedUrl.hostname === TRILIUM_APP_HOST;+ return isAppShell && (!parsedUrl.path || parsedUrl.path === "/" || parsedUrl.path === "/?");}/**