hermes-agent/apps/desktop/electron/preview-guest-preload.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

66 lines
2.3 KiB
TypeScript

// Logic half of the preview pane's guest preload. The wiring half lives in
// `preview-guest-preload-entry.ts` (the bundled preload itself); this split
// keeps the handoff rules unit-testable in a plain Node environment.
//
// Mirrored by `PREVIEW_EXTERNAL_CHANNEL` in src/lib/preview-external.ts — the
// renderer tsconfig cannot import from electron/.
export const GUEST_EXTERNAL_CHANNEL = 'preview-open-external'
interface GuestEventTarget {
closest(selector: string): { href: string } | null
}
export interface GuestClickEvent {
button?: number
isTrusted?: boolean
target?: unknown
}
export interface GuestHandoffHost {
addEventListener(type: 'click', listener: (event: GuestClickEvent) => void, capture?: boolean): void
sendToHost(channel: string, ...args: unknown[]): void
}
/**
* Wire the DOM-capture listener that forwards a clicked `_blank` anchor's
* absolute URL to the host. `host` is injected so the entry can pass the
* guest's `document` and `ipcRenderer`, and tests can drive the same rules
* without Electron or a DOM.
*
* Only a trusted primary-button click qualifies. `isTrusted` is what keeps
* this bridge out of the gesture-less forced-navigation class
* (GHSA-9f4c-93c8-jc8g): page script can `dispatchEvent` a synthetic click
* on an anchor, but it cannot forge `isTrusted`.
*/
export function installGuestExternalHandoff(host: GuestHandoffHost): void {
host.addEventListener(
'click',
event => {
if (event.isTrusted !== true || (event.button ?? 0) !== 0) {
return
}
const target = event.target as GuestEventTarget | null
if (!target || typeof target.closest !== 'function') {
return
}
// `closest` climbs through the click's own DOM, which this isolated
// preload world shares with the page: an inner `<span>` inside the
// anchor resolves to the enclosing `<a target="_blank">` the same way
// it would for page script.
const anchor = target.closest('a[target="_blank"]')
if (!anchor || typeof anchor.href !== 'string' || anchor.href === '') {
return
}
// `anchor.href` is the browser-resolved absolute URL, not the raw
// attribute, so relative and protocol-relative hrefs arrive fully
// qualified for the host's scheme admission check.
host.sendToHost(GUEST_EXTERNAL_CHANNEL, anchor.href)
},
true
)
}