Skip to content

Commit e7eeb3c

Browse files
committed
fix(fetchers): enforce body size limits in wikipedia fetcher
1 parent 539fa84 commit e7eeb3c

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

crates/fetchkit/src/fetchers/wikipedia.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
66
use crate::client::FetchOptions;
77
use crate::error::FetchError;
8+
use crate::fetchers::default::{
9+
read_body_with_timeout, BODY_TIMEOUT, DEFAULT_MAX_BODY_SIZE, TRUNCATION_MESSAGE,
10+
};
811
use crate::fetchers::Fetcher;
912
use crate::types::{FetchRequest, FetchResponse};
1013
use crate::DEFAULT_USER_AGENT;
@@ -154,7 +157,10 @@ impl Fetcher for WikipediaFetcher {
154157
});
155158
}
156159

157-
let summary: WikiSummary = summary_resp.json().await.map_err(|e| {
160+
let max_body_size = options.max_body_size.unwrap_or(DEFAULT_MAX_BODY_SIZE);
161+
let (summary_body, _) =
162+
read_body_with_timeout(summary_resp, BODY_TIMEOUT, max_body_size).await?;
163+
let summary: WikiSummary = serde_json::from_slice(&summary_body).map_err(|e| {
158164
FetchError::FetcherError(format!("Failed to parse Wikipedia data: {}", e))
159165
})?;
160166

@@ -171,8 +177,14 @@ impl Fetcher for WikipediaFetcher {
171177
.await
172178
{
173179
Ok(resp) if resp.status().is_success() => {
174-
let html = resp.text().await.ok();
175-
html.map(|h| crate::convert::html_to_markdown(&h))
180+
let (html_body, truncated) =
181+
read_body_with_timeout(resp, BODY_TIMEOUT, max_body_size).await?;
182+
let html = String::from_utf8_lossy(&html_body);
183+
let mut markdown = crate::convert::html_to_markdown(&html);
184+
if truncated {
185+
markdown.push_str(TRUNCATION_MESSAGE);
186+
}
187+
Some(markdown)
176188
}
177189
_ => None,
178190
};

0 commit comments

Comments
 (0)