summaryrefslogtreecommitdiffhomepage
path: root/packages/desktop-electron/src/main
diff options
context:
space:
mode:
authorBrendan Allan <[email protected]>2026-04-21 12:38:59 +0800
committerGitHub <[email protected]>2026-04-21 12:38:59 +0800
commiteb9906420fa8def2520b1b4950a9175af9116ea2 (patch)
treea4415265e2764c3b78cbefb0b4b0cddc1f2e42af /packages/desktop-electron/src/main
parent4964ce480c566a98b2b4ead4a6e163eb773c2b80 (diff)
downloadopencode-eb9906420fa8def2520b1b4950a9175af9116ea2.tar.gz
opencode-eb9906420fa8def2520b1b4950a9175af9116ea2.zip
refactor(desktop-electron): enable contextIsolation and sandbox (#23523)
Diffstat (limited to 'packages/desktop-electron/src/main')
-rw-r--r--packages/desktop-electron/src/main/index.ts11
-rw-r--r--packages/desktop-electron/src/main/ipc.ts13
-rw-r--r--packages/desktop-electron/src/main/menu.ts2
-rw-r--r--packages/desktop-electron/src/main/windows.ts37
4 files changed, 27 insertions, 36 deletions
diff --git a/packages/desktop-electron/src/main/index.ts b/packages/desktop-electron/src/main/index.ts
index 8a826bd27..ae9f58118 100644
--- a/packages/desktop-electron/src/main/index.ts
+++ b/packages/desktop-electron/src/main/index.ts
@@ -195,15 +195,10 @@ async function initialize() {
logger.log("loading task finished")
})()
- const globals = {
- updaterEnabled: UPDATER_ENABLED,
- deepLinks: pendingDeepLinks,
- }
-
if (needsMigration) {
const show = await Promise.race([loadingTask.then(() => false), delay(1_000).then(() => true)])
if (show) {
- overlay = createLoadingWindow(globals)
+ overlay = createLoadingWindow()
await delay(1_000)
}
}
@@ -215,7 +210,7 @@ async function initialize() {
await loadingComplete.promise
}
- mainWindow = createMainWindow(globals)
+ mainWindow = createMainWindow()
wireMenu()
overlay?.close()
@@ -252,6 +247,8 @@ registerIpcHandlers({
initEmitter.off("step", listener)
}
},
+ getWindowConfig: () => ({ updaterEnabled: UPDATER_ENABLED }),
+ consumeInitialDeepLinks: () => pendingDeepLinks.splice(0),
getDefaultServerUrl: () => getDefaultServerUrl(),
setDefaultServerUrl: (url) => setDefaultServerUrl(url),
getWslConfig: () => Promise.resolve(getWslConfig()),
diff --git a/packages/desktop-electron/src/main/ipc.ts b/packages/desktop-electron/src/main/ipc.ts
index 52d87ed7e..8dbca8eea 100644
--- a/packages/desktop-electron/src/main/ipc.ts
+++ b/packages/desktop-electron/src/main/ipc.ts
@@ -2,7 +2,14 @@ import { execFile } from "node:child_process"
import { BrowserWindow, Notification, app, clipboard, dialog, ipcMain, shell } from "electron"
import type { IpcMainEvent, IpcMainInvokeEvent } from "electron"
-import type { InitStep, ServerReadyData, SqliteMigrationProgress, TitlebarTheme, WslConfig } from "../preload/types"
+import type {
+ InitStep,
+ ServerReadyData,
+ SqliteMigrationProgress,
+ TitlebarTheme,
+ WindowConfig,
+ WslConfig,
+} from "../preload/types"
import { getStore } from "./store"
import { setTitlebar } from "./windows"
@@ -14,6 +21,8 @@ const pickerFilters = (ext?: string[]) => {
type Deps = {
killSidecar: () => void
awaitInitialization: (sendStep: (step: InitStep) => void) => Promise<ServerReadyData>
+ getWindowConfig: () => Promise<WindowConfig> | WindowConfig
+ consumeInitialDeepLinks: () => Promise<string[]> | string[]
getDefaultServerUrl: () => Promise<string | null> | string | null
setDefaultServerUrl: (url: string | null) => Promise<void> | void
getWslConfig: () => Promise<WslConfig>
@@ -37,6 +46,8 @@ export function registerIpcHandlers(deps: Deps) {
const send = (step: InitStep) => event.sender.send("init-step", step)
return deps.awaitInitialization(send)
})
+ ipcMain.handle("get-window-config", () => deps.getWindowConfig())
+ ipcMain.handle("consume-initial-deep-links", () => deps.consumeInitialDeepLinks())
ipcMain.handle("get-default-server-url", () => deps.getDefaultServerUrl())
ipcMain.handle("set-default-server-url", (_event: IpcMainInvokeEvent, url: string | null) =>
deps.setDefaultServerUrl(url),
diff --git a/packages/desktop-electron/src/main/menu.ts b/packages/desktop-electron/src/main/menu.ts
index fcf209fb6..0d9a697fa 100644
--- a/packages/desktop-electron/src/main/menu.ts
+++ b/packages/desktop-electron/src/main/menu.ts
@@ -47,7 +47,7 @@ export function createMenu(deps: Deps) {
{
label: "New Window",
accelerator: "Cmd+Shift+N",
- click: () => createMainWindow({ updaterEnabled: UPDATER_ENABLED }),
+ click: () => createMainWindow(),
},
{ type: "separator" },
{ role: "close" },
diff --git a/packages/desktop-electron/src/main/windows.ts b/packages/desktop-electron/src/main/windows.ts
index 892e9d40d..df55e8da2 100644
--- a/packages/desktop-electron/src/main/windows.ts
+++ b/packages/desktop-electron/src/main/windows.ts
@@ -4,11 +4,6 @@ import { dirname, isAbsolute, join, relative, resolve } from "node:path"
import { fileURLToPath, pathToFileURL } from "node:url"
import type { TitlebarTheme } from "../preload/types"
-type Globals = {
- updaterEnabled: boolean
- deepLinks?: string[]
-}
-
const root = dirname(fileURLToPath(import.meta.url))
const rendererRoot = join(root, "../renderer")
const rendererProtocol = "oc"
@@ -68,7 +63,7 @@ export function setDockIcon() {
if (!icon.isEmpty()) app.dock?.setIcon(icon)
}
-export function createMainWindow(globals: Globals) {
+export function createMainWindow() {
const state = windowState({
defaultWidth: 1280,
defaultHeight: 800,
@@ -98,15 +93,16 @@ export function createMainWindow(globals: Globals) {
}
: {}),
webPreferences: {
- preload: join(root, "../preload/index.mjs"),
- sandbox: false,
+ preload: join(root, "../preload/index.js"),
+ contextIsolation: true,
+ nodeIntegration: false,
+ sandbox: true,
},
})
state.manage(win)
loadWindow(win, "index.html")
wireZoom(win)
- injectGlobals(win, globals)
win.once("ready-to-show", () => {
win.show()
@@ -115,7 +111,7 @@ export function createMainWindow(globals: Globals) {
return win
}
-export function createLoadingWindow(globals: Globals) {
+export function createLoadingWindow() {
const mode = tone()
const win = new BrowserWindow({
width: 640,
@@ -134,13 +130,14 @@ export function createLoadingWindow(globals: Globals) {
}
: {}),
webPreferences: {
- preload: join(root, "../preload/index.mjs"),
- sandbox: false,
+ preload: join(root, "../preload/index.js"),
+ contextIsolation: true,
+ nodeIntegration: false,
+ sandbox: true,
},
})
loadWindow(win, "loading.html")
- injectGlobals(win, globals)
return win
}
@@ -174,20 +171,6 @@ function loadWindow(win: BrowserWindow, html: string) {
void win.loadURL(`${rendererProtocol}://${rendererHost}/${html}`)
}
-
-function injectGlobals(win: BrowserWindow, globals: Globals) {
- win.webContents.on("dom-ready", () => {
- const deepLinks = globals.deepLinks ?? []
- const data = {
- updaterEnabled: globals.updaterEnabled,
- deepLinks: Array.isArray(deepLinks) ? deepLinks.splice(0) : deepLinks,
- }
- void win.webContents.executeJavaScript(
- `window.__OPENCODE__ = Object.assign(window.__OPENCODE__ ?? {}, ${JSON.stringify(data)})`,
- )
- })
-}
-
function wireZoom(win: BrowserWindow) {
win.webContents.setZoomFactor(1)
win.webContents.on("zoom-changed", () => {