browser-commander 0.13.0

Universal browser automation library that supports multiple browser engines with a unified API
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
//! Translate a fingerprint profile into CDP `Emulation` commands.
//!
//! These are the overrides Chrome itself enforces. They apply to workers and to
//! outgoing HTTP headers, not only to the main world, which is what makes them
//! strictly better than patching JavaScript properties. Anything that has no
//! command here needs a page init script instead; [`super::init_script`]
//! carries the weaker half and `docs/case-studies/issue-79/requirements.md`
//! records why.
//!
//! This is the Rust side of `js/src/fingerprint/cdp-overrides.js`; the command
//! list is asserted field by field in every language so the three cannot drift.

use serde::Serialize;
use serde_json::{json, Map, Value};

use super::profile::{FingerprintProfile, UserAgentData};

/// One protocol call: a method name and its parameters.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct CdpCommand {
    /// The CDP method, for example `Emulation.setUserAgentOverride`.
    pub method: &'static str,
    /// The parameters, shaped exactly as the protocol expects them.
    pub params: Value,
}

impl CdpCommand {
    fn new(method: &'static str, params: Value) -> Self {
        Self { method, params }
    }
}

fn insert_if_some<T: Serialize>(params: &mut Map<String, Value>, key: &str, value: &Option<T>) {
    if let Some(value) = value {
        params.insert(key.to_string(), json!(value));
    }
}

fn user_agent_metadata(data: &UserAgentData) -> Value {
    // platform, platformVersion, architecture, model and mobile are required by
    // the protocol; Chrome rejects the command when any of them is missing.
    let mut metadata = Map::new();
    metadata.insert(
        "platform".to_string(),
        json!(data.platform.clone().unwrap_or_default()),
    );
    metadata.insert(
        "platformVersion".to_string(),
        json!(data.platform_version.clone().unwrap_or_default()),
    );
    metadata.insert(
        "architecture".to_string(),
        json!(data.architecture.clone().unwrap_or_default()),
    );
    metadata.insert(
        "model".to_string(),
        json!(data.model.clone().unwrap_or_default()),
    );
    metadata.insert("mobile".to_string(), json!(data.mobile.unwrap_or(false)));
    insert_if_some(&mut metadata, "brands", &data.brands);
    insert_if_some(&mut metadata, "fullVersionList", &data.full_version_list);
    insert_if_some(&mut metadata, "bitness", &data.bitness);
    // Deprecated in the protocol, but `fullVersionList` does not cover the
    // `uaFullVersion` hint: without this the page still reads the real Chrome
    // build number.
    insert_if_some(&mut metadata, "fullVersion", &data.full_version);
    insert_if_some(&mut metadata, "wow64", &data.wow64);
    insert_if_some(&mut metadata, "formFactors", &data.form_factors);
    Value::Object(metadata)
}

fn user_agent_command(profile: &FingerprintProfile) -> Option<CdpCommand> {
    if profile.user_agent.is_none() && profile.accept_language.is_none() {
        return None;
    }
    let mut params = Map::new();
    // userAgent is a required parameter even when only the language changes.
    params.insert(
        "userAgent".to_string(),
        json!(profile.user_agent.clone().unwrap_or_default()),
    );
    insert_if_some(&mut params, "acceptLanguage", &profile.accept_language);
    insert_if_some(&mut params, "platform", &profile.platform);
    if let Some(data) = &profile.user_agent_data {
        params.insert("userAgentMetadata".to_string(), user_agent_metadata(data));
    }
    Some(CdpCommand::new(
        "Emulation.setUserAgentOverride",
        Value::Object(params),
    ))
}

fn device_metrics(profile: &FingerprintProfile) -> Option<Value> {
    if profile.viewport.is_none() && profile.screen.is_none() {
        return None;
    }
    let viewport = profile.viewport.clone().unwrap_or_default();
    let mut params = Map::new();
    // 0 means "no override" for the viewport, so a profile that only sets
    // screen dimensions still leaves the real window size alone.
    params.insert("width".to_string(), json!(viewport.width.unwrap_or(0)));
    params.insert("height".to_string(), json!(viewport.height.unwrap_or(0)));
    params.insert(
        "deviceScaleFactor".to_string(),
        json!(viewport.device_scale_factor.unwrap_or(0.0)),
    );
    params.insert(
        "mobile".to_string(),
        json!(viewport.mobile.unwrap_or(false)),
    );
    // The profile guarantees width and height come as a pair, so the page never
    // sees a screen with one dimension overridden and the other real.
    if let Some(screen) = &profile.screen {
        if let (Some(width), Some(height)) = (screen.width, screen.height) {
            params.insert("screenWidth".to_string(), json!(width));
            params.insert("screenHeight".to_string(), json!(height));
        }
    }
    Some(Value::Object(params))
}

fn emulated_media_features(profile: &FingerprintProfile) -> Vec<Value> {
    let mut features = Vec::new();
    if let Some(value) = &profile.reduced_motion {
        features.push(json!({"name": "prefers-reduced-motion", "value": value}));
    }
    if let Some(value) = &profile.forced_colors {
        features.push(json!({"name": "forced-colors", "value": value}));
    }
    if let Some(value) = &profile.color_scheme {
        features.push(json!({"name": "prefers-color-scheme", "value": value}));
    }
    features
}

/// Build the ordered CDP command list for a normalized profile.
pub fn build_cdp_emulation_commands(profile: &FingerprintProfile) -> Vec<CdpCommand> {
    let mut commands = Vec::new();

    if let Some(command) = user_agent_command(profile) {
        commands.push(command);
    }

    if let Some(timezone_id) = &profile.timezone_id {
        commands.push(CdpCommand::new(
            "Emulation.setTimezoneOverride",
            json!({ "timezoneId": timezone_id }),
        ));
    }

    if let Some(locale) = &profile.locale {
        commands.push(CdpCommand::new(
            "Emulation.setLocaleOverride",
            json!({ "locale": locale }),
        ));
    }

    if let Some(hardware_concurrency) = profile.hardware_concurrency {
        commands.push(CdpCommand::new(
            "Emulation.setHardwareConcurrencyOverride",
            json!({ "hardwareConcurrency": hardware_concurrency }),
        ));
    }

    if let Some(metrics) = device_metrics(profile) {
        commands.push(CdpCommand::new(
            "Emulation.setDeviceMetricsOverride",
            metrics,
        ));
    }

    if let Some(max_touch_points) = profile.max_touch_points {
        commands.push(CdpCommand::new(
            "Emulation.setTouchEmulationEnabled",
            json!({
                "enabled": max_touch_points > 0,
                "maxTouchPoints": max_touch_points.max(1),
            }),
        ));
    }

    let features = emulated_media_features(profile);
    if !features.is_empty() {
        commands.push(CdpCommand::new(
            "Emulation.setEmulatedMedia",
            json!({ "features": features }),
        ));
    }

    if let Some(geolocation) = &profile.geolocation {
        commands.push(CdpCommand::new(
            "Emulation.setGeolocationOverride",
            json!(geolocation),
        ));
    }

    commands
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fingerprint::presets::create_default_fingerprint_preset;
    use crate::fingerprint::profile::{
        ColorScheme, ForcedColors, GeolocationProfile, ReducedMotion, ScreenProfile,
        ViewportProfile,
    };

    const WINDOWS_USER_AGENT: &str = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
         AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.7000.55 Safari/537.36";

    fn commands(profile: FingerprintProfile) -> Vec<CdpCommand> {
        build_cdp_emulation_commands(&profile.resolve().expect("profile resolves"))
    }

    fn methods(profile: FingerprintProfile) -> Vec<&'static str> {
        commands(profile)
            .into_iter()
            .map(|command| command.method)
            .collect()
    }

    fn params(profile: FingerprintProfile, method: &str) -> Value {
        commands(profile)
            .into_iter()
            .find(|command| command.method == method)
            .unwrap_or_else(|| panic!("{method} was not sent"))
            .params
    }

    #[test]
    fn emits_nothing_for_an_empty_profile() {
        assert!(commands(FingerprintProfile::default()).is_empty());
    }

    #[test]
    fn sends_only_the_commands_the_profile_asks_for() {
        assert_eq!(
            methods(FingerprintProfile::default().timezone_id("Europe/Berlin")),
            vec!["Emulation.setTimezoneOverride"]
        );
        assert_eq!(
            methods(FingerprintProfile::default().hardware_concurrency(4)),
            vec!["Emulation.setHardwareConcurrencyOverride"]
        );
    }

    #[test]
    fn keeps_a_stable_command_order_for_a_full_profile() {
        let profile = FingerprintProfile::default()
            .user_agent(WINDOWS_USER_AGENT)
            .timezone_id("Europe/Berlin")
            .locale("de-DE")
            .hardware_concurrency(12)
            .screen(ScreenProfile {
                width: Some(2560),
                height: Some(1440),
                ..ScreenProfile::default()
            })
            .viewport(ViewportProfile {
                width: Some(1280),
                height: Some(720),
                ..ViewportProfile::default()
            })
            .max_touch_points(0)
            .color_scheme(ColorScheme::Dark)
            .geolocation(GeolocationProfile {
                latitude: 52.52,
                longitude: 13.405,
                accuracy: Some(20.0),
            });

        assert_eq!(
            methods(profile),
            vec![
                "Emulation.setUserAgentOverride",
                "Emulation.setTimezoneOverride",
                "Emulation.setLocaleOverride",
                "Emulation.setHardwareConcurrencyOverride",
                "Emulation.setDeviceMetricsOverride",
                "Emulation.setTouchEmulationEnabled",
                "Emulation.setEmulatedMedia",
                "Emulation.setGeolocationOverride",
            ]
        );
    }

    #[test]
    fn supplies_the_required_empty_user_agent_when_only_the_language_changes() {
        let params = params(
            FingerprintProfile::default().languages(["fr-FR", "fr"]),
            "Emulation.setUserAgentOverride",
        );

        // userAgent is a required protocol parameter; an empty string means
        // "leave it alone" while acceptLanguage still takes effect.
        assert_eq!(params["userAgent"], json!(""));
        assert_eq!(params["acceptLanguage"], json!("fr-FR,fr"));
    }

    #[test]
    fn carries_the_client_hints_including_the_deprecated_full_version() {
        let params = params(
            FingerprintProfile::default()
                .user_agent(WINDOWS_USER_AGENT)
                .platform("Win32"),
            "Emulation.setUserAgentOverride",
        );
        let metadata = &params["userAgentMetadata"];

        assert_eq!(params["platform"], json!("Win32"));
        assert_eq!(metadata["platform"], json!("Windows"));
        // fullVersionList does not cover the uaFullVersion hint, so the
        // deprecated fullVersion field has to travel with it.
        assert_eq!(metadata["fullVersion"], json!("140.0.7000.55"));
        assert_eq!(metadata["bitness"], json!("64"));
        assert_eq!(metadata["wow64"], json!(false));
        assert_eq!(metadata["formFactors"], json!(["Desktop"]));
    }

    #[test]
    fn always_fills_the_protocol_required_metadata_fields() {
        let params = params(
            FingerprintProfile::default()
                .user_agent("custom agent")
                .user_agent_data(UserAgentData {
                    brands: Some(vec![crate::fingerprint::profile::BrandVersion {
                        brand: "Custom".to_string(),
                        version: "1".to_string(),
                    }]),
                    ..UserAgentData::default()
                }),
            "Emulation.setUserAgentOverride",
        );
        let metadata = params["userAgentMetadata"]
            .as_object()
            .expect("metadata object");

        // Chrome rejects setUserAgentOverride when any of these is missing.
        for field in [
            "platform",
            "platformVersion",
            "architecture",
            "model",
            "mobile",
        ] {
            assert!(metadata.contains_key(field), "{field} must be present");
        }
        assert_eq!(metadata["mobile"], json!(false));
        assert!(!metadata.contains_key("bitness"));
    }

    #[test]
    fn leaves_the_window_size_alone_when_only_the_screen_is_described() {
        let params = params(
            FingerprintProfile::default().screen(ScreenProfile {
                width: Some(2560),
                height: Some(1440),
                ..ScreenProfile::default()
            }),
            "Emulation.setDeviceMetricsOverride",
        );

        // Zeroes mean "no override" for the viewport, so a screen-only profile
        // does not resize the window it was applied to.
        assert_eq!(
            params,
            json!({
                "width": 0,
                "height": 0,
                "deviceScaleFactor": 0.0,
                "mobile": false,
                "screenWidth": 2560,
                "screenHeight": 1440,
            })
        );
    }

    #[test]
    fn sends_the_viewport_without_screen_dimensions_when_no_screen_is_set() {
        let params = params(
            FingerprintProfile::default().viewport(ViewportProfile {
                width: Some(1280),
                height: Some(720),
                device_scale_factor: Some(2.0),
                mobile: Some(true),
            }),
            "Emulation.setDeviceMetricsOverride",
        );

        assert_eq!(
            params,
            json!({
                "width": 1280,
                "height": 720,
                "deviceScaleFactor": 2.0,
                "mobile": true,
            })
        );
    }

    #[test]
    fn disables_touch_emulation_for_a_profile_that_names_zero_touch_points() {
        // maxTouchPoints must stay at least 1 because the protocol rejects 0,
        // so "no touch" is expressed through enabled instead.
        assert_eq!(
            params(
                FingerprintProfile::default().max_touch_points(0),
                "Emulation.setTouchEmulationEnabled"
            ),
            json!({ "enabled": false, "maxTouchPoints": 1 })
        );
        assert_eq!(
            params(
                FingerprintProfile::default().max_touch_points(5),
                "Emulation.setTouchEmulationEnabled"
            ),
            json!({ "enabled": true, "maxTouchPoints": 5 })
        );
    }

    #[test]
    fn collects_every_media_preference_into_a_single_command() {
        assert_eq!(
            params(
                FingerprintProfile::default()
                    .color_scheme(ColorScheme::Dark)
                    .reduced_motion(ReducedMotion::Reduce)
                    .forced_colors(ForcedColors::Active),
                "Emulation.setEmulatedMedia"
            ),
            json!({
                "features": [
                    { "name": "prefers-reduced-motion", "value": "reduce" },
                    { "name": "forced-colors", "value": "active" },
                    { "name": "prefers-color-scheme", "value": "dark" },
                ]
            })
        );
    }

    #[test]
    fn passes_geolocation_through_as_the_protocol_spells_it() {
        assert_eq!(
            params(
                FingerprintProfile::default().geolocation(GeolocationProfile {
                    latitude: 48.85,
                    longitude: 2.35,
                    accuracy: Some(10.0),
                }),
                "Emulation.setGeolocationOverride"
            ),
            json!({ "latitude": 48.85, "longitude": 2.35, "accuracy": 10.0 })
        );
    }

    // A preset is the shape most callers send, so the whole command list for one
    // is worth pinning: a field that silently stops being emulated is exactly
    // the regression this module exists to prevent.
    #[test]
    fn sends_every_browser_enforced_field_of_a_preset() {
        let profile = create_default_fingerprint_preset("android-chrome").expect("preset builds");
        let commands = build_cdp_emulation_commands(&profile);
        let methods: Vec<_> = commands.iter().map(|command| command.method).collect();

        assert_eq!(
            methods,
            vec![
                "Emulation.setUserAgentOverride",
                "Emulation.setTimezoneOverride",
                "Emulation.setLocaleOverride",
                "Emulation.setHardwareConcurrencyOverride",
                "Emulation.setDeviceMetricsOverride",
                "Emulation.setTouchEmulationEnabled",
            ]
        );
        assert_eq!(
            commands[0].params["userAgentMetadata"]["mobile"],
            json!(true)
        );
    }
}