barebrowse 0.23.0 → 0.24.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,71 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.24.1] - 2026-10-25
4
+
5
+ ### Fixed
6
+
7
+ - **Browsers are no longer orphaned when the launching process is hard-killed.**
8
+ `kill -9`, a process-group SIGKILL or a crash used to leave Chromium or
9
+ Firefox running forever. Chromium is now tethered to its launcher through
10
+ `--remote-debugging-pipe` (all launches), so it exits when the launcher dies.
11
+ Firefox is tethered with `setpriv --pdeathsig` on Linux when `setpriv` is
12
+ installed. Where there is no `setpriv`, on non-Linux systems, or with Snap
13
+ Firefox (a setuid wrapper defeats the tether), a hard kill can still orphan
14
+ the browser.
15
+ - **Stale temporary directories of dead launchers are cleaned up.** At the next
16
+ launch, leftover `barebrowse-*` profile and download directories whose
17
+ launcher is gone are removed, in both `/tmp` and the system temp directory. A
18
+ directory whose browser lock is still live is kept. Profile and download
19
+ directory names now carry the launcher's pid to make this possible. A reused
20
+ pid or a changed hostname keeps a directory instead of removing it.
21
+
22
+ ## [0.24.0] - 2026-10-23
23
+
24
+ ### Added
25
+
26
+ - **Switch browser engine mid-session.** The MCP server has a new
27
+ `switch_engine` tool (`chromium` or `firefox`), so an agent no longer needs a
28
+ server restart to try the other engine. The page, tabs and refs are reset
29
+ (call `goto` again); logins from your real browser are re-injected on the next
30
+ `goto`. Expect about 2s to Chromium and 4-5s to Firefox. Switching to the
31
+ active engine is a no-op.
32
+ - **`barebrowse switch-engine <chromium|firefox>` for CLI sessions.** Swaps the
33
+ daemon's browser in place. The new browser is connected first, so a failed
34
+ launch leaves the old one running. Refused when the session was opened with
35
+ `--port` (attached to your own browser). Console and network capture are
36
+ re-attached to the new browser.
37
+ - **MCP `capabilities` reports the last cookie injection.** It now returns a
38
+ `cookies` field with the URL, how many cookies were injected, and each as
39
+ `name @ domain/path`, plus any read error, so an agent can tell "logged out"
40
+ from "no cookie found" from "the cookie read failed". Names, domains and paths
41
+ only, never values.
42
+
43
+ ### Fixed
44
+
45
+ - **Same-name cookies on different paths are no longer dropped.** The default
46
+ merge of Chromium and Firefox cookie jars keyed cookies by name and domain, so
47
+ two cookies differing only by path collapsed into one. They are now keyed by
48
+ name, domain and path.
49
+ - **Partitioned (CHIPS) Firefox cookies are skipped.** A partitioned cookie
50
+ sharing a name, domain and path with the top-level cookie could replace it in
51
+ the injected set, so a site saw the embed's value instead of your login
52
+ cookie. Only top-level cookies are injected.
53
+ - **A non-numeric `wait_for` timeout no longer resets the page.** A value like
54
+ `"abc"` made the deadline invalid, which fired after about 1ms and wiped the
55
+ page. Numeric strings are now accepted, and anything not a positive number
56
+ falls back to the 30s default.
57
+ - **A browser that fails to start no longer leaks.** When Chromium or Firefox
58
+ timed out, exited early or failed to spawn, the process and its temporary
59
+ profile directory were left behind forever (and could hang a test run). Both
60
+ are now killed and removed before the error is raised.
61
+ - **Chromium cleanup kills helper processes even after the main process
62
+ exited.** Helpers sharing its process group used to survive and keep the
63
+ deleted profile directory open.
64
+
65
+ ### Docs
66
+
67
+ - The CLI skill docs now mention the 7-day output cleanup at `barebrowse open`.
68
+
3
69
  ## [0.23.0] - 2026-10-14
4
70
 
5
71
  ### Added
package/README.md CHANGED
@@ -96,10 +96,12 @@ Or manually add to your config (`claude_desktop_config.json`, `.cursor/mcp.json`
96
96
  }
97
97
  ```
98
98
 
99
- MCP tools: `browse`, `goto`, `snapshot`, `readable`, `click`, `type`, `press`, `scroll`, `hover`, `select`, `back`, `forward`, `reload`, `drag`, `upload`, `pdf`, `screenshot`, `wait_for`, `tabs`, `capabilities` (reports the active engine — chromium or firefox — and what the session can do; no side effects). Plus `assess` (privacy scan) if [wearehere](https://github.com/hamr0/wearehere) is installed. Plus opt-in `eval` (`BAREBROWSE_MCP_EVAL=1`) — runs JS in the authenticated session, off by default because it can read cookies/localStorage. Session runs in hybrid mode with automatic cookie injection. Per-tool timeouts (goto/reload/wait_for 60s, back/forward 30s, interactive ops 15s, pdf/screenshot/upload 45s) with auto-retry on transient failures (idempotent only — mutating tools fail loudly to avoid double-submits).
99
+ MCP tools: `browse`, `goto`, `snapshot`, `readable`, `click`, `type`, `press`, `scroll`, `hover`, `select`, `back`, `forward`, `reload`, `drag`, `upload`, `pdf`, `screenshot`, `wait_for`, `tabs`, `capabilities` (reports the active engine — chromium or firefox — and what the session can do, plus a `cookies` field naming the cookies injected on the last goto — names only, never values; no side effects), `switch_engine` (swap chromium/firefox without restarting; page, tabs and refs reset, so call `goto` after). Plus `assess` (privacy scan) if [wearehere](https://github.com/hamr0/wearehere) is installed. Plus opt-in `eval` (`BAREBROWSE_MCP_EVAL=1`) — runs JS in the authenticated session, off by default because it can read cookies/localStorage. Session runs in hybrid mode with automatic cookie injection. Per-tool timeouts (goto/reload/wait_for 60s, back/forward 30s, interactive ops 15s, pdf/screenshot/upload 45s) with auto-retry on transient failures (idempotent only — mutating tools fail loudly to avoid double-submits).
100
100
 
101
101
  **Idle release.** An MCP server can outlive its conversation, so it closes its browser after `BAREBROWSE_IDLE_MINUTES` (default `15`, `0` = never, blank/invalid = default, fractional allowed, capped at ~24.8 days) with no tool call in flight. The next call opens a fresh browser (a new job, not a resume — a page left open, e.g. a solved captcha in the headed window, is gone). A transient browser error also closes the broken browser rather than orphaning it; an ordinary `wait_for` timeout does not reset the page.
102
102
 
103
+ **Hard-kill safety (Linux).** If the process that launched a browser is `kill -9`'d or crashes, the browser dies with it: Chromium via a `--remote-debugging-pipe` tether, Firefox via `setpriv --pdeathsig` (needs `setpriv` from util-linux; without it, or off Linux, a hard kill still orphans Firefox, as does a Snap Firefox). The dead launcher's leftover `barebrowse-*` temp dirs are cleaned at the next launch. Known limits: a reused pid keeps its dir, and dirs created under a different hostname are never removed.
104
+
103
105
  `browse` and `snapshot` accept `pruneMode: 'act'|'read'`. `act` (default) keeps interactive elements — best for clicking/filling. `read` keeps paragraphs, headings, and long text — best for articles, docs, and content extraction. If act-mode collapses a content-heavy page near-totally, the snapshot includes a `hint: …` line suggesting `pruneMode='read'` so the agent doesn't bail to a separate HTTP fetch.
104
106
 
105
107
  `npx barebrowse doctor` reports the runtime environment first — default engine/mode, installed Chromium/Firefox binaries, cookie sources, and any live session's engine + capabilities — then scans every known MCP config location and flags scope conflicts. `npx barebrowse install --force` overwrites an existing entry pointing at a different endpoint.
@@ -95,7 +95,7 @@ const snapshot = await browse('https://example.com', {
95
95
  | `cdp` | -- | object | Raw CDP session (getter — survives hybrid fallback and switchTab) for escape hatch: `page.cdp.send(method, params)` |
96
96
  | `createTab()` | -- | tab handle | New tab in same browser. Returns `{ goto, botBlocked, injectCookies, waitForNetworkIdle, cdp, close }`. Tab close doesn't affect session. |
97
97
  | `engine` | -- | `'chromium'`\|`'firefox'` | Which engine drives this session. Always readable (both shapes carry it); a typed literal, so it also narrows the union: `if (page.engine === 'firefox') page.bidi.…`. |
98
- | `capabilities` | -- | object | Feature introspection: `{ engine, mode, attach, escapeHatch, reloadIgnoreCache, downloads, stealth, cookieInjection }`. Ask "what can this session do?" without probing for `cdp`/`bidi`. All fields are launch-fixed except `stealth`, which is a live getter reflecting the session's current headed/headless state (it can flip mid-session after a hybrid bot-challenge relaunch). `reloadIgnoreCache` is false on Firefox (upstream BiDi gap); `downloads`/`stealth`/`cookieInjection` reflect mode + attach + incognito. |
98
+ | `capabilities` | -- | object | Feature introspection: `{ engine, mode, attach, escapeHatch, reloadIgnoreCache, downloads, stealth, cookieInjection }`. Ask "what can this session do?" without probing for `cdp`/`bidi`. All fields are launch-fixed except `stealth`, which is a live getter reflecting the session's current headed/headless state (it can flip mid-session after a hybrid bot-challenge relaunch). `reloadIgnoreCache` is false on Firefox (upstream BiDi gap); `downloads`/`stealth`/`cookieInjection` reflect mode + attach + incognito. The MCP tool also returns `cookies: { url, injected, cookies: ["name @ domain/path"], error? }` for the last goto (names only, never values; `{ url, injected: 0, incognito: true }` under incognito; `null` before any goto). |
99
99
  | `close()` | -- | void | Close page, disconnect CDP, kill browser (if headless). The tab close is bounded (2.5s), so a dead/hung browser still gets killed and its profile + owned download dir removed. Attach mode never kills your browser |
100
100
 
101
101
  **connect() options** (in addition to mode/port/consent):
@@ -105,7 +105,7 @@ const snapshot = await browse('https://example.com', {
105
105
  - `viewport: '1280x720'` — Set viewport dimensions
106
106
  - `storageState: 'file.json'` — Load cookies/localStorage from saved state
107
107
  - `incognito: true|false` — Default `false`. When `true`, run a clean, unauthenticated session: skips `storageState` loading AND makes `injectCookies()` a no-op, so no auth ever enters the session even though callers (MCP `goto`, daemon) inject unconditionally. The temp profile is already throwaway; this gates the *other* auth source — the user's real browser cookies. Not Chrome's `--incognito` flag. Surfaced everywhere: library, MCP (`browse` tool param + `BAREBROWSE_INCOGNITO=1` env for the session), CLI (`--incognito`), bareagent (via `connect` opts).
108
- - `downloadPath: '/abs/dir'` — Where downloads land. Default: per-session `mkdtemp` under `/tmp/barebrowse-dl-*` that gets removed on `close()`. Caller-supplied paths are not cleaned up — caller owns the lifecycle.
108
+ - `downloadPath: '/abs/dir'` — Where downloads land. Default: per-session `mkdtemp` under `os.tmpdir()` named `barebrowse-dl-<pid>-*` (Firefox: `barebrowse-ff-dl-<pid>-*`) that gets removed on `close()`, or by the next launch's stale-dir sweep if the process was hard-killed. Caller-supplied paths are not cleaned up — caller owns the lifecycle.
109
109
  - `blockAds: true|false` — CDP-level URL blocking of 128 common ad/tracker patterns (Google ads/analytics, FB/Amazon/MS/Adobe ad+analytics, Segment/Amplitude/Mixpanel/Heap/PostHog, Hotjar/FullStory/LogRocket, Criteo/Taboola/Outbrain, the consumer-pixel cluster, AppNexus/Rubicon/PubMatic supply, marketing automation; v0.10.1 added AppsFlyer/Branch/Adjust, Cloudflare Web Analytics, Matomo Cloud). Default `true` for launched browsers, `false` in attach mode (would affect any tab in the user's running browser). Explicit `true` in attach mode is honored and follows the session across `switchTab()` (regression-tested). Shrinks ARIA snapshots and speeds page loads. On legacy Chromium lacking `Network.setBlockedURLs` a one-time `console.warn` surfaces the fallback.
110
110
  - `blockUrls: ['*://foo.com/*', ...]` — Extra glob patterns (CDP `Network.setBlockedURLs` format) to block in addition to the default. Merged with the default unless `blockAds: false`.
111
111
  - `allowLocalUrls: true|false` — (v0.11.0) Default `false`: navigation to local-resource / browser-internal schemes (`file:`, `view-source:`, `chrome:`, `filesystem:`, `devtools:`, …) is **blocked** to stop a prompt-injected agent reading local files. `http`/`https`/`data`/`blob`/`about` are always allowed. Set `true` to permit local schemes.
@@ -206,8 +206,8 @@ barebrowse can inject cookies from the user's real browser sessions, bypassing l
206
206
  | Stealth (headless tells) | v0.9.0 (H4): `Network.setUserAgentOverride` strips "HeadlessChrome" from UA in HTTP headers AND `navigator.userAgent`; JS patches for webdriver, plugins, languages, full `chrome.runtime` enum shape, `Notification` constructor + `permission: 'default'`, `hardwareConcurrency: 8`, `deviceMemory: 8`, WebGL `UNMASKED_VENDOR_WEBGL`/`UNMASKED_RENDERER_WEBGL` spoofed to Intel. v0.10.0: canvas fingerprint noise — `toDataURL`/`getImageData` XOR a per-session `crypto.getRandomValues`-seeded mask into ~1 byte per 64-byte stride (stable within a session, different across sessions; bitmap is restored after encoding so legitimate canvas use is unaffected). | Headless |
207
207
  | Ad / tracker URL blocking | v0.10.0: CDP `Network.setBlockedURLs` with 128 curated patterns (Google/FB/Amazon/MS/Adobe ad+analytics, the major SaaS analytics + session-replay stacks, content-rec, supply-side ad networks, marketing automation). v0.10.1 added long-tail: AppsFlyer/Branch/Adjust, Cloudflare Web Analytics, Matomo Cloud, broader Outbrain (`amplify`/`log`) and PostHog (`/static/array.js`). Default on for launched browsers, off in attach mode. `opts.blockUrls` extends; `opts.blockAds: false` disables. Shrinks ARIA snapshots and speeds loads. v0.10.1: regression-tested across `switchTab()` in attach mode; one-time `console.warn` if Chromium lacks the CDP method. | Launched |
208
208
  | iframe / OOPIF content (Stripe, reCAPTCHA, embedded forms) | v0.9.0 (H2): `Target.setAutoAttach({flatten:true})` registers a CDP session per iframe; `ariaTree()` walks `Page.getFrameTree`, fetches each frame's AX tree on the right session, splices children under iframe placeholders via `DOM.getFrameOwner`. Refs route via `{session, backendNodeId}` so clicks dispatch in the iframe's Input domain. `--site-per-process` launch flag forces every iframe — including same-origin — into OOPIF so coords work. | Both |
209
- | Downloads | v0.9.0 (H7): `Browser.setDownloadBehavior({behavior:'allowAndName', downloadPath, eventsEnabled:true})` + listeners populate `page.downloads`. Files land at `savedPath` (under `--download-path` if supplied, else per-session `/tmp/barebrowse-dl-*`). | Headless + Headed (skipped in attach mode) |
210
- | Profile locking | Unique temp dir per headless instance | Headless |
209
+ | Downloads | v0.9.0 (H7): `Browser.setDownloadBehavior({behavior:'allowAndName', downloadPath, eventsEnabled:true})` + listeners populate `page.downloads`. Files land at `savedPath` (under `--download-path` if supplied, else per-session `barebrowse-dl-<pid>-*` under `os.tmpdir()`). | Headless + Headed (skipped in attach mode) |
210
+ | Profile locking | Unique temp dir per headless instance. A launch that fails (start timeout, early exit, bad endpoint — both engines) SIGKILLs the browser's whole process group and removes that temp dir before rejecting. **Hard-kill safety (Linux):** if the launcher itself is `kill -9`'d or crashes (no exit handler can run), the browser still dies with it: Chromium is spawned with `--remote-debugging-pipe` and stdio fds 3/4 as a parent-death tether (the kernel closes the pipe, Chromium exits with its helpers); Firefox is spawned through `setpriv --pdeathsig SIGKILL` when `setpriv` exists (Linux only; without it, or off Linux, a hard kill still orphans Firefox). The dead launcher's leftover `barebrowse-*` temp dirs (Chromium profile, Firefox profile, download dirs, all named with the owner pid) are removed by the next launch's sweep, which keeps any dir whose owner pid or browser lock is still live. Known limits: a reused pid keeps its dir; a dir whose Chromium lock names a different hostname (renamed machine, container sharing `/tmp`) is kept forever; a Snap Firefox (its setuid wrapper clears pdeathsig) can still be orphaned, though its profile is kept while it runs | Headless |
211
211
  | Shared memory crash (Linux) | `--disable-dev-shm-usage` flag prevents `/dev/shm` exhaustion | Headless |
212
212
  | ARIA noise | 9-step pruning: wrapper collapse, noise removal, landmark promotion | Both |
213
213
 
@@ -262,6 +262,7 @@ barebrowse pdf # → .barebrowse/page-<timestamp>.pdf
262
262
  barebrowse wait-for --text="Success" # Wait for content to appear
263
263
  barebrowse tabs # List open tabs
264
264
  barebrowse save-state # → .barebrowse/state-<timestamp>.json
265
+ barebrowse switch-engine firefox # Swap chromium/firefox in place (page/tabs/refs reset; goto after; not with --port; cookies re-injected for open's URL only)
265
266
  barebrowse close # Kill daemon + browser
266
267
  ```
267
268
 
@@ -293,7 +294,7 @@ barebrowse ships an MCP server for direct use with Claude Desktop, Cursor, or an
293
294
  }
294
295
  ```
295
296
 
296
- 19 core tools: `browse` (one-shot), `goto`, `snapshot`, `readable`, `click`, `type`, `press`, `scroll`, `hover`, `select`, `back`, `forward`, `reload`, `drag`, `upload`, `pdf`, `screenshot`, `wait_for`, `tabs`. Plus `assess` (privacy scan) if `wearehere` is installed (`npm install wearehere`). Plus the **opt-in `eval` tool** gated by `BAREBROWSE_MCP_EVAL=1` (default OFF) — `Runtime.evaluate` in the user's authenticated session can read cookies/localStorage and hit any same-origin endpoint, so opt-in only.
297
+ 21 core tools: `browse` (one-shot), `goto`, `snapshot`, `readable`, `click`, `type`, `press`, `scroll`, `hover`, `select`, `back`, `forward`, `reload`, `drag`, `upload`, `pdf`, `screenshot`, `wait_for`, `tabs`, `capabilities` (engine + feature report, plus a `cookies` field naming the last goto's injected cookies — names only, never values), `switch_engine` (swap chromium/firefox without restarting; page, tabs and refs reset, so `goto` after). Plus `assess` (privacy scan) if `wearehere` is installed (`npm install wearehere`). Plus the **opt-in `eval` tool** gated by `BAREBROWSE_MCP_EVAL=1` (default OFF) — `Runtime.evaluate` in the user's authenticated session can read cookies/localStorage and hit any same-origin endpoint, so opt-in only.
297
298
 
298
299
  **Idle release:** the MCP server closes its browser after `BAREBROWSE_IDLE_MINUTES` of no tool calls (default `15`; `0` = off; blank/invalid = default; capped at ~24.8 days; calls in flight postpone it). The next call opens a fresh browser — page state (open tabs, a solved captcha) is not resumed. Transient browser errors close the broken browser instead of orphaning it; a `wait_for` that merely times out keeps the page (its outer deadline is `max(60s, timeout+5s)`).
299
300
 
@@ -386,7 +387,7 @@ Useful for agent threshold decisions: "skip sites above score 40", "warn if term
386
387
 
387
388
  9. **Screenshot returns base64.** Write to file with `fs.writeFileSync('shot.png', Buffer.from(base64, 'base64'))` or pass directly to a vision model.
388
389
 
389
- 10. **Two engines.** The default is CDP, which drives Chrome, Chromium, Edge, Brave, Vivaldi (~80% desktop share). Firefox is driven over WebDriver BiDi (CDP is deprecated there) — select it with `connect({ engine: 'firefox' })`, CLI `--engine firefox`, or MCP `BAREBROWSE_ENGINE=firefox`. Same `page.*` API on both; feature parity reached in v0.19.x, with the only remaining gap being `reload()` ignoring `ignoreCache` (upstream BiDi limitation). Note the engine is chosen at launch, not per-call. See the `engine` connect() option below for the full parity timeline.
390
+ 10. **Two engines.** The default is CDP, which drives Chrome, Chromium, Edge, Brave, Vivaldi (~80% desktop share). Firefox is driven over WebDriver BiDi (CDP is deprecated there) — select it with `connect({ engine: 'firefox' })`, CLI `--engine firefox`, or MCP `BAREBROWSE_ENGINE=firefox`. Same `page.*` API on both; feature parity reached in v0.19.x, with the only remaining gap being `reload()` ignoring `ignoreCache` (upstream BiDi limitation). The engine is chosen at launch and can be swapped at runtime via MCP `switch_engine` or CLI `switch-engine <chromium|firefox>` (page, tabs and refs reset, so call `goto` after; not available in attach mode). See the `engine` connect() option below for the full parity timeline.
390
391
 
391
392
  11. **`--site-per-process` is on by default (v0.9.0).** Required for iframe support — without it, same-origin iframes stay in the parent process and `Input.dispatchMouseEvent` coords don't match `DOM.getBoxModel` coords for iframe-internal elements. Memory cost: +50-150MB per cross-origin frame. Real Chrome does this for cross-origin by default; we just extend it to all iframes. If you attach via `connect({port})`, the user's browser is whatever they launched it as — for iframe interaction reliability, start it with `--site-per-process` too.
392
393
 
package/cli.js CHANGED
@@ -88,6 +88,8 @@ if (args.includes('--daemon-internal')) {
88
88
  await cmdProxy('save-state');
89
89
  } else if (cmd === 'dialog-log') {
90
90
  await cmdProxy('dialog-log');
91
+ } else if (cmd === 'switch-engine' && args[1]) {
92
+ await cmdProxy('switch-engine', { engine: args[1] });
91
93
  } else {
92
94
  printUsage();
93
95
  }
@@ -613,6 +615,7 @@ Debugging:
613
615
  barebrowse dialog-log JS dialog log -> .barebrowse/dialogs-*.json
614
616
  barebrowse downloads List Content-Disposition downloads + savedPath (JSON)
615
617
  barebrowse save-state Cookies + localStorage -> .barebrowse/state-*.json
618
+ barebrowse switch-engine <chromium|firefox> Swap the browser in place (page, tabs, refs are lost)
616
619
 
617
620
  One-shot:
618
621
  barebrowse browse <url> [mode] Browse + print snapshot to stdout
package/mcp-server.js CHANGED
@@ -13,6 +13,7 @@
13
13
  import { browse, connect } from './src/index.js';
14
14
  import { formatReadable } from './src/readable.js';
15
15
  import { cleanOld } from './src/output-clean.js';
16
+ import { scopedCookiesForUrl } from './src/auth.js';
16
17
  import { mkdirSync, writeFileSync, readFileSync } from 'node:fs';
17
18
  import { join, dirname } from 'node:path';
18
19
  import { pathToFileURL, fileURLToPath } from 'node:url';
@@ -120,6 +121,22 @@ export function saveSnapshot(text, { prefix = 'page', ext = 'yml' } = {}) {
120
121
 
121
122
  let _page = null;
122
123
  let _pageConnecting = null;
124
+ // Active engine: starts from BAREBROWSE_ENGINE, changed at runtime by switch_engine.
125
+ let _engine = process.env.BAREBROWSE_ENGINE === 'firefox' ? 'firefox' : 'chromium';
126
+ // Last goto's cookie injection (names/domains/paths only, never values), so an
127
+ // agent can tell "logged out" from "no cookie found" from "the read failed".
128
+ let _lastCookies = null;
129
+
130
+ /**
131
+ * Shape the capabilities `cookies` field. Takes cookie objects but copies only
132
+ * name, domain and path, so a value can never leak into the report.
133
+ */
134
+ export function cookieReport(url, { cookies = [], error, incognito } = {}) {
135
+ if (incognito) return { url, injected: 0, incognito: true };
136
+ const r = { url, injected: cookies.length, cookies: cookies.map((c) => `${c.name} @ ${c.domain}${c.path}`) };
137
+ if (error) r.error = error;
138
+ return r;
139
+ }
123
140
 
124
141
  /**
125
142
  * Forget the current session AND close its browser. A bare `_page = null`
@@ -129,6 +146,7 @@ let _pageConnecting = null;
129
146
  function resetPage() {
130
147
  const p = _page;
131
148
  _page = null;
149
+ _lastCookies = null;
132
150
  if (p) p.close().catch(() => {});
133
151
  }
134
152
 
@@ -146,7 +164,13 @@ const MAX_TIMER_MS = 2 ** 31 - 1;
146
164
  * the page.
147
165
  */
148
166
  export function waitForDeadline(timeout) {
149
- return Math.min(Math.max(TIMEOUTS.wait_for, (timeout || 30000) + 5000), MAX_TIMER_MS);
167
+ return Math.min(Math.max(TIMEOUTS.wait_for, waitTimeout(timeout) + 5000), MAX_TIMER_MS);
168
+ }
169
+
170
+ /** wait_for's requested wait in ms. MCP doesn't enforce schema types, so "abc" must not become NaN. */
171
+ export function waitTimeout(timeout) {
172
+ const n = Number(timeout);
173
+ return Number.isFinite(n) && n > 0 ? n : 30000;
150
174
  }
151
175
 
152
176
  export function idleMs(raw) {
@@ -168,16 +192,16 @@ function armIdle() {
168
192
  }
169
193
 
170
194
  /**
171
- * The engine + mode this server is configured to use, derived from env WITHOUT
172
- * launching a browser. Both engines ship in the library; which one this server
173
- * drives is fixed at launch by BAREBROWSE_ENGINE and cannot change mid-session
174
- * (one browser is connected and reused across tool calls). Surfaced to the
195
+ * The engine + mode this server is using, WITHOUT launching a browser. Both
196
+ * engines ship in the library; the starting engine is BAREBROWSE_ENGINE and the
197
+ * `switch_engine` tool changes it at runtime (one browser is connected and
198
+ * reused across tool calls; a switch replaces it). Surfaced to the
175
199
  * agent via `initialize.instructions`, the `capabilities` tool, and the `browse`
176
200
  * tool description so the Firefox engine is discoverable — it was invisible
177
201
  * before, and a session concluded (wrongly) that only Chromium was supported.
178
202
  */
179
203
  function engineInfo() {
180
- const engine = process.env.BAREBROWSE_ENGINE === 'firefox' ? 'firefox' : 'chromium';
204
+ const engine = _engine;
181
205
  const mode = process.env.BAREBROWSE_MODE || 'hybrid';
182
206
  const incognito = process.env.BAREBROWSE_INCOGNITO === '1';
183
207
  return {
@@ -185,10 +209,10 @@ function engineInfo() {
185
209
  mode,
186
210
  incognito,
187
211
  availableEngines: ['chromium', 'firefox'],
188
- // Engine is chosen once at server launch — the agent cannot switch it.
212
+ // BAREBROWSE_ENGINE only sets the starting engine; the agent can switch.
189
213
  switchEngine:
190
- "To use the other engine, restart the MCP server with BAREBROWSE_ENGINE=firefox "
191
- + "(or unset it for the chromium default). It cannot be changed during a session.",
214
+ 'To use the other engine, call the "switch_engine" tool (the page, tabs and refs are lost; '
215
+ + 'takes a few seconds). BAREBROWSE_ENGINE sets the starting engine.',
192
216
  };
193
217
  }
194
218
 
@@ -205,7 +229,7 @@ async function getPage() {
205
229
  // across tool calls, so it's an env var (BAREBROWSE_INCOGNITO=1), not a
206
230
  // per-request flag. Skips all auth injection for a clean, logged-out session.
207
231
  const incognito = process.env.BAREBROWSE_INCOGNITO === '1';
208
- const connectOpts = process.env.BAREBROWSE_ENGINE === 'firefox'
232
+ const connectOpts = _engine === 'firefox'
209
233
  ? { engine: 'firefox', mode: process.env.BAREBROWSE_MODE || 'hybrid', incognito }
210
234
  : { mode: process.env.BAREBROWSE_MODE || 'hybrid', incognito };
211
235
  _pageConnecting = connect(connectOpts);
@@ -437,9 +461,20 @@ export const TOOLS = [
437
461
  required: ['ref'],
438
462
  },
439
463
  },
464
+ {
465
+ name: 'switch_engine',
466
+ description: 'Switch the browser engine (chromium or firefox) without restarting the MCP server. LOSES all in-session state: the current page, tabs, refs, and any logins or form state made inside the session. Logins from your real browser are re-injected on the next goto. Costs a few seconds (about 2s to chromium, 4-5s to firefox) as the new browser launches on your next call. After switching, call goto. Same engine as the active one is a no-op.',
467
+ inputSchema: {
468
+ type: 'object',
469
+ properties: {
470
+ engine: { type: 'string', enum: ['chromium', 'firefox'], description: 'Engine to switch to' },
471
+ },
472
+ required: ['engine'],
473
+ },
474
+ },
440
475
  {
441
476
  name: 'capabilities',
442
- description: 'Report which browser engine this session drives (chromium or firefox) and what it can do — no navigation, no side effects. Use it to answer "can this drive Firefox?" or "is this an authenticated session?". Returns the active engine + mode, that both engines are supported, how to switch (a server-restart env var; the engine is fixed for a running session), and, if a page is already open, its live capability flags.',
477
+ description: 'Report which browser engine this session drives (chromium or firefox) and what it can do — no navigation, no side effects. Use it to answer "can this drive Firefox?" or "is this an authenticated session?". Returns the active engine + mode, that both engines are supported, how to switch (the switch_engine tool), and, if a page is already open, its live capability flags. Also returns `cookies`: the cookie injection of the last goto as { url, injected, cookies: ["name @ domain/path", ...], error? } (names, domains and paths only, never values; { url, injected: 0, incognito: true } under incognito; null before any goto or after the browser is released). Use it to tell "logged out" from "no cookie found" from "the cookie read failed".',
443
478
  inputSchema: { type: 'object', properties: {} },
444
479
  },
445
480
  ];
@@ -492,8 +527,20 @@ async function handleToolCall(name, args) {
492
527
  info.connected = false;
493
528
  info.capabilities = null; // populated on first navigation
494
529
  }
530
+ info.cookies = _lastCookies; // null until a goto
495
531
  return info;
496
532
  }
533
+ case 'switch_engine': {
534
+ const target = args.engine;
535
+ if (target !== 'chromium' && target !== 'firefox') throw new Error('engine must be "chromium" or "firefox"');
536
+ if (target === _engine) return `already on ${target}; nothing changed`;
537
+ // A launch in progress would land as _page with the old engine after the
538
+ // switch. Let it finish (its outcome is irrelevant), then close it.
539
+ if (_pageConnecting) await _pageConnecting.catch(() => {});
540
+ resetPage();
541
+ _engine = target;
542
+ return `switched to ${target} — the previous page, tabs and refs are gone; call goto`;
543
+ }
497
544
  case 'browse': {
498
545
  let timer;
499
546
  const text = await Promise.race([
@@ -510,7 +557,15 @@ async function handleToolCall(name, args) {
510
557
  }
511
558
  case 'goto': return withRetry(async () => {
512
559
  const page = await getPage();
513
- try { await page.injectCookies(args.url); } catch {}
560
+ if (process.env.BAREBROWSE_INCOGNITO === '1') {
561
+ _lastCookies = cookieReport(args.url, { incognito: true });
562
+ } else {
563
+ let cookies = [];
564
+ let error;
565
+ try { cookies = scopedCookiesForUrl(args.url); } catch (e) { error = e.message; }
566
+ try { await page.injectCookies(args.url); } catch (e) { error ??= e.message; }
567
+ _lastCookies = cookieReport(args.url, { cookies, error });
568
+ }
514
569
  await page.goto(args.url);
515
570
  return 'ok';
516
571
  }, TIMEOUTS.goto);
@@ -605,7 +660,7 @@ async function handleToolCall(name, args) {
605
660
  }, TIMEOUTS.screenshot);
606
661
  case 'wait_for': return withRetry(async () => {
607
662
  const page = await getPage();
608
- await page.waitFor({ text: args.text, selector: args.selector, timeout: args.timeout });
663
+ await page.waitFor({ text: args.text, selector: args.selector, timeout: waitTimeout(args.timeout) });
609
664
  return 'ok';
610
665
  }, waitForDeadline(args.timeout), { retry: false });
611
666
  case 'tabs': return withRetry(async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "barebrowse",
3
- "version": "0.23.0",
3
+ "version": "0.24.1",
4
4
  "description": "Authenticated web browsing for autonomous agents via CDP. URL in, pruned ARIA snapshot out.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/auth.js CHANGED
@@ -181,14 +181,16 @@ function extractChromiumCookies(dbPath, domain) {
181
181
  * @param {string} [domain] - Filter by domain
182
182
  * @returns {Array<object>} Cookies in CDP Network.setCookie format
183
183
  */
184
- function extractFirefoxCookies(dbPath, domain) {
184
+ export function extractFirefoxCookies(dbPath, domain) {
185
185
  const db = new DatabaseSync(`file://${dbPath}?immutable=1`, { readOnly: true });
186
186
 
187
+ // Partitioned (CHIPS) cookies carry partitionKey in originAttributes and can
188
+ // share name+domain+path with the top-level cookie; inject only top-level ones.
187
189
  let sql = `SELECT host, name, value, path, expiry, isSecure, isHttpOnly, sameSite
188
- FROM moz_cookies`;
190
+ FROM moz_cookies WHERE originAttributes NOT LIKE '%partitionKey=%'`;
189
191
  const params = [];
190
192
  if (domain) {
191
- sql += ` WHERE host LIKE ?`;
193
+ sql += ` AND host LIKE ?`;
192
194
  params.push(`%${domain}%`);
193
195
  }
194
196
 
@@ -235,19 +237,27 @@ export function extractCookies(opts = {}) {
235
237
  return extractChromiumCookies(path, domain);
236
238
  }
237
239
 
238
- // Auto: try all browsers, merge (last-write-wins by name+domain)
239
- const all = new Map();
240
+ // Auto: try all browsers, merge (last-write-wins by name+domain+path)
240
241
  const chromium = findChromiumCookieDb();
241
- if (chromium) {
242
- for (const c of extractChromiumCookies(chromium.path, domain))
243
- all.set(`${c.name}@${c.domain}`, c);
244
- }
245
242
  const firefox = findFirefoxCookieDb();
246
- if (firefox) {
247
- for (const c of extractFirefoxCookies(firefox, domain))
248
- all.set(`${c.name}@${c.domain}`, c);
249
- }
250
- if (all.size === 0) throw new Error('No browser cookie database found');
243
+ const lists = [];
244
+ if (chromium) lists.push(extractChromiumCookies(chromium.path, domain));
245
+ if (firefox) lists.push(extractFirefoxCookies(firefox, domain));
246
+ const merged = mergeCookies(lists);
247
+ if (merged.length === 0) throw new Error('No browser cookie database found');
248
+ return merged;
249
+ }
250
+
251
+ /**
252
+ * Merge cookie lists; a later list wins on an identical cookie. Identity is
253
+ * name + domain + path (RFC 6265): the same name on two paths are two cookies.
254
+ * @param {Array<Array<object>>} lists
255
+ * @returns {Array<object>}
256
+ */
257
+ export function mergeCookies(lists) {
258
+ const all = new Map();
259
+ for (const list of lists)
260
+ for (const c of list) all.set(`${c.name}@${c.domain}${c.path}`, c);
251
261
  return [...all.values()];
252
262
  }
253
263
 
package/src/chromium.js CHANGED
@@ -6,7 +6,8 @@
6
6
  */
7
7
 
8
8
  import { execFileSync, spawn } from 'node:child_process';
9
- import { existsSync, rmSync } from 'node:fs';
9
+ import { existsSync, rmSync, readdirSync, readlinkSync } from 'node:fs';
10
+ import { tmpdir, hostname } from 'node:os';
10
11
 
11
12
  /** Block the current thread for `ms` without spawning a process. */
12
13
  function sleepSync(ms) {
@@ -121,6 +122,7 @@ export async function launch(opts = {}) {
121
122
 
122
123
  const args = [
123
124
  `--remote-debugging-port=${port}`,
125
+ '--remote-debugging-pipe',
124
126
  '--no-first-run',
125
127
  '--no-default-browser-check',
126
128
  '--disable-background-networking',
@@ -156,6 +158,7 @@ export async function launch(opts = {}) {
156
158
  if (opts.userDataDir) {
157
159
  args.push(`--user-data-dir=${opts.userDataDir}`);
158
160
  } else {
161
+ sweepStaleProfiles();
159
162
  ownedProfileDir = `/tmp/barebrowse-${process.pid}-${Date.now()}`;
160
163
  args.push(`--user-data-dir=${ownedProfileDir}`);
161
164
  }
@@ -176,43 +179,61 @@ export async function launch(opts = {}) {
176
179
  // directly. The SIGINT handler in registerExitHandlers() that calls
177
180
  // reapAllSync() is what actually kills Chromium under Ctrl-C now. Do not
178
181
  // remove that handler without restoring some other path to reap children.
182
+ //
183
+ // Parent-death tether: --remote-debugging-pipe makes Chromium read CDP from
184
+ // fd 3 and write to fd 4. We never use them (CDP still goes over the ws
185
+ // port) — they exist so that when this Node process dies by SIGKILL/crash,
186
+ // where no exit handler can run, the kernel closes our end, Chromium reads
187
+ // EOF and exits itself, helpers included. Without it the browser is
188
+ // orphaned to init with its profile dir. Nothing may write to these fds.
179
189
  const child = spawn(binary, args, {
180
- stdio: ['ignore', 'pipe', 'pipe'],
190
+ stdio: ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'],
181
191
  detached: true,
182
192
  });
193
+ // Drop our ends once Chromium is gone so they can't hold the event loop.
194
+ child.once('exit', () => { child.stdio[3]?.destroy(); child.stdio[4]?.destroy(); });
183
195
 
184
- // Parse the WebSocket URL from stderr
185
- // Chrome prints: "DevTools listening on ws://127.0.0.1:PORT/devtools/browser/UUID"
186
- const wsUrl = await new Promise((resolve, reject) => {
187
- let stderr = '';
188
- const timeout = setTimeout(() => {
189
- reject(new Error(`Browser failed to start within 10s. stderr: ${stderr}`));
190
- }, 10000);
196
+ // Not in activeBrowsers yet, so nothing else would reap it: any failure
197
+ // before registration (start timeout, spawn error, early exit, bad URL)
198
+ // must kill the process group and remove the profile dir itself.
199
+ let wsUrl, actualPort;
200
+ try {
201
+ // Parse the WebSocket URL from stderr
202
+ // Chrome prints: "DevTools listening on ws://127.0.0.1:PORT/devtools/browser/UUID"
203
+ wsUrl = await new Promise((resolve, reject) => {
204
+ let stderr = '';
205
+ const timeout = setTimeout(() => {
206
+ reject(new Error(`Browser failed to start within 10s. stderr: ${stderr}`));
207
+ }, 10000);
191
208
 
192
- child.stderr.on('data', (chunk) => {
193
- stderr += chunk.toString();
194
- const match = stderr.match(/ws:\/\/[^\s]+/);
195
- if (match) {
196
- clearTimeout(timeout);
197
- resolve(match[0]);
198
- }
199
- });
209
+ child.stderr?.on('data', (chunk) => {
210
+ stderr += chunk.toString();
211
+ const match = stderr.match(/ws:\/\/[^\s]+/);
212
+ if (match) {
213
+ clearTimeout(timeout);
214
+ resolve(match[0]);
215
+ }
216
+ });
200
217
 
201
- child.on('error', (err) => {
202
- clearTimeout(timeout);
203
- reject(new Error(`Failed to launch browser: ${err.message}`));
204
- });
218
+ child.on('error', (err) => {
219
+ clearTimeout(timeout);
220
+ reject(new Error(`Failed to launch browser: ${err.message}`));
221
+ });
205
222
 
206
- child.on('exit', (code) => {
207
- clearTimeout(timeout);
208
- if (!stderr.includes('ws://')) {
209
- reject(new Error(`Browser exited with code ${code}. stderr: ${stderr}`));
210
- }
223
+ child.on('exit', (code) => {
224
+ clearTimeout(timeout);
225
+ if (!stderr.includes('ws://')) {
226
+ reject(new Error(`Browser exited with code ${code}. stderr: ${stderr}`));
227
+ }
228
+ });
211
229
  });
212
- });
213
230
 
214
- // Extract port from wsUrl
215
- const actualPort = parseInt(new URL(wsUrl).port, 10);
231
+ // Extract port from wsUrl
232
+ actualPort = parseInt(new URL(wsUrl).port, 10);
233
+ } catch (err) {
234
+ await cleanupBrowser({ process: child, ownedProfileDir });
235
+ throw err;
236
+ }
216
237
 
217
238
  const browser = { wsUrl, process: child, port: actualPort, ownedProfileDir };
218
239
 
@@ -225,6 +246,53 @@ export async function launch(opts = {}) {
225
246
  return browser;
226
247
  }
227
248
 
249
+ /**
250
+ * True when a profile dir's browser lock says a browser may still be using it: the
251
+ * lock's pid is alive, or (Chromium) it names another host (shared dir, can't judge).
252
+ * Chromium: SingletonLock -> '<hostname>-<pid>'; Firefox: lock -> '<ip>:+<pid>'.
253
+ * Download dirs have no lock.
254
+ */
255
+ function browserStillUsing(path, host) {
256
+ for (const f of ['SingletonLock', 'lock']) {
257
+ let t;
258
+ try { t = readlinkSync(`${path}/${f}`); } catch { continue; }
259
+ const m = f === 'lock' ? /:\+?(\d+)$/.exec(t) : /^(.*)-(\d+)$/.exec(t);
260
+ if (!m) continue;
261
+ if (f !== 'lock' && m[1] !== host) return true;
262
+ try { process.kill(Number(m.at(-1)), 0); return true; } catch (err) { if (err.code !== 'ESRCH') return true; }
263
+ }
264
+ return false;
265
+ }
266
+
267
+ /**
268
+ * Remove profile dirs left by launchers that were hard-killed (the pipe tether
269
+ * kills their Chromium but cannot delete the dir). Matches exactly
270
+ * barebrowse-<pid>-<ms> (Chromium) or barebrowse-ff-<pid>-<6 random chars>
271
+ * (Firefox), or the dl-/ff-dl- download-dir forms of those, and removes it only when that pid is dead (EPERM means alive), and only if no browser lock in it names a live pid or a foreign host.
272
+ * Dirs only, never processes; old pid-less dirs are never matched. Known limit: a reused pid keeps its dir.
273
+ * Foreign-host rule: if the hostname changed (renamed machine, container with a random hostname sharing /tmp), a dead launcher's dir is kept forever, a deliberate leak-over-delete tradeoff.
274
+ * With no argument sweeps both '/tmp' (Chromium profiles) and tmpdir() (download
275
+ * dirs, Firefox profiles); they differ when TMPDIR is set.
276
+ * @param {string} [dir]
277
+ * @param {string} [host] hostname a Chromium lock must name to be judged by its pid (default: this machine's; injectable for tests)
278
+ */
279
+ export function sweepStaleProfiles(dir, host = hostname()) {
280
+ if (dir === undefined) {
281
+ for (const d of new Set(['/tmp', tmpdir()])) sweepStaleProfiles(d, host);
282
+ return;
283
+ }
284
+ let names = [];
285
+ try { names = readdirSync(dir); } catch {}
286
+ for (const name of names) {
287
+ const m = /^barebrowse-(?:(\d+)-\d+|(?:ff-|dl-|ff-dl-)(\d+)-[A-Za-z0-9]{6})$/.exec(name);
288
+ const pid = Number(m?.[1] ?? m?.[2]);
289
+ if (!m || pid === process.pid) continue;
290
+ try { process.kill(pid, 0); continue; } catch (err) { if (err.code !== 'ESRCH') continue; }
291
+ if (browserStillUsing(`${dir}/${name}`, host)) continue;
292
+ try { rmSync(`${dir}/${name}`, { recursive: true, force: true }); } catch {}
293
+ }
294
+ }
295
+
228
296
  /**
229
297
  * Kill a launched browser and remove its temp profile dir (if we created one).
230
298
  * Waits up to 2s for the process to actually exit before unlinking the dir —
@@ -243,12 +311,14 @@ export async function cleanupBrowser(browser) {
243
311
  });
244
312
  try { browser.process.kill(); } catch {}
245
313
  await exited;
246
- // SIGKILL the whole Chromium process group. The parent may have exited
247
- // already (above) but renderer/GPU/network children — separate processes
248
- // under --site-per-process — can outlive it by seconds, and they hold
249
- // profile-dir file handles. Because launch() spawned with detached:true,
250
- // the children share Chromium's pgid (not Node's), so process.kill on a
251
- // negative PID reaps the whole group without touching anything else.
314
+ }
315
+ // SIGKILL the whole Chromium process group — unconditionally, even if the
316
+ // parent already exited or was already signalled: renderer/GPU/network
317
+ // children (separate processes under --site-per-process) can outlive it,
318
+ // and they hold profile-dir file handles. Because launch() spawned with
319
+ // detached:true, the children share Chromium's pgid (not Node's), so a
320
+ // negative PID reaps the whole group without touching anything else.
321
+ if (browser.process?.pid != null) {
252
322
  try { process.kill(-browser.process.pid, 'SIGKILL'); } catch {
253
323
  // ESRCH = group already gone; anything else is best-effort here.
254
324
  }
package/src/daemon.js CHANGED
@@ -219,9 +219,10 @@ export async function runDaemon(opts, outputDir, initialUrl) {
219
219
  // token is written into session.json (mode 0600) so only the owner reads it.
220
220
  const authToken = randomBytes(32).toString('hex');
221
221
 
222
- // Connect to browser
223
- const page = await connect({
224
- engine: opts.engine,
222
+ // Connect to browser. Engine is a parameter so `switch-engine` can reuse the
223
+ // exact same open options with a different one.
224
+ const connectPage = (engine) => connect({
225
+ engine,
225
226
  mode: opts.mode || 'headless',
226
227
  port: opts.port ? Number(opts.port) : undefined,
227
228
  consent: opts.consent,
@@ -237,6 +238,7 @@ export async function runDaemon(opts, outputDir, initialUrl) {
237
238
  // is the legacy per-open equivalent. Either yields a logged-out session.
238
239
  incognito: opts.incognito || opts.cookies === false,
239
240
  });
241
+ let page = await connectPage(opts.engine);
240
242
 
241
243
  // Console + network log capture. CDP uses Runtime/Network events off
242
244
  // page.cdp; the Firefox/BiDi engine (page.bidi) captures the same shape over
@@ -246,52 +248,56 @@ export async function runDaemon(opts, outputDir, initialUrl) {
246
248
  const networkLogs = [];
247
249
  const pendingRequests = new Map();
248
250
 
249
- if (page.cdp) {
250
- await page.cdp.send('Runtime.enable');
251
- page.cdp.on('Runtime.consoleAPICalled', (params) => {
252
- consoleLogs.push({
253
- type: params.type,
254
- timestamp: new Date().toISOString(),
255
- args: params.args.map((a) => a.value ?? a.description ?? a.type),
251
+ // Wire capture onto a page; called again for the new page after a switch.
252
+ async function attachCapture(page) {
253
+ if (page.cdp) {
254
+ await page.cdp.send('Runtime.enable');
255
+ page.cdp.on('Runtime.consoleAPICalled', (params) => {
256
+ consoleLogs.push({
257
+ type: params.type,
258
+ timestamp: new Date().toISOString(),
259
+ args: params.args.map((a) => a.value ?? a.description ?? a.type),
260
+ });
256
261
  });
257
- });
258
262
 
259
- // Network log capture (Network.enable already called by connect)
260
- page.cdp.on('Network.requestWillBeSent', (params) => {
261
- pendingRequests.set(params.requestId, {
262
- url: params.request.url,
263
- method: params.request.method,
264
- timestamp: new Date().toISOString(),
263
+ // Network log capture (Network.enable already called by connect)
264
+ page.cdp.on('Network.requestWillBeSent', (params) => {
265
+ pendingRequests.set(params.requestId, {
266
+ url: params.request.url,
267
+ method: params.request.method,
268
+ timestamp: new Date().toISOString(),
269
+ });
265
270
  });
266
- });
267
271
 
268
- page.cdp.on('Network.responseReceived', (params) => {
269
- const req = pendingRequests.get(params.requestId);
270
- if (req) {
271
- networkLogs.push({
272
- ...req,
273
- status: params.response.status,
274
- statusText: params.response.statusText,
275
- mimeType: params.response.mimeType,
276
- });
277
- pendingRequests.delete(params.requestId);
278
- }
279
- });
272
+ page.cdp.on('Network.responseReceived', (params) => {
273
+ const req = pendingRequests.get(params.requestId);
274
+ if (req) {
275
+ networkLogs.push({
276
+ ...req,
277
+ status: params.response.status,
278
+ statusText: params.response.statusText,
279
+ mimeType: params.response.mimeType,
280
+ });
281
+ pendingRequests.delete(params.requestId);
282
+ }
283
+ });
280
284
 
281
- page.cdp.on('Network.loadingFailed', (params) => {
282
- const req = pendingRequests.get(params.requestId);
283
- if (req) {
284
- networkLogs.push({
285
- ...req,
286
- status: 0,
287
- error: params.errorText,
288
- });
289
- pendingRequests.delete(params.requestId);
290
- }
291
- });
292
- } else if (page.bidi) {
293
- await attachBiDiCapture(page.bidi, { consoleLogs, networkLogs, pendingRequests });
285
+ page.cdp.on('Network.loadingFailed', (params) => {
286
+ const req = pendingRequests.get(params.requestId);
287
+ if (req) {
288
+ networkLogs.push({
289
+ ...req,
290
+ status: 0,
291
+ error: params.errorText,
292
+ });
293
+ pendingRequests.delete(params.requestId);
294
+ }
295
+ });
296
+ } else if (page.bidi) {
297
+ await attachBiDiCapture(page.bidi, { consoleLogs, networkLogs, pendingRequests });
298
+ }
294
299
  }
300
+ await attachCapture(page);
295
301
 
296
302
  // Navigate to initial URL if provided
297
303
  if (initialUrl) {
@@ -505,6 +511,34 @@ export async function runDaemon(opts, outputDir, initialUrl) {
505
511
  return { ok: true, file, count: logs.length };
506
512
  },
507
513
 
514
+ async 'switch-engine'({ engine }) {
515
+ if (engine !== 'chromium' && engine !== 'firefox') {
516
+ return { ok: false, error: 'engine must be "chromium" or "firefox"' };
517
+ }
518
+ if (engine === page.engine) return { ok: true, value: `already on ${engine}; nothing changed` };
519
+ if (opts.port) return { ok: false, error: 'cannot switch engines on an attached browser (--port)' };
520
+ // Connect the new browser first: if it fails to launch, the old page
521
+ // keeps serving. Only then retire the old one.
522
+ const next = await connectPage(engine);
523
+ try {
524
+ await attachCapture(next);
525
+ if (initialUrl && opts.cookies !== false) {
526
+ try { await next.injectCookies(initialUrl, { browser: opts.browser }); } catch { /* no cookies */ }
527
+ }
528
+ } catch (err) {
529
+ await next.close().catch(() => {});
530
+ throw err;
531
+ }
532
+ const old = page;
533
+ page = next;
534
+ consoleLogs.length = 0;
535
+ networkLogs.length = 0;
536
+ pendingRequests.clear();
537
+ await old.close().catch(() => {});
538
+ writeSession();
539
+ return { ok: true, value: `switched to ${engine} — the previous page, tabs and refs are gone; run goto` };
540
+ },
541
+
508
542
  async close() {
509
543
  await page.close();
510
544
  // Clean up session file
@@ -607,16 +641,19 @@ export async function runDaemon(opts, outputDir, initialUrl) {
607
641
  // Write session.json so parent/clients can find us. Owner-only: it carries
608
642
  // the auth token that gates /command.
609
643
  const sessionPath = join(absDir, SESSION_FILE);
610
- writeFilePrivate(sessionPath, JSON.stringify({
644
+ const startedAt = new Date().toISOString();
645
+ // Rewritten by switch-engine so status/doctor see the live engine.
646
+ const writeSession = () => writeFilePrivate(sessionPath, JSON.stringify({
611
647
  port,
612
648
  pid: process.pid,
613
649
  token: authToken,
614
- startedAt: new Date().toISOString(),
650
+ startedAt,
615
651
  // Persisted so `status`/`doctor` can report the live engine + feature set
616
652
  // without an HTTP round-trip (page.engine/capabilities, added in v0.21.0).
617
653
  engine: page.engine,
618
654
  capabilities: page.capabilities,
619
655
  }));
656
+ writeSession();
620
657
 
621
658
  // Handle SIGTERM gracefully
622
659
  process.on('SIGTERM', async () => {
package/src/firefox.js CHANGED
@@ -15,9 +15,10 @@
15
15
  */
16
16
 
17
17
  import { execFileSync, spawn } from 'node:child_process';
18
- import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';
18
+ import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
19
19
  import { tmpdir } from 'node:os';
20
20
  import { join } from 'node:path';
21
+ import { sweepStaleProfiles } from './chromium.js';
21
22
 
22
23
  /** Block the current thread for `ms` (sync, for the 'exit' reaper). */
23
24
  function sleepSync(ms) {
@@ -55,6 +56,8 @@ function registerExitHandlers() {
55
56
  }
56
57
  }
57
58
 
59
+ const SETPRIV = ['/usr/bin/setpriv', '/bin/setpriv'];
60
+
58
61
  const CANDIDATES = [
59
62
  'firefox',
60
63
  'firefox-esr',
@@ -103,7 +106,9 @@ export function findFirefox() {
103
106
  export async function launchFirefox(opts = {}) {
104
107
  const binary = opts.binary || findFirefox();
105
108
  const port = opts.port || 0;
106
- const profileDir = mkdtempSync(join(tmpdir(), 'barebrowse-ff-'));
109
+ // Always our own profile: sweep dirs of hard-killed launchers, then pid-tag ours.
110
+ sweepStaleProfiles();
111
+ const profileDir = mkdtempSync(join(tmpdir(), `barebrowse-ff-${process.pid}-`));
107
112
 
108
113
  // Proxy + prompt-suppressing prefs go in user.js (read at profile load).
109
114
  const prefs = [
@@ -167,27 +172,49 @@ export async function launchFirefox(opts = {}) {
167
172
  args.push('about:blank');
168
173
 
169
174
  // detached:true → own process group, so cleanup can signal the whole tree.
170
- const child = spawn(binary, args, { stdio: ['ignore', 'pipe', 'pipe'], detached: true });
171
-
172
- // Firefox prints the BiDi endpoint to stderr. Wait for it (or die trying).
173
- const wsUrl = await new Promise((resolve, reject) => {
174
- let buf = '';
175
- const timeout = setTimeout(() => reject(new Error(`Firefox BiDi did not start within 20s. stderr: ${buf}`)), 20000);
176
- const scan = (chunk) => {
177
- buf += chunk.toString();
178
- const m = buf.match(/WebDriver BiDi listening on (ws:\/\/\S+)/);
179
- if (m) { clearTimeout(timeout); resolve(m[1].replace(/\/?$/, '') + '/session'); }
180
- };
181
- child.stderr.on('data', scan);
182
- child.stdout.on('data', scan);
183
- child.on('error', (err) => { clearTimeout(timeout); reject(new Error(`Failed to launch Firefox: ${err.message}`)); });
184
- child.on('exit', (code) => {
185
- clearTimeout(timeout);
186
- if (!buf.includes('WebDriver BiDi')) reject(new Error(`Firefox exited with code ${code}. stderr: ${buf}`));
187
- });
175
+ //
176
+ // Parent-death tether: Firefox has no --remote-debugging-pipe, so a SIGKILLed
177
+ // or crashed Node parent (no exit handler runs) would orphan it. On Linux we
178
+ // exec it through util-linux `setpriv --pdeathsig SIGKILL`: setpriv execs
179
+ // Firefox (child.pid stays Firefox's pid) and the kernel SIGKILLs it when this
180
+ // process dies; its children follow. Without setpriv (or off Linux) we spawn
181
+ // exactly as before and a hard kill still orphans it.
182
+ // A setuid wrapper (e.g. Snap's snap-confine) clears pdeathsig on exec, so a Snap
183
+ // Firefox can still be orphaned even with setpriv; the stale-profile sweep keeps
184
+ // its profile dir while it runs (it checks the browser lock).
185
+ const setpriv = process.platform === 'linux' && SETPRIV.find(existsSync);
186
+ const child = spawn(setpriv || binary, setpriv ? ['--pdeathsig', 'SIGKILL', binary, ...args] : args, {
187
+ stdio: ['ignore', 'pipe', 'pipe'],
188
+ detached: true,
188
189
  });
189
190
 
190
- const actualPort = parseInt(new URL(wsUrl.replace('/session', '')).port, 10);
191
+ // Not in activeBrowsers yet, so nothing else would reap it: any failure
192
+ // before registration must kill the process group and remove the profile.
193
+ let wsUrl, actualPort;
194
+ try {
195
+ // Firefox prints the BiDi endpoint to stderr. Wait for it (or die trying).
196
+ wsUrl = await new Promise((resolve, reject) => {
197
+ let buf = '';
198
+ const timeout = setTimeout(() => reject(new Error(`Firefox BiDi did not start within 20s. stderr: ${buf}`)), 20000);
199
+ const scan = (chunk) => {
200
+ buf += chunk.toString();
201
+ const m = buf.match(/WebDriver BiDi listening on (ws:\/\/\S+)/);
202
+ if (m) { clearTimeout(timeout); resolve(m[1].replace(/\/?$/, '') + '/session'); }
203
+ };
204
+ child.stderr.on('data', scan);
205
+ child.stdout.on('data', scan);
206
+ child.on('error', (err) => { clearTimeout(timeout); reject(new Error(`Failed to launch Firefox: ${err.message}`)); });
207
+ child.on('exit', (code) => {
208
+ clearTimeout(timeout);
209
+ if (!buf.includes('WebDriver BiDi')) reject(new Error(`Firefox exited with code ${code}. stderr: ${buf}`));
210
+ });
211
+ });
212
+
213
+ actualPort = parseInt(new URL(wsUrl.replace('/session', '')).port, 10);
214
+ } catch (err) {
215
+ await cleanupFirefox({ process: child, ownedProfileDir: profileDir });
216
+ throw err;
217
+ }
191
218
  const browser = { wsUrl, process: child, port: actualPort, ownedProfileDir: profileDir };
192
219
 
193
220
  registerExitHandlers();
package/src/index.js CHANGED
@@ -397,7 +397,7 @@ export async function connect(opts = {}) {
397
397
  if (!downloadPath) {
398
398
  const { mkdtempSync } = await import('node:fs');
399
399
  const { tmpdir } = await import('node:os');
400
- ownedDownloadDir = mkdtempSync(pathJoin(tmpdir(), 'barebrowse-dl-'));
400
+ ownedDownloadDir = mkdtempSync(pathJoin(tmpdir(), `barebrowse-dl-${process.pid}-`));
401
401
  downloadPath = ownedDownloadDir;
402
402
  }
403
403
  // Register listeners BEFORE sending setDownloadBehavior so no
@@ -888,7 +888,7 @@ async function connectFirefox(opts) {
888
888
  if (!downloadDir) {
889
889
  const { mkdtempSync } = await import('node:fs');
890
890
  const { tmpdir } = await import('node:os');
891
- ownedDownloadDir = mkdtempSync(pathJoin(tmpdir(), 'barebrowse-ff-dl-'));
891
+ ownedDownloadDir = mkdtempSync(pathJoin(tmpdir(), `barebrowse-ff-dl-${process.pid}-`));
892
892
  downloadDir = ownedDownloadDir;
893
893
  }
894
894
 
package/types/auth.d.ts CHANGED
@@ -11,6 +11,13 @@ export function findChromiumCookieDb(): {
11
11
  * @returns {string | null} Path to cookies.sqlite
12
12
  */
13
13
  export function findFirefoxCookieDb(): string | null;
14
+ /**
15
+ * Extract cookies from Firefox (no encryption).
16
+ * @param {string} dbPath - Path to cookies.sqlite
17
+ * @param {string} [domain] - Filter by domain
18
+ * @returns {Array<object>} Cookies in CDP Network.setCookie format
19
+ */
20
+ export function extractFirefoxCookies(dbPath: string, domain?: string): Array<object>;
14
21
  /**
15
22
  * Extract cookies from the user's browser, auto-detecting which browser to use.
16
23
  * @param {object} [opts]
@@ -22,6 +29,13 @@ export function extractCookies(opts?: {
22
29
  browser?: string | undefined;
23
30
  domain?: string | undefined;
24
31
  }): Array<object>;
32
+ /**
33
+ * Merge cookie lists; a later list wins on an identical cookie. Identity is
34
+ * name + domain + path (RFC 6265): the same name on two paths are two cookies.
35
+ * @param {Array<Array<object>>} lists
36
+ * @returns {Array<object>}
37
+ */
38
+ export function mergeCookies(lists: Array<Array<object>>): Array<object>;
25
39
  /**
26
40
  * Inject cookies into a CDP session via Network.setCookie.
27
41
  * @param {object} session - CDP session handle (from cdp.session())
@@ -25,6 +25,19 @@ export function launch(opts?: {
25
25
  process: import("node:child_process").ChildProcess;
26
26
  port: number;
27
27
  }>;
28
+ /**
29
+ * Remove profile dirs left by launchers that were hard-killed (the pipe tether
30
+ * kills their Chromium but cannot delete the dir). Matches exactly
31
+ * barebrowse-<pid>-<ms> (Chromium) or barebrowse-ff-<pid>-<6 random chars>
32
+ * (Firefox), or the dl-/ff-dl- download-dir forms of those, and removes it only when that pid is dead (EPERM means alive), and only if no browser lock in it names a live pid or a foreign host.
33
+ * Dirs only, never processes; old pid-less dirs are never matched. Known limit: a reused pid keeps its dir.
34
+ * Foreign-host rule: if the hostname changed (renamed machine, container with a random hostname sharing /tmp), a dead launcher's dir is kept forever, a deliberate leak-over-delete tradeoff.
35
+ * With no argument sweeps both '/tmp' (Chromium profiles) and tmpdir() (download
36
+ * dirs, Firefox profiles); they differ when TMPDIR is set.
37
+ * @param {string} [dir]
38
+ * @param {string} [host] hostname a Chromium lock must name to be judged by its pid (default: this machine's; injectable for tests)
39
+ */
40
+ export function sweepStaleProfiles(dir?: string, host?: string): void;
28
41
  /**
29
42
  * Kill a launched browser and remove its temp profile dir (if we created one).
30
43
  * Waits up to 2s for the process to actually exit before unlinking the dir —