Skip to content

Build your personal knowledge base with Trilium Notes

refactor(desktop): integrate security into windowing

Elian Doran83eb43526f61
diff --git a/apps/desktop/src/main.spec.ts b/apps/desktop/src/main.spec.ts
index f13e936a3f..a859674b03 100644
--- a/apps/desktop/src/main.spec.ts
+++ b/apps/desktop/src/main.spec.ts
@@ -176,7 +176,6 @@ vi.mock("./services/custom_dictionary", () => ({ setupCustomDictionary: vi.fn()
vi.mock("./services/printing", () => ({ setupPrintingHandlers: vi.fn() }));
vi.mock("./services/tray", () => ({ setupSystemTray: vi.fn() }));
vi.mock("./services/shell", () => ({ setupShellHandlers: vi.fn() }));
-vi.mock("./services/web_contents_security", () => ({ setupWebContentsSecurity: vi.fn() }));
vi.mock("./services/security_settings", () => ({
getSecuritySettings: () => h.securitySettings,
registerSecurityIpcHandlers: vi.fn()
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 66d4cc73fa..3bd10426da 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -29,7 +29,6 @@ import { setupPrintingHandlers } from "./services/printing";
import { getSecuritySettings, registerSecurityIpcHandlers } from "./services/security_settings";
import { setupShellHandlers } from "./services/shell";
import { setupSystemTray } from "./services/tray";
-import { setupWebContentsSecurity } from "./services/web_contents_security";
export async function main() {
// Ignore EPIPE errors on stdout/stderr — these occur when the parent process
@@ -104,7 +103,6 @@ export async function main() {
});
setupWindowing();
- setupWebContentsSecurity();
setupSystemTray();
setupCustomDictionary();
setupShellHandlers();
diff --git a/apps/desktop/src/services/window.spec.ts b/apps/desktop/src/services/window.spec.ts
index 6a0af446fd..7d9b09da6f 100644
--- a/apps/desktop/src/services/window.spec.ts
+++ b/apps/desktop/src/services/window.spec.ts
@@ -180,6 +180,11 @@ vi.mock("electron-window-state", () => ({
default: () => ({ x: 0, y: 0, width: 1200, height: 800, manage: vi.fn() })
}));
+// setupWindowing() installs the WebContents security policy; that behaviour has
+// its own suite (web_contents_security.spec.ts), so stub it out here to keep the
+// windowing tests focused and free of the extra electron app/session surface.
+vi.mock("./web_contents_security", () => ({ setupWebContentsSecurity: vi.fn() }));
+
vi.mock("@triliumnext/core", async (importOriginal) => {
const actual = await importOriginal<typeof import("@triliumnext/core")>();
return {
diff --git a/apps/desktop/src/services/window.ts b/apps/desktop/src/services/window.ts
index 7180b6bd63..5d8c7673dd 100644
--- a/apps/desktop/src/services/window.ts
+++ b/apps/desktop/src/services/window.ts
@@ -3,6 +3,8 @@ import { RESOURCE_DIR } from "@triliumnext/server/src/services/resource_dir.js";
import { type BrowserWindow, type BrowserWindowConstructorOptions, default as electron, type Session, type WebContents } from "electron";
import path from "path";
+import { setupWebContentsSecurity } from "./web_contents_security.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
@@ -337,6 +339,12 @@ function getAllWindows() {
* Call once during desktop startup, before `app.ready` fires.
*/
export function setupWindowing() {
+ // Window-open, navigation, <webview>-attach and permission policy is applied
+ // to every WebContents the app creates. Installed here rather than left to
+ // each entry point so a new Electron launcher cannot silently ship without
+ // the renderer/main security boundary.
+ setupWebContentsSecurity();
+
electron.ipcMain.on("create-extra-window", (_event, arg) => {
createExtraWindow(arg.extraWindowHash);
});
diff --git a/apps/edit-docs/src/utils.ts b/apps/edit-docs/src/utils.ts
index 83e91a4c3a..b408875483 100644
--- a/apps/edit-docs/src/utils.ts
+++ b/apps/edit-docs/src/utils.ts
@@ -5,7 +5,6 @@ import ClsHookedExecutionContext from "@triliumnext/server/src/cls_provider.js";
import NodejsCryptoProvider from "@triliumnext/server/src/crypto_provider.js";
import ServerPlatformProvider from "@triliumnext/server/src/platform_provider.js";
import { serverImageProvider } from "@triliumnext/server/src/services/image_provider.js";
-import { setupWebContentsSecurity } from "@triliumnext/desktop/src/services/web_contents_security.js";
import windowService, { setupWindowing } from "@triliumnext/desktop/src/services/window.js";
import BetterSqlite3Provider from "@triliumnext/server/src/sql_provider.js";
import NodejsZipProvider from "@triliumnext/server/src/zip_provider.js";
@@ -108,12 +107,10 @@ export function startElectron(callback: () => void): DeferredPromise<void> {
// IPC calls hit no listener — which storms the TabHistoryNavigationButtons
// render loop with tens of thousands of blocking sendSync calls and pegs
// the renderer. Desktop registers these in apps/desktop/src/main.ts.
+ // (This also installs the WebContents security policy — webview-attach
+ // vetting, window-open/navigation guards, permission handlers.)
setupWindowing();
- // Vet <webview> attach attempts (the windows enable `webviewTag`);
- // desktop does the same in apps/desktop/src/main.ts.
- setupWebContentsSecurity();
-
// Create the main window.
await windowService.createMainWindow();