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
feat(electron): harden open file URL
Elian Dorana80617dfb6b1
diff --git a/apps/server/src/services/open_file_url.spec.ts b/apps/server/src/services/open_file_url.spec.tsnew file mode 100644index 0000000000..d99093eeb5--- /dev/null+++ b/apps/server/src/services/open_file_url.spec.ts@@ -0,0 +1,54 @@+import path from "path";+import { describe, expect, it } from "vitest";++import { validateOpenFileUrl } from "./open_file_url.js";++describe("validateOpenFileUrl", () => {+ it("rejects malformed input", () => {+ expect(() => validateOpenFileUrl(undefined)).toThrow(/invalid URL/);+ expect(() => validateOpenFileUrl(null)).toThrow(/invalid URL/);+ expect(() => validateOpenFileUrl(123)).toThrow(/invalid URL/);+ expect(() => validateOpenFileUrl("")).toThrow(/invalid URL/);+ });++ it("rejects strings that don't parse as URLs", () => {+ expect(() => validateOpenFileUrl("not a url")).toThrow(/not a valid URL/);+ expect(() => validateOpenFileUrl("/some/path")).toThrow(/not a valid URL/);+ });++ it("rejects non-file: schemes", () => {+ expect(() => validateOpenFileUrl("https://example.com/foo")).toThrow(/not a file: URL/);+ expect(() => validateOpenFileUrl("smb://attacker.example/share/x"))+ .toThrow(/not a file: URL/);+ });++ it("rejects UNC paths (the NTLM-credential-theft vector)", () => {+ // Classic UNC: file: URL with a non-empty hostname resolves to+ // \\host\share\x on Windows, triggering SMB auth and leaking NTLM hash.+ expect(() => validateOpenFileUrl("file://attacker.example/share/x"))+ .toThrow(/UNC path blocked: attacker\.example/);+ expect(() => validateOpenFileUrl("file://192.168.1.1/c$/Windows"))+ .toThrow(/UNC path blocked: 192\.168\.1\.1/);+ });++ it.runIf(process.platform === "win32")(+ "accepts and resolves normal Windows file: URLs",+ () => {+ expect(validateOpenFileUrl("file:///C:/Windows/notepad.exe"))+ .toBe(path.win32.normalize("C:\\Windows\\notepad.exe"));+ // Malformed form some sources emit: file://C:/path (drive letter as host).+ // The validator normalises this before the empty-host check.+ expect(validateOpenFileUrl("file://C:/Windows/notepad.exe"))+ .toBe(path.win32.normalize("C:\\Windows\\notepad.exe"));+ }+ );++ it.runIf(process.platform !== "win32")(+ "accepts and resolves normal POSIX file: URLs",+ () => {+ expect(validateOpenFileUrl("file:///etc/hosts")).toBe("/etc/hosts");+ expect(validateOpenFileUrl("file:///tmp/foo bar"))+ .toBe("/tmp/foo bar");+ }+ );+});diff --git a/apps/server/src/services/open_file_url.ts b/apps/server/src/services/open_file_url.tsnew file mode 100644index 0000000000..47e120e712--- /dev/null+++ b/apps/server/src/services/open_file_url.ts@@ -0,0 +1,40 @@+import url from "url";++/**+ * Validates a URL received from the renderer for the "open-file-url" IPC+ * channel and returns the resolved filesystem path. Throws on any invariant+ * violation so a hostile caller surfaces loudly in the main-process log.+ *+ * The specific threat blocked here is UNC paths: a file: URL with a non-empty+ * hostname (file://attacker.example/share/x) resolves on Windows to+ * \\attacker.example\share\x, which triggers SMB authentication and leaks the+ * logged-in user's NTLM hash to the attacker — the same primitive blocked for+ * smb:// URLs in the openExternal allowlist.+ */+export function validateOpenFileUrl(input: unknown): string {+ if (typeof input !== "string" || input.length === 0) {+ throw new Error("open-file-url: invalid URL");+ }++ // Some sources produce malformed file URLs that look like file://C:/path+ // (drive letter parsed as the host). Insert the missing slash so the URL+ // parser sees file:///C:/path with an empty host.+ const normalized = input.replace(/^file:\/\/(?=[a-zA-Z]:)/i, "file:///");++ let parsed: URL;+ try {+ parsed = new URL(normalized);+ } catch {+ throw new Error(`open-file-url: not a valid URL: ${input}`);+ }++ if (parsed.protocol !== "file:") {+ throw new Error(`open-file-url: not a file: URL: ${input}`);+ }++ if (parsed.hostname !== "") {+ throw new Error(`open-file-url: UNC path blocked: ${parsed.hostname}`);+ }++ return url.fileURLToPath(parsed);+}diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.tsindex ad9a32833c..40462c3145 100644--- a/apps/server/src/services/window.ts+++ b/apps/server/src/services/window.ts@@ -12,6 +12,7 @@ import keyboardActionsService from "./keyboard_actions.js";import log from "./log.js";import { validateOpenCustomPath } from "./open_custom.js";import { validateOpenExternalUrl } from "./open_external.js";+import { validateOpenFileUrl } from "./open_file_url.js";import optionService from "./options.js";import { initPrintingHandlers } from "./printing.js";import { RESOURCE_DIR } from "./resource_dir.js";@@ -226,8 +227,8 @@ electron.ipcMain.handle("open-path", (_event, filePath: string) => {});electron.ipcMain.handle("open-file-url", (_event, fileUrl: string) => {- const normalized = fileUrl.replace(/^file:\/\/(?=[a-zA-Z]:)/i, "file:///");- return electron.shell.openPath(url.fileURLToPath(normalized));+ const filePath = validateOpenFileUrl(fileUrl);+ return electron.shell.openPath(filePath);});electron.ipcMain.on("download-url", (event, downloadUrl: string) => {