* fix(js): fire trusted input/change from setFileInputFiles DOM.setFileInputFiles dispatched plain input/change events, so event.isTrusted was false. Upload flows and anti-bot code that gate their change handler on isTrusted ignored them, which silently broke the exact case the feature targets. The Input domain already marks its synthesized clicks/keys trusted via __obscura_markTrusted; do the same here. Fixes #384. * fix(cdp): bound IO stream buffers so downloads cannot leak Fetch.takeResponseBodyAsStream moved a response body out of the page's LRU-bounded cache into CdpContext.io_streams, a server-lifetime map freed only by an explicit IO.close. A client that opens a stream and disconnects, errors mid-download, or never closes it leaked the full buffer forever; no teardown path (WS close, Browser.close, Target.closeTarget) touched io_streams. That reintroduced the unbounded memory growth streaming was meant to avoid (#360). Replace the raw map + counter with an IoStreamStore that caps the number of open streams and their total bytes (OBSCURA_IO_STREAM_MAX_ENTRIES / _BYTES), evicting the oldest first and always keeping the just-opened stream. Reading an evicted handle fails cleanly, which is the right trade against an OOM. Read chunking is unchanged. Fixes #386.
215 lines
7.2 KiB
Rust
215 lines
7.2 KiB
Rust
use std::collections::HashMap;
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
use crate::dispatch::CdpContext;
|
|
|
|
pub struct PausedRequest {
|
|
pub request_id: String,
|
|
pub url: String,
|
|
pub method: String,
|
|
pub headers: HashMap<String, String>,
|
|
pub resource_type: String,
|
|
pub resolver: tokio::sync::oneshot::Sender<FetchResolution>,
|
|
}
|
|
|
|
pub enum FetchResolution {
|
|
Continue {
|
|
url: Option<String>,
|
|
method: Option<String>,
|
|
headers: Option<HashMap<String, String>>,
|
|
post_data: Option<String>,
|
|
},
|
|
Fulfill {
|
|
status: u16,
|
|
headers: Vec<(String, String)>,
|
|
body: String,
|
|
},
|
|
Fail {
|
|
reason: String,
|
|
},
|
|
}
|
|
|
|
pub struct FetchInterceptState {
|
|
pub enabled: bool,
|
|
pub patterns: Vec<String>,
|
|
pub paused: HashMap<String, PausedRequest>,
|
|
request_counter: u64,
|
|
}
|
|
|
|
impl FetchInterceptState {
|
|
pub fn new() -> Self {
|
|
FetchInterceptState {
|
|
enabled: false,
|
|
patterns: Vec::new(),
|
|
paused: HashMap::new(),
|
|
request_counter: 0,
|
|
}
|
|
}
|
|
|
|
pub fn next_request_id(&mut self) -> String {
|
|
self.request_counter += 1;
|
|
format!("interception-{}", self.request_counter)
|
|
}
|
|
}
|
|
|
|
pub async fn handle(
|
|
method: &str,
|
|
params: &Value,
|
|
ctx: &mut CdpContext,
|
|
session_id: &Option<String>,
|
|
) -> Result<Value, String> {
|
|
match method {
|
|
"enable" => {
|
|
let patterns = params
|
|
.get("patterns")
|
|
.and_then(|v| v.as_array())
|
|
.map(|arr| {
|
|
arr.iter()
|
|
.filter_map(|p| {
|
|
p.get("urlPattern")
|
|
.and_then(|v| v.as_str())
|
|
.map(|s| s.to_string())
|
|
})
|
|
.collect::<Vec<_>>()
|
|
})
|
|
.unwrap_or_else(|| vec!["*".to_string()]);
|
|
|
|
ctx.fetch_intercept.enabled = true;
|
|
ctx.fetch_intercept.patterns = patterns.clone();
|
|
let tx_clone = ctx.intercept_tx.clone();
|
|
if let Some(page) = ctx.get_session_page_mut(session_id) {
|
|
page.intercept_block_patterns = patterns.clone();
|
|
if let Some(tx) = tx_clone {
|
|
page.set_intercept_tx(tx);
|
|
}
|
|
page.enable_intercept(true);
|
|
}
|
|
|
|
tracing::info!("Fetch interception enabled");
|
|
Ok(json!({}))
|
|
}
|
|
"disable" => {
|
|
ctx.fetch_intercept.enabled = false;
|
|
ctx.fetch_intercept.patterns.clear();
|
|
if let Some(page) = ctx.get_session_page_mut(session_id) {
|
|
page.intercept_block_patterns.clear();
|
|
page.enable_intercept(false);
|
|
}
|
|
let paused: Vec<_> = ctx.fetch_intercept.paused.drain().collect();
|
|
for (_, req) in paused {
|
|
let _ = req.resolver.send(FetchResolution::Continue {
|
|
url: None,
|
|
method: None,
|
|
headers: None,
|
|
post_data: None,
|
|
});
|
|
}
|
|
Ok(json!({}))
|
|
}
|
|
"continueRequest" => {
|
|
let request_id = params
|
|
.get("requestId")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or("requestId required")?;
|
|
|
|
if let Some(paused) = ctx.fetch_intercept.paused.remove(request_id) {
|
|
let _ = paused.resolver.send(FetchResolution::Continue {
|
|
url: params.get("url").and_then(|v| v.as_str()).map(|s| s.to_string()),
|
|
method: params.get("method").and_then(|v| v.as_str()).map(|s| s.to_string()),
|
|
headers: None,
|
|
post_data: params.get("postData").and_then(|v| v.as_str()).map(|s| s.to_string()),
|
|
});
|
|
}
|
|
Ok(json!({}))
|
|
}
|
|
"fulfillRequest" => {
|
|
let request_id = params
|
|
.get("requestId")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or("requestId required")?;
|
|
|
|
let status = params
|
|
.get("responseCode")
|
|
.and_then(|v| v.as_u64())
|
|
.unwrap_or(200) as u16;
|
|
let headers: HashMap<String, String> = params
|
|
.get("responseHeaders")
|
|
.and_then(|v| v.as_array())
|
|
.map(|arr| {
|
|
arr.iter()
|
|
.filter_map(|h| {
|
|
let name = h.get("name")?.as_str()?.to_string();
|
|
let value = h.get("value")?.as_str()?.to_string();
|
|
Some((name, value))
|
|
})
|
|
.collect()
|
|
})
|
|
.unwrap_or_default();
|
|
let body = params
|
|
.get("body")
|
|
.and_then(|v| v.as_str())
|
|
.unwrap_or("")
|
|
.to_string();
|
|
|
|
if let Some(paused) = ctx.fetch_intercept.paused.remove(request_id) {
|
|
let _ = paused.resolver.send(FetchResolution::Fulfill {
|
|
status,
|
|
headers: headers.into_iter().collect(),
|
|
body,
|
|
});
|
|
}
|
|
Ok(json!({}))
|
|
}
|
|
"failRequest" => {
|
|
let request_id = params
|
|
.get("requestId")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or("requestId required")?;
|
|
|
|
let reason = params
|
|
.get("errorReason")
|
|
.and_then(|v| v.as_str())
|
|
.unwrap_or("Failed")
|
|
.to_string();
|
|
|
|
if let Some(paused) = ctx.fetch_intercept.paused.remove(request_id) {
|
|
let _ = paused.resolver.send(FetchResolution::Fail { reason });
|
|
}
|
|
Ok(json!({}))
|
|
}
|
|
"getResponseBody" => {
|
|
Ok(json!({ "body": "", "base64Encoded": false }))
|
|
}
|
|
"takeResponseBodyAsStream" => {
|
|
// Hand the client a streaming handle for a large response body so it
|
|
// can pull it in chunks via IO.read and free it with IO.close,
|
|
// instead of receiving one giant base64 blob (issue #360). The body
|
|
// is moved out of the page cache into the stream, so it is held once
|
|
// and released on close. Requires the body to have been cached
|
|
// (raise OBSCURA_NETWORK_BODY_BUFFER_BYTES for large downloads).
|
|
let request_id = params
|
|
.get("requestId")
|
|
.and_then(|v| v.as_str())
|
|
.ok_or("Fetch.takeResponseBodyAsStream requires requestId")?;
|
|
|
|
let bytes = {
|
|
let page = ctx.get_session_page_mut(session_id).ok_or("No page")?;
|
|
page.take_response_body_raw(request_id)
|
|
}
|
|
.or_else(|| {
|
|
ctx.pages
|
|
.iter_mut()
|
|
.find_map(|p| p.take_response_body_raw(request_id))
|
|
})
|
|
.ok_or_else(|| {
|
|
format!("Fetch.takeResponseBodyAsStream: no cached body for {request_id}")
|
|
})?;
|
|
|
|
let handle = ctx.io_streams.insert(bytes);
|
|
Ok(json!({ "stream": handle }))
|
|
}
|
|
_ => Err(format!("Unknown Fetch method: {}", method)),
|
|
}
|
|
}
|