Skip to content

Commit 24c49c3

Browse files
committed
fix(desktop): deny about:blank popups without navigating preview
1 parent 6b286ae commit 24c49c3

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,27 @@ describe("previewWindowOpenAction", () => {
161161
expect(PreviewManager.previewWindowOpenAction(details({ disposition: "background-tab" }))).toBe(
162162
"navigate",
163163
);
164+
expect(
165+
PreviewManager.previewWindowOpenAction(
166+
details({ url: "about:blank", disposition: "foreground-tab" }),
167+
),
168+
).toBe("navigate");
164169
});
165170

166-
it("does not hand a window to schemes that cannot be hardened", () => {
171+
it("denies scripted popups that cannot be hardened without navigating", () => {
167172
// A popup skips the `will-attach-webview` hardening, so it only gets a window
168173
// when its preferences can be overridden. Chromium copies the guest's
169-
// preferences for `about:blank` and forbids overriding them.
174+
// preferences for `about:blank` and forbids overriding them. Deny without
175+
// navigating so the opener survives and SDKs like MSAL can fall back.
170176
for (const url of [
171177
"about:blank",
178+
"",
172179
"javascript:alert(1)",
173180
"file:///etc/passwd",
174181
"vscode://vscode-remote/ssh-remote+box/tmp",
175182
"not a url",
176183
]) {
177-
expect(PreviewManager.previewWindowOpenAction(details({ url }))).toBe("navigate");
184+
expect(PreviewManager.previewWindowOpenAction(details({ url }))).toBe("deny");
178185
}
179186
});
180187
});

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ interface ExpectedAgentInput {
512512
*
513513
* `about:blank` stays out: Chromium skips browser-side navigation for it, so the
514514
* child copies the guest's `contextIsolation: false` preferences and Electron
515-
* gives no way to override them. Those popups keep loading in the preview tab.
515+
* gives no way to override them. Those popups are denied without navigating,
516+
* so the opener survives for the SDK fallback.
516517
*
517518
* Deliberately not `ElectronShell.parseSafeExternalUrl`: that also admits
518519
* `vscode://vscode-remote/...` deep links, which belong in `shell.openExternal`
@@ -553,14 +554,21 @@ const POPUP_WINDOW_OPTIONS = {
553554
* navigating the preview tab instead destroys the opener the popup has to
554555
* `postMessage` its result back to.
555556
*
557+
* `"deny"` blocks a scripted `new-window` without touching the opener. This is
558+
* for URLs that cannot be hardened in a popup, like `about:blank`: loading them
559+
* in the preview tab would replace the opener, while a plain deny lets SDKs
560+
* like MSAL fall back to redirect.
561+
*
556562
* `target="_blank"` links arrive as a tab disposition and keep loading in the
557563
* preview tab, which is what people expect from a link inside a preview.
558564
*/
559565
export const previewWindowOpenAction = (details: {
560566
readonly url: string;
561567
readonly disposition: Electron.HandlerDetails["disposition"];
562-
}): "popup" | "navigate" =>
563-
details.disposition === "new-window" && isPopupUrl(details.url) ? "popup" : "navigate";
568+
}): "popup" | "navigate" | "deny" => {
569+
if (details.disposition !== "new-window") return "navigate";
570+
return isPopupUrl(details.url) ? "popup" : "deny";
571+
};
564572

565573
export const isPreviewRefreshShortcut = (input: Electron.Input): boolean =>
566574
input.type === "keyDown" &&
@@ -2048,14 +2056,17 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20482056
wc.ipc.on(RECORDING_INPUT_CHANNEL, recordingInput);
20492057
wc.ipc.on(MOUSE_NAVIGATE_CHANNEL, mouseNavigate);
20502058
wc.setWindowOpenHandler((details) => {
2051-
if (previewWindowOpenAction(details) === "popup") {
2059+
const openAction = previewWindowOpenAction(details);
2060+
if (openAction === "popup") {
20522061
return { action: "allow", overrideBrowserWindowOptions: POPUP_WINDOW_OPTIONS };
20532062
}
2054-
runFork(
2055-
attemptPromise({ operation: "openPreviewWindow", tabId, webContentsId: wc.id }, () =>
2056-
wc.loadURL(details.url),
2057-
).pipe(Effect.ignore),
2058-
);
2063+
if (openAction === "navigate") {
2064+
runFork(
2065+
attemptPromise({ operation: "openPreviewWindow", tabId, webContentsId: wc.id }, () =>
2066+
wc.loadURL(details.url),
2067+
).pipe(Effect.ignore),
2068+
);
2069+
}
20592070
return { action: "deny" };
20602071
});
20612072
wc.on("did-create-window", windowCreated);

0 commit comments

Comments
 (0)