Skip to content

Commit adf1132

Browse files
committed
fix(preview): validate popup URLs before loading
1 parent f5fce74 commit adf1132

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ interface TestCapturedPreviewImage {
160160
const makeTestPreviewWebContents = (
161161
capturePage: () => Promise<TestCapturedPreviewImage>,
162162
id = 42,
163+
loadURL = vi.fn(async () => undefined),
164+
setWindowOpenHandler = vi.fn(),
163165
) =>
164166
({
165167
id,
@@ -170,12 +172,13 @@ const makeTestPreviewWebContents = (
170172
isLoading: () => false,
171173
getZoomFactor: () => 1,
172174
setZoomFactor: vi.fn(),
175+
loadURL,
173176
on: vi.fn(),
174177
off: vi.fn(),
175178
ipc: { on: vi.fn(), off: vi.fn() },
176179
send: webviewSend,
177180
navigationHistory: { canGoBack: () => false, canGoForward: () => false },
178-
setWindowOpenHandler: vi.fn(),
181+
setWindowOpenHandler,
179182
debugger: {
180183
isAttached: () => false,
181184
attach: vi.fn(),
@@ -230,6 +233,53 @@ describe("PreviewManager", () => {
230233
webviewSend.mockClear();
231234
});
232235

236+
effectIt.effect("validates preview popup URLs before loading them", () =>
237+
withManager((manager) =>
238+
Effect.gen(function* () {
239+
const loadURL = vi.fn(async () => undefined);
240+
const setWindowOpenHandler = vi.fn();
241+
const webContents = makeTestPreviewWebContents(
242+
async () => ({
243+
toJPEG: () => Buffer.from("popup-url-validation"),
244+
getSize: () => ({ width: 1, height: 1 }),
245+
}),
246+
42,
247+
loadURL,
248+
setWindowOpenHandler,
249+
);
250+
fromId.mockReturnValue(webContents);
251+
252+
yield* manager.createTab("tab_popup_url_validation");
253+
yield* manager.registerWebview("tab_popup_url_validation", 42);
254+
255+
expect(setWindowOpenHandler).toHaveBeenCalledOnce();
256+
const handler = setWindowOpenHandler.mock.calls[0]?.[0] as
257+
| ((details: { readonly url: string }) => { readonly action: string })
258+
| undefined;
259+
expect(handler).toBeDefined();
260+
if (!handler) return;
261+
262+
for (const url of [
263+
"",
264+
"data:text/html,<script>document.body.innerText='blocked'</script>",
265+
"file:///tmp/preview.html",
266+
"javascript:document.body.innerText='blocked'",
267+
"http://[::1",
268+
]) {
269+
expect(handler({ url })).toEqual({ action: "deny" });
270+
}
271+
expect(loadURL).not.toHaveBeenCalled();
272+
273+
expect(handler({ url: "http://localhost:3200" })).toEqual({ action: "deny" });
274+
expect(handler({ url: "https://example.com/path" })).toEqual({ action: "deny" });
275+
yield* Effect.yieldNow;
276+
277+
expect(loadURL).toHaveBeenNthCalledWith(1, "http://localhost:3200/");
278+
expect(loadURL).toHaveBeenNthCalledWith(2, "https://example.com/path");
279+
}),
280+
),
281+
);
282+
233283
effectIt.effect("reports an unregistered webview as temporarily unavailable", () =>
234284
withManager((manager) =>
235285
Effect.gen(function* () {

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,9 +1407,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
14071407
wc.on("did-fail-load", failed as never);
14081408
wc.ipc.on(HUMAN_INPUT_CHANNEL, humanInput);
14091409
wc.setWindowOpenHandler(({ url }) => {
1410+
let normalizedUrl: string;
1411+
try {
1412+
normalizedUrl = normalizePreviewUrl(url);
1413+
} catch {
1414+
return { action: "deny" };
1415+
}
14101416
runFork(
14111417
attemptPromise({ operation: "openPreviewWindow", tabId, webContentsId: wc.id }, () =>
1412-
wc.loadURL(url),
1418+
wc.loadURL(normalizedUrl),
14131419
).pipe(Effect.ignore),
14141420
);
14151421
return { action: "deny" };

0 commit comments

Comments
 (0)