fix: bound site JSON response bodies

This commit is contained in:
2026-09-24 21:25:03 +08:00
parent 6a87583cc0
commit 7b4e273533
8 changed files with 87 additions and 27 deletions
+26
View File
@@ -133,6 +133,32 @@ async fn next_chunk(response: &mut reqwest::Response) -> Result<Option<bytes::By
}
}
/// Reads a successful API response body with a hard byte cap.
pub(crate) async fn send_json_response(
mut response: reqwest::Response,
site: &'static str,
) -> Result<bytes::Bytes, FetchError> {
if let Some(len) = response.content_length()
&& len > crate::site::MAX_SITE_JSON_BYTES as u64
{
return Err(FetchError::Site {
site,
error: "site response exceeds JSON size cap".into(),
});
}
let mut body = Vec::new();
while let Some(chunk) = next_chunk(&mut response).await? {
if body.len().saturating_add(chunk.len()) > crate::site::MAX_SITE_JSON_BYTES {
return Err(FetchError::Site {
site,
error: "site response exceeds JSON size cap".into(),
});
}
body.extend_from_slice(&chunk);
}
Ok(bytes::Bytes::from(body))
}
/// Whether an address must never be fetched. Media URLs come from a site's own
/// API response and the bytes are uploaded to Telegram, so following one into
/// the host's own network would turn the bot into a proxy for it: a cloud