The headless browser for AI agents and web scraping
fix(net): allow disabling stealth tracker blocking
lisa03149ced49602eb9
diff --git a/crates/obscura-net/src/wreq_client.rs b/crates/obscura-net/src/wreq_client.rsindex 77861160..374f3931 100644--- a/crates/obscura-net/src/wreq_client.rs+++ b/crates/obscura-net/src/wreq_client.rs@@ -72,6 +72,19 @@ pub const STEALTH_UA_PLATFORM: &str = "Windows";#[cfg(feature = "stealth")]pub const STEALTH_UA_PLATFORM_VERSION: &str = "15.0.0";+#[cfg(feature = "stealth")]+fn tracker_blocking_enabled(value: Option<&str>) -> bool {+ !matches!(+ value.map(str::trim),+ Some(value) if matches!(value.to_ascii_lowercase().as_str(), "0" | "false" | "no" | "off")+ )+}++#[cfg(feature = "stealth")]+fn is_tracker_blocked(url: &Url, block_trackers: bool) -> bool {+ block_trackers && url.host_str().is_some_and(crate::blocklist::is_blocked)+}+#[cfg(feature = "stealth")]fn wreq_response_header_value<'a>(headers: &'a wreq::header::HeaderMap,@@ -174,6 +187,7 @@ async fn send_get_with_connection_reset_retry(pub struct StealthHttpClient {client: wreq::Client,allow_private_network: bool,+ pub block_trackers: bool,pub cookie_jar: Arc<CookieJar>,pub extra_headers: RwLock<HashMap<String, String>>,pub in_flight: Arc<std::sync::atomic::AtomicU32>,@@ -250,6 +264,9 @@ impl StealthHttpClient {StealthHttpClient {client,allow_private_network,+ block_trackers: tracker_blocking_enabled(+ std::env::var("OBSCURA_BLOCK_TRACKERS").ok().as_deref(),+ ),cookie_jar,extra_headers: RwLock::new(HashMap::new()),in_flight: Arc::new(std::sync::atomic::AtomicU32::new(0)),@@ -292,17 +309,15 @@ impl StealthHttpClient {let mut current_url = url.clone();- if let Some(host) = current_url.host_str() {- if crate::blocklist::is_blocked(host) {- tracing::debug!("Blocked tracker: {}", current_url);- return Ok(Response {- status: 0,- url: current_url,- headers: HashMap::new(),- body: Vec::new(),- redirected_from: Vec::new(),- });- }+ if is_tracker_blocked(¤t_url, self.block_trackers) {+ tracing::debug!("Blocked tracker: {}", current_url);+ return Ok(Response {+ status: 0,+ url: current_url,+ headers: HashMap::new(),+ body: Vec::new(),+ redirected_from: Vec::new(),+ });}let mut redirects = Vec::new();@@ -446,17 +461,15 @@ impl StealthHttpClient {send_cookies: bool,store_cookies: bool,) -> Result<Response, ObscuraNetError> {- if let Some(host) = url.host_str() {- if crate::blocklist::is_blocked(host) {- tracing::debug!("Blocked tracker: {}", url);- return Ok(Response {- status: 0,- url: url.clone(),- headers: HashMap::new(),- body: Vec::new(),- redirected_from: Vec::new(),- });- }+ if is_tracker_blocked(url, self.block_trackers) {+ tracing::debug!("Blocked tracker: {}", url);+ return Ok(Response {+ status: 0,+ url: url.clone(),+ headers: HashMap::new(),+ body: Vec::new(),+ redirected_from: Vec::new(),+ });}let req_method = method@@ -532,7 +545,10 @@ mod tests {use tokio::io::{AsyncReadExt, AsyncWriteExt};use url::Url;- use super::{StealthHttpClient, send_get_with_connection_reset_retry};+ use super::{+ StealthHttpClient, is_tracker_blocked, send_get_with_connection_reset_retry,+ tracker_blocking_enabled,+ };use crate::client::{ObscuraNetError, SsrfGuardResolver};use crate::cookies::CookieJar;use wreq::dns::{Name, Resolve};@@ -562,6 +578,118 @@ mod tests {const PLAIN_BODY: &str = "<!DOCTYPE html><html><body><p id=\"mark\">gzip ok</p></body></html>";+ #[test]+ fn tracker_blocking_environment_defaults_to_enabled() {+ for value in [+ None,+ Some(""),+ Some("1"),+ Some("true"),+ Some("yes"),+ Some("on"),+ Some("invalid"),+ ] {+ assert!(+ tracker_blocking_enabled(value),+ "{value:?} must leave tracker blocking enabled"+ );+ }+ for value in [+ Some("0"), Some("false"), Some(" NO "), Some("off"), Some(" FALSE "),+ ] {+ assert!(+ !tracker_blocking_enabled(value),+ "{value:?} must disable tracker blocking"+ );+ }+ }++ #[test]+ fn tracker_blocking_respects_host_and_setting() {+ let url = Url::parse("https://www.google-analytics.com/collect").unwrap();++ assert!(is_tracker_blocked(&url, true));+ assert!(!is_tracker_blocked(&url, false));+ assert!(!is_tracker_blocked(+ &Url::parse("https://example.com/").unwrap(), true,+ ));+ assert!(!is_tracker_blocked(+ &Url::parse("file:///tmp/page.html").unwrap(), true,+ ));+ }++ #[test]+ fn tracker_blocking_constructor_reads_environment() {+ let client = StealthHttpClient::new(Arc::new(CookieJar::new()));+ let expected = tracker_blocking_enabled(+ std::env::var("OBSCURA_BLOCK_TRACKERS").ok().as_deref(),+ );+ assert_eq!(client.block_trackers, expected);+ }++ async fn assert_tracker_blocking_request(scripted: bool) {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let proxy = format!("http://{}", listener.local_addr().unwrap());+ let mut client = StealthHttpClient::with_proxy(+ Arc::new(CookieJar::new()),+ Some(&proxy),+ true,+ );+ let url = Url::parse("http://www.google-analytics.com/collect").unwrap();+ let headers = std::collections::HashMap::new();++ client.block_trackers = true;+ let blocked = if scripted {+ client.send_single("GET", &url, &headers, &[], false, false)+ .await+ } else {+ client.fetch(&url).await+ }.expect("blocked tracker returns an empty response");+ assert_eq!(blocked.status, 0);+ assert!(blocked.body.is_empty());+ assert!(tokio::time::timeout(Duration::from_millis(50), listener.accept())+ .await.is_err());++ let server = tokio::spawn(async move {+ let (mut stream, _) = tokio::time::timeout(Duration::from_secs(5), listener.accept())+ .await.expect("request reaches local proxy").unwrap();+ let mut request = Vec::new();+ while !request.windows(4).any(|bytes| bytes == b"\r\n\r\n") {+ let mut buffer = [0u8; 1024];+ let count = tokio::time::timeout(Duration::from_secs(5), stream.read(&mut buffer))+ .await.expect("request headers arrive").unwrap();+ assert_ne!(count, 0);+ request.extend_from_slice(&buffer[..count]);+ }+ assert!(String::from_utf8(request).unwrap().starts_with(+ "GET http://www.google-analytics.com/collect HTTP/1.1\r\n",+ ));+ stream.write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 2\r\nconnection: close\r\n\r\nok")+ .await.unwrap();+ });++ client.block_trackers = false;+ let allowed = if scripted {+ client.send_single("GET", &url, &headers, &[], false, false)+ .await+ } else {+ client.fetch(&url).await+ }.expect("disabled blocking allows the request");+ assert_eq!(allowed.status, 200);+ assert_eq!(allowed.body, b"ok");+ server.await.unwrap();+ }++ #[tokio::test]+ async fn tracker_blocking_navigation_request() {+ assert_tracker_blocking_request(false).await;+ }++ #[tokio::test]+ async fn tracker_blocking_scripted_request() {+ assert_tracker_blocking_request(true).await;+ }+// gzip (level 9) of PLAIN_BODY, hardcoded so the fixture needs no// compression dependency. A wrong byte fails the assert below.const GZIP_BODY: &[u8] = &[@@ -631,6 +759,7 @@ mod tests {let client = StealthHttpClient {client: wreq::Client::builder().no_proxy().build().unwrap(),allow_private_network: true,+ block_trackers: true,cookie_jar: Arc::new(CookieJar::new()),extra_headers: tokio::sync::RwLock::new(std::collections::HashMap::new()),in_flight: Arc::new(std::sync::atomic::AtomicU32::new(0)),diff --git a/docs/Environment-variables.md b/docs/Environment-variables.mdindex 82535e6e..2f957bca 100644--- a/docs/Environment-variables.md+++ b/docs/Environment-variables.md@@ -80,6 +80,21 @@ OBSCURA_PROXY=http://proxy.example.com:8080 obscura scrape - < urls.txtThese tune the browser identity the engine presents so it stays internally consistent. See [Configure stealth and proxies](Configure-stealth-and-proxies.md) for the full picture.+### `OBSCURA_BLOCK_TRACKERS`++Controls the tracker blocklist used by the stealth HTTP transport. It is on by+default so `--stealth` retains its current privacy-first behavior. Set it to+`0`, `false`, `no`, or `off` to keep the stealth TLS/browser fingerprint while+allowing tracker requests.++The setting is read when a stealth client is created. Values are case-insensitive+and surrounding whitespace is ignored; unset, empty, and unrecognized values keep+blocking enabled. Non-stealth transport settings and SSRF protection are unchanged.++```bash+OBSCURA_BLOCK_TRACKERS=0 obscura --stealth fetch https://example.com+```+### `OBSCURA_TIMEZONE`Pins the process timezone before V8/ICU reads it, so `Date` (`getTimezoneOffset`, `toString`) and `Intl.DateTimeFormat` report one consistent zone. Default `Europe/Berlin`. Set it to match the exit IP's region.