Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/desktop/src/main/external-link.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, test } from "bun:test"
import { isSafeExternalLink } from "./external-link"

describe("external link", () => {
test("allows http and https links", () => {
expect(isSafeExternalLink("https://opencode.ai")).toBe(true)
expect(isSafeExternalLink("http://localhost:3000")).toBe(true)
})

test("blocks non-http protocols", () => {
expect(isSafeExternalLink("file:///tmp/demo.txt")).toBe(false)
expect(isSafeExternalLink("javascript:alert(1)")).toBe(false)
expect(isSafeExternalLink("smb://attacker/share")).toBe(false)
expect(isSafeExternalLink("ms-msdt:/id PCWDiagnostic")).toBe(false)
})

test("blocks malformed links", () => {
expect(isSafeExternalLink("not a url")).toBe(false)
expect(isSafeExternalLink("/relative/path")).toBe(false)
expect(isSafeExternalLink("")).toBe(false)
})
})
10 changes: 10 additions & 0 deletions packages/desktop/src/main/external-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const ALLOWED_PROTOCOLS = new Set(["http:", "https:"])

export function isSafeExternalLink(url: string) {
try {
const parsed = new URL(url)
return ALLOWED_PROTOCOLS.has(parsed.protocol)
} catch {
return false
}
}
2 changes: 2 additions & 0 deletions packages/desktop/src/main/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 { assertAttachmentBudget, createPickedFileAuthorizations } from "./attachment-picker"
import { isSafeExternalLink } from "./external-link"
import { getStore, removeStoreFileIfEmpty } from "./store"
import { getPinchZoomEnabled, getWindowID, setPinchZoomEnabled, setTitlebar, updateTitlebar } from "./windows"
import type { UpdaterController } from "./updater-controller"
Expand Down Expand Up @@ -172,6 +173,7 @@ export function registerIpcHandlers(deps: Deps) {
)

ipcMain.on("open-link", (_event: IpcMainEvent, url: string) => {
if (!isSafeExternalLink(url)) return
void shell.openExternal(url)
})

Expand Down
Loading