Skip to content

An agentic browser harness: an AI side panel & orb in your own logged-in Chrome, Edge, Arc, Brave or even Firefox run by the agent/subscription you already use (Claude Code, Codex, Antigravity). Free, open source, no API key. Also an MCP server.

fix(daemon): the wake-up registers with the Windows browsers that are installed, through the key each one reads, with no console window from the Bridge, and its launcher survives a home folder like C:\Users\José

Shahriar6880e5b76dda
diff --git a/src/daemon/native-host.test.ts b/src/daemon/native-host.test.ts
index cfad89f..21ea450 100644
--- a/src/daemon/native-host.test.ts
+++ b/src/daemon/native-host.test.ts
@@ -17,6 +17,7 @@ import {
registeredBrowsers,
removeNativeHost,
unpackedExtensionOrigin,
+ type Registry,
} from './native-host';
import { extensionDir } from './paths';
import { bits, modeOf } from './test/modes';
@@ -60,8 +61,25 @@ describe('hostTargets', () => {
expect(mac?.manifestDir).toBe('/Users/me/Library/Application Support/Microsoft Edge/NativeMessagingHosts');
const linux = hostTargets('linux', '/home/me').find((target) => target.browser === 'Firefox');
expect(linux).toMatchObject({ family: 'firefox', manifestDir: '/home/me/.mozilla/native-messaging-hosts' });
- const windows = hostTargets('win32').find((target) => target.browser === 'Chrome');
- expect(windows?.registryKey).toBe(`HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${NATIVE_HOST_NAME}`);
+ const windows = hostTargets('win32', 'C:\\Users\\me', {}).find((target) => target.browser === 'Chrome');
+ expect(windows).toMatchObject({
+ home: 'C:\\Users\\me\\AppData\\Local\\Google\\Chrome\\User Data',
+ registryKey: `HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\${NATIVE_HOST_NAME}`,
+ });
+ });
+
+ it('lets Brave and Vivaldi in through the key Chrome reads, which every Chromium build falls back to', () => {
+ const keys = Object.fromEntries(hostTargets('win32', 'C:\\Users\\me', {}).map((target) => [target.browser, target.registryKey]));
+ expect(keys.Brave).toBe(keys.Chrome);
+ expect(keys.Vivaldi).toBe(keys.Chrome);
+ expect(keys.Edge).toBe(`HKCU\\Software\\Microsoft\\Edge\\NativeMessagingHosts\\${NATIVE_HOST_NAME}`);
+ });
+
+ it('looks for Windows profiles where AppData really is', () => {
+ const env = { LOCALAPPDATA: 'D:\\Local', APPDATA: 'D:\\Roaming' };
+ const homes = Object.fromEntries(hostTargets('win32', 'C:\\Users\\me', env).map((target) => [target.browser, target.home]));
+ expect(homes.Edge).toBe('D:\\Local\\Microsoft\\Edge\\User Data');
+ expect(homes.Firefox).toBe('D:\\Roaming\\Mozilla\\Firefox');
});
});
@@ -103,6 +121,11 @@ describe('launcherScript', () => {
expect(script).toContain('"C:\\node.exe" "C:\\b\\cli.js" native-host %*');
expect(script).toContain('call npx --yes browsentic native-host %*');
});
+
+ it('switches cmd.exe to UTF-8 before the first path, so a name like José survives', () => {
+ const lines = launcherScript({ platform: 'win32', cliPath: 'C:\\Users\\José\\cli.js', nodePath: 'C:\\node.exe', pathEnv: 'C:\\bin' }).split('\r\n');
+ expect(lines.slice(0, 2)).toEqual(['@echo off', 'chcp 65001 >nul']);
+ });
});
describe('the message frame', () => {
@@ -136,17 +159,18 @@ describe('answering the browser', () => {
});
describe('registering the host', () => {
+ const linux = { platform: 'linux' } as const;
const chromeHome = join(homedir(), '.config', 'google-chrome');
const manifest = join(chromeHome, 'NativeMessagingHosts', `${NATIVE_HOST_NAME}.json`);
beforeEach(() => {
- removeNativeHost('linux');
+ removeNativeHost(linux);
rmSync(chromeHome, { recursive: true, force: true });
mkdirSync(chromeHome, { recursive: true });
});
it('registers with the browsers that are installed, letting in both store copies and the unpacked one', () => {
- const installed = installNativeHost('/opt/browsentic/dist/cli.js', 'linux');
+ const installed = installNativeHost('/opt/browsentic/dist/cli.js', linux);
expect(installed.browsers).toEqual(['Chrome']);
expect(modeOf(installed.launcher)).toBe(bits(0o755));
@@ -158,16 +182,64 @@ describe('registering the host', () => {
'chrome-extension://npmocgldfflonjjmdadmdefpnfagnjmp/',
].sort(),
});
- expect(registeredBrowsers('linux')).toEqual(['Chrome']);
+ expect(registeredBrowsers(linux)).toEqual(['Chrome']);
});
it('removes every trace on uninstall', () => {
- installNativeHost('/opt/browsentic/dist/cli.js', 'linux');
+ installNativeHost('/opt/browsentic/dist/cli.js', linux);
- removeNativeHost('linux');
+ removeNativeHost(linux);
expect(existsSync(manifest)).toBe(false);
expect(existsSync(nativeHostDir)).toBe(false);
- expect(registeredBrowsers('linux')).toEqual([]);
+ expect(registeredBrowsers(linux)).toEqual([]);
+ });
+});
+
+describe('registering the host on Windows', () => {
+ const home = 'C:\\Users\\me';
+ const profile = (dir: string) => `C:\\Users\\me\\AppData\\Local\\${dir}\\User Data`;
+ const key = (vendor: string) => `HKCU\\Software\\${vendor}\\NativeMessagingHosts\\${NATIVE_HOST_NAME}`;
+ let keys: Map<string, string>;
+ const registry: Registry = {
+ set: (path, value) => void keys.set(path, value),
+ has: (path) => keys.has(path),
+ remove: (path) => {
+ if (!keys.delete(path)) throw new Error(`no key ${path}`);
+ },
+ };
+ const windows = (installed: string[]) => ({ platform: 'win32', home, env: {}, registry, exists: (path: string) => installed.includes(path) }) as const;
+
+ beforeEach(() => {
+ keys = new Map();
+ removeNativeHost(windows([]));
+ });
+
+ it('registers only with the browsers that are installed, as macOS and Linux do', () => {
+ const machine = windows([profile('Microsoft\\Edge'), profile('BraveSoftware\\Brave-Browser')]);
+
+ const installed = installNativeHost('C:\\b\\dist\\cli.js', machine);
+
+ expect(installed.browsers).toEqual(['Edge', 'Brave']);
+ expect([...keys.keys()].sort()).toEqual([key('Google\\Chrome'), key('Microsoft\\Edge')]);
+ expect(JSON.parse(readFileSync(keys.get(key('Google\\Chrome'))!, 'utf8'))).toMatchObject({ path: installed.launcher });
+ expect(registeredBrowsers(machine)).toEqual(['Edge', 'Brave']);
+ });
+
+ it('says a browser installed since setup cannot start the daemon yet', () => {
+ installNativeHost('C:\\b\\dist\\cli.js', windows([profile('Microsoft\\Edge')]));
+
+ expect(registeredBrowsers(windows([profile('Microsoft\\Edge'), profile('Google\\Chrome')]))).toEqual(['Edge']);
+ });
+
+ it('removes every key on uninstall, the one written for Brave before it was known to read Chrome’s too', () => {
+ installNativeHost('C:\\b\\dist\\cli.js', windows([profile('Google\\Chrome')]));
+ keys.set(key('BraveSoftware\\Brave-Browser'), 'old');
+
+ const removed = removeNativeHost(windows([]));
+
+ expect(keys.size).toBe(0);
+ expect(removed).toEqual(expect.arrayContaining([key('Google\\Chrome'), key('BraveSoftware\\Brave-Browser'), nativeHostDir]));
+ expect(registeredBrowsers(windows([profile('Google\\Chrome')]))).toEqual([]);
});
});
diff --git a/src/daemon/native-host.ts b/src/daemon/native-host.ts
index 0558aed..3e2195c 100644
--- a/src/daemon/native-host.ts
+++ b/src/daemon/native-host.ts
@@ -12,7 +12,7 @@ import { FIREFOX_ADDON_ID, STORE_EXTENSION_IDS } from '@/lib/stores';
import { createHash } from 'node:crypto';
import { chmodSync, existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { homedir } from 'node:os';
-import { join, posix } from 'node:path';
+import { join, posix, win32 } from 'node:path';
import { listSessions } from './auth-store';
import { readAgentConfig } from './agent/config';
import { stateDir } from './lockfile';
@@ -54,15 +54,26 @@ const LINUX_BROWSERS: [string, Family, string, string][] = [
['Firefox', 'firefox', '.mozilla', '.mozilla/native-messaging-hosts'],
];
-const WINDOWS_BROWSERS: [string, Family, string][] = [
- ['Chrome', 'chromium', 'Google\\Chrome'],
- ['Chromium', 'chromium', 'Chromium'],
- ['Edge', 'chromium', 'Microsoft\\Edge'],
- ['Brave', 'chromium', 'BraveSoftware\\Brave-Browser'],
- ['Firefox', 'firefox', 'Mozilla'],
+/**
+ * Every Chromium build falls back to Chrome's key, and only Chromium's own reads another first
+ * (launch_context_win.cc), so Brave, Vivaldi and Chrome for Testing are let in through Chrome's.
+ */
+const WINDOWS_BROWSERS: [string, Family, string, 'LOCALAPPDATA' | 'APPDATA', string][] = [
+ ['Chrome', 'chromium', 'Google\\Chrome', 'LOCALAPPDATA', 'Google\\Chrome\\User Data'],
+ ['Chrome for Testing', 'chromium', 'Google\\Chrome', 'LOCALAPPDATA', 'Google\\Chrome for Testing\\User Data'],
+ ['Chromium', 'chromium', 'Chromium', 'LOCALAPPDATA', 'Chromium\\User Data'],
+ ['Edge', 'chromium', 'Microsoft\\Edge', 'LOCALAPPDATA', 'Microsoft\\Edge\\User Data'],
+ ['Brave', 'chromium', 'Google\\Chrome', 'LOCALAPPDATA', 'BraveSoftware\\Brave-Browser\\User Data'],
+ ['Vivaldi', 'chromium', 'Google\\Chrome', 'LOCALAPPDATA', 'Vivaldi\\User Data'],
+ ['Firefox', 'firefox', 'Mozilla', 'APPDATA', 'Mozilla\\Firefox'],
];
-export function hostTargets(platform: NodeJS.Platform = process.platform, home = homedir()): HostTarget[] {
+/** Written for Brave up to 0.8.1, which never read it; an uninstall still takes it away. */
+const RETIRED_WINDOWS_VENDORS = ['BraveSoftware\\Brave-Browser'];
+
+const windowsKey = (vendor: string) => `HKCU\\Software\\${vendor}\\NativeMessagingHosts\\${NATIVE_HOST_NAME}`;
+
+export function hostTargets(platform: NodeJS.Platform = process.platform, home = homedir(), env = process.env): HostTarget[] {
if (platform === 'darwin') {
const support = posix.join(home, 'Library', 'Application Support');
return MAC_BROWSERS.map(([browser, family, dir]) => ({
@@ -73,11 +84,15 @@ export function hostTargets(platform: NodeJS.Platform = process.platform, home =
}));
}
if (platform === 'win32') {
- return WINDOWS_BROWSERS.map(([browser, family, vendor]) => ({
+ const roots = {
+ LOCALAPPDATA: env.LOCALAPPDATA ?? win32.join(home, 'AppData', 'Local'),
+ APPDATA: env.APPDATA ?? win32.join(home, 'AppData', 'Roaming'),
+ };
+ return WINDOWS_BROWSERS.map(([browser, family, vendor, root, profile]) => ({
browser,
family,
- home: '',
- registryKey: `HKCU\\Software\\${vendor}\\NativeMessagingHosts\\${NATIVE_HOST_NAME}`,
+ home: win32.join(roots[root], profile),
+ registryKey: windowsKey(vendor),
}));
}
return LINUX_BROWSERS.map(([browser, family, dir, manifests]) => ({
@@ -132,8 +147,10 @@ export function launcherScript(options: {
}): string {
const { platform, cliPath, nodePath, pathEnv } = options;
if (platform === 'win32') {
+ // cmd.exe reads each line in the console's code page, which mangles a UTF-8 path like C:\Users\José.
return [
'@echo off',
+ 'chcp 65001 >nul',
`set "PATH=${pathEnv}"`,
`if exist "${cliPath}" (`,
` "${nodePath}" "${cliPath}" native-host %*`,
@@ -165,19 +182,64 @@ export interface HostInstall {
browsers: string[];
}
-function register(targets: HostTarget[], launcher: string, origins: readonly string[]): string[] {
+/** The keys Windows browsers read. A test hands in its own. */
+export interface Registry {
+ set(key: string, value: string): void;
+ has(key: string): boolean;
+ /** Throws when the key is not there. */
+ remove(key: string): void;
+}
+
+// The daemon re-registers on every pairing, and it has no console: each reg.exe would get a window of its own.
+const reg = (args: string[]) => execFileSync('reg', args, { stdio: 'ignore', windowsHide: true });
+
+export const windowsRegistry: Registry = {
+ set: (key, value) => reg(['add', key, '/ve', '/t', 'REG_SZ', '/d', value, '/f']),
+ has: (key) => {
+ try {
+ reg(['query', key, '/ve']);
+ return true;
+ } catch {
+ return false;
+ }
+ },
+ remove: (key) => reg(['delete', key, '/f']),
+};
+
+export interface HostOptions {
+ platform?: NodeJS.Platform;
+ home?: string;
+ env?: NodeJS.ProcessEnv;
+ /** Whether a browser's profile is there. */
+ exists?: (path: string) => boolean;
+ registry?: Registry;
+}
+
+const machine = ({
+ platform = process.platform,
+ home = homedir(),
+ env = process.env,
+ exists = existsSync,
+ registry = windowsRegistry,
+}: HostOptions) => ({ platform, targets: hostTargets(platform, home, env), exists, registry });
+
+function register(launcher: string, options: HostOptions): string[] {
+ const { platform, targets, exists, registry } = machine(options);
+ const origins = allowedOrigins(platform);
const registered: string[] = [];
- for (const target of targets) {
+ const written = new Set<string>();
+ for (const target of targets.filter((candidate) => exists(candidate.home))) {
const manifest = `${JSON.stringify(hostManifest(target.family, launcher, origins), null, 2)}\n`;
try {
- if (target.registryKey) {
+ if (target.registryKey && !written.has(target.registryKey)) {
const file = join(nativeHostDir, `${target.family}.json`);
writeFileSync(file, manifest);
- execFileSync('reg', ['add', target.registryKey, '/ve', '/t', 'REG_SZ', '/d', file, '/f'], { stdio: 'ignore' });
- } else if (target.manifestDir && existsSync(target.home)) {
+ registry.set(target.registryKey, file);
+ written.add(target.registryKey);
+ } else if (target.manifestDir) {
mkdirSync(target.manifestDir, { recursive: true });
writeFileSync(join(target.manifestDir, `${NATIVE_HOST_NAME}.json`), manifest);
- } else continue;
+ }
registered.push(target.browser);
} catch (error) {
log(`native host: could not register for ${target.browser}: ${String(error)}`);
@@ -186,38 +248,41 @@ function register(targets: HostTarget[], launcher: string, origins: readonly str
return registered;
}
-export function installNativeHost(cliPath: string, platform: NodeJS.Platform = process.platform): HostInstall {
+export function installNativeHost(cliPath: string, options: HostOptions = {}): HostInstall {
+ const { platform = process.platform } = options;
const launcher = launcherPath(platform);
mkdirSync(nativeHostDir, { recursive: true, mode: 0o700 });
writeFileSync(launcher, launcherScript({ platform, cliPath, nodePath: process.execPath, pathEnv: process.env.PATH ?? '' }));
if (platform !== 'win32') chmodSync(launcher, 0o755);
- return { launcher, browsers: register(hostTargets(platform), launcher, allowedOrigins(platform)) };
+ return { launcher, browsers: register(launcher, options) };
}
/** A newly paired browser has to be let in; the launcher stays as setup wrote it. */
-export function refreshNativeHost(platform: NodeJS.Platform = process.platform): void {
- const launcher = launcherPath(platform);
+export function refreshNativeHost(options: HostOptions = {}): void {
+ const launcher = launcherPath(options.platform);
if (!existsSync(launcher)) return;
- register(hostTargets(platform), launcher, allowedOrigins(platform));
+ register(launcher, options);
}
-export function removeNativeHost(platform: NodeJS.Platform = process.platform): string[] {
+export function removeNativeHost(options: HostOptions = {}): string[] {
+ const { platform, targets, registry } = machine(options);
const removed: string[] = [];
- for (const target of hostTargets(platform)) {
- try {
- if (target.registryKey) {
- execFileSync('reg', ['delete', target.registryKey, '/f'], { stdio: 'ignore' });
- removed.push(target.registryKey);
+ if (platform === 'win32') {
+ for (const key of new Set([...targets.map((target) => target.registryKey!), ...RETIRED_WINDOWS_VENDORS.map(windowsKey)])) {
+ try {
+ registry.remove(key);
+ removed.push(key);
+ } catch {
continue;
}
- const manifest = join(target.manifestDir!, `${NATIVE_HOST_NAME}.json`);
- if (!existsSync(manifest)) continue;
- rmSync(manifest, { force: true });
- removed.push(manifest);
- } catch {
- continue;
}
}
+ for (const target of targets.filter((candidate) => candidate.manifestDir)) {
+ const manifest = join(target.manifestDir!, `${NATIVE_HOST_NAME}.json`);
+ if (!existsSync(manifest)) continue;
+ rmSync(manifest, { force: true });
+ removed.push(manifest);
+ }
if (existsSync(nativeHostDir)) {
rmSync(nativeHostDir, { recursive: true, force: true });
removed.push(nativeHostDir);
@@ -225,13 +290,19 @@ export function removeNativeHost(platform: NodeJS.Platform = process.platform):
return removed;
}
-export function registeredBrowsers(platform: NodeJS.Platform = process.platform): string[] {
+/** The installed browsers that can start the daemon, read back from where each one looks. */
+export function registeredBrowsers(options: HostOptions = {}): string[] {
+ const { platform, targets, exists, registry } = machine(options);
if (!existsSync(launcherPath(platform))) return [];
- return hostTargets(platform)
+ const keys = new Map<string, boolean>();
+ const hasKey = (key: string) => {
+ if (!keys.has(key)) keys.set(key, registry.has(key));
+ return keys.get(key)!;
+ };
+ return targets
+ .filter((target) => exists(target.home))
.filter((target) =>
- target.registryKey
- ? existsSync(join(nativeHostDir, `${target.family}.json`))
- : existsSync(join(target.manifestDir!, `${NATIVE_HOST_NAME}.json`)),
+ target.registryKey ? hasKey(target.registryKey) : existsSync(join(target.manifestDir!, `${NATIVE_HOST_NAME}.json`)),
)
.map((target) => target.browser);
}