#![deny(missing_docs)]
#![deny(unnameable_types)]
#![deny(unreachable_pub)]
mod action;
mod bounded_fetch;
mod browser;
pub mod cdp;
mod download;
mod element;
pub use action::{
Action, ActionCall, ActionDefaults, ActionOptions, ActionOutcome, ActionReceipt, ControlState,
Destination, DispatchObserver, DispatchState, Expectation, InputMode, Locator, LocatorQuery,
LocatorSpec, TryActionCall, MAX_ACTION_TIMEOUT_MS,
};
mod error;
mod fetch;
mod keyboard;
mod network;
mod page;
mod recording;
mod session;
pub mod stealth;
mod util;
const _: fn() = || {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<browser::Browser>();
assert_send_sync::<page::Page>();
assert_send_sync::<ActionCall>();
assert_send_sync::<TryActionCall>();
};
pub use bounded_fetch::{BoundedFetchRequest, BoundedFetchResponse, MAX_TEXT_FETCH_BYTES};
pub use browser::{Browser, TabInfo};
pub use download::{
DownloadBeginning, DownloadEvent, DownloadProgress, DownloadWatch, NativeDownloadState,
};
pub use element::{BoundingBox, Element};
pub use error::{Error, ErrorCode, Result};
pub use fetch::{BrowserFetchOutcome, BrowserFetchRequest, BrowserFetchResponse};
pub use network::{
NetworkEvent, NetworkWatcher, RequestCapture, RequestCaptureOptions, RequestCaptureStats,
};
pub use page::{
CaptureBodyError, CapturedFrameResponse, FrameResponseCapture, FrameResponseCaptureOptions,
FrameResponseCaptureReport,
};
pub use page::{
CapturedRequest, FrameInfo, MouseButton, Page, PageState, ResponseBody, ShadowClickReceipt,
TextMatch,
};
pub use page::{ScreenshotClip, ScreenshotFormat, ScreenshotOptions};
pub use recording::{NativeRecording, NativeRecordingOptions, RecordingChunk, RecordingError};
pub use session::{BrowserSession, BrowserState, CookiePartitionKey, SessionCookie};
pub use stealth::{Fingerprint, HumanSpeed};
#[derive(Clone)]
pub struct StealthConfig {
pub webgl_spoof: bool,
pub canvas_spoof: bool,
pub audio_spoof: bool,
pub user_agent: Option<String>,
pub headless: bool,
pub chrome_path: Option<String>,
pub runtime_dir: Option<String>,
pub sandbox: bool,
pub patch_binary: bool,
pub viewport_width: u32,
pub viewport_height: u32,
pub device_scale_factor: f64,
pub debug_dir: Option<String>,
pub proxy: Option<String>,
pub user_data_dir: Option<String>,
pub profile_dir: Option<String>,
pub proxy_username: Option<String>,
pub proxy_password: Option<String>,
pub cdp_timeout: std::time::Duration,
pub timezone: Option<String>,
pub extra_args: Vec<String>,
pub live_session: bool,
pub filter_cdp: bool,
pub ignore_cert_errors: bool,
#[deprecated(
since = "0.13.0",
note = "has no effect; Chrome adds X-Client-Data after Fetch interception"
)]
pub strip_x_client_data: bool,
pub geo_align: bool,
}
pub(crate) struct Redacted;
impl std::fmt::Debug for Redacted {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("<redacted>")
}
}
#[allow(deprecated, reason = "prints every field, including deprecated ones")]
impl std::fmt::Debug for StealthConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Self {
webgl_spoof,
canvas_spoof,
audio_spoof,
user_agent,
headless,
chrome_path,
runtime_dir,
sandbox,
patch_binary,
viewport_width,
viewport_height,
device_scale_factor,
debug_dir,
proxy,
user_data_dir,
profile_dir,
proxy_username,
proxy_password,
cdp_timeout,
timezone,
extra_args,
live_session,
filter_cdp,
ignore_cert_errors,
strip_x_client_data,
geo_align,
} = self;
let mut output = f.debug_struct("StealthConfig");
output
.field("webgl_spoof", webgl_spoof)
.field("canvas_spoof", canvas_spoof)
.field("audio_spoof", audio_spoof)
.field("user_agent", user_agent)
.field("headless", headless)
.field("chrome_path", chrome_path)
.field("runtime_dir", runtime_dir)
.field("sandbox", sandbox)
.field("patch_binary", patch_binary)
.field("viewport_width", viewport_width)
.field("viewport_height", viewport_height)
.field("device_scale_factor", device_scale_factor)
.field("debug_dir", debug_dir);
match proxy {
Some(proxy) if proxy.contains('@') => output.field("proxy", &Some(Redacted)),
proxy => output.field("proxy", proxy),
};
output
.field("user_data_dir", user_data_dir)
.field("profile_dir", profile_dir)
.field("proxy_username", &proxy_username.as_ref().map(|_| Redacted))
.field("proxy_password", &proxy_password.as_ref().map(|_| Redacted))
.field("cdp_timeout", cdp_timeout)
.field("timezone", timezone)
.field("extra_args", &extra_args.len())
.field("live_session", live_session)
.field("filter_cdp", filter_cdp)
.field("ignore_cert_errors", ignore_cert_errors)
.field("strip_x_client_data", strip_x_client_data)
.field("geo_align", geo_align)
.finish()
}
}
#[allow(
deprecated,
reason = "initialises every field, including deprecated ones"
)]
impl Default for StealthConfig {
fn default() -> Self {
Self {
webgl_spoof: true,
canvas_spoof: true,
audio_spoof: true,
user_agent: None,
headless: true,
chrome_path: None,
runtime_dir: None,
sandbox: true,
patch_binary: false,
viewport_width: 1920,
viewport_height: 1080,
device_scale_factor: 1.0,
debug_dir: None,
proxy: None,
user_data_dir: None,
profile_dir: None,
proxy_username: None,
proxy_password: None,
cdp_timeout: cdp::transport::DEFAULT_CDP_TIMEOUT,
timezone: None, extra_args: Vec::new(),
live_session: false,
filter_cdp: true,
ignore_cert_errors: false,
strip_x_client_data: false,
geo_align: false,
}
}
}
impl StealthConfig {
pub(crate) fn proxy_credentials(&self) -> Option<(String, String)> {
self.proxy_username.clone().zip(self.proxy_password.clone())
}
pub fn minimal() -> Self {
Self {
webgl_spoof: false,
canvas_spoof: false,
audio_spoof: false,
headless: false,
patch_binary: false,
..Default::default()
}
}
pub fn visible() -> Self {
Self {
headless: false,
..Default::default()
}
}
pub fn debug() -> Self {
Self::visible()
}
pub fn live() -> Self {
Self {
live_session: true,
filter_cdp: false,
patch_binary: false,
webgl_spoof: false,
canvas_spoof: false,
audio_spoof: false,
..Default::default()
}
}
}
#[cfg(test)]
mod tests {
use super::StealthConfig;
#[test]
fn debug_output_omits_proxy_credentials() {
let printed = format!(
"{:?}",
StealthConfig {
proxy: Some("http://embedded-user:embedded-pass@proxy.test:8080".into()),
proxy_username: Some("named-user".into()),
proxy_password: Some("named-pass".into()),
extra_args: vec!["--proxy-server=arg-user:arg-pass@proxy.test".into()],
..Default::default()
}
);
for secret in [
"embedded-user",
"embedded-pass",
"named-user",
"named-pass",
"arg-user",
"arg-pass",
] {
assert!(!printed.contains(secret), "{secret} leaked: {printed}");
}
assert!(printed.contains("proxy_password: Some(<redacted>)"));
let plain = StealthConfig {
proxy: Some("http://proxy.test:8080".into()),
..Default::default()
};
assert!(format!("{plain:?}").contains("http://proxy.test:8080"));
assert!(format!("{:?}", StealthConfig::default()).contains("proxy_password: None"));
}
#[test]
fn debug_output_omits_values_from_actions_and_network_types() {
use crate::{Action, BrowserFetchRequest, CapturedRequest, Expectation, LocatorSpec};
let headers = std::collections::HashMap::from([("Cookie".into(), "header-secret".into())]);
let printed = format!(
"{:?} {:?} {:?} {:?} {:?} {:?} {:?}",
Action::Fill {
target: LocatorSpec::css("#pw"),
value: "value-secret".into(),
input_mode: Default::default(),
},
Action::NavigateWithHeaders {
url: "https://x.test/?t=url-secret".into(),
headers: headers.clone(),
},
Expectation::Value {
target: LocatorSpec::css("#pw"),
value: "value-secret".into(),
},
CapturedRequest {
request_id: "1".into(),
url: "https://x.test/?t=url-secret".into(),
method: "POST".into(),
headers: headers.clone(),
post_data: Some("body-secret".into()),
resource_type: None,
status: None,
status_text: None,
response_headers: Some(headers.clone()),
mime_type: None,
timestamp: 0.0,
complete: false,
},
crate::NetworkEvent::ResponseReceived {
request_id: "1".into(),
status: 200,
status_text: "OK".into(),
headers: headers.clone(),
mime_type: None,
},
BrowserFetchRequest {
headers,
body: Some("body-secret".into()),
..BrowserFetchRequest::get("https://x.test/?t=url-secret")
},
crate::ResponseBody::Text("body-secret".into()),
);
for secret in ["value-secret", "url-secret", "header-secret", "body-secret"] {
assert!(!printed.contains(secret), "{secret} leaked: {printed}");
}
assert!(printed.contains("Cookie"), "header names stay useful");
}
#[test]
fn default_config_is_headless() {
assert!(StealthConfig::default().headless);
}
#[test]
fn binary_patching_is_opt_in() {
assert!(!StealthConfig::default().patch_binary);
assert!(!StealthConfig::visible().patch_binary);
assert!(!StealthConfig::debug().patch_binary);
}
#[test]
fn visible_configs_are_explicitly_not_headless() {
assert!(!StealthConfig::visible().headless);
assert!(!StealthConfig::debug().headless);
}
}