crw icon indicating copy to clipboard operation
crw copied to clipboard

camoufox tier: reuse Cloudflare clearance across requests (per-host session) and allow camoufox-first ordering

Open iqiiidi-sudo opened this issue 1 month ago • 1 comments

Problem

The camoufox tier creates a fresh random userId for every request (rand_id("crw_") in crates/crw-renderer/src/camoufox.rs) and then calls DELETE /sessions/{userId}. A Cloudflare challenge solved by camoufox (cf_clearance) is therefore thrown away immediately, and every page of the same site waits for the challenge again (~12-20 s each on phoronix.com). The CDP tiers already reuse clearance through clearance.rs; camoufox does not.

camofox-browser already ships a persistence plugin that saves and restores cookies per userId (CAMOFOX_PROFILE_DIR), but with a random id per request it never gets a chance to help.

Proposal

  • Use a stable per-host userId (e.g. crw_<host>), so clearance is scoped to the site that issued it.
  • After the evaluate, close only the tab (DELETE /tabs/{tabId}?userId=) instead of the whole session. Concurrent requests to the same host share the session, so deleting it would kill a sibling's tab. The sidecar's own reaper closes empty sessions, and the persistence plugin checkpoints their cookies on close.
  • Optionally make this configurable (e.g. [renderer.camoufox] session_scope = "request" | "host"), keeping today's behaviour as the default if cookie isolation per request matters to you.

Measurements (crw 0.37.2, camofox-browser 1.11.2, residential IP)

Request upstream per-host session
phoronix.com, 1st page ~20 s 7.6 s
phoronix.com, 2nd/3rd page ~20 s each 1.5 s / 1.2 s
phoronix.com after camofox restart ~20 s 2.7 s (state restored from profile dir)

Related: tier order

In the auto ladder camoufox is always appended last (js_renderers build order), and there is no setting to change that. On our setup camoufox is about as fast as Chrome on ordinary pages (react.dev 1.8 s vs 1.2 s, docs.rs 1.9 s vs 2.2 s) but passes challenges Chrome is refused at, so Chrome-first costs a doomed 10-15 s attempt on protected sites. A config option for the JS tier order (or a "camoufox first" preference) would help. We currently patch fetch_with_js to sort_by_key(|r| r.name() != "camoufox") for non-pinned requests; screenshot requests are unaffected since camoufox is already filtered out for them.

Happy to send a PR if either direction is acceptable.

iqiiidi-sudo avatar Oct 16 '26 17:10 iqiiidi-sudo

thanks a lot for this, it's a really clear write-up and the phoronix numbers make the cost obvious.

we checked it against main and you're right on all counts. fetch mints a fresh rand_id("crw_") userId per request, and run_sequence always ends with DELETE /sessions/{userId}, so whatever camoufox solved gets dropped right away. the CDP tiers reuse clearance through clearance.rs, and camoufox doesn't touch it.

we'd happily take a PR for the per-host session. a few things that would make it easy to merge:

  • scope the userId the same way ClearanceCache::key scopes clearance: host|proxy|ua, with the host lowercased, the request proxy id (or -), and a constant ua since camoufox owns its fingerprint and ignores caller headers. please hash that into the userId (something like crw_<sha256 prefix>) rather than embedding it, because the proxy string can carry credentials and the userId ends up in logs and in the sidecar's profile dir.
  • make it the default behaviour, no new session_scope knob. the key already gives the isolation that matters.
  • close only the tab (DELETE /tabs/{tabId}?userId=) with the same best-effort cleanup timeout, and leave the session to the sidecar's reaper. taking clearance_cache().solve_lock(key) around the first cold solve would be a nice touch, so a burst of pages to one host waits for a single challenge instead of all hitting it at once.
  • update the wiremock tests that currently expect the /sessions delete, and the module doc that says "no cookie carry-over".

on the tier order: it's a fair point and something we're planning to look at, but it touches the auto ladder for everyone who has camoufox enabled, so let's keep it out of this PR and handle it separately.

thanks again, looking forward to it.

us avatar Oct 20 '26 20:10 us