diff --git a/apps/desktop/electron/browser-annotation-preload.ts b/apps/desktop/electron/browser-annotation-preload.ts index a6741350..7ebc82e0 100644 --- a/apps/desktop/electron/browser-annotation-preload.ts +++ b/apps/desktop/electron/browser-annotation-preload.ts @@ -15,7 +15,8 @@ import { createAnnotationOverlay } from "./browser-annotation-overlay"; * because a hostile page with a handle on any Pace API is exactly what the * annotation layer must not create. Traffic goes one way, over a channel of * its own that main re-validates on arrival (PRD S2 constraint 2) — never over - * `pigui:invoke`, which has no sender check at all. + * `pigui:invoke`, which is scoped to the main window's own sender and speaks + * a different, command-shaped protocol. * * It also shares no module with `preload.ts`: electron-vite would hoist a * common import into a chunk, and a sandboxed preload cannot require one diff --git a/apps/desktop/electron/browser-annotation.ts b/apps/desktop/electron/browser-annotation.ts index 950a43fa..6c31cde9 100644 --- a/apps/desktop/electron/browser-annotation.ts +++ b/apps/desktop/electron/browser-annotation.ts @@ -306,9 +306,10 @@ function readAnnotationsPayload( /** * The gate on the annotation channel: the message has to come from the * embedded view's own webContents, and it has to be one of the shapes - * this protocol knows. `ipcMain.handle("pigui:invoke")` has no sender check — - * that is precisely why annotations get their own channel (PRD S2 - * implementation constraint 2). + * this protocol knows. `pigui:invoke` also validates its sender now (see + * `ipc-sender-guard.ts`), but annotations still get their own channel (PRD S2 + * implementation constraint 2) rather than sharing one built for a different + * message shape and a different set of commands. */ export function acceptBrowserAnnotationMessage(input: { sender: Sender; diff --git a/apps/desktop/electron/capture-downsample.test.ts b/apps/desktop/electron/capture-downsample.test.ts new file mode 100644 index 00000000..c9de6e78 --- /dev/null +++ b/apps/desktop/electron/capture-downsample.test.ts @@ -0,0 +1,46 @@ +import { describe, expect, it, vi } from "vitest"; +import { downsampleToCssWidth } from "./capture-downsample"; + +/** A fake `nativeImage` result, just large enough to exercise the seam. */ +function fakeImage(width: number, height: number) { + const image = { + getSize: () => ({ width, height }), + resize: vi.fn((options: { width: number }) => + fakeImage(options.width, Math.round((height * options.width) / width)), + ), + }; + + return image; +} + +describe("downsampleToCssWidth", () => { + it("resizes a HiDPI (2x) capture down to the CSS width", () => { + // capturePage on a 2x display answers in physical pixels: double the CSS size. + const cssWidth = 400; + const image = fakeImage(cssWidth * 2, 300 * 2); + + const result = downsampleToCssWidth(image, cssWidth); + + expect(image.resize).toHaveBeenCalledWith({ width: cssWidth }); + expect(result.getSize().width).toBe(cssWidth); + }); + + it("leaves a 1x capture untouched, since it is already at the CSS width", () => { + const cssWidth = 400; + const image = fakeImage(cssWidth, 300); + + const result = downsampleToCssWidth(image, cssWidth); + + expect(image.resize).not.toHaveBeenCalled(); + expect(result).toBe(image); + }); + + it("leaves the image untouched when no cap is given", () => { + const image = fakeImage(800, 600); + + const result = downsampleToCssWidth(image, undefined); + + expect(image.resize).not.toHaveBeenCalled(); + expect(result).toBe(image); + }); +}); diff --git a/apps/desktop/electron/capture-downsample.ts b/apps/desktop/electron/capture-downsample.ts new file mode 100644 index 00000000..149beba5 --- /dev/null +++ b/apps/desktop/electron/capture-downsample.ts @@ -0,0 +1,27 @@ +/** The slice of `Electron.NativeImage` this seam actually needs. */ +export interface DownsamplableImage { + getSize(): { width: number; height: number }; + resize(options: { width: number }): Self; +} + +/** + * Shrinks a captured image back down to its CSS width. + * + * `capturePage` answers in physical pixels, which only exceed the CSS width + * on a HiDPI (>1x) display — so this branch was previously reachable only on + * a real HiDPI machine and never on CI's 1x runners. Taking the image as a + * parameter (rather than reading `webContents.capturePage()` itself) makes it + * exercisable with a fake image at both scale factors. + */ +export function downsampleToCssWidth>( + image: Image, + maxWidth: number | undefined, +): Image { + if (!maxWidth || maxWidth <= 0) { + return image; + } + + const { width } = image.getSize(); + + return width > maxWidth ? image.resize({ width: maxWidth }) : image; +} diff --git a/apps/desktop/electron/ipc-sender-guard.test.ts b/apps/desktop/electron/ipc-sender-guard.test.ts new file mode 100644 index 00000000..805d7497 --- /dev/null +++ b/apps/desktop/electron/ipc-sender-guard.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest"; +import { isTrustedInvokeSender } from "./ipc-sender-guard"; + +describe("isTrustedInvokeSender", () => { + it("accepts the main window's own webContents", () => { + const mainWebContents = { id: "main" }; + + expect( + isTrustedInvokeSender({ sender: mainWebContents, mainWebContents }), + ).toBe(true); + }); + + it("rejects a different sender, such as an embedded WebContentsView", () => { + const mainWebContents = { id: "main" }; + const embeddedViewSender = { id: "embedded-browser-view" }; + + expect( + isTrustedInvokeSender({ sender: embeddedViewSender, mainWebContents }), + ).toBe(false); + }); + + it("rejects every sender when there is no main window yet", () => { + const someSender = { id: "anything" }; + + expect( + isTrustedInvokeSender({ sender: someSender, mainWebContents: null }), + ).toBe(false); + }); +}); diff --git a/apps/desktop/electron/ipc-sender-guard.ts b/apps/desktop/electron/ipc-sender-guard.ts new file mode 100644 index 00000000..53262699 --- /dev/null +++ b/apps/desktop/electron/ipc-sender-guard.ts @@ -0,0 +1,17 @@ +/** + * `pigui:invoke` is the embedded page's escape hatch into main-process + * commands (dialogs, backend RPC, browser control). It must answer only the + * app's own main window, never an embedded `WebContentsView` browser tab or + * any other sender — the annotation channel exists precisely because this + * handler used to check nothing (see `browser-annotation.ts`). + * + * Generic over the sender type, mirroring `acceptBrowserAnnotationMessage`, + * so the check is unit-testable with a fake object instead of a real + * `Electron.WebContents`. + */ +export function isTrustedInvokeSender(input: { + sender: Sender; + mainWebContents: Sender | null; +}): boolean { + return input.mainWebContents !== null && input.sender === input.mainWebContents; +} diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.ts index f4d24c61..fe4322bb 100644 --- a/apps/desktop/electron/main.ts +++ b/apps/desktop/electron/main.ts @@ -38,6 +38,8 @@ import { isBrowserCommand, type BrowserHost, } from "./browser-host"; +import { downsampleToCssWidth } from "./capture-downsample"; +import { isTrustedInvokeSender } from "./ipc-sender-guard"; import { installAppMenu } from "./app-menu"; import { navigateAppWindow } from "./app-navigation"; import { createAppUpdater, type AppUpdater, type AutoUpdaterLike } from "./updater"; @@ -583,14 +585,10 @@ function createBrowserView(target: BrowserTabTarget) { } // Resizing by width alone keeps the aspect ratio; who asks for a cap, - // and why, is `browser_capture_annotation` in browser-host.ts. - const captured = image.getSize(); - const resized = - maxWidth && maxWidth > 0 && captured.width > maxWidth - ? image.resize({ width: maxWidth }) - : image; - - return resized.toDataURL(); + // and why, is `browser_capture_annotation` in browser-host.ts. The + // downsample itself lives in capture-downsample.ts so it can be unit + // tested at both 1x and 2x scale without a real HiDPI display. + return downsampleToCssWidth(image, maxWidth).toDataURL(); }, }; } @@ -606,9 +604,9 @@ function sendAnnotationCommand( /** * The embedded page's one way in. Everything it says is checked twice: the - * sender must be this window's own view (`pigui:invoke` checks nothing, which - * is why annotations never travel on it), and the message must be one of the - * three shapes the protocol knows. + * sender must be this window's own view (`pigui:invoke` now also checks its + * sender, but annotations still get their own channel rather than sharing + * one), and the message must be one of the three shapes the protocol knows. */ ipcMain.on(browserAnnotationChannel, (event, payload: unknown) => { const target = browserAnnotationSenders.get(event.sender); @@ -688,7 +686,19 @@ function killBackendForEndToEndTest() { ipcMain.handle( "pigui:invoke", - (_event, input: { command: string; args?: Record }) => { + (event, input: { command: string; args?: Record }) => { + // Only the app's own main window may drive commands (dialogs, backend + // RPC, browser control) through this channel — an embedded + // `WebContentsView` browser tab, or anything else, is rejected outright. + if ( + !isTrustedInvokeSender({ + sender: event.sender, + mainWebContents: mainWindow?.webContents ?? null, + }) + ) { + throw new Error("pigui:invoke rejected: sender is not the main window."); + } + if (input.command === e2eKillBackendCommand) { return killBackendForEndToEndTest(); }