A local-first multi-agent app development platform, built on PenguinHarness
fix(desktop,web): the shell opens no blank window, and Penguin Go authorization opens its page directly there (#788)
Yaowei Zhengbc71887ea850
diff --git a/changelog/unreleased/2026-09-18-desktop-auth-bridge.md b/changelog/unreleased/2026-09-18-desktop-auth-bridge.mdnew file mode 100644index 00000000..954fc3f4--- /dev/null+++ b/changelog/unreleased/2026-09-18-desktop-auth-bridge.md@@ -0,0 +1,40 @@+# The desktop app never opens a window the user cannot see, and Penguin Go authorization opens its page directly there++- **Date:** 2026-09-18+- **Type:** fix+- **Scope:** `desktop`, `web`+- **PR:** [#788](https://github.com/Prism-Shadow/penguin-harness/pull/788)++[中文版](2026-09-18-desktop-auth-bridge.zh.md)++An HTML file previewed in the Files panel could obtain a hidden window of the desktop app and+script it, invisibly, for as long as its script kept it — its iframe allows popups, the shell is+not told which frame asked, and a window opened from a page shares that page's origin. Two routes+led there: the hidden `about:blank` window the Web App itself opened while Penguin Go+authorization started, which the shell allowed for that purpose; and a `window.open` on a preview+URL with `show=no` in its feature string, which Electron applied to the window the shell allowed+for previews. The shell now refuses every blank window, pins what a page could hide, and the+Web App opens the authorization page directly when it runs inside the shell.++## Desktop++- A request for `about:blank` — `window.open()` with no URL — is refused like any other+ non-web scheme, from the main window as from every window it opened. The main window has no+ window-open exception of its own any more.+- A window the shell does open (the Workspace preview hand-off, a page on the preview host, a+ detached terminal) is always shown, in the taskbar, opaque, focusable and at least 320×240,+ whatever the opening page's `window.open` feature string asked for. It is centered when it+ appears, and the page's own `moveTo` / `resizeTo` is refused, so it cannot be parked off-screen.+- Every other rule stays as it was: `http(s)` and `mailto` links elsewhere go to the system+ browser; everything else on this instance is refused and logged.++## Web App++- In the desktop app's window, the Penguin Go "Authorize" click no longer opens a blank tab+ first. Once the server returns the authorization URL, the page opens it directly and the shell+ hands it to the system browser — Electron has no popup blocker, so the tab a browser needs to+ open inside the click is not needed there. The page tells the desktop window from a browser by+ the renderer (`Electron/` in the user agent), so this holds in attach mode too, where the+ window is signed in to a server that was not started in desktop mode.+- In a browser, including one signed into a desktop-mode server, the flow is unchanged: a blank+ tab opens inside the click and navigates to the authorization page when the URL arrives.diff --git a/changelog/unreleased/2026-09-18-desktop-auth-bridge.zh.md b/changelog/unreleased/2026-09-18-desktop-auth-bridge.zh.mdnew file mode 100644index 00000000..afa0ac2f--- /dev/null+++ b/changelog/unreleased/2026-09-18-desktop-auth-bridge.zh.md@@ -0,0 +1,33 @@+# 桌面端不再开出用户看不见的窗口,Penguin Go 授权在桌面端直接打开授权页++- **Date:** 2026-09-18+- **Type:** fix+- **Scope:** `desktop`, `web`+- **PR:** [#788](https://github.com/Prism-Shadow/penguin-harness/pull/788)++[English](2026-09-18-desktop-auth-bridge.md)++文件面板中预览的 HTML 文件曾能拿到桌面应用的一个隐藏窗口,并在不可见处操控它,只要脚本愿意就一直留着——+其 iframe 允许弹窗,壳分不清请求来自哪一帧,而从页面开出的窗口与该页面同源。通向它的路有两条:Penguin Go+授权开始时 Web App 自己开的那个隐藏 `about:blank` 窗口,壳为此放行;以及在预览地址上带 `show=no` 特性串的+`window.open`,Electron 会把它应用到壳为预览放行的窗口上。现在壳拒绝一切空白窗口、钉住页面能藏起窗口的+每一项,Web App 在壳内则直接打开授权页。++## 桌面端++- 对 `about:blank` 的请求——不带地址的 `window.open()`——与其他非 web 协议一样被拒绝,主窗口与它打开的+ 每个窗口一律如此。主窗口不再有自己的新窗口例外。+- 壳确实开出的窗口(Workspace 预览的交接路由、预览主机上的页面、拆出的终端)一律显示、进任务栏、不透明、+ 可聚焦、至少 320×240,无论打开它的页面在 `window.open` 特性串里要求什么。窗口出现时居中,页面自己的+ `moveTo` / `resizeTo` 被拒绝,因此无法被挪到屏幕外。+- 其余规则保持不变:指向其他地方的 `http(s)` 与 `mailto` 链接交给系统浏览器;本实例的其余地址一律拒绝并+ 记日志。++## Web App++- 在桌面应用的窗口里,Penguin Go 的「授权」点击不再先开空白标签页。server 返回授权地址后,页面直接打开它,+ 由壳转交系统浏览器——Electron 没有弹窗拦截器,浏览器里必须在点击内开出的那个标签页在这里并不需要。+ 页面凭渲染器(user agent 中的 `Electron/`)区分桌面窗口与浏览器,因此附着模式下同样成立——那时窗口+ 所登录的 server 并非以桌面模式启动。+- 在浏览器里——包括登录到桌面模式 server 的浏览器——流程不变:点击内先开一个空白标签页,地址返回后再+ 导航到授权页。diff --git a/packages/desktop/src/main.ts b/packages/desktop/src/main.tsindex 048719d7..9e963c4b 100644--- a/packages/desktop/src/main.ts+++ b/packages/desktop/src/main.ts@@ -89,12 +89,12 @@ import {import { getUpdaterStatus, handleUpdaterCommand, initUpdater, onUpdaterStatus } from "./updater.js";import { parseUpdaterCommand, updaterStatusMessage } from "./updater-status.js";import {+ APP_WINDOW_OPTIONS,childGoneLine,classifyWindowOpen,desktopLoginUrl,hidesOnClose,isAppUrl,- isAuthorizationBridgeUrl,isExternalScheme,isLocalSurfaceUrl,MAX_SERVER_RESTARTS,@@ -199,33 +199,11 @@ function createWindow(url: string): void {// (classifyWindowOpen): only the Workspace preview hand-off, a preview page and a detached// terminal get a window of this app; other sites go to the system browser; anything else// on this instance is refused, since every other app path boots a second copy of the App.- win.webContents.setWindowOpenHandler(({ url: target }) => {- // The one addition, and this window's alone: Penguin Go's authorization bridge. The Web- // App opens it from the Authorize click, then points it at the platform or closes it when- // `/start` fails. It is an implementation detail, not a second app window, so it stays- // hidden. It is decided here and not in openWindowFor, which every opened window shares, so- // HTML in a preview window cannot open hidden windows. An HTML preview in this window's own- // Files panel still can: its iframe allows popups, and this handler is not told which frame- // asked.- if (isAuthorizationBridgeUrl(target)) {- return {- action: "allow",- overrideBrowserWindowOptions: {- show: false,- webPreferences: {- contextIsolation: true,- nodeIntegration: false,- sandbox: true,- webviewTag: false,- },- },- };- }- return openWindowFor(target, iconPath);- });- win.webContents.on("did-create-window", (child, details) =>- guardOpenedWindow(child, iconPath, isAuthorizationBridgeUrl(details.url)),- );+ // This window gets no exception of its own: the handler is not told which frame asked, and+ // the Files panel previews Agent-written HTML in an iframe that allows popups, so any window+ // allowed here is one that HTML can open too — a hidden one, it could own outright.+ win.webContents.setWindowOpenHandler(({ url: target }) => openWindowFor(target, iconPath));+ win.webContents.on("did-create-window", (child) => guardOpenedWindow(child, iconPath));win.webContents.on("will-navigate", (event, target) => {if (!isAppUrl(target, appOrigin)) {event.preventDefault();@@ -283,10 +261,9 @@ function openWindowFor(target: string, iconPath: string | null): WindowOpenHandlcase "window":return {action: "allow",+ // Electron merges the page's `window.open` feature string under this override, so+ // every key a page could use to hide the window is pinned here (APP_WINDOW_OPTIONS).overrideBrowserWindowOptions: {- width: 1100,- height: 800,- autoHideMenuBar: true,...(iconPath !== null ? { icon: iconPath } : {}),// Same hardening as the main window: a preview is Agent-written, untrusted HTML// and must never get Node. Nor <webview>, the main window's alone: Electron copies@@ -299,6 +276,7 @@ function openWindowFor(target: string, iconPath: string | null): WindowOpenHandlsandbox: true,webviewTag: false,},+ ...APP_WINDOW_OPTIONS,},};case "external":@@ -315,18 +293,19 @@ function openWindowFor(target: string, iconPath: string | null): WindowOpenHandl* opens: a window-open handler belongs to one window, so a window opened from a preview page* would otherwise open anything at all. Its navigation policy is "stay within this instance's* loopback surface": a child lands on the preview host after the redirect, and the main- * window's stricter app-origin-only rule would bounce the preview itself out.+ * window's stricter app-origin-only rule would bounce the preview itself out. A window stays+ * open when one of its links opens externally.*- * `authorizationBridge` marks the main window's hidden Penguin Go bridge. Only the main window- * opens one, so every window further down passes false.+ * The window is also kept where the user can see it. APP_WINDOW_OPTIONS pins what the page's+ * feature string could hide, but a position is not an option to pin — `left`/`top` arrive as+ * `x`/`y` — so the window is centered here instead, and the page's own `moveTo`/`resizeTo`,+ * which Electron would otherwise apply, is refused: off-screen is hidden too.*/-function guardOpenedWindow(- child: BrowserWindow,- iconPath: string | null,- authorizationBridge: boolean,-): void {+function guardOpenedWindow(child: BrowserWindow, iconPath: string | null): void {+ child.center();+ child.webContents.on("content-bounds-updated", (event) => event.preventDefault());child.webContents.setWindowOpenHandler(({ url: target }) => openWindowFor(target, iconPath));- child.webContents.on("did-create-window", (next) => guardOpenedWindow(next, iconPath, false));+ child.webContents.on("did-create-window", (next) => guardOpenedWindow(next, iconPath));// A session that dies while a child window is open sends that window to the sign-in page// too — the App's guard redirects on the first 401, wherever it is rendered. A sign-in form// is of no use in a subordinate surface: there is no password to type, and signing back in@@ -345,9 +324,6 @@ function guardOpenedWindow(if (!isLocalSurfaceUrl(target, appOrigin)) {event.preventDefault();openInSystem(target);- // A preview window stays open when one of its links opens externally. The hidden- // authorization bridge has completed its only job and must not linger.- if (authorizationBridge) child.close();}});}diff --git a/packages/desktop/src/util.ts b/packages/desktop/src/util.tsindex 841525ad..6ec062ca 100644--- a/packages/desktop/src/util.ts+++ b/packages/desktop/src/util.ts@@ -112,6 +112,14 @@ export type WindowOpenAction = "window" | "external" | "deny";* exist — one more window per click. Other schemes (`file:`, custom protocol handlers) are* refused outright rather than handed to the OS (isExternalScheme). With no origin, which no* window ever sees, everything is refused.+ *+ * `about:blank` — `window.open()` with no URL — is refused with the other schemes, and that is+ * load-bearing: a blank window inherits its opener's origin, so the opener can script it, and a+ * handler is not told which frame asked. The Files panel previews Agent-written HTML in an+ * iframe that allows popups, so a blank window allowed for any purpose would be a hidden window+ * that HTML could own. The Web App therefore asks for none when it is drawn by this shell's+ * renderer (it reads `Electron/` in the user agent, whatever session it holds): it opens+ * Penguin Go's authorization URL directly, which this rule routes to the system browser.*/export function classifyWindowOpen(url: string, origin: string | null): WindowOpenAction {if (origin === null || !isExternalScheme(url)) return "deny";@@ -129,6 +137,40 @@ export function classifyWindowOpen(url: string, origin: string | null): WindowOp: "deny";}+/**+ * What a window of this app looks like, whatever the page that opened it asked for.+ *+ * Electron builds a new window's options as `{ show: true, width: 800, height: 600,+ * ...featuresFromThePage, ...override }` (lib/browser/guest-window-manager.ts), and the+ * page's `window.open` feature string may set `show`, `skipTaskbar`, `opacity`, `transparent`,+ * `focusable`, a position and size limits (allowedWindowOptions in+ * lib/browser/parse-features-string.ts). A preview page is Agent-written HTML, and a window it+ * is allowed to open must never be one the user cannot see: with `show=no`, `skipTaskbar=yes`+ * or `opacity=0` the "window" a `/preview/` URL earns would be as hidden as the blank one+ * classifyWindowOpen refuses, and same-origin with the preview that opened it. So every key+ * that can hide, dim, or shrink a window to nothing is spelled out here, and the override+ * spreads this last. Position is not an option — `left`/`top` land as `x`/`y` — so the window+ * is centered after creation, and a page's `moveTo`/`resizeTo` is refused (see+ * guardOpenedWindow in main.ts). The size limits are the defaults restated: 0 is Electron's+ * "no maximum".+ */+export const APP_WINDOW_OPTIONS = {+ width: 1100,+ height: 800,+ minWidth: 320,+ minHeight: 240,+ maxWidth: 0,+ maxHeight: 0,+ show: true,+ skipTaskbar: false,+ opacity: 1,+ transparent: false,+ focusable: true,+ hiddenInMissionControl: false,+ enableLargerThanScreen: false,+ autoHideMenuBar: true,+} as const;+/*** A URL as a log line may show it: origin and path for a web URL, the scheme alone for any* other. A query or a fragment can carry a token, and neither says where the request went.@@ -145,16 +187,6 @@ export function urlForLog(url: string): string {: `a ${target.protocol} URL`;}-/**- * The one inert window the Web App opens synchronously while Penguin Go creates an- * authorization flow. It must stay inside Electron until the real HTTPS URL arrives:- * handing `about:blank` to Windows asks the OS to find an application for the `about:`- * scheme and raises a system dialog instead of opening the user's browser.- */-export function isAuthorizationBridgeUrl(url: string): boolean {- return url === "about:blank";-}-/** Max automatic server restarts before giving up with an error dialog. */export const MAX_SERVER_RESTARTS = 3;diff --git a/packages/desktop/test/main-window-open.test.ts b/packages/desktop/test/main-window-open.test.tsnew file mode 100644index 00000000..76d21cd5--- /dev/null+++ b/packages/desktop/test/main-window-open.test.ts@@ -0,0 +1,67 @@+/**+ * The shape of main.ts's window-open handling, read from the source.+ *+ * main.ts imports Electron and cannot be loaded under vitest, and the rule it has to keep is+ * structural: every request to open a window, from the main window or from any window it+ * opened, is answered by the one function that applies classifyWindowOpen, and the one place+ * that allows a window pins what a page's feature string could hide (APP_WINDOW_OPTIONS).+ * The gap this guards was an exception in the main window's own handler — a hidden+ * `about:blank` window allowed before classification — which HTML previewed in the Files+ * panel could claim, because the handler is not told which frame asked. A second `allow`, or+ * a handler that does not delegate, is that exception coming back.+ */+import fs from "node:fs";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { describe, expect, it } from "vitest";++const MAIN_TS = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "src", "main.ts");+const source = fs.readFileSync(MAIN_TS, "utf8");++/** Occurrences of a pattern in main.ts. */+function count(pattern: RegExp): number {+ return source.match(new RegExp(pattern.source, `${pattern.flags}g`))?.length ?? 0;+}++describe("main.ts window-open handling", () => {+ it("allows a window in exactly one place, and that place is openWindowFor", () => {+ expect(count(/action:\s*"allow"/)).toBe(1);+ const openWindowFor = /function openWindowFor\([\s\S]*?\n\}/.exec(source)?.[0];+ expect(openWindowFor).toBeDefined();+ expect(openWindowFor).toMatch(/action:\s*"allow"/);+ expect(openWindowFor).toMatch(/classifyWindowOpen\(target, appOrigin\)/);+ });++ it("answers every window's requests through openWindowFor — the main window's too", () => {+ // One handler for the main window, one for each window it opens (guardOpenedWindow).+ // Both delegate; neither decides anything of its own first.+ expect(count(/setWindowOpenHandler\(/)).toBe(2);+ expect(+ count(/setWindowOpenHandler\(\(\{ url: target \}\) => openWindowFor\(target, iconPath\)\)/),+ ).toBe(2);+ });++ it("pins the page-controllable window options in the one allow", () => {+ // The whole literal, up to the brace at its own indentation: the webPreferences object+ // inside it closes on a deeper one.+ const literal = /^([ \t]*)overrideBrowserWindowOptions:\s*\{[\s\S]*?\n\1\},/m;+ const override = literal.exec(source)?.[0];+ expect(override).toBeDefined();+ // Spread last, so nothing above it in the literal wins over the pinned keys.+ expect(override).toMatch(/\.\.\.APP_WINDOW_OPTIONS,\s*\},$/);+ // A feature-string key spelled out here would be one the constant no longer governs.+ for (const key of ["show", "skipTaskbar", "opacity", "width", "height"]) {+ expect(override, key).not.toMatch(new RegExp(`\\b${key}:`));+ }+ });++ it("keeps an opened window on screen: centered, and deaf to the page's moveTo/resizeTo", () => {+ const guard = /function guardOpenedWindow\([\s\S]*?\n\}/.exec(source)?.[0];+ expect(guard).toBeDefined();+ expect(guard).toMatch(/child\.center\(\)/);+ // A WebContents event, not a window one: the request comes from the page.+ expect(guard).toMatch(+ /child\.webContents\.on\("content-bounds-updated", \(event\) => event\.preventDefault\(\)\)/,+ );+ });+});diff --git a/packages/desktop/test/util.test.ts b/packages/desktop/test/util.test.tsindex cf619305..f3d16c3f 100644--- a/packages/desktop/test/util.test.ts+++ b/packages/desktop/test/util.test.ts@@ -1,12 +1,12 @@import { describe, expect, it } from "vitest";import {+ APP_WINDOW_OPTIONS,appOriginFor,childGoneLine,classifyWindowOpen,desktopLoginUrl,hidesOnClose,isAppUrl,- isAuthorizationBridgeUrl,isExternalScheme,isLocalSurfaceUrl,parsePortFile,@@ -148,20 +148,47 @@ describe("classifyWindowOpen", () => {expect(classifyWindowOpen("file:///etc/passwd", null)).toBe("deny");});- it("refuses the Penguin Go authorization bridge, which only the main window may open", () => {- // The main window's handler allows the bridge before it classifies anything. Every window- // shares this classification, so a preview page asking for about:blank gets no window.- expect(isAuthorizationBridgeUrl("about:blank")).toBe(true);- expect(classifyWindowOpen("about:blank", origin)).toBe("deny");+ it("refuses a blank window from every window, the main window included", () => {+ // `window.open()` with no URL, from HTML previewed in the main window's Files panel: its+ // iframe allows popups, and the handler cannot tell that frame from the App. A blank window+ // inherits the opener's origin, so an allowed one would be a hidden window the preview could+ // script for as long as it liked. This classification is the whole rule for the main window+ // too (main-window-open.test.ts pins that it has no exception of its own); the Penguin Go+ // authorization URL comes here as a regular external request instead.+ for (const url of ["about:blank", "about:blank#other", "about:srcdoc"]) {+ expect(classifyWindowOpen(url, origin), url).toBe("deny");+ }+ expect(classifyWindowOpen("https://platform.example/authorize?flow=f-1", origin)).toBe(+ "external",+ );});});-describe("isAuthorizationBridgeUrl", () => {- it("accepts only the inert blank window used while authorization starts", () => {- expect(isAuthorizationBridgeUrl("about:blank")).toBe(true);- expect(isAuthorizationBridgeUrl("about:blank#other")).toBe(false);- expect(isAuthorizationBridgeUrl("about:srcdoc")).toBe(false);- expect(isAuthorizationBridgeUrl("https://example.com")).toBe(false);+describe("APP_WINDOW_OPTIONS", () => {+ it("pins every feature a page could use to hide the window it is allowed to open", () => {+ // Electron spreads the page's `window.open` feature string under the override, so a key+ // absent here is one the page decides: `show=no,skipTaskbar=yes` on a /preview/ URL was a+ // hidden, same-origin window — the blank-window gap by another route.+ expect(APP_WINDOW_OPTIONS).toMatchObject({+ show: true,+ skipTaskbar: false,+ opacity: 1,+ transparent: false,+ focusable: true,+ hiddenInMissionControl: false,+ enableLargerThanScreen: false,+ });+ });++ it("restates the size limits, so a page cannot shrink the window to a point", () => {+ // `maxWidth=1,maxHeight=1` would otherwise clamp the 1100×800 the override asks for.+ expect(APP_WINDOW_OPTIONS.minWidth).toBeGreaterThan(0);+ expect(APP_WINDOW_OPTIONS.minHeight).toBeGreaterThan(0);+ expect(APP_WINDOW_OPTIONS.width).toBeGreaterThanOrEqual(APP_WINDOW_OPTIONS.minWidth);+ expect(APP_WINDOW_OPTIONS.height).toBeGreaterThanOrEqual(APP_WINDOW_OPTIONS.minHeight);+ // 0 is Electron's "no maximum" — the default a page's feature would have replaced.+ expect(APP_WINDOW_OPTIONS.maxWidth).toBe(0);+ expect(APP_WINDOW_OPTIONS.maxHeight).toBe(0);});});diff --git a/packages/web/src/features/models/key-auth-dialog.tsx b/packages/web/src/features/models/key-auth-dialog.tsxindex db09674c..d79c4329 100644--- a/packages/web/src/features/models/key-auth-dialog.tsx+++ b/packages/web/src/features/models/key-auth-dialog.tsx@@ -9,6 +9,7 @@ import { ApiError } from "../../api/client";import { Button } from "../../components/ui/button";import { Modal } from "../../components/ui/modal";import { apiErrorText } from "../../lib/api-error";+import { isElectronRenderer } from "../../lib/desktop-renderer";import { S } from "../../lib/strings";const POLL_MS = 3_000;@@ -50,6 +51,13 @@ export function KeyAuthDialog({onClose: () => void;onApplied: (applied: number) => void;}) {+ // A browser needs a tab opened inside the click, before the server has the URL, or its+ // popup blocker eats the navigation. The desktop shell's window has no popup blocker and its+ // shell refuses every blank window, so there the URL is opened once it is known and the+ // shell hands it to the system browser. Decided by the renderer, not the session: in attach+ // mode the shell's window is signed in to a server that was not started in desktop mode+ // (see lib/desktop-renderer).+ const bridge = !isElectronRenderer(navigator.userAgent);const [phase, setPhase] = useState<Phase>("ready");const [flow, setFlow] = useState<PlatformAuthStartResponse | null>(null);const [error, setError] = useState<string | null>(null);@@ -160,7 +168,7 @@ export function KeyAuthDialog({// Open synchronously from the click so browsers do not treat the eventual navigation as// an unsolicited popup while the server creates the one-time authorization request.- const authorizationTab = window.open("about:blank", "_blank");+ const authorizationTab = bridge ? window.open("about:blank", "_blank") : null;if (authorizationTab !== null) authorizationTab.opener = null;try {const started = await endpoints.start(projectId);@@ -170,6 +178,11 @@ export function KeyAuthDialog({return;}setFlow(started);+ if (!bridge) {+ window.open(started.authorizeUrl, "_blank", "noopener,noreferrer");+ setPhase("waiting");+ return;+ }if (authorizationTab === null) {// Popup blockers can still intervene. Keep the created request and let the next// explicit click open its URL without consuming another platform start quota.diff --git a/packages/web/src/lib/desktop-renderer.ts b/packages/web/src/lib/desktop-renderer.tsnew file mode 100644index 00000000..49c96b74--- /dev/null+++ b/packages/web/src/lib/desktop-renderer.ts@@ -0,0 +1,20 @@+/**+ * Whether this page is drawn by the desktop shell's renderer — an Electron window — whatever+ * session it holds.+ *+ * This is a different question from isDesktopShellWindow (lib/account-menu), which asks+ * whether the page may drive the shell around it and is answered by the session, because the+ * server enforces it. What the renderer is decides only how the page itself may behave, and+ * the session cannot answer it: in attach mode the shell's window is signed in to a server+ * that was not started in desktop mode (`desktopMode` false, whatever `sessionVia` says), yet+ * it is still an Electron window — one with no popup blocker, whose shell refuses every blank+ * window.+ *+ * Electron's default user agent carries `Electron/<version>` after Chrome's, and the shell+ * leaves it alone on the App's windows: the one setUserAgent in packages/desktop is on the+ * built-in browser's own partition (builtin-browser.ts). No browser's user agent contains that+ * token.+ */+export function isElectronRenderer(userAgent: string): boolean {+ return /\bElectron\//.test(userAgent);+}diff --git a/packages/web/test/desktop-renderer.test.ts b/packages/web/test/desktop-renderer.test.tsnew file mode 100644index 00000000..d4773ebf--- /dev/null+++ b/packages/web/test/desktop-renderer.test.ts@@ -0,0 +1,36 @@+/**+ * desktop-renderer.ts unit tests: telling the desktop shell's Electron window from a browser+ * by the user agent, which is the fact that decides whether a blank tab can be opened inside+ * a click (a browser needs one against its popup blocker; the shell refuses one). vitest runs+ * node-only here, so this asserts the pure helper, not the dialog that reads navigator.+ */+import { describe, expect, it } from "vitest";+import { isElectronRenderer } from "../src/lib/desktop-renderer";++const ELECTRON_UA =+ "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) PenguinHarness/0.2.13 Chrome/146.0.7680.0 Electron/43.2.0 Safari/537.36";+const CHROME_UA =+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36";+const FIREFOX_UA =+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 14.6; rv:132.0) Gecko/20100101 Firefox/132.0";+const SAFARI_UA =+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15";++describe("isElectronRenderer", () => {+ it("recognises the shell's window by Electron's own token, in any session", () => {+ // In attach mode the shell's window holds no desktop-mode session; it is still Electron.+ expect(isElectronRenderer(ELECTRON_UA)).toBe(true);+ });++ it("answers false for every browser, including one signed into a desktop-mode server", () => {+ for (const ua of [CHROME_UA, FIREFOX_UA, SAFARI_UA, "", "Node.js/24"]) {+ expect(isElectronRenderer(ua), ua).toBe(false);+ }+ });++ it("wants the token, not the word", () => {+ // A product name or a path that merely mentions Electron is not the renderer's own mark.+ expect(isElectronRenderer("Mozilla/5.0 ElectronNotes/1.0 Chrome/146.0.0.0")).toBe(false);+ expect(isElectronRenderer("Mozilla/5.0 Chrome/146.0.0.0 electron/43.2.0")).toBe(false);+ });+});