Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(desktop): keep in-app "open in new tab" out of the OS browser (#2082)

Andrew Mikofalvy64b9cb2b4b61
diff --git a/.changeset/fix-desktop-internal-link-new-tab-browser-leak.md b/.changeset/fix-desktop-internal-link-new-tab-browser-leak.md
new file mode 100644
index 000000000..5edf3207b
--- /dev/null
+++ b/.changeset/fix-desktop-internal-link-new-tab-browser-leak.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Fix internal document links opening in the wrong place from the desktop app. "Open in new tab" (Cmd/Ctrl-click, or the link panel's open-in-new-tab) on an in-app document link now navigates to that document inside Open Knowledge. Previously the desktop window handler treated the in-app route as an external URL: in development builds this opened a `localhost` tab in your web browser (and repeated follows could flood the browser with tabs), and in packaged builds the click did nothing.
diff --git a/packages/desktop/src/main/asset-safety-net.ts b/packages/desktop/src/main/asset-safety-net.ts
index c0bc88dab..c15c658d3 100644
--- a/packages/desktop/src/main/asset-safety-net.ts
+++ b/packages/desktop/src/main/asset-safety-net.ts
@@ -7,6 +7,8 @@ interface WebContentsLike {
event: 'will-navigate',
handler: (event: { preventDefault: () => void }, url: string) => void,
): void;
+ getURL?(): string;
+ executeJavaScript?(code: string): Promise<unknown>;
}
interface AttachAssetSafetyNetDeps {
@@ -53,6 +55,31 @@ export function matchAssetUrl(url: string, editorOrigin: string): string | null
return path;
}
+function safeOrigin(url: string | undefined | null): string | null {
+ if (!url) return null;
+ try {
+ return new URL(url).origin;
+ } catch {
+ return null;
+ }
+}
+
+export function matchInAppRoute(url: string, rendererOrigin: string | null): string | null {
+ if (!rendererOrigin) return null;
+ let parsed: URL;
+ try {
+ parsed = new URL(url);
+ } catch {
+ return null;
+ }
+ if (parsed.origin !== rendererOrigin) return null;
+ return parsed.hash.startsWith('#/') ? parsed.hash : null;
+}
+
+function navigateToHashScript(hash: string): string {
+ return `window.location.hash = ${JSON.stringify(hash)};`;
+}
+
export function attachAssetSafetyNet(
webContents: WebContentsLike,
deps: AttachAssetSafetyNetDeps,
@@ -73,6 +100,20 @@ export function attachAssetSafetyNet(
});
return { action: 'deny' };
}
+ const inAppHash = matchInAppRoute(details.url, safeOrigin(webContents.getURL?.()));
+ if (inAppHash !== null) {
+ const nav = webContents.executeJavaScript?.(navigateToHashScript(inAppHash));
+ if (nav) {
+ void nav.catch((err: unknown) => {
+ log({
+ level: 'warn',
+ message: 'in-app navigation failed from setWindowOpenHandler',
+ data: { hash: inAppHash, err: (err as Error).message },
+ });
+ });
+ }
+ return { action: 'deny' };
+ }
void deps.openExternal(details.url).catch((err: unknown) => {
log({
level: 'warn',
@@ -104,7 +145,12 @@ export function attachAssetSafetyNet(
} catch {
return;
}
- if (parsed.origin === deps.editorOrigin) return;
+ if (
+ parsed.origin === deps.editorOrigin ||
+ parsed.origin === safeOrigin(webContents.getURL?.())
+ ) {
+ return;
+ }
event.preventDefault();
void deps.openExternal(url).catch((err: unknown) => {
log({
diff --git a/packages/desktop/tests/main/asset-safety-net.test.ts b/packages/desktop/tests/main/asset-safety-net.test.ts
index be91dc8a5..fc36afbe3 100644
--- a/packages/desktop/tests/main/asset-safety-net.test.ts
+++ b/packages/desktop/tests/main/asset-safety-net.test.ts
@@ -1,5 +1,9 @@
import { describe, expect, mock, test } from 'bun:test';
-import { attachAssetSafetyNet, matchAssetUrl } from '../../src/main/asset-safety-net.ts';
+import {
+ attachAssetSafetyNet,
+ matchAssetUrl,
+ matchInAppRoute,
+} from '../../src/main/asset-safety-net.ts';
const ORIGIN = 'http://localhost:5173';
@@ -74,6 +78,30 @@ describe('matchAssetUrl', () => {
});
});
+describe('matchInAppRoute', () => {
+ test('same-renderer-origin hash route → returns the hash', () => {
+ expect(
+ matchInAppRoute('http://localhost:5173/#/people/ray-zaragoza', 'http://localhost:5173'),
+ ).toBe('#/people/ray-zaragoza');
+ });
+
+ test('different origin (renderer route vs api/editor port) → null', () => {
+ expect(matchInAppRoute('http://localhost:5173/#/doc', 'http://localhost:8765')).toBeNull();
+ });
+
+ test('same origin but non-hash URL (bundle entry) → null', () => {
+ expect(matchInAppRoute('http://localhost:5173/index.html', 'http://localhost:5173')).toBeNull();
+ });
+
+ test('null renderer origin (URL not yet loaded) → null', () => {
+ expect(matchInAppRoute('http://localhost:5173/#/doc', null)).toBeNull();
+ });
+
+ test('bogus URL → null (no throw)', () => {
+ expect(matchInAppRoute('not a url', 'http://localhost:5173')).toBeNull();
+ });
+});
+
function noopOpenExternal(_: string): Promise<void> {
return Promise.resolve();
}
@@ -193,6 +221,68 @@ describe('attachAssetSafetyNet — setWindowOpenHandler', () => {
data: { reason: 'path-escape' },
});
});
+
+ test('same-renderer-origin in-app route (open-in-new-tab) → navigated in-app, NOT openExternal', async () => {
+ const openAsset = mock(async (_: string) => ({ ok: true }) as const);
+ const openExternal = mock(noopOpenExternal);
+ const executeJavaScript = mock(async (_: string) => undefined);
+
+ let installedHandler: ((details: { url: string }) => { action: 'allow' | 'deny' }) | null =
+ null;
+ const webContents = {
+ setWindowOpenHandler(handler: (details: { url: string }) => { action: 'allow' | 'deny' }) {
+ installedHandler = handler;
+ },
+ on: () => {},
+ getURL: () => 'http://localhost:5173/#/home',
+ executeJavaScript,
+ };
+
+ attachAssetSafetyNet(webContents, {
+ openAsset,
+ openExternal,
+ editorOrigin: 'http://localhost:8765',
+ });
+
+ const result = installedHandler?.({ url: 'http://localhost:5173/#/people/ray-zaragoza' });
+ expect(result).toEqual({ action: 'deny' });
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(executeJavaScript).toHaveBeenCalledTimes(1);
+ expect(executeJavaScript.mock.calls[0]?.[0]).toBe(
+ 'window.location.hash = "#/people/ray-zaragoza";',
+ );
+ expect(openExternal).not.toHaveBeenCalled();
+ expect(openAsset).not.toHaveBeenCalled();
+ });
+
+ test('cross-origin new-window with renderer URL present → still openExternal (not in-app)', async () => {
+ const openAsset = mock(async (_: string) => ({ ok: true }) as const);
+ const openExternal = mock(noopOpenExternal);
+ const executeJavaScript = mock(async (_: string) => undefined);
+
+ let installedHandler: ((details: { url: string }) => { action: 'allow' | 'deny' }) | null =
+ null;
+ const webContents = {
+ setWindowOpenHandler(handler: (details: { url: string }) => { action: 'allow' | 'deny' }) {
+ installedHandler = handler;
+ },
+ on: () => {},
+ getURL: () => 'http://localhost:5173/#/home',
+ executeJavaScript,
+ };
+
+ attachAssetSafetyNet(webContents, {
+ openAsset,
+ openExternal,
+ editorOrigin: 'http://localhost:8765',
+ });
+
+ installedHandler?.({ url: 'https://example.com/path' });
+ await Promise.resolve();
+ expect(openExternal).toHaveBeenCalledWith('https://example.com/path');
+ expect(executeJavaScript).not.toHaveBeenCalled();
+ });
});
describe('attachAssetSafetyNet — will-navigate', () => {
@@ -249,6 +339,36 @@ describe('attachAssetSafetyNet — will-navigate', () => {
expect(openExternal).not.toHaveBeenCalled();
});
+ test('same-renderer-origin navigation (distinct from editorOrigin) → no preventDefault, no delegation', async () => {
+ const openAsset = mock(async (_: string) => ({ ok: true }) as const);
+ const openExternal = mock(noopOpenExternal);
+
+ let installedHandler: ((event: { preventDefault: () => void }, url: string) => void) | null =
+ null;
+ const webContents = {
+ setWindowOpenHandler: () => {},
+ on(
+ event: 'will-navigate',
+ handler: (event: { preventDefault: () => void }, url: string) => void,
+ ) {
+ if (event === 'will-navigate') installedHandler = handler;
+ },
+ getURL: () => 'http://localhost:5173/#/home',
+ };
+
+ attachAssetSafetyNet(webContents, {
+ openAsset,
+ openExternal,
+ editorOrigin: 'http://localhost:8765',
+ });
+
+ const preventDefault = mock(() => {});
+ installedHandler?.({ preventDefault }, 'http://localhost:5173/#/people/ray-zaragoza');
+ expect(preventDefault).not.toHaveBeenCalled();
+ await Promise.resolve();
+ expect(openExternal).not.toHaveBeenCalled();
+ });
+
test('malformed URL → silent drop (no preventDefault, no delegation, no crash)', async () => {
const openAsset = mock(async (_: string) => ({ ok: true }) as const);
const openExternal = mock(noopOpenExternal);