runtime-foxdriver 0.1.4

Firefox browser automation via WebDriver BiDi (rustenium)
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
//! Captured browser cookies, preserve a solved-captcha session
//! across page loads.
//!
//! When a captcha is solved, the upstream WAF / vendor typically
//! issues one or more cookies (`cf_clearance`, `_pxhd`, `datadome`,
//! etc.) that grant the browser a window of trusted access. Without
//! capturing + replaying these cookies, every navigation re-triggers
//! the captcha challenge.
//!
//! [`capture_from_page`] grabs every cookie from the live page after
//! a successful solve. [`apply_to_page`] re-installs them on a fresh
//! page so the next request rides the trusted session.
//!
//! The capture path uses WebDriver BiDi `storage.getCookies`; the apply
//! path uses `storage.setCookie`. Both are wrapped here so consumers
//! don't need to import rustenium BiDi storage types directly.
use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};

/// A single captured browser cookie. Fields mirror the subset of
/// `Network.Cookie` that's relevant for replay.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct CapturedCookie {
    pub name: String,
    pub value: String,
    /// Cookie domain (leading-dot form preserved as-is).
    pub domain: String,
    pub path: String,
    /// Unix epoch seconds; `None` for session cookies that expire
    /// when the browser closes.
    pub expires: Option<i64>,
    pub secure: bool,
    pub http_only: bool,
    /// Cookie SameSite attribute as a lowercase string ("strict" /
    /// "lax" / "none"); `None` if unset.
    pub same_site: Option<String>,
}

impl CapturedCookie {
    /// True iff the cookie has an explicit expiry that has already
    /// passed. Session cookies (no expiry) are NOT considered
    /// expired by this helper, caller decides whether to replay
    /// them across browser restarts.
    pub fn is_expired_now(&self) -> bool {
        let Some(exp) = self.expires else {
            return false;
        };
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        exp <= now
    }

    /// Drop session cookies (no expiry) AND already-expired cookies
    /// from a slice. The remaining cookies are safe to persist
    /// across browser restarts and apply later.
    pub fn keep_persistent_alive(input: &[CapturedCookie]) -> Vec<CapturedCookie> {
        input
            .iter()
            .filter(|c| c.expires.is_some() && !c.is_expired_now())
            .cloned()
            .collect()
    }
}

/// Capture every cookie on `page` via BiDi `storage.getCookies`.
/// Includes HttpOnly cookies (no JavaScript limitation).
pub async fn capture_from_page(page: &crate::browser::Page) -> anyhow::Result<Vec<CapturedCookie>> {
    page.get_cookies().await
}

/// Apply previously-[`capture_from_page`]-captured cookies to a
/// fresh `page` via BiDi `storage.setCookie`. Skips entries that
/// have already expired; returns the count of cookies actually
/// installed.
pub async fn apply_to_page(
    page: &crate::browser::Page,
    cookies: &[CapturedCookie],
) -> anyhow::Result<usize> {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs() as i64)
        .unwrap_or(0);

    let mut installed = 0usize;
    for c in cookies {
        if let Some(exp) = c.expires {
            if exp <= now {
                continue; // skip expired
            }
        }
        let same_site = c.same_site.as_ref().and_then(|s| match s.to_lowercase().as_str() {
            "strict" => Some(rustenium_bidi_definitions::network::types::SameSite::Strict),
            "lax" => Some(rustenium_bidi_definitions::network::types::SameSite::Lax),
            "none" => Some(rustenium_bidi_definitions::network::types::SameSite::None),
            _ => None,
        });
        page.set_cookie(
            &c.name,
            &c.value,
            &c.domain,
            Some(&c.path),
            c.expires.map(|e| e as u64),
            Some(c.secure),
            Some(c.http_only),
            same_site,
        )
        .await?;
        installed += 1;
    }
    Ok(installed)
}

/// Filter `cookies` to only those whose name matches one of `vendor`'s
/// known anti-bot tokens. Useful for trimming a full session capture
/// down to the minimum subset that proves a vendor's challenge passed
///: handy when you want to forward auth state to a non-browser HTTP
/// client (curl/reqwest) without leaking unrelated session data.
///
/// The vendor → cookie-name map is derived from the bundled rule
/// pack's `cookie_names` triggers.
pub fn vendor_cookies(input: &[CapturedCookie], vendor: &str) -> Vec<CapturedCookie> {
    let names: &[&str] = match vendor.to_lowercase().as_str() {
        "cloudflare" | "cf" => &["__cf_bm", "cf_chl_2", "cf_clearance"],
        "akamai" => &["_abck", "bm_sz", "ak_bmsc"],
        "datadome" => &["datadome", "_dd_s"],
        "perimeterx" | "human" => &["_px2", "_pxhd", "_px3", "_pxvid"],
        "incapsula" | "imperva" => &["visid_incap", "incap_ses"],
        "kasada" => &["KP_UIDz", "x-kpsdk-cd", "x-kpsdk-ct"],
        "fastly" => &["_fastly_ngwaf"],
        "sucuri" => &["sucuri_cloudproxy_uuid"],
        "anubis" => &["anubis-auth"],
        _ => return Vec::new(),
    };
    input
        .iter()
        .filter(|c| names.iter().any(|n| c.name.starts_with(*n) || c.name == *n))
        .cloned()
        .collect()
}

#[cfg(test)]
mod vendor_cookie_tests {
    use super::*;

    fn ck(name: &str) -> CapturedCookie {
        CapturedCookie {
            name: name.into(),
            value: "test".into(),
            domain: ".example.com".into(),
            path: "/".into(),
            expires: None,
            secure: false,
            http_only: false,
            same_site: None,
        }
    }

    #[test]
    fn vendor_cookies_filters_cloudflare_set() {
        let all = vec![
            ck("__cf_bm"),
            ck("cf_clearance"),
            ck("session_id"),
            ck("_ga"),
        ];
        let filtered = vendor_cookies(&all, "cloudflare");
        let names: Vec<&str> = filtered.iter().map(|c| c.name.as_str()).collect();
        assert!(names.contains(&"__cf_bm"));
        assert!(names.contains(&"cf_clearance"));
        assert!(!names.contains(&"_ga"));
    }

    #[test]
    fn vendor_cookies_matches_prefixed_cookie_names() {
        // Imperva uses dynamic cookie suffixes like `visid_incap_<n>`.
        let all = vec![ck("visid_incap_12345"), ck("incap_ses_99_99")];
        let filtered = vendor_cookies(&all, "imperva");
        assert_eq!(filtered.len(), 2);
    }

    #[test]
    fn vendor_cookies_unknown_vendor_returns_empty() {
        let all = vec![ck("__cf_bm")];
        let filtered = vendor_cookies(&all, "totally-not-a-vendor");
        assert!(filtered.is_empty());
    }

    #[test]
    fn vendor_cookies_is_case_insensitive_on_vendor() {
        let all = vec![ck("__cf_bm")];
        assert_eq!(vendor_cookies(&all, "Cloudflare").len(), 1);
        assert_eq!(vendor_cookies(&all, "CLOUDFLARE").len(), 1);
        assert_eq!(vendor_cookies(&all, "cf").len(), 1);
    }

    #[test]
    fn vendor_cookies_akamai_set() {
        let all = vec![ck("_abck"), ck("bm_sz"), ck("ak_bmsc"), ck("other")];
        let filtered = vendor_cookies(&all, "akamai");
        assert_eq!(filtered.len(), 3);
    }

    #[test]
    fn vendor_cookies_datadome_set() {
        let all = vec![ck("datadome"), ck("_dd_s"), ck("session")];
        let filtered = vendor_cookies(&all, "datadome");
        assert_eq!(filtered.len(), 2);
    }

    #[test]
    fn vendor_cookies_perimeterx_aliases() {
        let all = vec![ck("_px2"), ck("_pxhd"), ck("_px3")];
        assert_eq!(vendor_cookies(&all, "perimeterx").len(), 3);
        assert_eq!(vendor_cookies(&all, "human").len(), 3);
    }

    #[test]
    fn vendor_cookies_kasada_set() {
        let all = vec![ck("KP_UIDz"), ck("x-kpsdk-cd")];
        assert_eq!(vendor_cookies(&all, "kasada").len(), 2);
    }

    #[test]
    fn vendor_cookies_fastly_set() {
        let all = vec![ck("_fastly_ngwaf"), ck("other")];
        assert_eq!(vendor_cookies(&all, "fastly").len(), 1);
    }

    #[test]
    fn vendor_cookies_sucuri_set() {
        let all = vec![ck("sucuri_cloudproxy_uuid")];
        assert_eq!(vendor_cookies(&all, "sucuri").len(), 1);
    }

    #[test]
    fn vendor_cookies_anubis_set() {
        let all = vec![ck("anubis-auth")];
        assert_eq!(vendor_cookies(&all, "anubis").len(), 1);
    }

    #[test]
    fn vendor_cookies_empty_input_returns_empty() {
        assert!(vendor_cookies(&[], "cloudflare").is_empty());
    }

    #[test]
    fn vendor_cookies_prefix_match_dynamic_suffix() {
        // Imperva cookies have dynamic suffixes; the vendor rule uses
        // `starts_with` so `visid_incap_` matches `visid_incap_12345`.
        let all = vec![ck("visid_incap_12345"), ck("visid_incap_99999")];
        let filtered = vendor_cookies(&all, "imperva");
        assert_eq!(filtered.len(), 2);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn cookie(name: &str, expires: Option<i64>) -> CapturedCookie {
        CapturedCookie {
            name: name.into(),
            value: "v".into(),
            domain: ".example.com".into(),
            path: "/".into(),
            expires,
            secure: true,
            http_only: false,
            same_site: None,
        }
    }

    #[test]
    fn is_expired_now_true_for_past_expires() {
        let c = cookie("c", Some(1)); // 1970-01-01: definitely past
        assert!(c.is_expired_now());
    }

    #[test]
    fn is_expired_now_false_for_far_future_expires() {
        let c = cookie("c", Some(i64::MAX));
        assert!(!c.is_expired_now());
    }

    #[test]
    fn is_expired_now_false_for_session_cookie() {
        let c = cookie("c", None);
        assert!(!c.is_expired_now());
    }

    #[test]
    fn keep_persistent_alive_drops_session_cookies() {
        let cookies = vec![
            cookie("session", None),
            cookie("persistent", Some(i64::MAX)),
        ];
        let kept = CapturedCookie::keep_persistent_alive(&cookies);
        assert_eq!(kept.len(), 1);
        assert_eq!(kept[0].name, "persistent");
    }

    #[test]
    fn keep_persistent_alive_drops_expired_cookies() {
        let cookies = vec![cookie("expired", Some(1)), cookie("alive", Some(i64::MAX))];
        let kept = CapturedCookie::keep_persistent_alive(&cookies);
        assert_eq!(kept.len(), 1);
        assert_eq!(kept[0].name, "alive");
    }

    #[test]
    fn captured_cookie_serde_roundtrip() {
        let c = cookie("cf_clearance", Some(1234567890));
        let json = serde_json::to_string(&c).unwrap();
        let back: CapturedCookie = serde_json::from_str(&json).unwrap();
        assert_eq!(c, back);
    }

    #[test]
    fn is_expired_now_false_at_exact_boundary() {
        // Since we compare `exp <= now`, a cookie that expires exactly at
        // the current second may or may not be expired depending on timing.
        // We test the structural property: `now` is >= 0 and `exp` = 0
        // should be expired because `0 <= now` is always true for now >= 0.
        let c = cookie("c", Some(0));
        assert!(c.is_expired_now());
    }

    #[test]
    fn is_expired_now_negative_expiry_treated_as_expired() {
        // Negative epoch seconds are in the past
        let c = cookie("c", Some(-1));
        assert!(c.is_expired_now());
    }

    #[test]
    fn keep_persistent_alive_empty_input() {
        let kept = CapturedCookie::keep_persistent_alive(&[]);
        assert!(kept.is_empty());
    }

    #[test]
    fn keep_persistent_alive_all_expired_returns_empty() {
        let cookies = vec![cookie("a", Some(1)), cookie("b", Some(2))];
        let kept = CapturedCookie::keep_persistent_alive(&cookies);
        assert!(kept.is_empty());
    }

    #[test]
    fn keep_persistent_alive_all_session_returns_empty() {
        let cookies = vec![cookie("a", None), cookie("b", None)];
        let kept = CapturedCookie::keep_persistent_alive(&cookies);
        assert!(kept.is_empty());
    }

    #[test]
    fn keep_persistent_alive_preserves_order() {
        let cookies = vec![
            cookie("first", Some(i64::MAX)),
            cookie("second", Some(i64::MAX - 1)),
        ];
        let kept = CapturedCookie::keep_persistent_alive(&cookies);
        assert_eq!(kept.len(), 2);
        assert_eq!(kept[0].name, "first");
        assert_eq!(kept[1].name, "second");
    }

    #[test]
    fn captured_cookie_equality() {
        let a = cookie("a", Some(100));
        let b = cookie("a", Some(100));
        let c = cookie("a", Some(200));
        assert_eq!(a, b);
        assert_ne!(a, c);
    }

    #[test]
    fn captured_cookie_serde_with_all_fields() {
        let c = CapturedCookie {
            name: "session".into(),
            value: "abc123".into(),
            domain: ".example.com".into(),
            path: "/api".into(),
            expires: Some(1893456000),
            secure: true,
            http_only: true,
            same_site: Some("strict".into()),
        };
        let json = serde_json::to_string(&c).unwrap();
        let back: CapturedCookie = serde_json::from_str(&json).unwrap();
        assert_eq!(c, back);
        assert!(json.contains("same_site"));
        assert!(json.contains("http_only"));
    }

    #[test]
    fn same_site_case_insensitive_mapping() {
        let inputs = ["Strict", "STRICT", "Lax", "LAX", "None", "NONE"];
        for s in inputs {
            let same_site = Some(s.to_string())
                .as_ref()
                .and_then(|str_val| match str_val.to_lowercase().as_str() {
                    "strict" => Some(rustenium_bidi_definitions::network::types::SameSite::Strict),
                    "lax" => Some(rustenium_bidi_definitions::network::types::SameSite::Lax),
                    "none" => Some(rustenium_bidi_definitions::network::types::SameSite::None),
                    _ => None,
                });
            assert!(same_site.is_some(), "failed to map same_site string '{s}'");
        }
    }
}