Skip to content

The agent that grows with you

fix(webapp): report a browser-blocked pop-out as blocked

Adolaniumc9cf1d4181d6
diff --git a/apps/desktop/src/lib/browser-desktop-bridge.test.ts b/apps/desktop/src/lib/browser-desktop-bridge.test.ts
index 850d796d70d..a5aaba6715d 100644
--- a/apps/desktop/src/lib/browser-desktop-bridge.test.ts
+++ b/apps/desktop/src/lib/browser-desktop-bridge.test.ts
@@ -329,7 +329,7 @@ describe('browser-hosted Desktop bridge', () => {
// A gated host opens the window directly; a launch-link tab hands it a session first.
win.__HERMES_AUTH_REQUIRED__ = true
window.history.replaceState(null, '', '/?profile=work#/existing')
- const open = vi.spyOn(window, 'open').mockImplementation(() => null)
+ const open = vi.spyOn(window, 'open').mockImplementation(() => ({}) as Window)
expect(installBrowserDesktopBridge()).toBe(true)
await expect(
@@ -343,7 +343,7 @@ describe('browser-hosted Desktop bridge', () => {
expect(url.searchParams.get('watch')).toBe('1')
expect(url.hash).toBe('#/session%20%2F%201')
expect(target).toBe('_blank')
- expect(features).toBe('noopener,noreferrer')
+ expect(features).toBeUndefined()
})
it('hands only the schemes Electron opens externally to window.open', async () => {
@@ -395,7 +395,7 @@ describe('browser-hosted Desktop bridge', () => {
// A gated host opens the window directly; a launch-link tab hands it a session first.
win.__HERMES_AUTH_REQUIRED__ = true
window.history.replaceState(null, '', '/?profile=work#/existing')
- const open = vi.spyOn(window, 'open').mockImplementation(() => null)
+ const open = vi.spyOn(window, 'open').mockImplementation(() => ({}) as Window)
expect(installBrowserDesktopBridge()).toBe(true)
await expect(win.hermesDesktop!.openSessionWindow('session-1', opts)).resolves.toEqual({ ok: true })
diff --git a/apps/desktop/src/lib/browser-launch-session.test.ts b/apps/desktop/src/lib/browser-launch-session.test.ts
index 602f44095fc..8e2176ca43f 100644
--- a/apps/desktop/src/lib/browser-launch-session.test.ts
+++ b/apps/desktop/src/lib/browser-launch-session.test.ts
@@ -152,7 +152,7 @@ it.each(['window', 'session'] as const)('hands off a private %s through a one-us
close = vi.fn()
}
vi.stubGlobal('BroadcastChannel', Channel)
- const open = vi.spyOn(window, 'open').mockReturnValue(null)
+ const open = vi.spyOn(window, 'open').mockReturnValue({} as Window)
expect(installBrowserDesktopBridge()).toBe(true)
const pending = kind === 'window'
@@ -183,7 +183,8 @@ it.each(['window', 'session'] as const)('hands off a private %s through a one-us
expect(handoff.get('query')).toContain('profile=research')
expect(handoff.get('route')).toBe(kind === 'window' ? '/' : '/session%20%2F%201')
expect(handoff.get('query')?.includes('watch=1')).toBe(kind === 'session')
- expect([target, features]).toEqual(['_blank', 'noopener,noreferrer'])
+ expect(target).toBe('_blank')
+ expect(features).toBeUndefined()
expect(String(opened)).not.toContain(token)
expect(String(opened)).not.toContain(ticket)
expect(window.location.search).toBe('?profile=coder')
diff --git a/apps/desktop/src/lib/browser-profile.test.ts b/apps/desktop/src/lib/browser-profile.test.ts
index baaa58f8177..d3fd146d64a 100644
--- a/apps/desktop/src/lib/browser-profile.test.ts
+++ b/apps/desktop/src/lib/browser-profile.test.ts
@@ -80,7 +80,7 @@ it('opens the requested profile on this host without carrying another session or
// A gated host opens the window directly; a launch-link tab hands it a session first.
bootstrap.__HERMES_AUTH_REQUIRED__ = true
window.history.replaceState(null, '', '/hermes?profile=active&win=secondary&watch=1#/session-1')
- const open = vi.spyOn(window, 'open').mockImplementation(() => null)
+ const open = vi.spyOn(window, 'open').mockImplementation(() => ({}) as Window)
expect(installBrowserDesktopBridge()).toBe(true)
const originalUrl = window.location.href
diff --git a/apps/desktop/src/lib/browser-window.test.ts b/apps/desktop/src/lib/browser-window.test.ts
new file mode 100644
index 00000000000..cc633d3f519
--- /dev/null
+++ b/apps/desktop/src/lib/browser-window.test.ts
@@ -0,0 +1,51 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { createBrowserWindowOpener } from './browser-window'
+
+const POPUP_BLOCKED = 'Webapp window did not open. Allow popups for this site and try again.'
+
+afterEach(() => {
+ vi.restoreAllMocks()
+ vi.unstubAllGlobals()
+})
+
+function opener(privateSession: boolean, api = vi.fn()) {
+ return createBrowserWindowOpener({ api, basePath: '/one', privateSession })
+}
+
+function target(): URL {
+ return new URL('/?win=secondary#/session-1', window.location.origin)
+}
+
+describe('browser pop-out', () => {
+ it('reports a blocked popup instead of success', async () => {
+ vi.spyOn(window, 'open').mockReturnValue(null)
+
+ await expect(opener(false)(target())).rejects.toThrow(POPUP_BLOCKED)
+ })
+
+ it('resolves when the browser returns a window and severs its opener', async () => {
+ const child = { opener: window } as Window
+ const open = vi.spyOn(window, 'open').mockReturnValue(child)
+ const href = target().href
+
+ await expect(opener(false)(new URL(href))).resolves.toEqual({ ok: true })
+ expect(open).toHaveBeenCalledWith(href, '_blank')
+ expect(child.opener).toBeNull()
+ })
+
+ it('reports a blocked private-session popup without waiting for the handoff', async () => {
+ vi.spyOn(window, 'open').mockReturnValue(null)
+ vi.stubGlobal(
+ 'BroadcastChannel',
+ class {
+ onmessage: ((event: { data: unknown }) => void) | null = null
+ close() {}
+ }
+ )
+ const api = vi.fn()
+
+ await expect(opener(true, api)(target())).rejects.toThrow(POPUP_BLOCKED)
+ expect(api).not.toHaveBeenCalled()
+ })
+})
diff --git a/apps/desktop/src/lib/browser-window.ts b/apps/desktop/src/lib/browser-window.ts
index c7253513e45..a2f23fe44c1 100644
--- a/apps/desktop/src/lib/browser-window.ts
+++ b/apps/desktop/src/lib/browser-window.ts
@@ -8,11 +8,27 @@ interface BrowserWindowOptions {
privateSession: boolean
}
+const POPUP_BLOCKED = 'Webapp window did not open. Allow popups for this site and try again.'
+
+// `noopener` makes a successful open return null (HTML window open step 18),
+// so a blocked popup is indistinguishable. Sever opener on the handle instead.
+function openWebappWindow(href: string): Window | null {
+ const opened = window.open(href, '_blank')
+
+ if (!opened) {return null}
+
+ opened.opener = null
+
+ return opened
+}
+
/** Open synchronously in the user gesture; never rely on an opener/storage copy. */
export function createBrowserWindowOpener({ api, basePath, privateSession }: BrowserWindowOptions) {
return (target: URL): Promise<{ ok: true }> => {
if (!privateSession) {
- window.open(target.href, '_blank', 'noopener,noreferrer')
+ if (!openWebappWindow(target.href)) {
+ return Promise.reject(new Error(POPUP_BLOCKED))
+ }
return Promise.resolve({ ok: true })
}
@@ -35,7 +51,7 @@ export function createBrowserWindowOpener({ api, basePath, privateSession }: Bro
if (error) {reject(error)} else {resolve({ ok: true })}
}
- const timeout = window.setTimeout(() => finish(new Error('Webapp window did not open. Allow popups for this site and try again.')), 30_000)
+ const timeout = window.setTimeout(() => finish(new Error(POPUP_BLOCKED)), 30_000)
channel.onmessage = event => {
if (settled) {return}
@@ -58,7 +74,9 @@ export function createBrowserWindowOpener({ api, basePath, privateSession }: Bro
}
try {
- window.open(launch.href, '_blank', 'noopener,noreferrer')
+ if (!openWebappWindow(launch.href)) {
+ finish(new Error(POPUP_BLOCKED))
+ }
} catch (error) {
finish(error instanceof Error ? error : new Error(String(error)))
}