Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(app): stop external links from navigating the main webview away

Deploy21b19c7726d6
diff --git a/app/src/App.tsx b/app/src/App.tsx
index 887f719fcf..603e08a1d7 100644
--- a/app/src/App.tsx
+++ b/app/src/App.tsx
@@ -50,6 +50,7 @@ import {
} from './services/internetStatusListener';
import { persistor, store } from './store';
import { DEV_FORCE_ONBOARDING } from './utils/config';
+import { installExternalLinkGuard } from './utils/externalLinkGuard';
startNativeNotificationsService();
// Connectivity status (#1527): wire navigator.onLine + start core sidecar
@@ -70,6 +71,13 @@ if (import.meta.hot) {
function App() {
const onMobile = getIsMobile();
+ // The desktop shell is one webview with no back button, so a link that
+ // navigates it away strands the user on that page with no route back to the
+ // chat. Chat bubbles route their own links through `openUrl`; this guard
+ // covers every other anchor the app renders. Installed here, above the
+ // router, so it is live for the whole session.
+ useEffect(() => installExternalLinkGuard(), []);
+
// On mobile (iOS or Android) the SocketProvider would try to connect to the
// local core HTTP socket, which does not exist on device (the core runs on
// the remote desktop). Gate it out to prevent spurious connection errors —
diff --git a/app/src/utils/externalLinkGuard.test.ts b/app/src/utils/externalLinkGuard.test.ts
new file mode 100644
index 0000000000..79c74f445e
--- /dev/null
+++ b/app/src/utils/externalLinkGuard.test.ts
@@ -0,0 +1,85 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { installExternalLinkGuard, isExternalNavigation } from './externalLinkGuard';
+import { openUrl } from './openUrl';
+
+vi.mock('./openUrl', () => ({ openUrl: vi.fn(() => Promise.resolve()) }));
+
+const APP_ORIGIN = 'http://localhost:1420';
+
+describe('isExternalNavigation', () => {
+ it('flags a remote page, which is the one-way navigation we must stop', () => {
+ expect(isExternalNavigation('https://example.com/built-site', APP_ORIGIN)).toBe(true);
+ });
+
+ it('leaves the app’s own hash routes alone', () => {
+ expect(isExternalNavigation('#/settings/notifications', APP_ORIGIN)).toBe(false);
+ expect(isExternalNavigation(`${APP_ORIGIN}/#/chat`, APP_ORIGIN)).toBe(false);
+ });
+
+ it('ignores non-http schemes — those are handled by their own components', () => {
+ expect(isExternalNavigation('mailto:[email protected]', APP_ORIGIN)).toBe(false);
+ expect(isExternalNavigation('data:text/html,<p>hi</p>', APP_ORIGIN)).toBe(false);
+ expect(isExternalNavigation('openhuman://thread/1', APP_ORIGIN)).toBe(false);
+ });
+});
+
+describe('installExternalLinkGuard', () => {
+ let teardown: () => void = () => {};
+
+ beforeEach(() => {
+ vi.mocked(openUrl).mockClear();
+ document.body.innerHTML = '';
+ });
+
+ afterEach(() => teardown());
+
+ const clickAnchor = (html: string): MouseEvent => {
+ document.body.innerHTML = html;
+ const anchor = document.querySelector('a') as HTMLAnchorElement;
+ const event = new MouseEvent('click', { bubbles: true, cancelable: true, button: 0 });
+ anchor.dispatchEvent(event);
+ return event;
+ };
+
+ it('stops an external link from navigating the webview and opens it outside', () => {
+ teardown = installExternalLinkGuard();
+
+ const event = clickAnchor('<a href="https://example.com/site">my site</a>');
+
+ expect(event.defaultPrevented).toBe(true);
+ expect(openUrl).toHaveBeenCalledWith('https://example.com/site');
+ });
+
+ it('lets in-app routes through untouched', () => {
+ teardown = installExternalLinkGuard();
+
+ const event = clickAnchor('<a href="#/chat">back to chat</a>');
+
+ expect(event.defaultPrevented).toBe(false);
+ expect(openUrl).not.toHaveBeenCalled();
+ teardown();
+ });
+
+ // The chat bubble's own anchor already calls preventDefault + openUrl; if the
+ // guard did not defer, one click would open two tabs.
+ it('defers to a component that already handled the click itself', () => {
+ teardown = installExternalLinkGuard();
+ document.body.innerHTML = '<a href="https://example.com/site">handled</a>';
+ const anchor = document.querySelector('a') as HTMLAnchorElement;
+ anchor.addEventListener('click', e => e.preventDefault());
+
+ anchor.dispatchEvent(new MouseEvent('click', { bubbles: true, cancelable: true, button: 0 }));
+
+ expect(openUrl).not.toHaveBeenCalled();
+ });
+
+ it('stops listening after teardown', () => {
+ installExternalLinkGuard()();
+
+ const event = clickAnchor('<a href="https://example.com/site">my site</a>');
+
+ expect(event.defaultPrevented).toBe(false);
+ expect(openUrl).not.toHaveBeenCalled();
+ });
+});
diff --git a/app/src/utils/externalLinkGuard.ts b/app/src/utils/externalLinkGuard.ts
new file mode 100644
index 0000000000..61e47d0ec6
--- /dev/null
+++ b/app/src/utils/externalLinkGuard.ts
@@ -0,0 +1,68 @@
+import { openUrl } from './openUrl';
+
+/**
+ * True when `href` would take the main webview away from the app itself.
+ *
+ * The desktop shell is a single webview with no browser chrome — no back
+ * button, no address bar — so any top-level navigation to a remote page is
+ * one-way: the chat is gone until the app is restarted. Only the app's own
+ * origin (its hash routes, `#/chat`, `#/settings/...`) is safe to follow.
+ *
+ * `about:`/`blob:`/`data:` and in-page anchors are left alone: they are not
+ * remote pages, and a `data:` preview is a deliberate in-app render.
+ */
+export function isExternalNavigation(href: string, appOrigin: string): boolean {
+ const trimmed = href.trim();
+ if (!trimmed || trimmed.startsWith('#')) return false;
+ let url: URL;
+ try {
+ url = new URL(trimmed, appOrigin);
+ } catch {
+ return false;
+ }
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;
+ return url.origin !== appOrigin;
+}
+
+/**
+ * Install a document-level, capture-phase guard that keeps a link click from
+ * navigating the main webview away from the app.
+ *
+ * Chat bubbles already route their links through `openUrl` (see
+ * `AgentMessageBubble`'s `MarkdownAnchor`), but that is one component's
+ * discipline, and every other rendered anchor — tool output, a panel, raw
+ * HTML inside a message — inherits the webview's default behaviour instead.
+ * When one of those is clicked the shell navigates and the user is stranded
+ * on the page with no way back, which is what this guard exists to prevent.
+ *
+ * It listens in the BUBBLE phase, deliberately. In the capture phase this
+ * document-level listener would run *before* the component's own handler, so
+ * a chat link would be opened once here and again by `MarkdownAnchor` — two
+ * browser tabs for one click. Bubbling lets the owning component go first;
+ * anything it already called `preventDefault` on is skipped, and the default
+ * navigation has still not happened by the time this runs, so preventing it
+ * here is not too late.
+ *
+ * Returns the teardown function.
+ */
+export function installExternalLinkGuard(doc: Document = document): () => void {
+ const onClick = (event: MouseEvent) => {
+ if (event.defaultPrevented || event.button !== 0) return;
+ if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
+
+ const target = event.target as Element | null;
+ const anchor = target?.closest?.('a[href]') as HTMLAnchorElement | null;
+ if (!anchor) return;
+
+ const href = anchor.getAttribute('href') ?? '';
+ if (!isExternalNavigation(href, doc.location.origin)) return;
+
+ event.preventDefault();
+ void openUrl(anchor.href).catch(() => {
+ // The OS handler refused; staying in the app beats a one-way navigation.
+ });
+ };
+
+ doc.addEventListener('click', onClick);
+ return () => doc.removeEventListener('click', onClick);
+}