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): enforce scheme allowlist on window-open external URLs
Elian Doranf2c54b4d2ddc
diff --git a/apps/desktop/src/services/window.spec.ts b/apps/desktop/src/services/window.spec.tsindex de5dafb844..9758563728 100644--- a/apps/desktop/src/services/window.spec.ts+++ b/apps/desktop/src/services/window.spec.ts@@ -390,7 +390,25 @@ describe("window service", () => {const result = handlerArg({ url: "https://example.com" });expect(result).toEqual({ action: "deny" });await new Promise((r) => setTimeout(r, 0));- expect(fakeShell.openExternal).toHaveBeenCalledWith("https://example.com");+ // URL round-trips through the validator, which normalizes it.+ expect(fakeShell.openExternal).toHaveBeenCalledWith("https://example.com/");+ });++ it("refuses to open URLs with blocked schemes externally", async () => {+ const wc = state.windows[state.windows.length - 1].webContents;+ const handlerArg = wc.setWindowOpenHandler.mock.calls[0][0] as Handler;++ // Follina-class and credential-leak schemes must not reach the OS+ // handler via window.open / target=_blank either — same allowlist+ // as the open-external IPC channel.+ for (const hostileUrl of ["ms-msdt:/id PCWDiagnostic", "smb://attacker.example/share", "not a url"]) {+ const result = handlerArg({ url: hostileUrl });+ expect(result).toEqual({ action: "deny" });+ }+ await new Promise((r) => setTimeout(r, 0));++ expect(fakeShell.openExternal).not.toHaveBeenCalled();+ expect(state.log.error).toHaveBeenCalledTimes(3);});it("logs when external open fails", async () => {diff --git a/apps/desktop/src/services/window.ts b/apps/desktop/src/services/window.tsindex 86e06e8b57..30c3610cd8 100644--- a/apps/desktop/src/services/window.ts+++ b/apps/desktop/src/services/window.ts@@ -4,6 +4,8 @@ import { type BrowserWindow, type BrowserWindowConstructorOptions, default as elimport path from "path";import url from "url";+import { validateOpenExternalUrl } from "./shell.js";+// Preload bundle path. Two layouts:// - Dev: this file lives at apps/desktop/src/services/window.ts, and the// preload bundle is one level up at apps/desktop/src/preload.compiled.cjs@@ -168,7 +170,13 @@ function getWindowExtraOpts() {async function configureWebContents(webContents: WebContents, spellcheckEnabled: boolean) {webContents.setWindowOpenHandler((details) => {async function openExternal() {- (await import("electron")).shell.openExternal(details.url);+ // A target=_blank link or window.open() from note content is just+ // as attacker-controllable as an IPC payload, so enforce the same+ // scheme allowlist as the open-external IPC channel — otherwise+ // Follina-class (ms-msdt:) and credential-leak (smb:) URLs would+ // bypass it here.+ const validated = validateOpenExternalUrl(details.url);+ (await import("electron")).shell.openExternal(validated.toString());}openExternal().catch(err => {