The headless browser for AI agents and web scraping
fix(net): accept folded HTTP/1 response headers
Florian Leprat15c00de62c83
diff --git a/crates/obscura-net/src/client.rs b/crates/obscura-net/src/client.rsindex 52788d6a..86295e36 100644--- a/crates/obscura-net/src/client.rs+++ b/crates/obscura-net/src/client.rs@@ -1070,6 +1070,9 @@ impl ObscuraHttpClient {.redirect(Policy::none()).timeout(self.timeout).danger_accept_invalid_certs(false)+ // Read a folded header line (obs-fold) as browsers do instead of+ // failing the response.+ .http1_allow_obsolete_multiline_headers_in_responses(true)// SSRF guard: reject hostnames that resolve to a private/loopback IP..dns_resolver(Arc::new(SsrfGuardResolver::new(self.allow_private_network)));@@ -2037,6 +2040,23 @@ mod ssrf_tests {.to_string()}+ // Browsers read a folded header line (obs-fold, RFC 9112 5.2) as part of+ // the line above. Some origins still fold long values: promodirect.com+ // sends its Content-Security-Policy over several lines, each continuation+ // after a bare LF and two spaces. httparse rejects the whole response+ // unless obs-fold is allowed.+ #[tokio::test]+ async fn navigation_accepts_a_folded_response_header() {+ let folded = "Content-Security-Policy: default-src 'self'\n https://a.example\n https://b.example\r\n";+ let (target, _rx) = http_fixture(vec![ok_response(folded, "folded")]).await;+ let client = ObscuraHttpClient::with_full_options(Arc::new(CookieJar::new()), None, true);+ let response = client+ .fetch(&target)+ .await+ .expect("a folded header must not fail the response");+ assert_eq!(response.body, b"folded");+ }+// WPT fetch/api/redirect/redirect-count: the 20th redirect must still be// followed, the 21st must fail. Guards the `0..=max_redirects` boundary in// fetch_with_profile_uncached; `0..max_redirects` regressed this to 19.diff --git a/crates/obscura-net/src/wreq_client.rs b/crates/obscura-net/src/wreq_client.rsindex e2964799..b0ab86ff 100644--- a/crates/obscura-net/src/wreq_client.rs+++ b/crates/obscura-net/src/wreq_client.rs@@ -219,6 +219,14 @@ impl StealthHttpClient {let mut builder = wreq::Client::builder().emulation(emulation_opts)+ // Read a folded header line (obs-fold) as Chrome does instead of+ // failing the response. Set after `emulation()`, which overwrites+ // `http1_options`; the Chrome profile leaves them unset.+ .http1_options(+ wreq::http1::Http1Options::builder()+ .allow_obsolete_multiline_headers_in_responses(true)+ .build(),+ ).timeout(Duration::from_secs(30))// SSRF guard: reject hostnames that resolve to a private/loopback// IP. Use the same opt-in as the `validate_url` calls below so@@ -838,6 +846,31 @@ mod tests {assert_eq!(server.join().unwrap(), 1);}+ // See `navigation_accepts_a_folded_response_header` in client.rs: the+ // stealth transport must read a folded header as Chrome does.+ #[tokio::test]+ async fn stealth_client_accepts_a_folded_response_header() {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let port = listener.local_addr().unwrap().port();+ tokio::spawn(async move {+ let (mut stream, _) = listener.accept().await.unwrap();+ let mut buf = [0u8; 1024];+ let _ = stream.read(&mut buf).await;+ let response = "HTTP/1.1 200 OK\r\nContent-Security-Policy: default-src 'self'\n https://a.example\n https://b.example\r\ncontent-length: 6\r\nconnection: close\r\n\r\nfolded";+ let _ = stream.write_all(response.as_bytes()).await;+ let _ = stream.shutdown().await;+ });+ let client = StealthHttpClient::with_proxy(Arc::new(CookieJar::new()), None, true);+ let url = Url::parse(&format!("http://127.0.0.1:{port}/")).unwrap();++ let resp = client+ .fetch(&url)+ .await+ .expect("a folded header must not fail the response");+ assert_eq!(resp.status, 200);+ assert_eq!(resp.text(), "folded");+ }+/// Serve one `Content-Encoding: gzip` response on an ephemeral port.async fn gzip_fixture() -> u16 {let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();