use super::DivergenceKind;
use crate::fingerprint::surface;
use crate::fingerprint::SurfaceCategory;
use std::collections::BTreeSet;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProbeSurfaceLink {
pub probe: &'static str,
pub covers: &'static [&'static str],
}
const fn link(probe: &'static str, covers: &'static [&'static str]) -> ProbeSurfaceLink {
ProbeSurfaceLink { probe, covers }
}
pub const PROBE_SURFACE_LINKS: &[ProbeSurfaceLink] = &[
link(
"Function.prototype.toString returns native code for Navigator.prototype.webdriver getter",
&["navigator.webdriver"],
),
link(
"Function.prototype.toString native for WebGL getParameter",
&["webgl.getParameter"],
),
link(
"Function.prototype.toString native for toDataURL",
&["canvas.toDataURL"],
),
link("navigator.webdriver", &["navigator.webdriver"]),
link(
"navigator.webdriver is inherited, not an own property",
&["navigator.webdriver"],
),
link(
"navigator.plugins.length > 0",
&["navigator.plugins.length"],
),
link(
"navigator.plugins is a real PluginArray",
&["navigator.plugins"],
),
link("navigator.languages.length > 0", &["navigator.languages"]),
link("navigator.language", &["navigator.language"]),
link("navigator.vendor non-empty", &["navigator.vendor"]),
link("navigator.userAgent", &["navigator.userAgent"]),
link("navigator.platform non-empty", &["navigator.platform"]),
link("navigator.cookieEnabled true", &["navigator.cookieEnabled"]),
link(
"navigator.hardwareConcurrency in [2, 16]",
&["navigator.hardwareConcurrency"],
),
link(
"navigator.deviceMemory in [1, 64]",
&["navigator.deviceMemory"],
),
link(
"navigator.connection.effectiveType exists",
&["navigator.connection.effectiveType"],
),
link(
"WebGL UNMASKED_VENDOR not SwiftShader",
&["webgl.getParameter"],
),
link(
"WebGL UNMASKED_RENDERER not SwiftShader",
&["webgl.getParameter"],
),
link(
"CanvasRenderingContext2D getImageData session-stable (no per-read tell)",
&["canvas.getImageData", "canvas.getContext"],
),
link(
"HTMLCanvasElement.toDataURL session-stable (no per-read tell)",
&["canvas.toDataURL", "canvas.getContext"],
),
link(
"CanvasRenderingContext2D.measureText session-stable (no per-read tell)",
&["canvas.measureText"],
),
link(
"AudioBuffer.getChannelData session-stable (no per-read tell)",
&["audio.getChannelData"],
),
link(
"AnalyserNode.getFloatFrequencyData is a function",
&["audio.getFloatFrequencyData"],
),
link("RTCPeerConnection exists", &["RTCPeerConnection"]),
link(
"RTCPeerConnection.createOffer is a function",
&["RTCPeerConnection.createOffer"],
),
link("navigator.getBattery() exists", &["navigator.getBattery"]),
link(
"navigator.permissions.query exists",
&["navigator.permissions.query"],
),
link(
"navigator.mediaDevices.enumerateDevices returns >=2",
&["navigator.mediaDevices.enumerateDevices"],
),
link(
"navigator.mediaDevices.getUserMedia is a function",
&["navigator.mediaDevices.getUserMedia"],
),
link(
"navigator.bluetooth exists",
&["navigator.bluetooth.requestDevice"],
),
link("navigator.usb exists", &["navigator.usb.requestDevice"]),
link("navigator.hid exists", &["navigator.hid.requestDevice"]),
link("navigator.serial exists", &["navigator.serial.requestPort"]),
link(
"navigator.getInstalledRelatedApps exists",
&["navigator.getInstalledRelatedApps"],
),
link("navigator.wakeLock exists", &["navigator.wakeLock.request"]),
link("window.PaymentRequest exists", &["PaymentRequest"]),
link("navigator.share exists", &["navigator.share"]),
link("navigator.canShare exists", &["navigator.canShare"]),
link(
"navigator.serviceWorker exists",
&["navigator.serviceWorker.register"],
),
link(
"speechSynthesis.getVoices returns >=16 voices",
&["speechSynthesis.getVoices"],
),
link("VideoDecoder exists", &["VideoDecoder.isConfigSupported"]),
link("VideoEncoder exists", &["VideoEncoder.isConfigSupported"]),
link("VideoFrame exists", &["VideoFrame"]),
link(
"MediaCapabilities.decodingInfo exists",
&["MediaCapabilities.decodingInfo"],
),
link("screen.colorDepth == 24", &["screen.colorDepth"]),
link("screen.pixelDepth == 24", &["screen.colorDepth"]),
link(
"screen.orientation.type exists",
&["screen.orientation.type"],
),
link("screen.width plausible", &["screen.width"]),
link("screen.height plausible", &["screen.height"]),
link(
"Intl.DateTimeFormat resolves an IANA time zone",
&["Intl.DateTimeFormat", "Intl.DateTimeFormat.resolvedOptions"],
),
link(
"Notification.permission not 'denied'",
&["Notification.requestPermission"],
),
link("matchMedia exists", &["matchMedia"]),
link("ResizeObserver exists", &["ResizeObserver.observe"]),
link(
"IntersectionObserver exists",
&["IntersectionObserver.observe"],
),
link("MutationObserver exists", &["MutationObserver.observe"]),
link("Notification exists", &["Notification.requestPermission"]),
link("StorageManager exists", &["navigator.storage.estimate"]),
link(
"Element.requestFullscreen exists",
&["element.requestFullscreen"],
),
link("document.fonts exists", &["document.fonts.forEach"]),
link("performance.now precision floored", &["performance.now"]),
link(
"navigator.userActivation exists",
&["navigator.userActivation"],
),
link("scheduler.postTask exists", &["scheduler.postTask"]),
link(
"navigator.userAgent is Gecko/Firefox",
&["navigator.userAgent"],
),
link("navigator.vendor is empty (Firefox)", &["navigator.vendor"]),
link(
"navigator.getBattery absent (Firefox)",
&["navigator.getBattery"],
),
link(
"navigator.usb absent (Firefox)",
&["navigator.usb.requestDevice"],
),
link(
"navigator.hid absent (Firefox)",
&["navigator.hid.requestDevice"],
),
link(
"navigator.bluetooth absent (Firefox desktop)",
&["navigator.bluetooth.requestDevice"],
),
link(
"window.PaymentRequest absent (Firefox)",
&["PaymentRequest"],
),
link(
"navigator.connection absent (Firefox)",
&["navigator.connection.effectiveType"],
),
];
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SpoofSurfaceLink {
pub field: &'static str,
pub surface: &'static str,
}
const fn spoof(field: &'static str, surface: &'static str) -> SpoofSurfaceLink {
SpoofSurfaceLink { field, surface }
}
pub const SPOOF_SURFACE_LINKS: &[SpoofSurfaceLink] = &[
spoof("ProfileOverrides::user_agent", "navigator.userAgent"),
spoof("ProfileOverrides::platform", "navigator.platform"),
spoof("ProfileOverrides::navigator_vendor", "navigator.vendor"),
spoof("ProfileOverrides::languages", "navigator.languages"),
spoof(
"ProfileOverrides::hardware_concurrency",
"navigator.hardwareConcurrency",
),
spoof("ProfileOverrides::device_memory", "navigator.deviceMemory"),
spoof("ProfileOverrides::screen_width", "screen.width"),
spoof("ProfileOverrides::screen_height", "screen.height"),
spoof("ProfileOverrides::color_depth", "screen.colorDepth"),
spoof(
"ProfileOverrides::timezone",
"Intl.DateTimeFormat.resolvedOptions",
),
spoof("ProfileOverrides::webgl_vendor", "webgl.getParameter"),
spoof("ProfileOverrides::webgl_renderer", "webgl.getParameter"),
];
#[must_use]
pub fn spoofed_surface_ids() -> BTreeSet<&'static str> {
SPOOF_SURFACE_LINKS.iter().map(|l| l.surface).collect()
}
#[must_use]
pub fn persona_overridden_surface_ids() -> BTreeSet<&'static str> {
SPOOF_SURFACE_LINKS
.iter()
.map(|l| l.surface)
.chain(NOISE_SPOOF_LINKS.iter().map(|l| l.surface))
.collect()
}
#[must_use]
pub fn divergence_kind_for_probe(probe_name: &str) -> DivergenceKind {
let overridden = persona_overridden_surface_ids();
let intended = PROBE_SURFACE_LINKS
.iter()
.filter(|l| l.probe == probe_name)
.flat_map(|l| l.covers.iter().copied())
.any(|id| overridden.contains(id));
if intended {
DivergenceKind::PersonaIntended
} else {
DivergenceKind::EngineDivergence
}
}
#[must_use]
pub fn category_for_probe(probe_name: &str) -> Option<SurfaceCategory> {
surface_for_probe(probe_name).map(|s| s.category)
}
#[must_use]
pub fn surface_for_probe(probe_name: &str) -> Option<&'static surface::FingerprintSurface> {
let link = PROBE_SURFACE_LINKS.iter().find(|l| l.probe == probe_name)?;
let first = link.covers.first()?;
surface::SURFACES.iter().find(|s| s.surface == *first)
}
#[must_use]
pub fn surface_id_for_probe(probe_name: &str) -> Option<&'static str> {
surface_for_probe(probe_name).map(|s| s.surface)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NoiseSpoofLink {
pub axis: &'static str,
pub surface: &'static str,
pub js_token: &'static str,
}
const fn noise(
axis: &'static str,
surface: &'static str,
js_token: &'static str,
) -> NoiseSpoofLink {
NoiseSpoofLink {
axis,
surface,
js_token,
}
}
pub const NOISE_SPOOF_LINKS: &[NoiseSpoofLink] = &[
noise(
"FingerprintConfig::canvas_noise",
"canvas.toDataURL",
"toDataURL",
),
noise(
"FingerprintConfig::canvas_noise",
"canvas.getImageData",
"getImageData",
),
noise(
"FingerprintConfig::audio_noise",
"audio.getChannelData",
"getChannelData",
),
noise(
"FingerprintConfig::audio_noise",
"audio.getFloatFrequencyData",
"getFloatFrequencyData",
),
noise(
"FingerprintConfig::font_noise",
"canvas.measureText",
"measureText",
),
noise(
"FingerprintConfig::performance_noise",
"performance.now",
"target.now = __seal",
),
noise(
"FingerprintConfig::webgl_override",
"webgl.getSupportedExtensions",
"getSupportedExtensions",
),
noise(
"FingerprintConfig::hardware_concurrency",
"navigator.hardwareConcurrency",
"hardwareConcurrency",
),
noise(
"FingerprintConfig::device_memory",
"navigator.deviceMemory",
"deviceMemory",
),
];
#[derive(Debug, Clone)]
pub struct SurfaceCoverage {
pub covered: Vec<&'static str>,
pub uncovered_must_cover: Vec<&'static str>,
}
fn covered_ids() -> BTreeSet<&'static str> {
PROBE_SURFACE_LINKS
.iter()
.flat_map(|l| l.covers.iter().copied())
.collect()
}
#[must_use]
pub fn surface_coverage() -> SurfaceCoverage {
let covered_set = covered_ids();
let covered: Vec<&'static str> = covered_set.iter().copied().collect();
let uncovered_must_cover: Vec<&'static str> = surface::must_cover()
.iter()
.map(|s| s.surface)
.filter(|id| !covered_set.contains(id))
.collect::<BTreeSet<_>>()
.into_iter()
.collect();
SurfaceCoverage {
covered,
uncovered_must_cover,
}
}
#[cfg(test)]
#[path = "surface_coverage/tests.rs"]
mod tests;