From b49908690fded377fa16d933b6acdd308147c395 Mon Sep 17 00:00:00 2001 From: AyushRajgor Date: Wed, 2 Sep 2026 18:38:43 +0530 Subject: [PATCH] fix(browse): stop faking chrome.runtime.connect/sendMessage in Layer C stealth Real Chrome exposes callable chrome.runtime.connect / sendMessage to a web page only when an installed extension lists that origin in externally_connectable. On an ordinary page they are undefined. Layer C injected stubs that throw "No matching signature" for any call, a shape real Chrome never produces. Sites that probe chrome.runtime.sendMessage to detect their own companion extension concluded it was installed and routed auth through the extension flow, which fails and loops back to login. Keep the enum shape (OnInstalledReason, PlatformOs, ...) that presence checks look at; drop only the two callable stubs. Update the Layer C static tests and the Chromium runtime test accordingly. --- browse/src/stealth.ts | 14 ++++++++------ browse/test/stealth-layer-c.test.ts | 20 +++++++++++++++----- browse/test/stealth-webdriver.test.ts | 19 ++++++++++++------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/browse/src/stealth.ts b/browse/src/stealth.ts index fc3c8a502cd..7456469685c 100644 --- a/browse/src/stealth.ts +++ b/browse/src/stealth.ts @@ -137,12 +137,14 @@ export function buildStealthScript(hw: HostProfile): string { MAC: 'mac', OPENBSD: 'openbsd', WIN: 'win' }, RequestUpdateCheckStatus: { NO_UPDATE: 'no_update', THROTTLED: 'throttled', UPDATE_AVAILABLE: 'update_available' }, - connect: markNative(function connect() { - throw new TypeError('Error in invocation of runtime.connect: No matching signature.'); - }, 'connect'), - sendMessage: markNative(function sendMessage() { - throw new TypeError('Error in invocation of runtime.sendMessage: No matching signature.'); - }, 'sendMessage'), + // No connect/sendMessage here. Real Chrome only exposes callable + // chrome.runtime methods to a web page when an installed extension + // lists that origin in externally_connectable; on an ordinary page + // they are undefined. Faking them is itself an inconsistency, and + // sites that probe chrome.runtime.sendMessage to detect their own + // companion extension misfire (they assume the extension is + // installed and route auth through it). The enum shape alone + // satisfies the presence checks detectors actually run. id: undefined, }; } diff --git a/browse/test/stealth-layer-c.test.ts b/browse/test/stealth-layer-c.test.ts index fbc5fed3798..a026edc09b4 100644 --- a/browse/test/stealth-layer-c.test.ts +++ b/browse/test/stealth-layer-c.test.ts @@ -62,9 +62,19 @@ describe('buildStealthScript — T3 Layer C', () => { expect(s).toContain('PlatformArch'); expect(s).toContain('PlatformOs'); expect(s).toContain('RequestUpdateCheckStatus'); - // sendMessage / connect must throw native-shaped errors - expect(s).toContain('runtime.connect'); - expect(s).toContain('runtime.sendMessage'); + }); + + test('window.chrome.runtime does NOT fake connect / sendMessage', () => { + // Real Chrome exposes callable runtime.connect / runtime.sendMessage to a + // web page only when an installed extension lists that origin in + // externally_connectable. On an ordinary page they are undefined, and + // sites probe `chrome.runtime.sendMessage` to detect their own companion + // extension — a fake makes them think it is installed. + const s = buildStealthScript(hw); + expect(s).not.toMatch(/\bconnect:\s*markNative/); + expect(s).not.toMatch(/\bsendMessage:\s*markNative/); + expect(s).not.toContain('runtime.sendMessage: No matching signature'); + expect(s).not.toContain('runtime.connect: No matching signature'); }); test('chrome.csi and chrome.loadTimes provide method bodies', () => { @@ -106,9 +116,9 @@ describe('buildStealthScript — T3 Layer C', () => { // Every getter (hardwareConcurrency, deviceMemory, webdriver, Notification.permission) // should be wrapped through markNative so the toString Proxy covers it. const markNativeMatches = s.match(/markNative\(/g) || []; - // At least 8 markNative wrappings (webdriver, csi, loadTimes, connect, sendMessage, + // At least 6 markNative wrappings (webdriver, csi, loadTimes, // notification permission, hwConcurrency, deviceMemory) - expect(markNativeMatches.length).toBeGreaterThanOrEqual(7); + expect(markNativeMatches.length).toBeGreaterThanOrEqual(6); }); test('script does not include "GStackBrowser" branding string', () => { diff --git a/browse/test/stealth-webdriver.test.ts b/browse/test/stealth-webdriver.test.ts index b5365ea80ed..0aa1863e94c 100644 --- a/browse/test/stealth-webdriver.test.ts +++ b/browse/test/stealth-webdriver.test.ts @@ -170,25 +170,30 @@ describe('applyStealth — context level', () => { } }); - test('chrome.csi() and chrome.loadTimes() execute, runtime.connect() throws native-shaped', async () => { + test('chrome.csi() and chrome.loadTimes() execute; runtime.connect / sendMessage are absent', async () => { // Presence (typeof === 'function') is not enough — a real detector calls - // them. loadTimes() dereferences performance.timing; connect() must throw - // the native "No matching signature" TypeError. + // them. loadTimes() dereferences performance.timing. runtime.connect and + // runtime.sendMessage must NOT exist: real Chrome only exposes them to a + // page when an installed extension declares that origin in + // externally_connectable, and sites use `chrome.runtime.sendMessage` to + // detect their own companion extension. const page = await context.newPage(); try { const r = await page.evaluate(() => { const c = (window as any).chrome; - let connectErr = ''; - try { c.runtime.connect(); } catch (e) { connectErr = String(e); } return { csiOk: typeof c.csi().onloadT === 'number', loadTimesOk: typeof c.loadTimes().wasFetchedViaSpdy === 'boolean', - connectErr, + runtimePresent: typeof c.runtime === 'object', + connectType: typeof c.runtime.connect, + sendMessageType: typeof c.runtime.sendMessage, }; }); expect(r.csiOk).toBe(true); expect(r.loadTimesOk).toBe(true); - expect(r.connectErr).toContain('No matching signature'); + expect(r.runtimePresent).toBe(true); + expect(r.connectType).toBe('undefined'); + expect(r.sendMessageType).toBe('undefined'); } finally { await page.close(); }