guise 0.1.8

Canonical browser fingerprint bundles, human-behavior timing, and TLS profile re-exports
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! guise → lurien engine-config bridge.
//!
//! lurien spoofs fingerprints in patched Gecko C++ rather than via injected
//! JS. It reads one JSON config blob from [`LURIEN_CONFIG_ENV`] at startup
//! and applies every surface natively, so there is nothing for a page to
//! `toString`-probe.
//!
//! This module maps guise's pure-data [`crate::ProfileOverrides`] onto that
//! config's key schema (`additions/camoucfg` `properties.json`), so the SAME
//! persona that drives the JS disguise also drives the engine, one source of
//! truth, two backends.
//!
//! The native-passthrough rule from the JS path carries over verbatim: when a
//! matched-host persona leaves `webgl_vendor`/`webgl_renderer` empty, the WebGL
//! keys are OMITTED so lurien exposes the host's real, self-consistent adapter
//! (whose pixels match) instead of a constant, see the WebGL note on
//! `FIREFOX_STEALTH_BODY`.

use crate::ProfileOverrides;
use runtime_foxdriver::browser::{launch_firefox_self_managed, FoxBrowserConfig, Page};
use serde_json::{json, Map, Value};
use std::os::unix::fs::PermissionsExt;
use std::sync::atomic::{AtomicU64, Ordering};

/// Per-launch counter so concurrent launches get unique temp dirs.
static LAUNCH_SEQ: AtomicU64 = AtomicU64::new(0);

/// Environment variable the lurien engine reads its JSON config from.
///
/// `MaskConfig.hpp` reads `LURIEN_CONFIG[_<n>]` first, then the one-release
/// alias `REYNARD_CONFIG[_<n>]`, then upstream `CAMOU_CONFIG[_<n>]`.
pub const LURIEN_CONFIG_ENV: &str = "LURIEN_CONFIG";
/// One-release alias. Prefer [`LURIEN_CONFIG_ENV`].
pub const REYNARD_CONFIG_ENV: &str = "REYNARD_CONFIG";

const MISSING_ENGINE: &str = "lurien engine not installed. Run install.sh or set LURIEN_BIN.";

/// Maximum bytes per env var before chunking into `LURIEN_CONFIG_1..N`.
/// Generous headroom under typical `ARG_MAX`/env limits; the engine concatenates
/// the numbered parts in order before parsing.
const ENV_CHUNK_BYTES: usize = 100_000;

/// Build the lurien engine config object from a persona.
///
/// Surfaces spoofed natively by the lurien engine (this function sets only the
/// keys guise owns):
///   - navigator: userAgent, webdriver, platform, hardwareConcurrency, language(s),
///     maxTouchPoints
///   - screen / window geometry: width, height, colorDepth, pixelDepth, avail*,
///     screenX/Y, outerWidth/Height, innerWidth/Height (coherent chrome band)
///   - fonts: system whitelist restricted to standard Linux families
///   - WebGL vendor/renderer when the persona carries one
///   - HTTP headers: User-Agent, Accept-Language (engine-emitted, not JS)
///
/// These are intentionally handled by the engine rather than `profile_js` / the
/// guise fingerprint evasion layer. The lurien path therefore does NOT call
/// `apply_stealth_profile` or `apply_fingerprint`; injecting the JS layer would
/// fight the engine's own patches and re-introduce coherence tells.
///
/// DEVICE-NOISE SEEDS are NOT set here: [`lurien_config`] is the pure persona map;
/// the per-identity `audio:seed` / `fonts:spacing_seed` / `canvas:seed` are added by
/// [`launch_with_config`] (it owns the profile_dir → seed derivation). What
/// each does on the lurien ENGINE, verified live
/// (`tests/lurien_canvas_audio_farble_live.rs`):
///   * `audio:seed` → `AudioFingerprintManager` farbles the audio fingerprint. Audio
///     FP is a WINDOW-only surface (Firefox exposes no `OfflineAudioContext` in a
///     Worker), so window coverage is total coverage.
///   * `fonts:spacing_seed` → `FontSpacingSeedManager` perturbs glyph spacing, which
///     shifts any TEXT-based canvas fingerprint, and it reaches EVERY realm
///     (window == worker confirmed), the engine-level coverage the stock-FF JS preload
///     cannot give a Worker's OffscreenCanvas.
///   * `canvas:seed` → INERT: NO reader exists in the lurien tree, so pure-SHAPE
///     (non-text) 2D-canvas pixels are NOT noised. Set for forward-compat (a future
///     engine canvas manager would activate it with no wiring change).
///
/// So with a seed the lurien canvas/audio FP DIFFERS from the bare host and is
/// realm-coherent; WITHOUT one (ephemeral) it is the real host FP. (`fonts`/`webGl`/
/// navigator/screen ARE engine-spoofed by the keys below.)
#[must_use]
pub fn lurien_config(overrides: &ProfileOverrides) -> Value {
    let mut cfg = Map::new();

    // ── navigator ───────────────────────────────────────────────────────────
    cfg.insert("navigator.userAgent".into(), json!(overrides.user_agent));
    // Driven over BiDi, the engine's Navigator::Webdriver() otherwise returns
    // true (remote agent active), the sannysoft "WebDriver" tell. The lurien
    // engine patch honors this LURIEN_CONFIG key; pin it false for every persona.
    cfg.insert("navigator.webdriver".into(), json!(false));
    cfg.insert("navigator.platform".into(), json!(overrides.platform));
    cfg.insert(
        "navigator.hardwareConcurrency".into(),
        json!(overrides.hardware_concurrency),
    );
    if let Some(primary) = overrides.languages.first() {
        cfg.insert("navigator.language".into(), json!(primary));
    }
    if !overrides.languages.is_empty() {
        cfg.insert("navigator.languages".into(), json!(overrides.languages));
        // The engine's browser-init derives `intl.accept_languages`: which is what
        // populates the `navigator.languages` array, from `locale:all` first, and
        // only falls back to the single `navigator.language` otherwise. Without
        // `locale:all` the array collapses to the primary alone (`["en-US"]`,
        // length 1), a coherence tell the differential oracle flags against a real
        // FF-Linux en-US (`["en-US","en"]`). Emit the full list in the comma-space
        // form Firefox uses for the pref so the derived array matches exactly.
        cfg.insert("locale:all".into(), json!(overrides.languages.join(", ")));
    }
    cfg.insert(
        "navigator.maxTouchPoints".into(),
        json!(if overrides.mobile { 5 } else { 0 }),
    );

    // ── screen ──────────────────────────────────────────────────────────────
    cfg.insert("screen.width".into(), json!(overrides.screen_width));
    cfg.insert("screen.height".into(), json!(overrides.screen_height));
    cfg.insert("screen.colorDepth".into(), json!(overrides.color_depth));
    cfg.insert("screen.pixelDepth".into(), json!(overrides.color_depth));
    // ── window / available-screen geometry coherence ─────────────────────────
    // The engine spoofs `screen.width/height` but NOT, by default, the available
    // area or the window box, so it leaks the HOST display via `screen.avail*`
    // (e.g. avail 3840x2160 while screen claims 1920x1080) AND opens a headful
    // window sized for the real monitor, taller than the spoofed screen. Both are
    // the incolumitas `PHANTOM_WINDOW_HEIGHT` class of tell (`outerHeight >
    // screen.height`, window off-screen), proven by `tests/lurien_window_geometry`
    // against a real Firefox baseline. Pin a coherent MAXIMIZED window on the
    // persona's screen: a Linux desktop reserves no persistent taskbar (avail ==
    // screen, matching a real FF-Linux capture), the frame fills the screen at the
    // origin, and the content area sits below a realistic toolbar/tab/bookmarks
    // band. The 124px band is tuned so the spoofed `innerHeight` equals the engine's
    // ACTUAL rendered viewport (visualViewport / documentElement.clientHeight), a
    // larger band left `window.innerHeight` reporting ~36px more than the real
    // layout, itself a softer tell; `tests/lurien_window_geometry` pins the match.
    const CHROME_BAND: u32 = 124;
    cfg.insert("screen.availLeft".into(), json!(0));
    cfg.insert("screen.availTop".into(), json!(0));
    cfg.insert("screen.availWidth".into(), json!(overrides.screen_width));
    cfg.insert("screen.availHeight".into(), json!(overrides.screen_height));
    cfg.insert("window.screenX".into(), json!(0));
    cfg.insert("window.screenY".into(), json!(0));
    cfg.insert("window.outerWidth".into(), json!(overrides.screen_width));
    cfg.insert("window.outerHeight".into(), json!(overrides.screen_height));
    cfg.insert("window.innerWidth".into(), json!(overrides.screen_width));
    // Desktop carries a toolbar/tab band; a mobile persona is chromeless fullscreen
    // (outer == inner == screen), so subtracting a desktop chrome band there would
    // itself be the tell. Verified for desktop by `lurien_window_geometry`.
    let inner_height = if overrides.mobile {
        overrides.screen_height
    } else {
        overrides.screen_height.saturating_sub(CHROME_BAND)
    };
    cfg.insert("window.innerHeight".into(), json!(inner_height));

    // ── fonts, restrict the visible set to a common standard-Linux list. The
    //    engine maps this to font.system.whitelist, so font-list fingerprinting
    //    sees a small coherent set instead of the host's identifying ~300-family
    //    install (which leaked CJK/extra families). Every family below ships on
    //    typical Linux desktops, so the engine's measureText enumeration stays
    //    coherent. Linux persona only (cross-OS personas need their own list). ──
    if overrides.platform.contains("Linux") {
        cfg.insert(
            "fonts".into(),
            json!(crate::fingerprint::font_tier_b::LINUX_STANDARD_FONTS),
        );
    }

    // ── WebGL, native passthrough: only pin when the persona explicitly
    //    carries a (cross-OS) adapter. Empty = expose the real one. ──
    if !overrides.webgl_vendor.is_empty() {
        cfg.insert("webGl:vendor".into(), json!(overrides.webgl_vendor));
    }
    if !overrides.webgl_renderer.is_empty() {
        cfg.insert("webGl:renderer".into(), json!(overrides.webgl_renderer));
    }

    // ── HTTP header coherence (engine emits these so the network layer matches
    //    the JS surface) ──
    cfg.insert("headers.User-Agent".into(), json!(overrides.user_agent));
    if !overrides.languages.is_empty() {
        cfg.insert(
            "headers.Accept-Language".into(),
            json!(accept_language(&overrides.languages)),
        );
    }

    Value::Object(cfg)
}

/// Render the persona as the `(env_name, value)` pairs to set on the lurien
/// process. Returns chunked `LURIEN_CONFIG_1..N` pairs when the JSON exceeds
/// [`ENV_CHUNK_BYTES`], else a single [`LURIEN_CONFIG_ENV`] pair.
#[must_use]
pub fn lurien_config_env(overrides: &ProfileOverrides) -> Vec<(String, String)> {
    let blob = lurien_config(overrides).to_string();
    if blob.len() <= ENV_CHUNK_BYTES {
        return vec![(LURIEN_CONFIG_ENV.to_string(), blob)];
    }
    blob.as_bytes()
        .chunks(ENV_CHUNK_BYTES)
        .enumerate()
        .map(|(i, part)| {
            (
                format!("{LURIEN_CONFIG_ENV}_{}", i + 1),
                String::from_utf8_lossy(part).into_owned(),
            )
        })
        .collect()
}

/// Build an `Accept-Language` header value from a language list
/// (`en-US,en;q=0.9,fr;q=0.8` style) so it coheres with `navigator.languages`.
fn accept_language(langs: &[String]) -> String {
    langs
        .iter()
        .enumerate()
        .map(|(i, lang)| {
            if i == 0 {
                lang.clone()
            } else {
                // q drops 0.1 per position, floored at 0.1.
                let q = (10 - i.min(9)) as f64 / 10.0;
                format!("{lang};q={q}")
            }
        })
        .collect::<Vec<_>>()
        .join(",")
}

/// The wrapper shell script that injects the persona config and execs the real
/// lurien binary. Pure (no IO) so the shell logic is unit-testable.
///
/// rustenium spawns the browser binary directly and exposes no per-child env
/// hook, so we front `engine_bin` with this wrapper: it loads `LURIEN_CONFIG`
/// from `cfg_path` (a file, so any JSON byte survives shell quoting) and forwards
/// rustenium's BiDi launch flags to the binary via `"$@"`. Per-launch and
/// concurrency-safe, no process-global env, no `unsafe` set_var (guise forbids
/// unsafe).
fn lurien_wrapper_script(cfg_path: &str, engine_bin: &str) -> String {
    // Installed Camoufox (June 2026) reads REYNARD_CONFIG then CAMOU_CONFIG.
    // Source MaskConfig also reads LURIEN_CONFIG. Export all three so either
    // binary applies persona geometry. Same JSON, per-launch, no global env.
    format!(
        "#!/bin/sh\n\
         # guise -> lurien launch wrapper (per-launch config, no global env)\n\
         _lurien_cfg=\"$(cat '{cfg_path}')\"\n\
         LURIEN_CONFIG=\"$_lurien_cfg\"\n\
         REYNARD_CONFIG=\"$_lurien_cfg\"\n\
         CAMOU_CONFIG=\"$_lurien_cfg\"\n\
         export LURIEN_CONFIG REYNARD_CONFIG CAMOU_CONFIG\n\
         exec '{engine_bin}' \"$@\"\n",
    )
}

/// A Firefox-family binary's **major** version, parsed from `<bin> --version`
/// (`"Mozilla Camoufox 150.0.2-beta.25"` → `150`; `"Mozilla Firefox 151.0.3"` →
/// `151`). Works for the lurien engine binary AND a stock Firefox.
///
/// A real Firefox build's TLS (JA3/JA4) and version-gated JS features are the
/// engine's actual version. The persona UA, by contrast, is pinned to whatever
/// the TLS-impersonate **HTTP-client** profile ships, a different path. Both
/// the lurien launch and the stock-Firefox profiled launch align the persona UA
/// to this engine major so navigator.userAgent claims the version the engine
/// truly is. Returns `None` (caller keeps the persona UA) if `--version` can't
/// run.
#[must_use]
pub fn firefox_engine_major(bin: &str) -> Option<u32> {
    let out = std::process::Command::new(bin)
        .arg("--version")
        .output()
        .ok()?;
    let text = String::from_utf8_lossy(&out.stdout);
    // First whitespace token that looks like a dotted version (e.g. "150.0.2").
    text.split_whitespace()
        .find(|t| t.contains('.') && t.chars().next().is_some_and(|c| c.is_ascii_digit()))
        .and_then(|t| t.split('.').next())
        .and_then(|n| n.parse::<u32>().ok())
}

/// Rewrite the Firefox **major** version in a UA string to `engine_major`,
/// preserving structure (only `rv:<n>.` and `Firefox/<n>.` change). A no-op when
/// the persona already matches or no Firefox token is present.
#[must_use]
pub fn align_ua_to_engine(ua: &str, engine_major: u32) -> String {
    let persona_major = ua
        .rsplit("Firefox/")
        .next()
        .filter(|_| ua.contains("Firefox/"))
        .and_then(|s| s.split('.').next())
        .and_then(|s| s.parse::<u32>().ok());
    match persona_major {
        Some(p) if p != engine_major => ua
            .replace(&format!("rv:{p}."), &format!("rv:{engine_major}."))
            .replace(
                &format!("Firefox/{p}."),
                &format!("Firefox/{engine_major}."),
            ),
        _ => ua.to_string(),
    }
}

/// Stable 64-bit seed from an account key (its `profile_dir`), so per-identity
/// canvas/audio/font noise is deterministic per account, identical across that
/// account's sessions, different across accounts. FNV-1a: no deps, well spread.
#[must_use]
pub fn identity_seed(key: &str) -> u64 {
    let mut h: u64 = 0xcbf2_9ce4_8422_2325;
    for b in key.as_bytes() {
        h ^= u64::from(*b);
        h = h.wrapping_mul(0x0000_0100_0000_01b3);
    }
    h
}

/// Resolve the lurien engine binary. Missing binary is an error.
/// Never falls back to `/usr/bin/firefox`.
///
/// Order (first non-empty wins):
/// 1. `LURIEN_BIN`
/// 2. `REYNARD_BIN` (one-release alias)
/// 3. `GUISE_REYNARD_BIN` (one-release alias)
/// 4. `~/.local/share/lurien/lurien`, then the old `~/.local/share/reynard/reynard`
///    (and cache / `/opt` twins)
pub fn resolve_lurien_bin() -> anyhow::Result<String> {
    resolve_lurien_bin_from(
        |k| std::env::var(k).ok(),
        |p| std::path::Path::new(p).exists(),
    )
}

/// Live-test seam. `LURIEN_BIN`, then one-release aliases. Unset → `None`
/// so gates skip-loud. Does not require the path to exist.
#[must_use]
pub fn live_engine_bin() -> Option<String> {
    for var in ["LURIEN_BIN", "REYNARD_BIN", "GUISE_REYNARD_BIN"] {
        if let Ok(s) = std::env::var(var) {
            let s = s.trim().to_string();
            if !s.is_empty() {
                return Some(s);
            }
        }
    }
    None
}

fn resolve_lurien_bin_from(
    env: impl Fn(&str) -> Option<String>,
    exists: impl Fn(&str) -> bool,
) -> anyhow::Result<String> {
    for var in ["LURIEN_BIN", "REYNARD_BIN", "GUISE_REYNARD_BIN"] {
        if let Some(s) = env(var) {
            let s = s.trim().to_string();
            if !s.is_empty() {
                return Ok(s);
            }
        }
    }
    let home = env("HOME").unwrap_or_default();
    [
        format!("{home}/.local/share/lurien/lurien"),
        format!("{home}/.local/share/reynard/reynard"),
        format!("{home}/.cache/lurien/lurien"),
        format!("{home}/.cache/reynard/reynard"),
        "/opt/lurien/lurien".to_string(),
        "/opt/reynard/reynard".to_string(),
    ]
    .into_iter()
    .find(|p| exists(p))
    .ok_or_else(|| anyhow::anyhow!("{MISSING_ENGINE}"))
}

/// Launch the lurien engine binary, driven over BiDi, with `profile`'s
/// fingerprint applied **natively** via `LURIEN_CONFIG` (no JS injection).
///
/// Each call writes its own config + wrapper to a unique temp dir, so concurrent
/// launches with different personas never collide. Run `headless = false` on a
/// real-GPU display for zero headless tells; lurien's pointer/headless patches
/// also keep headless coherent if you must use it.
///
/// The returned [`Page`] is the same handle the JS-disguise path returns, so the
/// differential oracle ([`crate::probe::diff_pages`]) can diff a lurien page
/// against a stock-Firefox page directly, the "patched vs stock = identical"
/// gate.
pub async fn launch_lurien(
    lurien_bin: &str,
    profile: &crate::StealthProfile,
    headless: bool,
) -> anyhow::Result<Page> {
    launch_with_config(
        lurien_bin,
        profile,
        FoxBrowserConfig {
            headless,
            viewport_width: 1280,
            viewport_height: 720,
            ..Default::default()
        },
    )
    .await
}

/// Launch the lurien engine, MERGING the per-launch wrapper into a
/// caller-supplied [`FoxBrowserConfig`] so the caller's proxy, profile dir,
/// viewport, and any extra prefs are preserved.
///
/// This is the integration point for long-lived drivers (e.g. the guise bridge
/// that Meridian talks to): they build a `FoxBrowserConfig` with the session's
/// `profile_dir` + residential `proxy`, and lurien adds only the engine
/// wrapper (`executable_path` → the LURIEN_CONFIG-exporting launcher) and the
/// `navigator.webdriver` pref. `config.executable_path` is overridden; the
/// lurien pref is appended to any existing `user_js_content`; `config.proxy`
/// and `config.profile_dir` flow through to `launch_firefox_self_managed`
/// (which still appends the proxy prefs), so residential egress + session
/// persistence are unchanged versus the plain-Firefox path.
pub async fn launch_with_config(
    lurien_bin: &str,
    profile: &crate::StealthProfile,
    mut config: FoxBrowserConfig,
) -> anyhow::Result<Page> {
    // X007 / X045, enforce the unified persona coherence contract at the lurien
    // launch boundary too, via the SAME shared primitive the stock-Firefox path uses.
    // lurien drives a REAL Gecko engine, but the persona it wears must still be
    // self-consistent JS-to-wire; a malformed persona is just as detectable behind
    // lurien. `config.proxy` flows through to the engine, so a configured proxy
    // correctly suppresses the host-TTL warning.
    super::enforce_persona_launch_coherence(profile, config.proxy.is_none())?;

    let mut overrides = crate::profile_to_overrides(profile);
    // Align the persona UA to the ACTUAL lurien engine version. The persona's
    // own UA major tracks the TLS-impersonate HTTP profile (a different,
    // HTTP-client path); the lurien BROWSER uses real Gecko TLS + features, so
    // claiming the engine's true version keeps navigator.userAgent coherent with
    // JA3/JA4 and version-gated JS. No-op if `--version` is unreadable.
    if let Some(major) = firefox_engine_major(lurien_bin) {
        overrides.user_agent = align_ua_to_engine(&overrides.user_agent, major);
    }
    // Per-identity device noise seed. ALWAYS set, mirroring the stock-FF path:
    //   * profile_dir persona → STABLE seed from the dir, so the account reads as the
    //     SAME device across its own sessions but DIFFERENT from other accounts
    //     (defeating the serial-signup correlation an anti-bot blocks on).
    //   * ephemeral persona (no profile_dir) → RANDOM per-launch seed, so repeated
    //     ephemeral launches from one host are NOT linkable by audio/canvas FP. Before
    //     this, ephemeral left the seeds UNSET and leaked the stable REAL-host audio +
    //     text-canvas FP (confirmed live, tests/lurien_canvas_audio_farble_live.rs:
    //     no-pdir == bare host), a parity gap vs the stock-FF path, which already
    //     random-seeds an ephemeral persona.
    // Effect of each seed on the engine (verified, same test):
    //   * audio:seed → AudioFingerprintManager farbles the (window-only) audio FP.
    //   * fonts:spacing_seed → FontSpacingSeedManager shifts TEXT-canvas FP in EVERY
    //     realm (window == worker) (engine-level worker coverage the JS path lacks).
    //   * canvas:seed → INERT (no engine reader); set for forward-compat only.
    // Safe for lurien_gate: its canvas/audio probes are Medium-severity STABILITY/shape
    // checks (farble-invariant), and the gate asserts only on High-severity divergence.
    let mut cfg = lurien_config(&overrides);
    if let Value::Object(map) = &mut cfg {
        let seed = match config.profile_dir.as_deref().filter(|d| !d.is_empty()) {
            Some(dir_key) => identity_seed(dir_key) % 1_000_000,
            None => u64::from(rand::random::<u32>()) % 1_000_000,
        };
        map.insert("canvas:seed".into(), json!(seed));
        map.insert("audio:seed".into(), json!(seed));
        map.insert("fonts:spacing_seed".into(), json!(seed));
        // NB: WebRTC IP masking is NOT a config key (the engine reads it only
        // via the content-callable window.setWebRTCIPv4); the guise-bridge
        // applies it per-launch from the same identity_seed.
    }
    let config_json = cfg.to_string();

    let seq = LAUNCH_SEQ.fetch_add(1, Ordering::Relaxed);
    let dir = std::env::temp_dir().join(format!("lurien-launch-{}-{}", std::process::id(), seq));
    std::fs::create_dir_all(&dir)
        .map_err(|e| anyhow::anyhow!("create lurien launch dir {dir:?}: {e}"))?;

    let cfg_path = dir.join("config.json");
    std::fs::write(&cfg_path, &config_json)
        .map_err(|e| anyhow::anyhow!("write lurien config: {e}"))?;

    let wrapper_path = dir.join("launch.sh");
    let script = lurien_wrapper_script(&cfg_path.to_string_lossy(), lurien_bin);
    std::fs::write(&wrapper_path, script)
        .map_err(|e| anyhow::anyhow!("write lurien wrapper: {e}"))?;
    let mut perms = std::fs::metadata(&wrapper_path)
        .map_err(|e| anyhow::anyhow!("stat lurien wrapper: {e}"))?
        .permissions();
    perms.set_mode(0o755);
    std::fs::set_permissions(&wrapper_path, perms)
        .map_err(|e| anyhow::anyhow!("chmod lurien wrapper: {e}"))?;

    // Timezone: pin the persona's IANA zone as a per-process `TZ` env var on the
    // engine process (the same mechanism the stock-Firefox path uses). ICU reads TZ
    // in EVERY realm, so window AND dedicated-Worker `Intl`/`Date` report the
    // persona zone, DST-correct. Without it lurien inherited the HOST zone in both
    // realms (verified live, `tests/lurien_session_tells.rs`: a FirefoxLinux
    // persona reported `America/Phoenix`: the host, not the persona's
    // `America/New_York`), leaking the host machine's location and correlating
    // every identity launched from one host. The wrapper `exec`s the engine, so the
    // env set on the wrapper process is inherited by lurien. Per-process, so
    // concurrent launches with different personas never race. The caller can
    // realign `overrides.timezone` to a proxy's egress geography via
    // `ProfileOverrides::with_timezone` before launch.
    config
        .env
        .push(("TZ".to_string(), overrides.timezone.clone()));

    // The engine wrapper replaces the executable; everything else the caller set
    // (proxy, profile_dir, viewport, headless) is preserved.
    config.executable_path = Some(wrapper_path.to_string_lossy().into_owned());
    // `navigator.webdriver` is set true by the BiDi remote agent; disable the
    // pref at the PROFILE layer so the getter reads false WITHOUT a JS override
    // that `.toString()` could leak. (`remote.enabled` is deliberately NOT
    // touched: BiDi needs it to connect.) Append rather than replace so a
    // caller's prefs survive; foxdriver still appends `config.proxy` prefs.
    config.user_js_content = Some(merge_lurien_prefs(config.user_js_content.take()));

    // G126 / R148: capture the identity key before `config` is consumed by
    // launch, so session-age seeding can reuse the same stable seed that drives
    // canvas/audio/font noise.
    let age_key: String = config
        .profile_dir
        .as_deref()
        .filter(|d| !d.is_empty())
        .unwrap_or("guise-default-session-age")
        .to_string();

    // Self-managed launch: foxdriver spawns the wrapper (which exports
    // LURIEN_CONFIG then execs the engine) and polls the debug port until it is
    // live before attaching. A freshly-built Camoufox binds its BiDi port in
    // ~1 s, past rustenium's fixed 500 ms post-spawn sleep, so the default
    // managed launch races to a ConnectionRefused; the readiness poll fixes it.
    let page = launch_firefox_self_managed(config).await?;

    // Seed a plausible session age so a fresh profile does not broadcast
    // `history.length == 0` and an empty localStorage.
    let session_age = crate::browser::generate_session_age(identity_seed(&age_key) % 1_000_000);
    crate::browser::apply_session_age(&page, &session_age)
        .await
        .map_err(|e| anyhow::anyhow!("launch_with_config: apply session age: {e}"))?;

    Ok(page)
}

/// Append [`LURIEN_PROFILE_PREFS`] to any caller-supplied `user.js`, so the
/// `navigator.webdriver` pref is always present without clobbering the caller's
/// prefs. Pure (unit-tested).
fn merge_lurien_prefs(existing: Option<String>) -> String {
    match existing {
        Some(e) if !e.trim().is_empty() => format!("{e}\n{LURIEN_PROFILE_PREFS}"),
        _ => LURIEN_PROFILE_PREFS.to_string(),
    }
}

/// Profile prefs written for every lurien launch. Only automation tells that
/// the engine/`LURIEN_CONFIG` does not already cover belong here, and nothing that
/// would break the BiDi transport (`remote.enabled` stays on).
///
/// `security.ssl3.ecdhe_ecdsa_aes_128_sha` (cipher 0xc009,
/// TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA) is the RUNTIME twin of the
/// `settings/camoufox.cfg` build-time fix: Camoufox's `StaticPrefList.yaml`
/// defaults this single ECDHE-CBC suite to FALSE while every sibling defaults
/// TRUE, so lurien's ClientHello carries 16 ciphers where stock Firefox 150
/// carries 17, a JA3/JA4 cipher-hash + peetprint tell. Setting it here closes
/// the tell on EVERY binary at launch, including a binary built before the
/// camoufox.cfg bake (the current release), so the fix does not wait on a rebuild.
/// Harmless once the rebuild bakes the same default in (defence in depth).
/// Verified == stock FF-150 (17 ciphers) live on tls.peet.ws.
const LURIEN_PROFILE_PREFS: &str = "user_pref(\"dom.webdriver.enabled\", false);\n\
     user_pref(\"security.ssl3.ecdhe_ecdsa_aes_128_sha\", true);";

#[cfg(test)]
#[path = "lurien/tests.rs"]
mod tests;