Compare commits

..

1 Commits

Author SHA1 Message Date
Luke Parker a81ab9385b fix(desktop): constrain renderer navigation 2026-08-07 03:10:29 +00:00
5 changed files with 32 additions and 10 deletions
@@ -0,0 +1,18 @@
import { describe, expect, test } from "bun:test"
import { isSafeExternalUrl } from "./external-url"
describe("external URL policy", () => {
test.each(["https://opencode.ai", "http://127.0.0.1:4096", "mailto:hello@opencode.ai"])("allows %s", (url) =>
expect(isSafeExternalUrl(url)).toBe(true),
)
test.each(["file:///tmp/test", "javascript:alert(1)", "data:text/html,test", "vscode://file/tmp/test", "nope"])(
"rejects %s",
(url) => expect(isSafeExternalUrl(url)).toBe(false),
)
test("rejects non-string values", () => {
expect(isSafeExternalUrl(null)).toBe(false)
expect(isSafeExternalUrl({ toString: () => "https://opencode.ai" })).toBe(false)
})
})
@@ -0,0 +1,6 @@
const protocols = new Set(["http:", "https:", "mailto:"])
export function isSafeExternalUrl(value: unknown) {
if (typeof value !== "string" || !URL.canParse(value)) return false
return protocols.has(new URL(value).protocol)
}
+1 -10
View File
@@ -5,7 +5,7 @@ import { homedir, tmpdir } from "node:os"
import { join } from "node:path"
import { getCACertificates, setDefaultCACertificates } from "node:tls"
import type { Event } from "electron"
import { app, BrowserWindow } from "electron"
import { app } from "electron"
import { Deferred, Effect, Fiber } from "effect"
import contextMenu from "electron-context-menu"
@@ -316,15 +316,6 @@ const main = Effect.gen(function* () {
yield* Fiber.await(loadingTask)
app.on("window-all-closed", () => {
if (process.platform === "darwin") return
app.quit()
})
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length > 0) return
restoreMainWindows()
})
const windows = restoreMainWindows()
if (windows.length) {
createMenu({
+2
View File
@@ -8,6 +8,7 @@ import type { DesktopMenuAction } from "@opencode-ai/app/desktop-menu"
import type { FatalRendererError, ServerReadyData, TitlebarTheme } from "../preload/types"
import { runDesktopMenuAction } from "./desktop-menu-actions"
import { setForceFocus } from "./debug"
import { isSafeExternalUrl } from "./external-url"
import { assertAttachmentBudget, createPickedFileAuthorizations } from "./attachment-picker"
import { getStore, removeStoreFileIfEmpty } from "./store"
import { getPinchZoomEnabled, getWindowID, setPinchZoomEnabled, setTitlebar, updateTitlebar } from "./windows"
@@ -178,6 +179,7 @@ export function registerIpcHandlers(deps: Deps) {
)
ipcMain.on("open-link", (_event: IpcMainEvent, url: string) => {
if (!isSafeExternalUrl(url)) return
void shell.openExternal(url)
})
+5
View File
@@ -220,6 +220,11 @@ export function createMainWindow(id: string = randomUUID()) {
state.manage(win)
registerWindow(win, id)
wireFullscreen(win)
win.webContents.on("will-frame-navigate", (event) => {
if (isRendererUrl(event.url)) return
event.preventDefault()
})
win.webContents.setWindowOpenHandler(() => ({ action: "deny" }))
loadWindow(win, "index.html")
wireZoom(win)