hermes-agent/apps/desktop/electron/preview-guest-preload-entry.ts
teknium1 408f5b7802 fix(desktop): gate the preview guest bridge on a trusted click; trim salvage
The salvaged bridge forwarded any click event, including one page script
dispatches itself, which would let a guest page open the OS browser without
a user gesture — the same class GHSA-9f4c-93c8-jc8g closed. The preload now
forwards only `event.isTrusted` primary-button clicks. The renderer admits
http/https only (mailto dropped: the guest chose the URL, not the user).

Trim to the salvage bar: drop the unrelated window-open-policy tests and the
admission unit file; keep two invariants per side (preload forwards a trusted
_blank click / drops synthetic ones; pane opens admitted URLs via
hermes:openExternal / never file:, javascript:, mailto: or foreign channels).
Document the new preload trust surface in apps/desktop/AGENTS.md.
2026-09-17 09:05:24 -07:00

27 lines
1.4 KiB
TypeScript

// Preload for the preview pane's `<webview>` guests. main.ts installs this
// file via `will-attach-webview` on the `persist:hermes-preview` partition
// only (see `installPreviewGuestPreload`), so no other webview inherits it.
//
// The guest runs with contextIsolation, so this preload shares the guest's
// DOM but never its JavaScript world. A preview page's `target="_blank"`
// anchors (Streamlit traceback's "Ask Google" / "Ask …" buttons —
// #112941) are intercepted here in the DOM's capture phase and handed to the
// host renderer via `sendToHost`; the host admits the scheme and routes the
// URL through the audited `hermes:openExternal` channel. A guest URL never
// becomes an Electron popup and this side never opens anything by itself.
//
// Deliberate scope: only trusted anchor clicks are forwarded. A page's direct
// `window.open` calls stay blocked (the webview has no `allowpopups`): hooking
// them would mean reaching into the guest's JS world, and this preload exposes
// nothing there.
import { installGuestExternalHandoff } from './preview-guest-preload'
const electron = require('electron') as {
ipcRenderer: { sendToHost(channel: string, ...args: unknown[]): void }
}
installGuestExternalHandoff({
addEventListener: (type, listener, capture) => document.addEventListener(type, listener, capture),
sendToHost: (channel, ...args) => electron.ipcRenderer.sendToHost(channel, ...args)
})