browser-commander 0.12.1

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
//! What Browser Commander cannot make identical, and why.
//!
//! Issue 79 asks for the limitations to be stated clearly rather than implied,
//! so every entry names the surface, the mechanism that would be needed, the
//! privacy consequence, and the evidence it rests on. Entries marked
//! [`LimitationEvidence::Measured`] were reproduced in this repository; the
//! artifacts are under `docs/case-studies/issue-79/analysis-artifacts/`.
//!
//! The catalogue itself is data, not code. `limitations.json` next to this
//! module is a byte-for-byte copy of `js/src/fingerprint/limitations.json`,
//! embedded with `include_str!` and kept in step by
//! `scripts/check-shared-fingerprint-assets.sh`. Three hand-written
//! translations of the same eleven paragraphs would drift within a release.

use std::sync::LazyLock;

use serde::{Deserialize, Serialize};

use super::profile::FingerprintProfile;

/// The shared catalogue source, embedded at compile time.
pub const FINGERPRINT_LIMITATIONS_SOURCE: &str = include_str!("limitations.json");

/// How much a limitation helps someone identify the browser.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LimitationSeverity {
    /// On its own it identifies automation or the physical machine.
    High,
    /// A strong signal when combined with others.
    Medium,
    /// It narrows the field but is common in real browsers too.
    Low,
}

/// Where the claim in an entry comes from.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LimitationEvidence {
    /// Reproduced in this repository; `reference` names the artifact.
    Measured,
    /// Taken from browser source or a specification.
    Documented,
}

/// One documented difference a page can still observe.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FingerprintLimitation {
    /// Stable identifier.
    pub id: String,
    /// What the page can observe.
    pub surface: String,
    /// How much it gives away.
    pub severity: LimitationSeverity,
    /// Whether the entry was measured here or read from a source.
    pub evidence: LimitationEvidence,
    /// What happens and why it cannot be fixed here.
    pub detail: String,
    /// What a caller can do about it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub workaround: Option<String>,
    /// Artifact, source file or specification.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reference: Option<String>,
}

/// Every documented limitation, in the order the catalogue declares them.
pub static FINGERPRINT_LIMITATIONS: LazyLock<Vec<FingerprintLimitation>> = LazyLock::new(|| {
    serde_json::from_str(FINGERPRINT_LIMITATIONS_SOURCE)
        .expect("limitations.json is embedded at compile time and has to parse")
});

/// Limitations that apply no matter what the profile asks for.
const ALWAYS_RELEVANT: &[&str] = &[
    "canvas-audio-font-follow-the-host",
    "network-layer-not-covered",
];

/// What a caller knows about the browser that the profile does not say.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct LimitationContext {
    /// Whether the browser runs headless.
    pub headless: bool,
    /// Whether the browser was launched by somebody else, so the automation
    /// switches are already fixed.
    pub attached: bool,
}

/// Look a limitation up by id, or `None` when there is no such id.
pub fn find_fingerprint_limitation(id: &str) -> Option<&'static FingerprintLimitation> {
    FINGERPRINT_LIMITATIONS
        .iter()
        .find(|limitation| limitation.id == id)
}

/// Whether the screen asks for a field the `Emulation` domain does not carry.
fn screen_needs_patching(profile: &FingerprintProfile) -> bool {
    profile.screen.as_ref().is_some_and(|screen| {
        screen.color_depth.is_some()
            || screen.pixel_depth.is_some()
            || screen.avail_width.is_some()
            || screen.avail_height.is_some()
    })
}

/// Fields a worker reads differently from its document: the ones no override
/// reaches, plus the ones the page session keeps to itself.
fn touches_a_worker_visible_field(profile: &FingerprintProfile) -> bool {
    profile.device_memory.is_some()
        || profile.vendor.is_some()
        || profile.do_not_track.is_some()
        || profile.webgl.is_some()
        || profile.platform.is_some()
        || profile.languages.is_some()
        || profile.hardware_concurrency.is_some()
}

/// The limitations that apply to a specific profile.
///
/// A profile that never touches WebGL does not need to hear about the WebGL
/// limitation, and hiding the irrelevant entries is what makes the relevant
/// ones worth reading.
pub fn relevant_fingerprint_limitations(
    profile: &FingerprintProfile,
    context: LimitationContext,
) -> Vec<&'static FingerprintLimitation> {
    FINGERPRINT_LIMITATIONS
        .iter()
        .filter(|limitation| {
            if ALWAYS_RELEVANT.contains(&limitation.id.as_str()) {
                return true;
            }
            match limitation.id.as_str() {
                "automation-controlled-is-launch-only" => context.attached,
                "no-cdp-device-memory-override" => profile.device_memory.is_some(),
                "no-cdp-vendor-or-dnt-override" => {
                    profile.vendor.is_some() || profile.do_not_track.is_some()
                }
                "screen-depth-and-avail-not-emulated" => screen_needs_patching(profile),
                "webgl-strings-only" => profile.webgl.is_some(),
                "grease-brand-not-reproduced" => profile.user_agent_data.is_some(),
                "touch-emulation-changes-pointer-media" => {
                    profile.max_touch_points.is_some_and(|points| points > 0)
                }
                "headless-is-distinguishable" => context.headless,
                "init-script-does-not-reach-workers" => touches_a_worker_visible_field(profile),
                _ => false,
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::fingerprint::profile::{ScreenProfile, UserAgentData, WebglProfile};

    fn ids(limitations: &[&FingerprintLimitation]) -> Vec<String> {
        limitations
            .iter()
            .map(|limitation| limitation.id.clone())
            .collect()
    }

    #[test]
    fn gives_every_entry_a_unique_id_and_the_fields_a_reader_needs() {
        let mut seen = Vec::new();
        for limitation in FINGERPRINT_LIMITATIONS.iter() {
            assert!(
                !seen.contains(&limitation.id),
                "duplicate {}",
                limitation.id
            );
            seen.push(limitation.id.clone());
            assert!(!limitation.surface.is_empty(), "{}", limitation.id);
            assert!(!limitation.detail.is_empty(), "{}", limitation.id);
        }
        assert_eq!(seen.len(), 11);
    }

    #[test]
    fn points_every_measured_entry_at_the_artifact_that_proves_it() {
        for limitation in FINGERPRINT_LIMITATIONS.iter() {
            if limitation.evidence == LimitationEvidence::Measured {
                assert!(
                    limitation.reference.is_some(),
                    "{} needs a reference",
                    limitation.id
                );
            }
        }
    }

    #[test]
    fn ships_the_catalogue_that_the_javascript_package_owns() {
        // The check script compares the bytes; this asserts the copy next to
        // this module is the one the crate actually embeds.
        let canonical = std::fs::read_to_string(
            std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
                .join("../js/src/fingerprint/limitations.json"),
        )
        .expect("the JavaScript package owns the catalogue");

        assert_eq!(canonical, FINGERPRINT_LIMITATIONS_SOURCE);
    }

    #[test]
    fn looks_an_entry_up_by_id() {
        assert_eq!(
            find_fingerprint_limitation("webgl-strings-only")
                .expect("the entry is in the catalogue")
                .surface,
            "WebGL renderer strings and driver limits"
        );
        assert!(find_fingerprint_limitation("no-such-limitation").is_none());
    }

    #[test]
    fn always_reports_the_two_nothing_can_be_done_about() {
        let relevant = relevant_fingerprint_limitations(
            &FingerprintProfile::default(),
            LimitationContext::default(),
        );

        assert_eq!(
            ids(&relevant),
            vec![
                "canvas-audio-font-follow-the-host",
                "network-layer-not-covered"
            ]
        );
    }

    #[test]
    fn mentions_the_launch_only_switch_only_when_attached() {
        let profile = FingerprintProfile::default();
        let detached = relevant_fingerprint_limitations(&profile, LimitationContext::default());
        assert!(!ids(&detached).contains(&"automation-controlled-is-launch-only".to_string()));

        let attached = relevant_fingerprint_limitations(
            &profile,
            LimitationContext {
                attached: true,
                ..Default::default()
            },
        );
        assert!(ids(&attached).contains(&"automation-controlled-is-launch-only".to_string()));
    }

    #[test]
    fn mentions_headless_only_for_a_headless_browser() {
        let relevant = relevant_fingerprint_limitations(
            &FingerprintProfile::default(),
            LimitationContext {
                headless: true,
                ..Default::default()
            },
        );

        assert!(ids(&relevant).contains(&"headless-is-distinguishable".to_string()));
    }

    #[test]
    fn mentions_the_javascript_only_fields_when_the_profile_sets_them() {
        let memory = FingerprintProfile {
            device_memory: Some(8.0),
            ..Default::default()
        };
        assert!(ids(&relevant_fingerprint_limitations(
            &memory,
            LimitationContext::default()
        ))
        .contains(&"no-cdp-device-memory-override".to_string()));

        let dnt = FingerprintProfile {
            do_not_track: Some("1".to_string()),
            ..Default::default()
        };
        assert!(ids(&relevant_fingerprint_limitations(
            &dnt,
            LimitationContext::default()
        ))
        .contains(&"no-cdp-vendor-or-dnt-override".to_string()));

        let webgl = FingerprintProfile {
            webgl: Some(WebglProfile {
                vendor: Some("WebKit".to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        assert!(ids(&relevant_fingerprint_limitations(
            &webgl,
            LimitationContext::default()
        ))
        .contains(&"webgl-strings-only".to_string()));
    }

    #[test]
    fn mentions_the_screen_entry_only_for_the_fields_cdp_does_not_emulate() {
        let emulated = FingerprintProfile {
            screen: Some(ScreenProfile {
                width: Some(1920),
                height: Some(1080),
                ..Default::default()
            }),
            ..Default::default()
        };
        assert!(!ids(&relevant_fingerprint_limitations(
            &emulated,
            LimitationContext::default()
        ))
        .contains(&"screen-depth-and-avail-not-emulated".to_string()));

        let patched = FingerprintProfile {
            screen: Some(ScreenProfile {
                width: Some(1920),
                height: Some(1080),
                avail_height: Some(1032),
                ..Default::default()
            }),
            ..Default::default()
        };
        assert!(ids(&relevant_fingerprint_limitations(
            &patched,
            LimitationContext::default()
        ))
        .contains(&"screen-depth-and-avail-not-emulated".to_string()));
    }

    #[test]
    fn mentions_the_pointer_side_effect_only_when_touch_is_enabled() {
        let mouse = FingerprintProfile {
            max_touch_points: Some(0),
            ..Default::default()
        };
        assert!(!ids(&relevant_fingerprint_limitations(
            &mouse,
            LimitationContext::default()
        ))
        .contains(&"touch-emulation-changes-pointer-media".to_string()));

        let touch = FingerprintProfile {
            max_touch_points: Some(5),
            ..Default::default()
        };
        assert!(ids(&relevant_fingerprint_limitations(
            &touch,
            LimitationContext::default()
        ))
        .contains(&"touch-emulation-changes-pointer-media".to_string()));
    }

    #[test]
    fn mentions_the_grease_brand_when_the_profile_pins_client_hints() {
        let hints = FingerprintProfile {
            user_agent_data: Some(UserAgentData::default()),
            ..Default::default()
        };

        assert!(ids(&relevant_fingerprint_limitations(
            &hints,
            LimitationContext::default()
        ))
        .contains(&"grease-brand-not-reproduced".to_string()));
    }

    #[test]
    fn mentions_workers_for_every_field_a_worker_reads_differently() {
        // Measured in worker-visibility.json: platform, languages and
        // hardwareConcurrency revert to the host values inside a worker even
        // though the page session overrides them.
        let profiles = [
            FingerprintProfile {
                platform: Some("Win32".to_string()),
                ..Default::default()
            },
            FingerprintProfile {
                languages: Some(vec!["de-DE".to_string()]),
                ..Default::default()
            },
            FingerprintProfile {
                hardware_concurrency: Some(8),
                ..Default::default()
            },
            FingerprintProfile {
                device_memory: Some(8.0),
                ..Default::default()
            },
        ];
        for profile in &profiles {
            assert!(
                ids(&relevant_fingerprint_limitations(
                    profile,
                    LimitationContext::default()
                ))
                .contains(&"init-script-does-not-reach-workers".to_string()),
                "{profile:?}"
            );
        }

        let timezone = FingerprintProfile {
            timezone_id: Some("UTC".to_string()),
            ..Default::default()
        };
        assert!(!ids(&relevant_fingerprint_limitations(
            &timezone,
            LimitationContext::default()
        ))
        .contains(&"init-script-does-not-reach-workers".to_string()));
    }

    #[test]
    fn keeps_the_declaration_order_of_the_catalogue() {
        let relevant = relevant_fingerprint_limitations(
            &FingerprintProfile {
                device_memory: Some(8.0),
                webgl: Some(WebglProfile {
                    vendor: Some("WebKit".to_string()),
                    ..Default::default()
                }),
                ..Default::default()
            },
            LimitationContext {
                headless: true,
                attached: true,
            },
        );

        let declared: Vec<String> = FINGERPRINT_LIMITATIONS
            .iter()
            .filter(|limitation| ids(&relevant).contains(&limitation.id))
            .map(|limitation| limitation.id.clone())
            .collect();
        assert_eq!(ids(&relevant), declared);
    }
}