From 8bf74a8a166d6cce41d84797d3c81474635b507c Mon Sep 17 00:00:00 2001 From: JustasMonkev Date: Sun, 19 Jul 2026 11:19:05 +0300 Subject: [PATCH 1/3] fix: remove extension reconnect seed tab --- src/extension/browserModel.ts | 5 +++++ tests/extension-protocol.test.ts | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/extension/browserModel.ts b/src/extension/browserModel.ts index 9d80e818..7a0df3a8 100644 --- a/src/extension/browserModel.ts +++ b/src/extension/browserModel.ts @@ -15,6 +15,7 @@ */ import { logUnhandledError } from '../utils/log.js'; +import { EXTENSION_ID } from './protocol.js'; import type { DebuggerSession, Debuggee, Tab } from './protocol.js'; @@ -115,6 +116,10 @@ export class BrowserModel { throw new Error('Failed to create tab'); this._knownTabs.set(tab.id, tab); const tabSession = await this._attachTab(tab.id); + const connectPagePrefix = `chrome-extension://${EXTENSION_ID}/connect.html?mcpRelayUrl=`; + await Promise.all([...this._knownTabs.values()] + .filter(knownTab => knownTab.id !== tab.id && knownTab.url?.startsWith(connectPagePrefix)) + .map(knownTab => this._sendToExtension('chrome.tabs.remove', [knownTab.id]))); return { targetId: tabSession.targetInfo?.targetId }; } diff --git a/tests/extension-protocol.test.ts b/tests/extension-protocol.test.ts index 66cf3be8..b1b189d9 100644 --- a/tests/extension-protocol.test.ts +++ b/tests/extension-protocol.test.ts @@ -126,6 +126,26 @@ describe('extension protocol v2', () => { .rejects.toThrow('attach failed'); }); + it('removes the token-bypass connect page after creating a target', async () => { + const sendCommand = vi.fn(async (method: string, params: any[]) => { + if (method === 'chrome.tabs.create') + return { id: 8, url: params[0].url }; + if (method === 'chrome.debugger.sendCommand' && params[1] === 'Target.getTargetInfo') + return { targetInfo: { targetId: `target-${params[0].tabId}`, type: 'page' } }; + return {}; + }); + const handler = new ExtensionProtocolV2(sendCommand); + handler.handleExtensionEvent('chrome.tabs.onCreated', [{ + id: 7, + url: `chrome-extension://${EXTENSION_ID}/connect.html?mcpRelayUrl=ws%3A%2F%2F127.0.0.1`, + }]); + + await handler.handleCDPCommand('Target.setAutoAttach', { autoAttach: true }, undefined); + await handler.handleCDPCommand('Target.createTarget', {}, undefined); + + expect(sendCommand).toHaveBeenCalledWith('chrome.tabs.remove', [7]); + }); + it('serializes concurrent auto-attach state changes', async () => { let resolveDetach!: () => void; const detach = new Promise(resolve => resolveDetach = resolve); From 31e38ad6355b7204795d429d5a15a47ff41f5036 Mon Sep 17 00:00:00 2001 From: JustasMonkev Date: Sun, 19 Jul 2026 11:53:09 +0300 Subject: [PATCH 2/3] fix: scope extension seed cleanup --- src/extension/browserModel.ts | 8 +++----- src/extension/cdpRelay.ts | 10 ++++++---- src/extension/cdpRelayV2.ts | 4 ++-- tests/extension-protocol.test.ts | 21 ++++++++++++++------- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/extension/browserModel.ts b/src/extension/browserModel.ts index 7a0df3a8..abafe250 100644 --- a/src/extension/browserModel.ts +++ b/src/extension/browserModel.ts @@ -15,7 +15,6 @@ */ import { logUnhandledError } from '../utils/log.js'; -import { EXTENSION_ID } from './protocol.js'; import type { DebuggerSession, Debuggee, Tab } from './protocol.js'; @@ -48,7 +47,7 @@ export class BrowserModel { private _autoAttach = false; private _nextSessionId = 1; - constructor(sendToExtension: SendCommand) { + constructor(sendToExtension: SendCommand, private _connectPagePrefix?: string) { this._sendToExtension = sendToExtension; } @@ -116,9 +115,8 @@ export class BrowserModel { throw new Error('Failed to create tab'); this._knownTabs.set(tab.id, tab); const tabSession = await this._attachTab(tab.id); - const connectPagePrefix = `chrome-extension://${EXTENSION_ID}/connect.html?mcpRelayUrl=`; - await Promise.all([...this._knownTabs.values()] - .filter(knownTab => knownTab.id !== tab.id && knownTab.url?.startsWith(connectPagePrefix)) + await Promise.allSettled([...this._knownTabs.values()] + .filter(knownTab => knownTab.id !== tab.id && this._connectPagePrefix && knownTab.url?.startsWith(this._connectPagePrefix)) .map(knownTab => this._sendToExtension('chrome.tabs.remove', [knownTab.id]))); return { targetId: tabSession.targetInfo?.targetId }; } diff --git a/src/extension/cdpRelay.ts b/src/extension/cdpRelay.ts index cd9b7a74..2ac42291 100644 --- a/src/extension/cdpRelay.ts +++ b/src/extension/cdpRelay.ts @@ -59,6 +59,7 @@ export class CDPRelayServer { private _executablePath?: string; private _cdpPath: string; private _extensionPath: string; + private _connectPagePrefix: string; private _wss: WebSocketServer; private _playwrightConnection: WebSocket | null = null; private _extensionConnection: ExtensionConnection | null = null; @@ -74,6 +75,9 @@ export class CDPRelayServer { const uuid = crypto.randomUUID(); this._cdpPath = `/cdp/${uuid}`; this._extensionPath = `/extension/${uuid}`; + const connectPageUrl = new URL(`chrome-extension://${protocol.EXTENSION_ID}/connect.html`); + connectPageUrl.searchParams.set('mcpRelayUrl', this.extensionEndpoint()); + this._connectPagePrefix = connectPageUrl.toString(); this._resetExtensionConnection(); this._wss = new WebSocketServer({ server }); @@ -105,10 +109,8 @@ export class CDPRelayServer { } private _connectBrowser(clientInfo: ClientInfo) { - const mcpRelayEndpoint = `${this._wsHost}${this._extensionPath}`; // Need to specify "key" in the manifest.json to make the id stable when loading from file. - const url = new URL(`chrome-extension://${protocol.EXTENSION_ID}/connect.html`); - url.searchParams.set('mcpRelayUrl', mcpRelayEndpoint); + const url = new URL(this._connectPagePrefix); const client = { name: clientInfo.name, version: clientInfo.version, @@ -216,7 +218,7 @@ export class CDPRelayServer { if (!this._extensionConnection) throw new Error('Extension not connected'); return this._extensionConnection.send(method as keyof ExtensionCommandV2, params); - }); + }, this._connectPagePrefix); } private _closePlaywrightConnection(reason: string) { diff --git a/src/extension/cdpRelayV2.ts b/src/extension/cdpRelayV2.ts index 649581ec..bf812a50 100644 --- a/src/extension/cdpRelayV2.ts +++ b/src/extension/cdpRelayV2.ts @@ -25,8 +25,8 @@ export class ExtensionProtocolV2 { private _model: BrowserModel; private _ready = new ManualPromise(); - constructor(sendCommand: SendCommand) { - this._model = new BrowserModel(sendCommand); + constructor(sendCommand: SendCommand, connectPagePrefix?: string) { + this._model = new BrowserModel(sendCommand, connectPagePrefix); void this._ready.catch(logUnhandledError); } diff --git a/tests/extension-protocol.test.ts b/tests/extension-protocol.test.ts index b1b189d9..c678061d 100644 --- a/tests/extension-protocol.test.ts +++ b/tests/extension-protocol.test.ts @@ -126,24 +126,31 @@ describe('extension protocol v2', () => { .rejects.toThrow('attach failed'); }); - it('removes the token-bypass connect page after creating a target', async () => { + it('best-effort removes only this relay\'s token-bypass connect page', async () => { + const connectPage = new URL(`chrome-extension://${EXTENSION_ID}/connect.html`); + connectPage.searchParams.set('mcpRelayUrl', 'ws://127.0.0.1/current'); + const connectPagePrefix = connectPage.toString(); const sendCommand = vi.fn(async (method: string, params: any[]) => { if (method === 'chrome.tabs.create') return { id: 8, url: params[0].url }; if (method === 'chrome.debugger.sendCommand' && params[1] === 'Target.getTargetInfo') return { targetInfo: { targetId: `target-${params[0].tabId}`, type: 'page' } }; + if (method === 'chrome.tabs.remove') + throw new Error('tab already closed'); return {}; }); - const handler = new ExtensionProtocolV2(sendCommand); - handler.handleExtensionEvent('chrome.tabs.onCreated', [{ - id: 7, - url: `chrome-extension://${EXTENSION_ID}/connect.html?mcpRelayUrl=ws%3A%2F%2F127.0.0.1`, - }]); + const handler = new ExtensionProtocolV2(sendCommand, connectPagePrefix); + handler.handleExtensionEvent('chrome.tabs.onCreated', [ + { id: 7, url: `${connectPagePrefix}&client=current` }, + { id: 9, url: `chrome-extension://${EXTENSION_ID}/connect.html?mcpRelayUrl=ws%3A%2F%2F127.0.0.1%2Fother` }, + ]); await handler.handleCDPCommand('Target.setAutoAttach', { autoAttach: true }, undefined); - await handler.handleCDPCommand('Target.createTarget', {}, undefined); + await expect(handler.handleCDPCommand('Target.createTarget', {}, undefined)) + .resolves.toEqual({ result: { targetId: 'target-8' } }); expect(sendCommand).toHaveBeenCalledWith('chrome.tabs.remove', [7]); + expect(sendCommand).not.toHaveBeenCalledWith('chrome.tabs.remove', [9]); }); it('serializes concurrent auto-attach state changes', async () => { From 5a6f6134a0d8af8a00ba832ab939bf6f343235c7 Mon Sep 17 00:00:00 2001 From: JustasMonkev Date: Sun, 19 Jul 2026 13:26:51 +0300 Subject: [PATCH 3/3] fix: preserve navigated extension seed tab --- src/extension/browserModel.ts | 13 ++++++++++--- tests/extension-protocol.test.ts | 11 ++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/extension/browserModel.ts b/src/extension/browserModel.ts index abafe250..253bd50f 100644 --- a/src/extension/browserModel.ts +++ b/src/extension/browserModel.ts @@ -115,9 +115,16 @@ export class BrowserModel { throw new Error('Failed to create tab'); this._knownTabs.set(tab.id, tab); const tabSession = await this._attachTab(tab.id); - await Promise.allSettled([...this._knownTabs.values()] - .filter(knownTab => knownTab.id !== tab.id && this._connectPagePrefix && knownTab.url?.startsWith(this._connectPagePrefix)) - .map(knownTab => this._sendToExtension('chrome.tabs.remove', [knownTab.id]))); + if (this._connectPagePrefix) { + const connectPagePrefix = this._connectPagePrefix; + await Promise.allSettled([...this._knownTabs] + .filter(([tabId, knownTab]) => tabId !== tab.id && knownTab.url?.startsWith(connectPagePrefix)) + .map(async ([tabId]) => { + const result = await this._sendDebuggerCommand({ tabId }, 'Target.getTargetInfo', undefined); + if (result?.targetInfo?.url?.startsWith(connectPagePrefix)) + await this._sendToExtension('chrome.tabs.remove', [tabId]); + })); + } return { targetId: tabSession.targetInfo?.targetId }; } diff --git a/tests/extension-protocol.test.ts b/tests/extension-protocol.test.ts index c678061d..de8dfb92 100644 --- a/tests/extension-protocol.test.ts +++ b/tests/extension-protocol.test.ts @@ -126,15 +126,18 @@ describe('extension protocol v2', () => { .rejects.toThrow('attach failed'); }); - it('best-effort removes only this relay\'s token-bypass connect page', async () => { + it('best-effort removes only this relay\'s seed while it is still on the connect page', async () => { const connectPage = new URL(`chrome-extension://${EXTENSION_ID}/connect.html`); connectPage.searchParams.set('mcpRelayUrl', 'ws://127.0.0.1/current'); const connectPagePrefix = connectPage.toString(); const sendCommand = vi.fn(async (method: string, params: any[]) => { if (method === 'chrome.tabs.create') return { id: 8, url: params[0].url }; - if (method === 'chrome.debugger.sendCommand' && params[1] === 'Target.getTargetInfo') - return { targetInfo: { targetId: `target-${params[0].tabId}`, type: 'page' } }; + if (method === 'chrome.debugger.sendCommand' && params[1] === 'Target.getTargetInfo') { + const tabId = params[0].tabId; + const url = tabId === 7 ? `${connectPagePrefix}&client=current` : tabId === 10 ? 'https://example.com' : undefined; + return { targetInfo: { targetId: `target-${tabId}`, type: 'page', url } }; + } if (method === 'chrome.tabs.remove') throw new Error('tab already closed'); return {}; @@ -143,6 +146,7 @@ describe('extension protocol v2', () => { handler.handleExtensionEvent('chrome.tabs.onCreated', [ { id: 7, url: `${connectPagePrefix}&client=current` }, { id: 9, url: `chrome-extension://${EXTENSION_ID}/connect.html?mcpRelayUrl=ws%3A%2F%2F127.0.0.1%2Fother` }, + { id: 10, url: `${connectPagePrefix}&client=current` }, ]); await handler.handleCDPCommand('Target.setAutoAttach', { autoAttach: true }, undefined); @@ -151,6 +155,7 @@ describe('extension protocol v2', () => { expect(sendCommand).toHaveBeenCalledWith('chrome.tabs.remove', [7]); expect(sendCommand).not.toHaveBeenCalledWith('chrome.tabs.remove', [9]); + expect(sendCommand).not.toHaveBeenCalledWith('chrome.tabs.remove', [10]); }); it('serializes concurrent auto-attach state changes', async () => {