Skip to content

The headless browser for AI agents and web scraping

fix(js): block unparsable fetch URLs instead of skipping the SSRF gate

Andrey Mnatsakanov5bfac24b4f0f
diff --git a/crates/obscura-js/src/ops.rs b/crates/obscura-js/src/ops.rs
index 18564d24..b257e27c 100644
--- a/crates/obscura-js/src/ops.rs
+++ b/crates/obscura-js/src/ops.rs
@@ -3018,18 +3018,22 @@ async fn op_fetch_url(
let allow_private_network = http_client
.as_ref()
.is_some_and(|client| client.allow_private_network);
- if let Ok(parsed_url) = url::Url::parse(&url) {
- if let Err(e) = validate_fetch_url(&parsed_url, allow_private_network) {
- return Ok(serde_json::json!({
- "status": 0,
- "body": "",
- "url": url,
- "headers": {},
- "blocked": true,
- "error": e,
- })
- .to_string());
- }
+ // Same rule as the rewrite below: a URL the gate cannot parse is one it
+ // cannot check, so it never reaches the transport.
+ let gate_error = match url::Url::parse(&url) {
+ Ok(parsed_url) => validate_fetch_url(&parsed_url, allow_private_network).err(),
+ Err(err) => Some(format!("Unparsable URL blocked: {}", err)),
+ };
+ if let Some(e) = gate_error {
+ return Ok(serde_json::json!({
+ "status": 0,
+ "body": "",
+ "url": url,
+ "headers": {},
+ "blocked": true,
+ "error": e,
+ })
+ .to_string());
}
struct PageInFlightGuard(Arc<std::sync::atomic::AtomicU32>);
impl Drop for PageInFlightGuard {
@@ -3121,17 +3125,30 @@ async fn op_fetch_url(
// below). Without this re-validation a rewrite to an internal address would
// bypass validate_fetch_url entirely.
let url = if let Some(new_url) = override_url {
- if let Ok(parsed) = url::Url::parse(&new_url) {
- if let Err(reason) = validate_fetch_url(&parsed, allow_private_network) {
+ // A rewrite the gate cannot parse is a rewrite the gate cannot check:
+ // refuse it here rather than hand the raw string to the transport.
+ let parsed = match url::Url::parse(&new_url) {
+ Ok(parsed) => parsed,
+ Err(err) => {
return Ok(serde_json::json!({
"status": 0,
"body": "",
"url": new_url,
"blocked": true,
- "error": format!("Intercept rewrite to forbidden URL blocked: {}", reason),
+ "error": format!("Intercept rewrite to unparsable URL blocked: {}", err),
})
.to_string());
}
+ };
+ if let Err(reason) = validate_fetch_url(&parsed, allow_private_network) {
+ return Ok(serde_json::json!({
+ "status": 0,
+ "body": "",
+ "url": new_url,
+ "blocked": true,
+ "error": format!("Intercept rewrite to forbidden URL blocked: {}", reason),
+ })
+ .to_string());
}
new_url
} else {
diff --git a/crates/obscura/tests/interception.rs b/crates/obscura/tests/interception.rs
index 4bbe0548..03d488ff 100644
--- a/crates/obscura/tests/interception.rs
+++ b/crates/obscura/tests/interception.rs
@@ -27,6 +27,12 @@ fn spawn_echo_server() -> String {
("application/json", "{\"hello\":\"world\"}".to_string())
} else if path.starts_with("/modified") {
("text/plain", "REWRITTEN".to_string())
+ } else if path.starts_with("/probe") {
+ // Report how the page's own fetch('/api') settled.
+ (
+ "text/html",
+ "<script>fetch('/api').then(r => { document.title = 'resolved:' + r.status; }, e => { document.title = 'rejected:' + e.name; });</script>".to_string(),
+ )
} else {
("text/html", "<script>fetch('/api');</script>".to_string())
};
@@ -213,6 +219,60 @@ async fn page_rewrites_request_url_via_interception() {
);
}
+/// A Continue rewrite that is not a URL must be refused like a forbidden one.
+/// Before the fix the SSRF re-validation was skipped when the override failed
+/// to parse and the raw string went on to the transport, which reported it as
+/// an ordinary transport failure (an opaque status-0 response) instead of a
+/// blocked request.
+#[tokio::test]
+async fn unparsable_continue_rewrite_is_blocked() {
+ std::env::set_var("OBSCURA_ALLOW_PRIVATE_NETWORK", "1");
+ let base = spawn_echo_server();
+
+ let browser = Browser::new().unwrap();
+ let mut page = browser.new_page().await.unwrap();
+
+ let mut rx = page.enable_interception();
+ tokio::spawn(async move {
+ while let Some(req) = rx.recv().await {
+ let new_url = req
+ .url
+ .contains("/api")
+ .then(|| "not a url at all".to_string());
+ if req
+ .resolver
+ .send(InterceptResolution::Continue {
+ url: new_url,
+ method: None,
+ headers: None,
+ body: None,
+ })
+ .is_err()
+ {
+ break;
+ }
+ }
+ });
+
+ page.goto(&format!("{}/probe", base)).await.unwrap();
+ let mut outcome = String::new();
+ for _ in 0..20 {
+ page.settle(500).await;
+ outcome = page
+ .evaluate("document.title")
+ .as_str()
+ .unwrap_or("")
+ .to_string();
+ if !outcome.is_empty() {
+ break;
+ }
+ }
+ assert_eq!(
+ outcome, "rejected:AbortError",
+ "an unparsable Continue rewrite must be blocked, not attempted"
+ );
+}
+
// Issue #408: a callback registered with on_response must be detachable via the
// returned id, so a crawler can stop capturing after a phase. Before the fix the
// vecs were append-only and a callback fired for the client's whole lifetime.