The headless browser for AI agents and web scraping
fix(cli): apply --stealth to --dump original fetches
Zed128a16a94656b79
diff --git a/crates/obscura-cli/src/main.rs b/crates/obscura-cli/src/main.rsindex 5b8d63fa..e3ae9734 100644--- a/crates/obscura-cli/src/main.rs+++ b/crates/obscura-cli/src/main.rs@@ -384,7 +384,7 @@ async fn main() -> anyhow::Result<()> {),}let urls = read_urls_from_file(&file)?;- run_batch_fetch(urls, concurrency.get(), timeout, user_agent, global_proxy, output, quiet).await?;+ run_batch_fetch(urls, concurrency.get(), timeout, user_agent, global_proxy, output, quiet, stealth).await?;} else {let url = url.ok_or_else(|| {anyhow::anyhow!("No URL provided. Pass a URL, or a list of URLs with --file <path>.")@@ -583,6 +583,7 @@ async fn run_fetch(proxy,user_agent.clone(),timeout_secs,+ stealth,).await?;write_or_print_bytes(&bytes, output.as_ref()).await?;@@ -701,10 +702,34 @@ async fn fetch_original_response(proxy: Option<String>,user_agent: Option<String>,timeout_secs: u64,+ stealth: bool,) -> anyhow::Result<obscura_net::Response> {let url = url::Url::parse(url_str).map_err(|e| anyhow::anyhow!("Invalid URL '{}': {}", url_str, e))?;+ // `--dump original` short-circuits the browser stack (see run_fetch) and+ // builds its own client here instead of going through BrowserContext /+ // Page::do_fetch, which is where --stealth is normally applied. Without+ // this, the request stays on plain HTTP/1.1 with no TLS impersonation+ // regardless of --stealth (issue #482). file:// has no TLS handshake to+ // impersonate and the wreq client only speaks http(s), so it is excluded+ // here the same way ObscuraHttpClient::fetch_with_method excludes it+ // internally.+ if stealth && url.scheme() != "file" {+ #[cfg(feature = "stealth")]+ {+ let client = obscura_net::StealthHttpClient::with_proxy(+ Arc::new(obscura_net::CookieJar::new()),+ proxy.as_deref(),+ );+ return match timeout(Duration::from_secs(timeout_secs), client.fetch(&url)).await {+ Ok(Ok(resp)) => Ok(resp),+ Ok(Err(e)) => anyhow::bail!("Failed to fetch {}: {}", url_str, e),+ Err(_) => anyhow::bail!("Timed out fetching {} after {}s", url_str, timeout_secs),+ };+ }+ }+let client = obscura_net::ObscuraHttpClient::with_options(Arc::new(obscura_net::CookieJar::new()),proxy.as_deref(),@@ -725,8 +750,9 @@ async fn fetch_original_bytes(proxy: Option<String>,user_agent: Option<String>,timeout_secs: u64,+ stealth: bool,) -> anyhow::Result<Vec<u8>> {- Ok(fetch_original_response(url_str, proxy, user_agent, timeout_secs).await?.body)+ Ok(fetch_original_response(url_str, proxy, user_agent, timeout_secs, stealth).await?.body)}/// Read newline-delimited URLs from `path` (or stdin when `path` is `-`).@@ -765,6 +791,7 @@ async fn run_batch_fetch(proxy: Option<String>,output: Option<std::path::PathBuf>,quiet: bool,+ stealth: bool,) -> anyhow::Result<()> {let total = urls.len();if total == 0 {@@ -792,9 +819,14 @@ async fn run_batch_fetch(handles.push(tokio::spawn(async move {let _permit = sem.acquire().await.unwrap();let task_start = Instant::now();- let result =- fetch_original_response(&url, (*proxy).clone(), (*user_agent).clone(), timeout_secs)- .await;+ let result = fetch_original_response(+ &url,+ (*proxy).clone(),+ (*user_agent).clone(),+ timeout_secs,+ stealth,+ )+ .await;let elapsed_ms = task_start.elapsed().as_millis();let line = match result {@@ -1539,7 +1571,7 @@ mod tests {.expect("seed temp PNG fixture");let file_url = format!("file://{}", path.display());- let bytes = fetch_original_bytes(&file_url, None, None, 5)+ let bytes = fetch_original_bytes(&file_url, None, None, 5, false).await.expect("fetch_original_bytes should round-trip the file body");@@ -1548,6 +1580,39 @@ mod tests {assert_eq!(bytes, PNG_BYTES, "raw response body must match the file byte-for-byte");}+ // A stealth-enabled build routes `--dump original` through+ // StealthHttpClient (wreq), which only speaks http(s). file:// must keep+ // working the same as without --stealth instead of being handed to wreq+ // (issue #482).+ #[tokio::test(flavor = "current_thread")]+ async fn fetch_original_bytes_file_url_ignores_stealth_flag() {+ const PNG_BYTES: &[u8] = &[+ 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48,+ 0x44, 0x52, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x06, 0x00, 0x00,+ 0x00, 0x1F, 0x15, 0xC4, 0x89, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x44, 0x41, 0x54, 0x78,+ 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, 0x00, 0x01, 0x0D, 0x0A, 0x2D, 0xB4, 0x00,+ 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82,+ ];++ let path = std::env::temp_dir().join(format!(+ "obscura-fetch-original-stealth-test-{}.png",+ std::process::id()+ ));+ let _ = tokio::fs::remove_file(&path).await;+ tokio::fs::write(&path, PNG_BYTES)+ .await+ .expect("seed temp PNG fixture");++ let file_url = format!("file://{}", path.display());+ let bytes = fetch_original_bytes(&file_url, None, None, 5, true)+ .await+ .expect("fetch_original_bytes should still round-trip file:// with stealth=true");++ let _ = tokio::fs::remove_file(&path).await;++ assert_eq!(bytes, PNG_BYTES, "stealth=true must not change file:// handling");+ }+#[tokio::test(flavor = "current_thread")]async fn write_or_print_bytes_writes_without_trailing_newline() {// Regression guard for #117: stdout must receive raw bytes. The file