Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(rust-sdk): pin pagination next URLs to the api_url origin (#4866)
Gergő Móricz6174f2b1e545
diff --git a/apps/rust-sdk/CHANGELOG.md b/apps/rust-sdk/CHANGELOG.mdindex 1e13dcafd..267b483dd 100644--- a/apps/rust-sdk/CHANGELOG.md+++ b/apps/rust-sdk/CHANGELOG.md@@ -1,5 +1,13 @@## CHANGELOG+## [2.21.1] - 2026-09-30++### Security++- Pagination `next` URLs followed by `get_crawl_status`,+ `get_batch_scrape_status`, and `get_monitor_check` are now pinned to the+ configured API origin, so the API key is never sent to another host.+## [2.19.0] - 2026-09-08### Addeddiff --git a/apps/rust-sdk/Cargo.lock b/apps/rust-sdk/Cargo.lockindex f18f3652b..857f9a89a 100644--- a/apps/rust-sdk/Cargo.lock+++ b/apps/rust-sdk/Cargo.lock@@ -250,7 +250,7 @@ checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be"[[package]]name = "firecrawl"-version = "2.21.0"+version = "2.21.1"dependencies = ["mockito","reqwest",diff --git a/apps/rust-sdk/Cargo.toml b/apps/rust-sdk/Cargo.tomlindex a32c81a5c..933944eff 100644--- a/apps/rust-sdk/Cargo.toml+++ b/apps/rust-sdk/Cargo.toml@@ -1,6 +1,6 @@[package]name = "firecrawl"-version = "2.21.0"+version = "2.21.1"edition = "2021"license = "MIT"homepage = "https://www.firecrawl.dev/"diff --git a/apps/rust-sdk/src/batch_scrape.rs b/apps/rust-sdk/src/batch_scrape.rsindex 672c3ea45..1111dde64 100644--- a/apps/rust-sdk/src/batch_scrape.rs+++ b/apps/rust-sdk/src/batch_scrape.rs@@ -228,7 +228,7 @@ impl Client {) -> Result<BatchScrapeJob, FirecrawlError> {let response = self.client- .get(next)+ .get(self.pin_to_api_origin(next)?).headers(self.prepare_headers(None)).send().await@@ -633,4 +633,67 @@ mod tests {assert_eq!(errors.errors[0].error, "Connection timeout");mock.assert();}++ #[tokio::test]+ async fn test_get_batch_scrape_status_pins_next_to_api_origin() {+ let mut server = mockito::Server::new_async().await;+ let mut other = mockito::Server::new_async().await;+ let foreign = other+ .mock("GET", Matcher::Any)+ .expect(0)+ .create_async()+ .await;+ let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();+ let path = "/v2/batch/scrape/batch-123?skip=1";+ let mut nexts = vec![format!("{}{}", server.url(), path)];+ nexts.extend(crate::client::foreign_next_urls(+ &server.url(),+ &other.url(),+ path,+ ));++ for next in nexts {+ let first = server+ .mock("GET", "/v2/batch/scrape/batch-123")+ .with_status(200)+ .with_header("content-type", "application/json")+ .with_body(+ json!({+ "status": "completed",+ "total": 2,+ "completed": 2,+ "creditsUsed": 2,+ "next": next,+ "data": [{ "markdown": "# Page 1" }]+ })+ .to_string(),+ )+ .create_async()+ .await;+ let page = server+ .mock("GET", path)+ .match_header("authorization", "Bearer test_key")+ .with_status(200)+ .with_header("content-type", "application/json")+ .with_body(+ json!({+ "status": "completed",+ "total": 2,+ "completed": 2,+ "creditsUsed": 2,+ "data": [{ "markdown": "# Page 2" }]+ })+ .to_string(),+ )+ .create_async()+ .await;++ let status = client.get_batch_scrape_status("batch-123").await.unwrap();++ assert_eq!(status.data.len(), 2, "next = {}", next);+ first.assert_async().await;+ page.assert_async().await;+ }+ foreign.assert_async().await;+ }}diff --git a/apps/rust-sdk/src/client.rs b/apps/rust-sdk/src/client.rsindex 8f57d9b52..bc65bb953 100644--- a/apps/rust-sdk/src/client.rs+++ b/apps/rust-sdk/src/client.rs@@ -194,6 +194,39 @@ impl Client {pub(crate) fn url(&self, path: &str) -> String {format!("{}{}{}", self.api_url, API_VERSION, path)}++ /// Resolves a pagination `next` URL against `api_url`, rewriting absolute and+ /// protocol-relative URLs onto the `api_url` origin so credentials never leave it.+ pub(crate) fn pin_to_api_origin(&self, next: &str) -> Result<reqwest::Url, FirecrawlError> {+ let mut pinned = reqwest::Url::parse(&format!("{}/", self.api_url))+ .ok()+ .filter(reqwest::Url::has_host)+ .ok_or_else(|| {+ FirecrawlError::Misuse(format!(+ "api_url must be an absolute URL, got {:?}",+ self.api_url+ ))+ })?;+ let resolved = pinned.join(next).map_err(|e| {+ FirecrawlError::ResponseParseError(serde::de::Error::custom(format!(+ "invalid pagination URL {:?}: {}",+ next, e+ )))+ })?;+ pinned.set_path(resolved.path());+ pinned.set_query(resolved.query());+ Ok(pinned)+ }+}++#[cfg(test)]+pub(crate) fn foreign_next_urls(api_url: &str, other_port_url: &str, path: &str) -> Vec<String> {+ vec![+ format!("https://evil.example{}", path),+ format!("//evil.example{}", path),+ format!("{}{}", other_port_url, path),+ format!("{}{}", api_url.replacen("http://", "https://", 1), path),+ ]}#[cfg(test)]@@ -255,4 +288,65 @@ mod tests {let client = Client::new_selfhosted("http://localhost:3000/", None::<&str>).unwrap();assert_eq!(client.api_url, "http://localhost:3000");}++ #[test]+ fn test_pin_to_api_origin() {+ let client = Client::new_selfhosted("https://api.firecrawl.dev", Some("key")).unwrap();+ let pinned = |next: &str| client.pin_to_api_origin(next).unwrap().to_string();+ let expected = "https://api.firecrawl.dev/v2/crawl/abc?skip=10";++ assert_eq!(+ pinned("https://api.firecrawl.dev/v2/crawl/abc?skip=10"),+ expected+ );+ assert_eq!(+ pinned("https://evil.example/v2/crawl/abc?skip=10"),+ expected+ );+ assert_eq!(pinned("//evil.example/v2/crawl/abc?skip=10"), expected);+ assert_eq!(+ pinned("https://api.firecrawl.dev:8443/v2/crawl/abc?skip=10"),+ expected+ );+ assert_eq!(+ pinned("http://api.firecrawl.dev/v2/crawl/abc?skip=10"),+ expected+ );+ assert_eq!(+ pinned("https://user:[email protected]/v2/crawl/abc?skip=10#frag"),+ expected+ );+ assert_eq!(pinned("/v2/crawl/abc?skip=10"), expected);+ assert_eq!(pinned("https://evil.example"), "https://api.firecrawl.dev/");+ }++ #[test]+ fn test_pin_to_api_origin_keeps_self_hosted_port() {+ let client = Client::new_selfhosted("http://localhost:3002/", None::<&str>).unwrap();+ assert_eq!(+ client+ .pin_to_api_origin("https://evil.example:444/v2/batch/scrape/x?skip=5")+ .unwrap()+ .as_str(),+ "http://localhost:3002/v2/batch/scrape/x?skip=5"+ );+ }++ #[test]+ fn test_pin_to_api_origin_reports_malformed_next_as_response_error() {+ let client = Client::new("key").unwrap();+ assert!(matches!(+ client.pin_to_api_origin("https://evil.example:99999/v2/crawl/abc"),+ Err(FirecrawlError::ResponseParseError(_))+ ));+ }++ #[test]+ fn test_pin_to_api_origin_rejects_relative_api_url() {+ let client = Client::new_selfhosted("localhost:3002", None::<&str>).unwrap();+ assert!(matches!(+ client.pin_to_api_origin("https://evil.example/v2/crawl/abc"),+ Err(FirecrawlError::Misuse(_))+ ));+ }}diff --git a/apps/rust-sdk/src/crawl.rs b/apps/rust-sdk/src/crawl.rsindex 25de1f293..aa196b630 100644--- a/apps/rust-sdk/src/crawl.rs+++ b/apps/rust-sdk/src/crawl.rs@@ -246,7 +246,7 @@ impl Client {async fn get_crawl_status_next(&self, next: &str) -> Result<CrawlJob, FirecrawlError> {let response = self.client- .get(next)+ .get(self.pin_to_api_origin(next)?).headers(self.prepare_headers(None)).send().await@@ -681,4 +681,67 @@ mod tests {start_mock.assert();status_mock.assert();}++ #[tokio::test]+ async fn test_get_crawl_status_pins_next_to_api_origin() {+ let mut server = mockito::Server::new_async().await;+ let mut other = mockito::Server::new_async().await;+ let foreign = other+ .mock("GET", Matcher::Any)+ .expect(0)+ .create_async()+ .await;+ let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();+ let path = "/v2/crawl/crawl-123?skip=1";+ let mut nexts = vec![format!("{}{}", server.url(), path)];+ nexts.extend(crate::client::foreign_next_urls(+ &server.url(),+ &other.url(),+ path,+ ));++ for next in nexts {+ let first = server+ .mock("GET", "/v2/crawl/crawl-123")+ .with_status(200)+ .with_header("content-type", "application/json")+ .with_body(+ json!({+ "status": "completed",+ "total": 2,+ "completed": 2,+ "creditsUsed": 2,+ "next": next,+ "data": [{ "markdown": "# Page 1" }]+ })+ .to_string(),+ )+ .create_async()+ .await;+ let page = server+ .mock("GET", path)+ .match_header("authorization", "Bearer test_key")+ .with_status(200)+ .with_header("content-type", "application/json")+ .with_body(+ json!({+ "status": "completed",+ "total": 2,+ "completed": 2,+ "creditsUsed": 2,+ "data": [{ "markdown": "# Page 2" }]+ })+ .to_string(),+ )+ .create_async()+ .await;++ let status = client.get_crawl_status("crawl-123").await.unwrap();++ assert_eq!(status.data.len(), 2, "next = {}", next);+ first.assert_async().await;+ page.assert_async().await;+ }+ foreign.assert_async().await;+ }}diff --git a/apps/rust-sdk/src/monitor.rs b/apps/rust-sdk/src/monitor.rsindex 0897eddf7..9bf9f8ee1 100644--- a/apps/rust-sdk/src/monitor.rs+++ b/apps/rust-sdk/src/monitor.rs@@ -440,7 +440,7 @@ impl Client {while let Some(next) = check.next.clone() {let response = self.client- .get(next)+ .get(self.pin_to_api_origin(&next)?).headers(self.prepare_headers(None)).send().await@@ -564,4 +564,77 @@ mod tests {assert_eq!(json["targetId"], "t1");assert_eq!(json["searchCredits"], 2.5);}++ #[tokio::test]+ async fn test_get_monitor_check_pins_next_to_api_origin() {+ use mockito::Matcher;++ let check = |next: Option<&str>, page_id: &str| {+ serde_json::json!({+ "success": true,+ "data": {+ "id": "check-1",+ "monitorId": "mon-1",+ "status": "completed",+ "trigger": "manual",+ "billingStatus": "billed",+ "summary": { "totalPages": 2, "same": 2, "changed": 0, "new": 0, "removed": 0, "error": 0 },+ "createdAt": "2026-09-01T00:00:00Z",+ "updatedAt": "2026-09-01T00:00:00Z",+ "pages": [{+ "id": page_id,+ "targetId": "t1",+ "url": "https://example.com",+ "status": "same",+ "createdAt": "2026-09-01T00:00:00Z"+ }],+ "next": next+ }+ })+ .to_string()+ };+ let mut server = mockito::Server::new_async().await;+ let mut other = mockito::Server::new_async().await;+ let foreign = other+ .mock("GET", Matcher::Any)+ .expect(0)+ .create_async()+ .await;+ let client = Client::new_selfhosted(server.url(), Some("test_key")).unwrap();+ let path = "/v2/monitor/mon-1/checks/check-1?skip=1";+ let mut nexts = vec![format!("{}{}", server.url(), path)];+ nexts.extend(crate::client::foreign_next_urls(+ &server.url(),+ &other.url(),+ path,+ ));++ for next in nexts {+ let first = server+ .mock("GET", "/v2/monitor/mon-1/checks/check-1")+ .with_status(200)+ .with_header("content-type", "application/json")+ .with_body(check(Some(&next), "p1"))+ .create_async()+ .await;+ let page = server+ .mock("GET", path)+ .match_header("authorization", "Bearer test_key")+ .with_status(200)+ .with_header("content-type", "application/json")+ .with_body(check(None, "p2"))+ .create_async()+ .await;++ let detail = client+ .get_monitor_check("mon-1", "check-1", None, None, None)+ .await+ .unwrap();++ assert_eq!(detail.pages.len(), 2, "next = {}", next);+ first.assert_async().await;+ page.assert_async().await;+ }+ foreign.assert_async().await;+ }}