Skip to content

Build your personal knowledge base with Trilium Notes

fix(desktop): require an exact guest partition on <webview> attach

Elian Doranb238727ce573
diff --git a/apps/desktop/src/services/web_contents_security.spec.ts b/apps/desktop/src/services/web_contents_security.spec.ts
index facb83f145..958cf242e7 100644
--- a/apps/desktop/src/services/web_contents_security.spec.ts
+++ b/apps/desktop/src/services/web_contents_security.spec.ts
@@ -135,7 +135,8 @@ function resetState() {
describe("hardenWebviewPreferences", () => {
it("reports no violations for a benign attach and forces isolation on", () => {
- const prefs: Electron.WebPreferences = {};
+ // A legitimate attach (WebView.tsx) always declares the guest partition.
+ const prefs: Electron.WebPreferences = { partition: "persist:webview" };
const violations = hardenWebviewPreferences(prefs);
@@ -151,7 +152,7 @@ describe("hardenWebviewPreferences", () => {
});
});
- it("accepts the dedicated guest partition and rejects any other", () => {
+ it("requires exactly the guest partition and rejects any other, unset, or empty value", () => {
const legit: Electron.WebPreferences = { partition: "persist:webview" };
expect(hardenWebviewPreferences(legit)).toEqual([]);
@@ -166,10 +167,21 @@ describe("hardenWebviewPreferences", () => {
const viaParams: Electron.WebPreferences = {};
expect(hardenWebviewPreferences(viaParams, "persist:evil")).toEqual(["partition 'persist:evil'"]);
expect(viaParams.partition).toBe("persist:webview");
+
+ // Unset partition (no attribute) — must not slip through to the default
+ // session, so it is a violation and the attach is denied.
+ const unset: Electron.WebPreferences = {};
+ expect(hardenWebviewPreferences(unset)).toEqual(["partition '<unset>'"]);
+ expect(unset.partition).toBe("persist:webview");
+
+ // Empty partition — Electron surfaces an omitted <webview partition> as
+ // "", which must be rejected just the same.
+ const empty: Electron.WebPreferences = {};
+ expect(hardenWebviewPreferences(empty, "")).toEqual(["partition ''"]);
});
it("normalizes explicitly-disabled isolation silently (Electron default plumbing, not hostile markup)", () => {
- const prefs: Electron.WebPreferences = { contextIsolation: false, sandbox: false };
+ const prefs: Electron.WebPreferences = { contextIsolation: false, sandbox: false, partition: "persist:webview" };
const violations = hardenWebviewPreferences(prefs);
@@ -179,8 +191,8 @@ describe("hardenWebviewPreferences", () => {
});
it("strips preload scripts and reports them, including the legacy preloadURL alias", () => {
- const prefs = { preload: "/evil.js" } as Electron.WebPreferences;
- const legacyPrefs = { preloadURL: "file:///evil.js" } as Electron.WebPreferences & { preloadURL?: string };
+ const prefs = { preload: "/evil.js", partition: "persist:webview" } as Electron.WebPreferences;
+ const legacyPrefs = { preloadURL: "file:///evil.js", partition: "persist:webview" } as Electron.WebPreferences & { preloadURL?: string };
expect(hardenWebviewPreferences(prefs)).toEqual(["preload script"]);
expect(hardenWebviewPreferences(legacyPrefs)).toEqual(["preload script"]);
@@ -193,7 +205,8 @@ describe("hardenWebviewPreferences", () => {
nodeIntegration: true,
nodeIntegrationInSubFrames: true,
webSecurity: false,
- allowRunningInsecureContent: true
+ allowRunningInsecureContent: true,
+ partition: "persist:webview"
};
const violations = hardenWebviewPreferences(prefs);
@@ -254,7 +267,7 @@ describe("setupWebContentsSecurity", () => {
});
it("denies an attach requesting nodeIntegration and logs the src", () => {
- const { preventDefault } = attachWebview({ nodeIntegration: true }, "https://evil.example");
+ const { preventDefault } = attachWebview({ nodeIntegration: true }, "https://evil.example", "persist:webview");
expect(preventDefault).toHaveBeenCalledOnce();
expect(state.errors).toHaveLength(1);
@@ -263,7 +276,7 @@ describe("setupWebContentsSecurity", () => {
});
it("denies an attach requesting a preload script", () => {
- const { preventDefault } = attachWebview({ preload: "/evil.js" } as Electron.WebPreferences);
+ const { preventDefault } = attachWebview({ preload: "/evil.js" } as Electron.WebPreferences, "https://example.com", "persist:webview");
expect(preventDefault).toHaveBeenCalledOnce();
expect(state.errors[0]).toContain("preload script");
diff --git a/apps/desktop/src/services/web_contents_security.ts b/apps/desktop/src/services/web_contents_security.ts
index 7e7e8b3af9..9102890eb5 100644
--- a/apps/desktop/src/services/web_contents_security.ts
+++ b/apps/desktop/src/services/web_contents_security.ts
@@ -100,13 +100,16 @@ export function hardenWebviewPreferences(webPreferences: Electron.WebPreferences
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.
+ // the dedicated guest partition. Anything else is a violation: a different
+ // partition is hostile markup, and an unset/empty one (Electron surfaces an
+ // omitted attribute as "") would otherwise risk attaching the guest into
+ // the *default* session — shared cookie jar and trilium-app:// registry.
+ // Requiring an exact match keeps a single source of truth; supporting
+ // per-note partitions later means relaxing this to a `persist:webview`
+ // prefix check AND having WebView.tsx set the chosen value.
const partition = prefs.partition ?? requestedPartition;
- if (partition !== undefined && partition !== WEBVIEW_SESSION_PARTITION) {
- violations.push(`partition '${partition}'`);
+ if (partition !== WEBVIEW_SESSION_PARTITION) {
+ violations.push(`partition '${partition ?? "<unset>"}'`);
}
prefs.partition = WEBVIEW_SESSION_PARTITION;