Skip to content

The headless browser for AI agents and web scraping

fix(obscura-cdp): bring Fetch domain path to parity with server.rs (#919)

Andrey Mnatsakanovdc88742ac1d4
diff --git a/crates/obscura-cdp/src/domains/fetch.rs b/crates/obscura-cdp/src/domains/fetch.rs
index 2c5e39d3..7b09890c 100644
--- a/crates/obscura-cdp/src/domains/fetch.rs
+++ b/crates/obscura-cdp/src/domains/fetch.rs
@@ -123,7 +123,9 @@ pub async fn handle(
.get("method")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
- headers: None,
+ // Honor client header overrides (route.continue({ headers }))
+ // — parity with server.rs handle_fetch_resolution (#919).
+ headers: crate::server::parse_cdp_headers(params),
post_data: params
.get("postData")
.and_then(|v| v.as_str())
@@ -155,11 +157,11 @@ pub async fn handle(
.collect()
})
.unwrap_or_default();
- let body = params
- .get("body")
- .and_then(|v| v.as_str())
- .unwrap_or("")
- .to_string();
+ // The CDP fulfillRequest body is base64-encoded; decode it — parity
+ // with server.rs handle_fetch_resolution (#919). (Binary-safe body
+ // transport across the JS boundary remains tracked in #912.)
+ let body =
+ crate::server::decode_base64(params.get("body").and_then(|v| v.as_str()).unwrap_or(""));
if let Some(paused) = ctx.fetch_intercept.paused.remove(request_id) {
let _ = paused.resolver.send(FetchResolution::Fulfill {
@@ -222,3 +224,76 @@ pub async fn handle(
_ => Err(format!("Unknown Fetch method: {}", method)),
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::dispatch::CdpContext;
+ use serde_json::json;
+ use std::collections::HashMap;
+
+ fn pause(ctx: &mut CdpContext, id: &str) -> tokio::sync::oneshot::Receiver<FetchResolution> {
+ let (tx, rx) = tokio::sync::oneshot::channel();
+ ctx.fetch_intercept.paused.insert(
+ id.to_string(),
+ PausedRequest {
+ request_id: id.to_string(),
+ url: "https://example.test/".to_string(),
+ method: "GET".to_string(),
+ headers: HashMap::new(),
+ resource_type: "Fetch".to_string(),
+ resolver: tx,
+ },
+ );
+ rx
+ }
+
+ // Parity with server.rs handle_fetch_resolution: continueRequest must
+ // forward the client's header overrides (route.continue({ headers })), not
+ // drop them. See #919.
+ #[tokio::test]
+ async fn continue_request_forwards_header_overrides() {
+ let mut ctx = CdpContext::new();
+ let rx = pause(&mut ctx, "req-1");
+ handle(
+ "continueRequest",
+ &json!({ "requestId": "req-1", "headers": [{ "name": "X-Test", "value": "42" }] }),
+ &mut ctx,
+ &None,
+ )
+ .await
+ .expect("continueRequest should succeed");
+
+ match rx.await.expect("resolver should fire") {
+ FetchResolution::Continue { headers, .. } => {
+ let mut expected = HashMap::new();
+ expected.insert("X-Test".to_string(), "42".to_string());
+ assert_eq!(headers, Some(expected), "continue must forward header overrides");
+ }
+ _ => panic!("expected FetchResolution::Continue"),
+ }
+ }
+
+ // Parity with server.rs: the fulfillRequest body is base64-encoded per CDP
+ // and must be decoded, not passed through as raw base64 text. See #919/#912.
+ #[tokio::test]
+ async fn fulfill_request_base64_decodes_body() {
+ let mut ctx = CdpContext::new();
+ let rx = pause(&mut ctx, "req-2");
+ handle(
+ "fulfillRequest",
+ &json!({ "requestId": "req-2", "responseCode": 200, "body": "SGVsbG8=" }),
+ &mut ctx,
+ &None,
+ )
+ .await
+ .expect("fulfillRequest should succeed");
+
+ match rx.await.expect("resolver should fire") {
+ FetchResolution::Fulfill { body, .. } => {
+ assert_eq!(body, "Hello", "fulfill body must be base64-decoded");
+ }
+ _ => panic!("expected FetchResolution::Fulfill"),
+ }
+ }
+}
diff --git a/crates/obscura-cdp/src/server.rs b/crates/obscura-cdp/src/server.rs
index d5917634..258b8d1c 100644
--- a/crates/obscura-cdp/src/server.rs
+++ b/crates/obscura-cdp/src/server.rs
@@ -1198,7 +1198,7 @@ fn is_navigate_method(text: &str) -> bool {
// Fetch.continueRequest / fulfillRequest) into a map. Returns None when the
// `headers` field is absent, so the caller can leave the request's headers
// untouched rather than clearing them.
-fn parse_cdp_headers(params: &serde_json::Value) -> Option<HashMap<String, String>> {
+pub(crate) fn parse_cdp_headers(params: &serde_json::Value) -> Option<HashMap<String, String>> {
let arr = params.get("headers")?.as_array()?;
Some(
arr.iter()
@@ -1576,7 +1576,7 @@ async fn process_cdp_message(
}
}
-fn decode_base64(input: &str) -> String {
+pub(crate) fn decode_base64(input: &str) -> String {
fn val(c: u8) -> Option<u8> {
match c {
b'A'..=b'Z' => Some(c - b'A'),