Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(desktop): enable the renderer process sandbox (#7225)

BuXiuDN8f48c380d496
diff --git a/apps/desktop/electron.vite.config.ts b/apps/desktop/electron.vite.config.ts
index 49202691a5..04b44b8bc6 100644
--- a/apps/desktop/electron.vite.config.ts
+++ b/apps/desktop/electron.vite.config.ts
@@ -8,7 +8,13 @@ export default defineConfig({
plugins: [externalizeDepsPlugin()],
},
preload: {
- plugins: [externalizeDepsPlugin()],
+ // `@electron-toolkit/preload` must be bundled INTO the preload script:
+ // the renderer windows run with `sandbox: true`, and a sandboxed preload's
+ // `require` can only load `electron` plus a couple of node builtins — an
+ // externalized `require("@electron-toolkit/preload")` would throw and
+ // every contextBridge API would vanish. electron-vite emits preload as a
+ // single CJS bundle, which is exactly what the sandbox requires.
+ plugins: [externalizeDepsPlugin({ exclude: ["@electron-toolkit/preload"] })],
},
renderer: {
server: {
diff --git a/apps/desktop/src/main/index.ts b/apps/desktop/src/main/index.ts
index 274e6bb3aa..7a7fb3c8c4 100644
--- a/apps/desktop/src/main/index.ts
+++ b/apps/desktop/src/main/index.ts
@@ -12,6 +12,7 @@ import { installContextMenu } from "./context-menu";
import { handleAppShortcut } from "./keyboard-shortcuts";
import { installNavigationGestures } from "./navigation-gestures";
import { installNavigationGuard } from "./navigation-guard";
+import { createRendererWebPreferences } from "./renderer-web-preferences";
import { getAppVersion } from "./app-version";
import { loadRuntimeConfig } from "./runtime-config-loader";
import type { RuntimeConfigResult } from "../shared/runtime-config";
@@ -219,41 +220,6 @@ function getSystemLocale(): string {
return app.getPreferredSystemLanguages()[0] ?? "en";
}
-function createRendererWebPreferences(
- systemLocale: string,
- additionalArguments: string[] = [],
-): Electron.WebPreferences {
- return {
- preload: join(__dirname, "../preload/index.js"),
- sandbox: false,
- webSecurity: false,
- // Required for the Chromium PDF viewer (PDFium) to activate inside
- // iframes — used by the attachment preview modal for application/pdf
- // files. Default is false in Electron; without it <iframe src=*.pdf>
- // renders blank.
- //
- // Security trade-off, accepted intentionally:
- // 1. These windows already run with `webSecurity: false` +
- // `sandbox: false`, so `plugins: true` does not meaningfully widen
- // the renderer's attack surface beyond what is already accepted.
- // 2. The only PDFs that reach an iframe here are signed CloudFront URLs
- // we ourselves issued (see useDownloadAttachment); user-supplied URLs
- // are routed through `setWindowOpenHandler` → `openExternalSafely` and
- // cannot land in this renderer.
- // 3. Chromium's PDFium plugin is itself sandboxed inside its own process
- // and only handles the `application/pdf` MIME.
- //
- // If we ever tighten `webSecurity` / `sandbox`, revisit this by hosting
- // the PDF viewer in a dedicated BrowserView with `plugins: true` scoped
- // to that view, keeping the main renderer plugin-free.
- plugins: true,
- additionalArguments: [
- `--multica-locale=${systemLocale}`,
- ...additionalArguments,
- ],
- };
-}
-
function loadRenderer(window: BrowserWindow): void {
const rendererEntry = join(__dirname, "../renderer/index.html");
const rendererURL =
@@ -348,7 +314,10 @@ function createWindow(): BrowserWindow {
...(is.dev || process.platform === "linux"
? { icon: BUNDLED_ICON_PATH }
: {}),
- webPreferences: createRendererWebPreferences(systemLocale),
+ webPreferences: createRendererWebPreferences(
+ join(__dirname, "../preload/index.js"),
+ systemLocale,
+ ),
});
const window = mainWindow;
@@ -501,9 +470,11 @@ function createIssueWindow(context: IssueWindowContext): void {
...(is.dev || process.platform === "linux"
? { icon: BUNDLED_ICON_PATH }
: {}),
- webPreferences: createRendererWebPreferences(systemLocale, [
- encodeIssueWindowArgument(context),
- ]),
+ webPreferences: createRendererWebPreferences(
+ join(__dirname, "../preload/index.js"),
+ systemLocale,
+ [encodeIssueWindowArgument(context)],
+ ),
});
issueWindows.add(window);
@@ -675,8 +646,8 @@ if (!gotTheLock) {
// IPC: open URL in default browser (used by renderer for Google login).
// All scheme-allowlist enforcement lives in openExternalSafely — this
// is the single audit point for renderer-controlled URLs reaching the
- // OS shell under the app's intentional webSecurity: false + sandbox:
- // false configuration.
+ // OS shell under the app's intentional webSecurity: false configuration
+ // (the renderer itself runs sandboxed).
ipcMain.handle("shell:openExternal", (_event, url: string) => {
return openExternalSafely(url);
});
diff --git a/apps/desktop/src/main/navigation-guard.ts b/apps/desktop/src/main/navigation-guard.ts
index 5258ca26fe..59651b2fdb 100644
--- a/apps/desktop/src/main/navigation-guard.ts
+++ b/apps/desktop/src/main/navigation-guard.ts
@@ -15,10 +15,10 @@
* What it does catch is a real document-level navigation away from the trusted
* renderer: `window.location = 'https://evil.example'`, an anchor Chromium
* resolves to a remote URL in the same window, a dropped file URL. These windows
- * run with `webSecurity: false` and `sandbox: false`, so a foreign document
- * landing in one would inherit privileged context. New windows and external
- * links already route through `setWindowOpenHandler` → `openExternalSafely`;
- * this closes the same-window path.
+ * run with `webSecurity: false` (the process sandbox is enabled), so a
+ * foreign document landing in one would inherit privileged context. New
+ * windows and external links already route through `setWindowOpenHandler` →
+ * `openExternalSafely`; this closes the same-window path.
*/
/** The subset of BrowserWindow this module needs — keeps it unit-testable. */
diff --git a/apps/desktop/src/main/renderer-web-preferences.test.ts b/apps/desktop/src/main/renderer-web-preferences.test.ts
new file mode 100644
index 0000000000..008f87349e
--- /dev/null
+++ b/apps/desktop/src/main/renderer-web-preferences.test.ts
@@ -0,0 +1,53 @@
+// @vitest-environment node
+import { describe, expect, it } from "vitest";
+import { createRendererWebPreferences } from "./renderer-web-preferences";
+
+const PRELOAD = "/app/out/preload/index.js";
+
+describe("createRendererWebPreferences", () => {
+ it("enables the Chromium process sandbox", () => {
+ expect(createRendererWebPreferences(PRELOAD, "en").sandbox).toBe(true);
+ });
+
+ it("keeps webSecurity off only until the privileged-protocol migration lands", () => {
+ // Pinned deliberately: webSecurity must not come back on by accident.
+ // Restoring it requires moving the renderer off the opaque file:// origin
+ // onto a custom privileged protocol (so CORS preflights carry a real
+ // Origin), plus server-side CORS coordination. Any change to this
+ // expectation should be that migration, in its own PR.
+ expect(createRendererWebPreferences(PRELOAD, "en").webSecurity).toBe(false);
+ });
+
+ it("leaves contextIsolation and nodeIntegration at Electron's secure defaults", () => {
+ // Not setting them means contextIsolation: true and nodeIntegration:
+ // false (Electron defaults). The guard is that nothing re-enables the
+ // insecure side of either default.
+ const prefs = createRendererWebPreferences(PRELOAD, "en");
+ expect(prefs.contextIsolation).not.toBe(false);
+ expect(prefs.nodeIntegration).not.toBe(true);
+ });
+
+ it("keeps plugins enabled for the PDF preview iframe", () => {
+ expect(createRendererWebPreferences(PRELOAD, "en").plugins).toBe(true);
+ });
+
+ it("loads the given preload script", () => {
+ expect(createRendererWebPreferences(PRELOAD, "en").preload).toBe(PRELOAD);
+ });
+
+ it("passes the system locale and extra arguments to the renderer", () => {
+ const prefs = createRendererWebPreferences(PRELOAD, "de", [
+ "--issue-window=<ctx>",
+ ]);
+ expect(prefs.additionalArguments).toEqual([
+ "--multica-locale=de",
+ "--issue-window=<ctx>",
+ ]);
+ });
+
+ it("defaults to the locale flag alone", () => {
+ expect(
+ createRendererWebPreferences(PRELOAD, "fr").additionalArguments,
+ ).toEqual(["--multica-locale=fr"]);
+ });
+});
diff --git a/apps/desktop/src/main/renderer-web-preferences.ts b/apps/desktop/src/main/renderer-web-preferences.ts
new file mode 100644
index 0000000000..09b7c6b77e
--- /dev/null
+++ b/apps/desktop/src/main/renderer-web-preferences.ts
@@ -0,0 +1,64 @@
+import type { WebPreferences } from "electron";
+
+/**
+ * WebPreferences shared by every renderer window — the tabbed main window and
+ * the dedicated issue windows.
+ *
+ * Extracted from index.ts so the security-relevant flags are pinned by a unit
+ * test beside this file: a silent regression here re-opens the renderer attack
+ * surface, and the entry module cannot be imported in tests (it registers app
+ * lifecycle handlers on import).
+ *
+ * `preloadPath` is injected rather than derived from `__dirname` here to keep
+ * this a pure function — the bundled main process resolves it relative to its
+ * own output directory at the call sites.
+ */
+export function createRendererWebPreferences(
+ preloadPath: string,
+ systemLocale: string,
+ additionalArguments: string[] = [],
+): WebPreferences {
+ return {
+ preload: preloadPath,
+ // Sandboxed preload. The preload script only uses sandbox-safe APIs: the
+ // `electron` module (contextBridge, ipcRenderer — including sendSync) and
+ // the polyfilled `process` (platform, argv). It therefore must remain a
+ // single CJS bundle, because the sandboxed preload `require` can only load
+ // `electron` plus a couple of node builtins — see electron.vite.config.ts,
+ // which bundles @electron-toolkit/preload into the output instead of
+ // leaving it external.
+ sandbox: true,
+ // Still intentionally off. Restoring webSecurity requires migrating the
+ // renderer off the opaque file:// origin onto a privileged custom protocol
+ // (so CORS preflight requests carry a real Origin the server can allow),
+ // which needs server-side coordination first. The rest of the secure
+ // baseline (contextIsolation on, nodeIntegration off, sandbox on) is
+ // enforced regardless of this flag.
+ webSecurity: false,
+ // Required for the Chromium PDF viewer (PDFium) to activate inside
+ // iframes — used by the attachment preview modal for application/pdf
+ // files. Default is false in Electron; without it <iframe src=*.pdf>
+ // renders blank.
+ //
+ // Security trade-off, accepted intentionally:
+ // 1. These windows still run with `webSecurity: false` (see above), so
+ // `plugins: true` does not meaningfully widen the renderer's attack
+ // surface beyond what is already accepted. The process sandbox is
+ // now on, which is the containment boundary that matters here.
+ // 2. The only PDFs that reach an iframe here are signed CloudFront URLs
+ // we ourselves issued (see useDownloadAttachment); user-supplied URLs
+ // are routed through `setWindowOpenHandler` → `openExternalSafely` and
+ // cannot land in this renderer.
+ // 3. Chromium's PDFium plugin is itself sandboxed inside its own process
+ // and only handles the `application/pdf` MIME.
+ //
+ // When webSecurity comes back on, revisit this by hosting the PDF viewer
+ // in a dedicated WebContentsView with `plugins: true` scoped to that view,
+ // keeping the main renderer plugin-free.
+ plugins: true,
+ additionalArguments: [
+ `--multica-locale=${systemLocale}`,
+ ...additionalArguments,
+ ],
+ };
+}