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
chore(electron): restrict open external
Elian Dorand5770e0d15ef
diff --git a/apps/server/src/services/open_external.spec.ts b/apps/server/src/services/open_external.spec.tsnew file mode 100644index 0000000000..ce9618e914--- /dev/null+++ b/apps/server/src/services/open_external.spec.ts@@ -0,0 +1,77 @@+import { describe, expect, it } from "vitest";++import { validateOpenExternalUrl } from "./open_external.js";++describe("validateOpenExternalUrl", () => {+ it("rejects malformed input", () => {+ expect(() => validateOpenExternalUrl(undefined)).toThrow(/invalid URL/);+ expect(() => validateOpenExternalUrl(null)).toThrow(/invalid URL/);+ expect(() => validateOpenExternalUrl(123)).toThrow(/invalid URL/);+ expect(() => validateOpenExternalUrl("")).toThrow(/invalid URL/);+ });++ it("rejects strings that don't parse as URLs", () => {+ expect(() => validateOpenExternalUrl("not a url")).toThrow(/not a valid URL/);+ expect(() => validateOpenExternalUrl("example.com")).toThrow(/not a valid URL/);+ expect(() => validateOpenExternalUrl("/relative/path")).toThrow(/not a valid URL/);+ });++ it("rejects known-dangerous OS protocol-handler schemes", () => {+ // Follina (CVE-2022-30190) and friends+ expect(() => validateOpenExternalUrl("ms-msdt:/id PCWDiagnostic"))+ .toThrow(/blocked scheme 'ms-msdt'/);+ expect(() => validateOpenExternalUrl("search-ms:query=x&crumb=location:\\\\evil.example\\share"))+ .toThrow(/blocked scheme 'search-ms'/);+ expect(() => validateOpenExternalUrl("ms-officecmd:%7B%22LocalProviders%22:..."))+ .toThrow(/blocked scheme 'ms-officecmd'/);++ // Script execution+ expect(() => validateOpenExternalUrl("javascript:alert(1)"))+ .toThrow(/blocked scheme 'javascript'/);+ expect(() => validateOpenExternalUrl("vbscript:msgbox(1)"))+ .toThrow(/blocked scheme 'vbscript'/);+ });++ it("rejects schemes explicitly dropped from the openExternal allowlist", () => {+ // local-file launcher (must go through openFileUrl instead)+ expect(() => validateOpenExternalUrl("file:///C:/Windows/System32/calc.exe"))+ .toThrow(/blocked scheme 'file'/);+ // phishing surface+ expect(() => validateOpenExternalUrl("data:text/html,<script>alert(1)</script>"))+ .toThrow(/blocked scheme 'data'/);+ // NTLM credential theft / SMB relay+ expect(() => validateOpenExternalUrl("smb://attacker.example/share/file"))+ .toThrow(/blocked scheme 'smb'/);+ // NTLM relay / JNDI lookup+ expect(() => validateOpenExternalUrl("ldap://attacker.example/dc=x"))+ .toThrow(/blocked scheme 'ldap'/);+ expect(() => validateOpenExternalUrl("ldaps://attacker.example/dc=x"))+ .toThrow(/blocked scheme 'ldaps'/);+ });++ it("accepts standard web and communication schemes", () => {+ expect(validateOpenExternalUrl("https://example.com/path").toString())+ .toBe("https://example.com/path");+ expect(validateOpenExternalUrl("http://example.com/").toString())+ .toBe("http://example.com/");+ expect(validateOpenExternalUrl("mailto:[email protected]").toString())+ .toBe("mailto:[email protected]");+ expect(validateOpenExternalUrl("tel:+15551234").toString())+ .toBe("tel:+15551234");+ });++ it("accepts the note-app integration schemes (zotero/obsidian/etc.)", () => {+ expect(() => validateOpenExternalUrl("zotero://select/items/0_ABC")).not.toThrow();+ expect(() => validateOpenExternalUrl("obsidian://open?vault=Notes")).not.toThrow();+ expect(() => validateOpenExternalUrl("logseq://x-callback-url/foo")).not.toThrow();+ expect(() => validateOpenExternalUrl("evernote://x")).not.toThrow();+ expect(() => validateOpenExternalUrl("onenote://x")).not.toThrow();+ });++ it("matches the scheme case-insensitively", () => {+ // URL.protocol returns lowercase, so HTTPS:// becomes "https:".+ expect(() => validateOpenExternalUrl("HTTPS://example.com")).not.toThrow();+ expect(() => validateOpenExternalUrl("MS-MSDT:/id x"))+ .toThrow(/blocked scheme 'ms-msdt'/);+ });+});diff --git a/apps/server/src/services/open_external.ts b/apps/server/src/services/open_external.tsnew file mode 100644index 0000000000..6f87f99799--- /dev/null+++ b/apps/server/src/services/open_external.ts@@ -0,0 +1,33 @@+import { SHELL_OPEN_EXTERNAL_PROTOCOLS } from "@triliumnext/commons";++/**+ * Validates a URL received from the renderer for the "open-external" IPC+ * channel. Returns the parsed URL on success; throws on any invariant+ * violation so a hostile or buggy caller surfaces loudly in the main-process+ * log rather than silently dispatching to an OS protocol handler.+ *+ * The handler this gates (electron.shell.openExternal) historically has been+ * a Follina-class RCE primitive on Windows (ms-msdt:, search-ms:, etc.) and a+ * credential-leak primitive (smb:, ldap:). Allowlisting the scheme is the+ * only reliable defence.+ */+export function validateOpenExternalUrl(input: unknown): URL {+ if (typeof input !== "string" || input.length === 0) {+ throw new Error("open-external: invalid URL");+ }++ let parsed: URL;+ try {+ parsed = new URL(input);+ } catch {+ throw new Error(`open-external: not a valid URL: ${input}`);+ }++ // URL.protocol returns "http:" — strip the trailing colon for comparison.+ const scheme = parsed.protocol.replace(/:$/, "").toLowerCase();+ if (!SHELL_OPEN_EXTERNAL_PROTOCOLS.includes(scheme)) {+ throw new Error(`open-external: blocked scheme '${scheme}'`);+ }++ return parsed;+}diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.tsindex b3efe454a6..2bb2f8aa42 100644--- a/apps/server/src/services/window.ts+++ b/apps/server/src/services/window.ts@@ -10,6 +10,7 @@ import dataDirs from "./data_dir.js";import keyboardActionsService from "./keyboard_actions.js";import log from "./log.js";import { validateOpenCustomPath } from "./open_custom.js";+import { validateOpenExternalUrl } from "./open_external.js";import optionService from "./options.js";import { initPrintingHandlers } from "./printing.js";import { RESOURCE_DIR } from "./resource_dir.js";@@ -215,7 +216,8 @@ electron.ipcMain.on("web-contents-action", (event, action: string, text?: string});electron.ipcMain.on("open-external", (_event, url: string) => {- electron.shell.openExternal(url);+ const validated = validateOpenExternalUrl(url);+ electron.shell.openExternal(validated.toString());});electron.ipcMain.handle("open-path", (_event, filePath: string) => {diff --git a/packages/commons/src/lib/shared_constants.ts b/packages/commons/src/lib/shared_constants.tsindex 0682a71a02..2e5425143d 100644--- a/packages/commons/src/lib/shared_constants.ts+++ b/packages/commons/src/lib/shared_constants.ts@@ -20,3 +20,23 @@ export const ALLOWED_PROTOCOLS = ['view-source', 'vlc', 'vnc', 'ws', 'wss', 'xmpp', 'jdbc', 'slack', 'tel', 'smb', 'zotero', 'geo','logseq', 'mid', 'obsidian', 'bookends', 'highlights'];++// Subset of ALLOWED_PROTOCOLS that the main process will hand to+// electron.shell.openExternal. ALLOWED_PROTOCOLS gates DISPLAY (sanitizer /+// CKEditor); this list gates DISPATCH to the OS protocol handler. Derived+// by filtering rather than duplicating so the two stay in sync when+// ALLOWED_PROTOCOLS gains new entries.+//+// Excluded schemes:+// - file local-file launcher; routed separately via openFileUrl+// - data phishing / arbitrary HTML in the default browser+// - smb NTLM credential theft, SMB relay+// - ldap/ldaps NTLM relay, JNDI lookup vectors+// - jar Java loader RCE history+// - view-source browser-internal, no value via shell dispatch+const SHELL_OPEN_EXTERNAL_BLOCKLIST = new Set([+ 'file', 'data', 'smb', 'ldap', 'ldaps', 'jar', 'view-source'+]);+export const SHELL_OPEN_EXTERNAL_PROTOCOLS = ALLOWED_PROTOCOLS.filter(+ p => !SHELL_OPEN_EXTERNAL_BLOCKLIST.has(p)+);