Skip to content
Merged
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
3 changes: 2 additions & 1 deletion apps/desktop/electron/browser-annotation-preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions apps/desktop/electron/browser-annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sender>(input: {
sender: Sender;
Expand Down
46 changes: 46 additions & 0 deletions apps/desktop/electron/capture-downsample.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
27 changes: 27 additions & 0 deletions apps/desktop/electron/capture-downsample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/** The slice of `Electron.NativeImage` this seam actually needs. */
export interface DownsamplableImage<Self> {
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 extends DownsamplableImage<Image>>(
image: Image,
maxWidth: number | undefined,
): Image {
if (!maxWidth || maxWidth <= 0) {
return image;
}

const { width } = image.getSize();

return width > maxWidth ? image.resize({ width: maxWidth }) : image;
}
29 changes: 29 additions & 0 deletions apps/desktop/electron/ipc-sender-guard.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
17 changes: 17 additions & 0 deletions apps/desktop/electron/ipc-sender-guard.ts
Original file line number Diff line number Diff line change
@@ -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<Sender>(input: {
sender: Sender;
mainWebContents: Sender | null;
}): boolean {
return input.mainWebContents !== null && input.sender === input.mainWebContents;
}
34 changes: 22 additions & 12 deletions apps/desktop/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
},
};
}
Expand All @@ -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);
Expand Down Expand Up @@ -688,7 +686,19 @@ function killBackendForEndToEndTest() {

ipcMain.handle(
"pigui:invoke",
(_event, input: { command: string; args?: Record<string, unknown> }) => {
(event, input: { command: string; args?: Record<string, unknown> }) => {
// 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();
}
Expand Down