Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): pin <webview> attaches to the dedicated guest partition

Elian Dorandad968d56389
diff --git a/apps/desktop/src/services/web_contents_security.spec.ts b/apps/desktop/src/services/web_contents_security.spec.ts
index ed36a5ea18..e77d4e394c 100644
--- a/apps/desktop/src/services/web_contents_security.spec.ts
+++ b/apps/desktop/src/services/web_contents_security.spec.ts
@@ -34,10 +34,28 @@ describe("hardenWebviewPreferences", () => {
webSecurity: true,
allowRunningInsecureContent: false,
contextIsolation: true,
- sandbox: true
+ sandbox: true,
+ partition: "persist:webview"
});
});
+ it("accepts the dedicated guest partition and rejects any other", () => {
+ const legit: Electron.WebPreferences = { partition: "persist:webview" };
+ expect(hardenWebviewPreferences(legit)).toEqual([]);
+
+ // Wrong partition in the web preferences (e.g. trying to share the
+ // default session's persistent storage under another name).
+ const hostile: Electron.WebPreferences = { partition: "persist:evil" };
+ expect(hardenWebviewPreferences(hostile)).toEqual(["partition 'persist:evil'"]);
+ expect(hostile.partition).toBe("persist:webview");
+
+ // Wrong partition surfaced only via the attach params (attribute
+ // plumbing differs between Electron versions).
+ const viaParams: Electron.WebPreferences = {};
+ expect(hardenWebviewPreferences(viaParams, "persist:evil")).toEqual(["partition 'persist:evil'"]);
+ expect(viaParams.partition).toBe("persist:webview");
+ });
+
it("normalizes explicitly-disabled isolation silently (Electron default plumbing, not hostile markup)", () => {
const prefs: Electron.WebPreferences = { contextIsolation: false, sandbox: false };
@@ -96,7 +114,7 @@ describe("setupWebContentsSecurity", () => {
});
/** Simulates a window being created and then a <webview> attaching inside it. */
- function attachWebview(prefs: Electron.WebPreferences, src = "https://example.com"): MockAttach {
+ function attachWebview(prefs: Electron.WebPreferences, src = "https://example.com", partition?: string): MockAttach {
const created = state.appHandlers.get("web-contents-created");
if (!created) throw new Error("web-contents-created not registered");
@@ -107,17 +125,25 @@ describe("setupWebContentsSecurity", () => {
if (!willAttach) throw new Error("will-attach-webview not registered");
const preventDefault = vi.fn();
- willAttach({ preventDefault }, prefs, { src });
+ willAttach({ preventDefault }, prefs, { src, partition });
return { preventDefault, prefs };
}
it("lets a benign webview attach with hardened preferences", () => {
- const { preventDefault, prefs } = attachWebview({});
+ const { preventDefault, prefs } = attachWebview({}, "https://example.com", "persist:webview");
expect(preventDefault).not.toHaveBeenCalled();
expect(state.errors).toEqual([]);
expect(prefs.nodeIntegration).toBe(false);
expect(prefs.contextIsolation).toBe(true);
+ expect(prefs.partition).toBe("persist:webview");
+ });
+
+ it("denies an attach requesting a foreign partition passed via attach params", () => {
+ const { preventDefault } = attachWebview({}, "https://evil.example", "persist:other");
+
+ expect(preventDefault).toHaveBeenCalledOnce();
+ expect(state.errors[0]).toContain("partition 'persist:other'");
});
it("denies an attach requesting nodeIntegration and logs the src", () => {
diff --git a/apps/desktop/src/services/web_contents_security.ts b/apps/desktop/src/services/web_contents_security.ts
index 8ff449e415..0d405318ca 100644
--- a/apps/desktop/src/services/web_contents_security.ts
+++ b/apps/desktop/src/services/web_contents_security.ts
@@ -1,3 +1,4 @@
+import { WEBVIEW_SESSION_PARTITION } from "@triliumnext/commons";
import { getLog } from "@triliumnext/core";
import electron from "electron";
@@ -32,7 +33,10 @@ import electron from "electron";
export function setupWebContentsSecurity() {
electron.app.on("web-contents-created", (_event, webContents) => {
webContents.on("will-attach-webview", (attachEvent, webPreferences, params) => {
- const violations = hardenWebviewPreferences(webPreferences);
+ // Depending on the Electron version the `partition` attribute
+ // surfaces on `webPreferences` or only on the attach params, so
+ // feed both into the check.
+ const violations = hardenWebviewPreferences(webPreferences, params.partition);
if (violations.length > 0) {
attachEvent.preventDefault();
getLog().error(`Blocked <webview> attach requesting [${violations.join(", ")}] for src: ${params.src}`);
@@ -50,7 +54,7 @@ export function setupWebContentsSecurity() {
* reported: Electron may legitimately pass them unset for guests, so they are
* normalization, not evidence of hostile markup.
*/
-export function hardenWebviewPreferences(webPreferences: Electron.WebPreferences): string[] {
+export function hardenWebviewPreferences(webPreferences: Electron.WebPreferences, requestedPartition?: string): string[] {
const violations: string[] = [];
// `preloadURL` is the legacy alias of `preload`; Electron still honours it.
@@ -73,7 +77,17 @@ export function hardenWebviewPreferences(webPreferences: Electron.WebPreferences
if (prefs.allowRunningInsecureContent) {
violations.push("allowRunningInsecureContent");
}
+ // The only legitimate <webview> (the Web View note type) always declares
+ // the dedicated guest partition. Explicitly requesting a different one is
+ // hostile markup; omitting it would attach the guest into the *default*
+ // session — shared cookie jar and trilium-app:// protocol registry — so
+ // it is force-set below either way.
+ const partition = prefs.partition ?? requestedPartition;
+ if (partition !== undefined && partition !== WEBVIEW_SESSION_PARTITION) {
+ violations.push(`partition '${partition}'`);
+ }
+ prefs.partition = WEBVIEW_SESSION_PARTITION;
prefs.nodeIntegration = false;
prefs.nodeIntegrationInSubFrames = false;
prefs.webSecurity = true;