- Instance stop, shutdown and close-tab finalize recordings within a 15 s deadline; unfinished exchanges are reported as skipped. - index.json is opened like bodies (no FIFOs, no symlinks) and capped in size. - Plain output shows skipped exchanges, repeated stops and the real miss count. - A failed record/replay start no longer replays the previous stop result. - Removes the replay encoding cache and the legacy multipart hash fallback; streams hashing through io::copy; splits the event loop. - Strengthens Set-Cookie, multipart, owner-check and large-response tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
camoufox-rs
Pure Rust client for controlling Camoufox through the Firefox Juggler protocol.
This crate implements the full stack needed to automate a Camoufox browser process over the -juggler-pipe transport: process launch, null-delimited JSON framing, protocol request/response/event routing, and ergonomic Browser / BrowserContext / MainFrame wrappers.
Current Scope
- Library crate with a synchronous API for Juggler domains (
Browser,MainFrame,Network,Runtime,Heap) - Optional CLI (
--features cli) with a Unix socket daemon for multi-instance management, persistent profiles (launch --profile-dir), external subcommand plugins (camoufox-<name>onPATH), and structured errorkindclassifications - Page HTTP cassette recording and deterministic offline replay with
record start/stopandreplay start/stop - Unix implementation (process + fd pipe model): Linux and macOS. Windows is not supported.
Documentation
The project's documentation follows the Diataxis framework:
| Category | Document | Description |
|---|---|---|
| Tutorial | Getting Started | Hands-on step-by-step introduction from installation to scraping |
| How-To | CLI Daemon & Management | Managing the daemon, multiple browser instances, and scraping flows |
| How-To | Iframes and Shadow DOM | Navigating nested frames and piercing shadow DOM with >>> |
| How-To | Downloads and Dialogs | Safe file downloads, the click-then-poll download pattern, and handling JS dialogs |
| How-To | Library Usage Guide | Using camoufox-rs as a Rust library crate in your application |
| Reference | CLI Reference | Exhaustive command reference for CLI subcommands |
| Reference | Library API Reference | Complete crate and module API reference |
| Reference | Juggler Protocol Reference | Wire-level Juggler protocol specification |
| Explanation | Architecture & Design | Pipe transport, security model, coordinate spaces, and process lifecycle |
| Explanation | Protocol & Transport Internals | Deep-dive on Juggler framing, pipe IPC, and session multiplexing |
Requirements
- Rust 1.85+ (
rust-versioninCargo.toml) - Unix-like OS for full functionality (process spawning + Unix sockets)
- Camoufox binary available on disk
The CLI daemon resolves the Camoufox binary in this order:
--executable <path>passed tolaunch- the
CAMOUFOX_BINenvironment variable - the install marked active in
~/.cache/camoufox/config.json(active_version), as set by the Pythoncamoufox fetch/camoufox setcommands - the newest
~/.cache/camoufox/browsers/<repo>/<version>/camoufox ~/.cache/camoufox/camoufox, the legacy flat layout
~/.cache means $XDG_CACHE_HOME when it is set to an absolute path, else
$HOME/.cache (/root/.cache when HOME is unset). Library users get the
same lookup from camoufox::config::default_executable(). On macOS, set
CAMOUFOX_BIN or pass --executable.
Build
Library only:
cargo build
CLI binary:
cargo build --features cli --bin camoufox
CLI Quick Start
The CLI uses a daemon process and newline-delimited JSON over a Unix domain socket.
Built-in commands: serve, launch, dialog, list, stop, new-page, navigate,
evaluate, click, screenshot, shutdown, ping, cookies, text, html, links,
data, url, back, forward, reload, wait, cookie, header, fill, upload,
type, press, hover, select, scroll, tabs, close-tab, downloads, record,
replay.
External subcommands: executing camoufox <name> for any command not built-in searches for
camoufox-<name> on PATH, passing through all arguments and setting CAMOUFOX_SOCKET (and
CAMOUFOX_JSON=1 when --json is supplied).
Start daemon (run in a dedicated shell):
cargo run --features cli --bin camoufox -- serve
Launch an instance (--profile-dir specifies an optional persistent profile directory):
cargo run --features cli --bin camoufox -- launch
cargo run --features cli --bin camoufox -- launch --profile-dir /path/to/profile
For persistent browsing data, add --profile-dir /absolute/path/to/profile to
launch. The directory must be empty or contain prefs.js or times.json.
It is created or made private, canonicalized, and kept after stop. The daemon
confirms the canonical path in launch success data; restart older daemons that
cannot confirm persistent profile support.
Create a page:
cargo run --features cli --bin camoufox -- new-page <instance_id>
Navigate and evaluate:
cargo run --features cli --bin camoufox -- navigate <instance_id> <page_id> https://example.com
cargo run --features cli --bin camoufox -- evaluate <instance_id> <page_id> "document.title"
evaluate runs in an isolated JavaScript world (Camoufox 158+): the DOM is shared with the
page, but globals defined by page scripts (window.__NEXT_DATA__, app state) are not visible.
To read them, add --main-world:
cargo run --features cli --bin camoufox -- evaluate --main-world <instance_id> <page_id> "window.__NEXT_DATA__.page"
--main-world injects a <script> into the page, so it accepts synchronous expressions only,
the page can observe it, and a strict Content-Security-Policy blocks it (reporting a hint to relaunch with --bypass-csp).
For pages with strict CSP that prohibit inline scripts without nonces, launch the browser instance with --bypass-csp:
cargo run --features cli --bin camoufox -- launch --bypass-csp
Note: --bypass-csp disables Content-Security-Policy at the browser context level and is detectable by pages. For nonce-protected pages, bridge v2 automatically borrows an existing script's nonce attribute from the DOM, allowing main-world evaluation to succeed without --bypass-csp.
Navigate with wait-until and status_code:
# Block until the load event fires (bounded by --timeout seconds):
cargo run --features cli --bin camoufox -- navigate <instance_id> <page_id> https://example.com \
--wait-until load --timeout 30
# Or wait only until DOMContentLoaded:
cargo run --features cli --bin camoufox -- navigate <instance_id> <page_id> https://example.com \
--wait-until domcontentloaded --timeout 15
# --wait-until accepts: load, domcontentloaded. Any other value is an error.
# navigate always succeeds even on 4xx/5xx responses.
# --json output includes status_code (final main-document HTTP status after following
# all redirects; null if uncapturable, e.g. about: pages or navigation errors):
# { "ok": true, "data": { "navigation_id": "...", "status_code": 200 } }
Export the session cookie jar:
# Export all cookies for the specified instance (includes HttpOnly cookies):
cargo run --features cli --bin camoufox -- cookies <instance_id>
# --json returns full cookie objects (name, value, domain, path, httpOnly, secure, ...):
cargo run --features cli --bin camoufox -- --json cookies <instance_id>
# The exported jar can drive host-side fetches without in-session XHR:
# curl --cookie "name=value" https://example.com/gated-endpoint
Set a cookie or an extra request header:
# Bind the cookie to the page's current URL (or pass --url / --domain explicitly):
cargo run --features cli --bin camoufox -- cookie <instance_id> <page_id> 'session=abc123'
cargo run --features cli --bin camoufox -- cookie <instance_id> <page_id> 'tracker=xyz' \
--domain example.com --path / --secure
# Extra request headers accumulate per page across calls:
cargo run --features cli --bin camoufox -- header <instance_id> <page_id> 'Accept-Language: fr-FR'
Read the page (no hand-written extraction JS required):
# Rendered text, whole page or scoped to a selector:
cargo run --features cli --bin camoufox -- text <instance_id> <page_id>
cargo run --features cli --bin camoufox -- text <instance_id> <page_id> --selector 'article'
# outerHTML of one element, or the whole document when --selector is omitted:
cargo run --features cli --bin camoufox -- html <instance_id> <page_id> --selector h1
# Every <a href> as `text → href`, with hrefs resolved to absolute URLs:
cargo run --features cli --bin camoufox -- links <instance_id> <page_id>
# Structured metadata; no flag returns og + jsonld + meta together:
cargo run --features cli --bin camoufox -- data <instance_id> <page_id> --og
# Current URL (stdout) and title (stderr):
cargo run --features cli --bin camoufox -- url <instance_id> <page_id>
Wait for content to appear:
# Polls document.querySelector until it matches, or --timeout seconds elapse:
cargo run --features cli --bin camoufox -- wait <instance_id> <page_id> \
--selector '#results' --timeout 15
Interact with the page (click, hover, press, type, and the typing phase of fill dispatch trusted browser-level input events; select, scroll, and fill's clear step use JavaScript):
# Click by CSS selector — resolves the element, scrolls it into view, clicks its centre:
cargo run --features cli --bin camoufox -- click <instance_id> <page_id> '#submit'
# Or dispatch a trusted left-click at raw viewport coordinates (x, y). Because the event
# originates from the browser (not JavaScript), the page sees isTrusted === true, so it
# can drive widgets like Cloudflare Turnstile that reject synthetic click events:
cargo run --features cli --bin camoufox -- click <instance_id> <page_id> 200 300
# Fill a field, then submit with a real Enter keypress:
cargo run --features cli --bin camoufox -- fill <instance_id> <page_id> 'input[name=q]' camoufox
cargo run --features cli --bin camoufox -- press <instance_id> <page_id> Enter
# Set a file input's files (also works with hidden inputs):
cargo run --features cli --bin camoufox -- upload <instance_id> <page_id> 'input[type=file]' ./fixture.pdf
# Also available: type <text>, hover <selector>, select <selector> <value>, scroll [selector]
Iframe and Shadow-DOM Scoping
After upgrading, restart any running daemon: camoufox shutdown, then
camoufox serve. An older daemon ignores --frame, even when the client is new.
Scope commands to iframes with --frame <selector> (repeatable for nested iframes):
# Scope text extraction to an iframe:
cargo run --features cli --bin camoufox -- text <instance_id> <page_id> --frame '#ad-frame'
# Scope click to a nested iframe (accumulating frame coordinate offsets):
cargo run --features cli --bin camoufox -- click <instance_id> <page_id> '#btn' \
--frame '#outer-frame' --frame '#inner-frame'
# Supported across 12 commands: text, html, links, click, fill, upload, type, press, hover, select, wait, evaluate.
Clicks and hovers scroll ancestor frames and the target into view before measuring coordinates. Axis-aligned positive CSS scaling, borders, and padding are supported, including nested frames; rotated or skewed iframes are unsupported.
wait follows late-created/replaced iframes and late-attached open shadow roots.
Closed roots cannot be pierced in Camoufox's isolated world; an absent root cannot
reliably be distinguished from a closed one. Other commands report that the host
has no open shadow root. type and press also accept --timeout (seconds),
including the client socket budget. With --frame, they focus each ancestor
iframe, then the target window, and dispatch input only if a field is currently
focused there. If the active element is absent, body, or documentElement,
they fail with --frame <sel>: nothing is focused inside the frame; click or fill an element inside it first.
There is no remembered focus target and no focus-listener or blur-hook
instrumentation. After interacting with the top-level page, an agent must
click or fill inside the iframe again before using type --frame or
press --frame. Give the full --frame chain down to the frame that holds
the field (for a field in a nested iframe, pass both the outer and the inner
--frame): the chain is focused before the check, so a partial chain fails and
moves focus out of the inner frame. Plain type and press still type into
whatever is focused.
Pierce open shadow-DOM boundaries using the >>> combinator in any selector.
Operators inside CSS quotes, brackets, parentheses, or escapes remain ordinary CSS.
All matching hosts are searched depth-first in document order:
# Click a button inside an open shadow root:
cargo run --features cli --bin camoufox -- click <instance_id> <page_id> 'custom-element >>> button.action'
# Deeply nested shadow roots:
cargo run --features cli --bin camoufox -- text <instance_id> <page_id> --selector 'host-a >>> host-b >>> span.label'
Take a screenshot:
# Current viewport:
cargo run --features cli --bin camoufox -- screenshot <instance_id> <page_id> --format png -o /tmp/example.png
# Cropped to one element (scrolled into view first), or to an explicit region:
cargo run --features cli --bin camoufox -- screenshot <instance_id> <page_id> --selector 'article' -o /tmp/article.png
cargo run --features cli --bin camoufox -- screenshot <instance_id> <page_id> --clip 0,0,800,600 -o /tmp/region.png
Manage pages as tabs:
cargo run --features cli --bin camoufox -- tabs <instance_id>
cargo run --features cli --bin camoufox -- close-tab <instance_id> <page_id>
Tabs and popups opened by the site (e.g. via <a target="_blank"> or window.open()) are automatically tracked and appear in tabs on subsequent commands, complete with an "opener" field pointing to the parent page. When a page closes itself (via window.close()) or is closed, it is cleanly removed from tabs, and any subsequent command targeting it reports that the page was closed.
Manage downloads:
# Launch with an absolute download directory (defaults to cancel if omitted):
cargo run --features cli --bin camoufox -- launch --downloads-dir /path/to/downloads
# Click a download link, then wait/poll for download lifecycle events:
cargo run --features cli --bin camoufox -- click <instance_id> <page_id> '#download-link'
cargo run --features cli --bin camoufox -- downloads <instance_id> --wait 10
# Note: --wait wakes on the next event advance; poll until state is completed, failed, or canceled.
# Click responses also automatically carry a "downloads" list if triggered during the click.
# Inspect the download log for an instance:
cargo run --features cli --bin camoufox -- downloads <instance_id>
Inspect and stop:
cargo run --features cli --bin camoufox -- list
cargo run --features cli --bin camoufox -- stop <instance_id>
cargo run --features cli --bin camoufox -- shutdown
JSON output mode is available for all commands (--json). On failure, JSON error responses include a kind field identifying the error category (e.g. selector_not_found, timeout, dialog_open, invalid_argument, page_not_found, instance_not_found, io, daemon, etc.):
cargo run --features cli --bin camoufox -- --json list
Socket resolution:
--socket <path>to override- else
CAMOUFOX_SOCKETwhen set - else
$XDG_RUNTIME_DIR/camoufox/daemon.sock - else
/tmp/camoufox-<uid>/daemon.sock
Daemon security and lifecycle:
- The default runtime directory is created
0700. The daemon and the client both refuse to use it unless it is a real directory (not a symlink), owned by the current user, with no group/other permissions. On Linux the daemon also rejects connections from other uids (SO_PEERCRED). An explicit--socketis used as given, and the daemon never chmods its parent. - The daemon only ever replaces a stale socket at the path, never another kind of file.
It holds an exclusive lock on
<socket>.lockfor its lifetime, so a secondserveon the same path fails withdaemon is already running. shutdown, SIGTERM and SIGINT stop every browser, wait for in-flight requests, and remove the socket. On Linux, browsers are spawned withPR_SET_PDEATHSIG, so they die with the daemon even on SIGKILL (temporary profiles may remain in that case).- Screenshots without
-oare written to the runtime directory asscreenshot-<instance>-<page>.<ext>with mode0600. A relative-opath is resolved against the client's working directory. - Requests for different instances run in parallel. Requests to the same instance are
serialized.
launchholds no lock while the browser starts.
JavaScript dialogs
Dialogs use manual handling by default. When a page opens an alert, confirm,
prompt, or beforeunload dialog, respond with camoufox dialog <instance> <page> accept
or camoufox dialog <instance> <page> dismiss. For a prompt, supply the answer
with --text. Launch with --dialogs accept or --dialogs dismiss to handle
dialogs automatically; --dialogs manual restores the default.
Library Bootstrap Example
The low-level lifecycle is:
- Build
LaunchConfig - Spawn process (
process::unix::spawn) - Wait readiness sentinel on stdout or stderr
- Build
PipeTransport - Build
Connection+ root session Browser::connect(...)
use std::path::PathBuf;
use camoufox::api::{Browser, BrowserOptions, ContextOptions};
use camoufox::config::LaunchConfig;
use camoufox::process;
use camoufox::protocol::client::Connection;
use camoufox::transport::pipe::PipeTransport;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let profile_dir = std::env::temp_dir().join("camoufox-rs-profile");
std::fs::create_dir_all(&profile_dir)?;
let config = LaunchConfig {
executable: PathBuf::from("/root/.cache/camoufox/camoufox"),
profile_dir: Some(profile_dir),
headless: true,
..Default::default()
};
let mut launched = process::unix::spawn(&config)?;
process::readiness::wait_for_ready(&mut launched.child, config.timeout)?;
let transport = PipeTransport::new(launched.command_pipe, launched.response_pipe);
let conn = Connection::new(Box::new(transport));
let root = conn.root_session();
let browser = Browser::connect(conn, root, BrowserOptions::default())?;
let _context = browser.new_context(ContextOptions::default())?;
browser.close()?;
Ok(())
}
Pages via MainFrame
BrowserContext::new_main_frame() returns a fully wired MainFrame — a page
handle that is structurally pinned to the top frame. The call blocks until
the page target, top frame, and main-world execution context are all resolved
from authoritative protocol responses, so there is no manual session or
execution-context wiring to do.
This is the fix for the cross-origin-iframe attach bug: on sites that embed an
early out-of-process iframe (e.g. an ad pixel), the old handle could bind to the
iframe instead of the page — evaluate then ran in the wrong document.
new_main_frame() applies three filters so it can only ever resolve to the real
top frame:
- Layer 1 — accept only
Browser.attachedToTargetevents wheretargetInfo.type == "page" - Layer 2 — accept only the top frame's
Page.frameAttached(emptyparentFrameId) - Layer 3 — accept only the main-world
Runtime.executionContextCreatedwhoseauxData.frameIdmatches the top frame
# fn doc_test(browser: &camoufox::api::Browser) -> Result<(), Box<dyn std::error::Error>> {
use camoufox::api::ContextOptions;
use std::time::Duration;
// ...continuing from the bootstrap example, after `Browser::connect`:
let context = browser.new_context(ContextOptions::default())?;
// One call — no manual attachedToTarget / frameAttached / executionContext wiring:
let main_frame = context.new_main_frame()?;
// navigate(url, NavigateOptions, timeout) -> NavigateOutcome (nav_id and status_code)
let outcome: camoufox::api::main_frame::NavigateOutcome = main_frame.navigate("https://example.com", Default::default(), Duration::from_secs(30))?;
println!("status: {:?}", outcome.status_code);
// evaluate(expr, timeout) runs in the isolated world and returns the raw
// Runtime.evaluate envelope: read result.value and check exceptionDetails.
let raw: serde_json::Value = main_frame.evaluate("document.title", Duration::from_secs(15))?;
let title = raw["result"]["value"].as_str().unwrap_or_default();
println!("title: {title}");
// evaluate_main_world runs a synchronous expression in the page's own world
// and returns the plain JSON value (no envelope)
let app_state: serde_json::Value = main_frame.evaluate_main_world("window.location.href", Duration::from_secs(15))?;
println!("state: {app_state}");
# Ok(())
# }
A complete, runnable version is in examples/web_browse.rs:
cargo run --example web_browse -- --url https://example.com
End-to-end wiring is also exercised in tests/integration.rs and src/cli/instance.rs.
Reliability
- Every protocol request is bounded —
Session::sendenforces a default deadline (60s), and commands likenavigate/evaluate/waitaccept an explicittimeout(CLI:--timeout <seconds>), so a stuck call can no longer hang the daemon. - Navigations that the browser diverts into a download (e.g. a
Content-Disposition: attachmentURL) are detected viaBrowser.downloadCreatedand surfaced promptly as aNavigationBecameDownloaderror instead of blocking forever waiting for a navigation response that never arrives. Every context created byBrowser::new_contextcancels downloads unlessContextOptions::download_optionssays otherwise, so nothing is written to disk. - Event handlers run outside the connection lock and inside
catch_unwind: a panicking handler is logged instead of killing the connection, and a handler may call back into the connection. An undecodable frame from the browser is logged and skipped. - When a page closes (
Browser.detachedFromTarget) or crashes (Page.crashed), in-flight calls on it fail immediately withClosed/Crashed. If the browser goes away entirely, every pending call and event waiter is released. MainFrameremoves its event listeners when dropped, so opening and closing tabs in a long-running process does not accumulate handlers.- A failed launch (readiness timeout, bootstrap error) kills and reaps the browser.
Architecture
Core layers (top to bottom):
api/: high-levelBrowser,BrowserContext,MainFrameprotocol/: request IDs, pending map, session state, event router, reader threadtransport/: transport traits + Unix pipe transportcodec/: null-byte-delimited JSON framing (NulJsonCodec)process/: spawn/readiness/lifecycle around Camoufox child processcli/(feature-gated): daemon + command dispatch over Unix socketobs/: protocol logging helpers
Testing
Unit tests (--all-features is needed to build and test the CLI; plain
cargo test covers only the library):
cargo test --all-features
Integration tests against a real Camoufox binary are ignored by default:
cargo test --all-features --test integration -- --ignored --test-threads=1
Integration tests resolve the binary the same way as the daemon (see
Requirements), so a camoufox fetch install is found
automatically. Override per run with:
CAMOUFOX_BIN="/path/to/camoufox" cargo test --all-features --test integration -- --ignored --test-threads=1
Observability
Use log + env_logger filters to inspect protocol behavior:
RUST_LOG=camoufox::protocol=trace cargo test
Protocol traffic is traced at camoufox::protocol=trace with bounded payload previews.
Known Limitations
- Windows pipe transport is not implemented (
src/transport/pipe/windows.rshard errors at compile time) - API is synchronous/blocking today (no async runtime integration)
- Sub-frames are reached through
MainFrame(frames(),has_frame(),resolve_child_frame(),evaluate_in_frame()) and the CLI's--frame; there is no separate frame object type - Closed shadow roots cannot be pierced (
>>>reports them); rotated or skewed iframes are not supported for pointer coordinates (translation and scale are) type --frame/press --frameonly type into a field that is currently focused inside the frame; after touching the top-level page, click or fill inside the frame again- CLI daemon uses in-memory instance state only
- Requests to the same instance are serialized: a long-polling command (e.g.
wait --timeout 60) on one page blocks other commands for that instance until it returns. Other instances are unaffected. stopinterrupts in-flight commands and typically takes about one second.shutdownstops browsers in parallel and replies only after cleanup and socket removal.back/forwardare wired toPage.goBack/Page.goForwardbut are inert against the Camoufox builds tested here: pages created viaBrowser.newPageexpose no session history (history.length === 0), so both always report "no history entry". Re-navigateinstead.
License
MIT (see crate metadata in Cargo.toml).