Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(inference): read at most 64 KiB of a provider's error body

M3gA-Minda91b27b400e7
diff --git a/crates/openhuman-core/src/inference/provider/http_error.rs b/crates/openhuman-core/src/inference/provider/http_error.rs
index e74013b0d0..31a62c0f2b 100644
--- a/crates/openhuman-core/src/inference/provider/http_error.rs
+++ b/crates/openhuman-core/src/inference/provider/http_error.rs
@@ -259,14 +259,38 @@ impl ErrorReporter for HostErrorReporter<'_> {
}
}
+/// An error body is only classified and quoted (a few hundred characters at
+/// most), so read no more than this: a misbehaving endpoint must not make the
+/// client buffer an unbounded body.
+const MAX_ERROR_BODY_BYTES: usize = 64 * 1024;
+
+/// The first [`MAX_ERROR_BODY_BYTES`] of a failed response's body.
+async fn read_error_body(mut response: reqwest::Response) -> String {
+ let mut body = Vec::new();
+ loop {
+ match response.chunk().await {
+ Ok(Some(chunk)) => {
+ let room = MAX_ERROR_BODY_BYTES - body.len();
+ body.extend_from_slice(&chunk[..chunk.len().min(room)]);
+ if body.len() == MAX_ERROR_BODY_BYTES {
+ break;
+ }
+ }
+ Ok(None) => break,
+ Err(_) if body.is_empty() => {
+ return "<failed to read provider error body>".to_string();
+ }
+ Err(_) => break,
+ }
+ }
+ String::from_utf8_lossy(&body).into_owned()
+}
+
/// Build a sanitized provider error from a failed HTTP response, classifying
/// it upstream and routing the reaction through [`HostErrorReporter`].
pub async fn api_error(provider: &str, response: reqwest::Response) -> anyhow::Error {
let status = response.status();
- let body = response
- .text()
- .await
- .unwrap_or_else(|_| "<failed to read provider error body>".to_string());
+ let body = read_error_body(response).await;
let message = format!(
"{provider} API error ({status}): {}",
sanitize_api_error(&body)
diff --git a/crates/openhuman-core/src/inference/provider/http_error_tests.rs b/crates/openhuman-core/src/inference/provider/http_error_tests.rs
index 42dab3b4fd..b2bc2ed16b 100644
--- a/crates/openhuman-core/src/inference/provider/http_error_tests.rs
+++ b/crates/openhuman-core/src/inference/provider/http_error_tests.rs
@@ -11,6 +11,16 @@ fn response(status: StatusCode, body: &str) -> reqwest::Response {
)
}
+#[tokio::test]
+async fn a_provider_error_body_is_read_only_up_to_the_cap() {
+ let huge = "x".repeat(MAX_ERROR_BODY_BYTES * 4);
+ let body = read_error_body(response(StatusCode::INTERNAL_SERVER_ERROR, &huge)).await;
+ assert_eq!(body.len(), MAX_ERROR_BODY_BYTES);
+
+ let small = read_error_body(response(StatusCode::BAD_REQUEST, "short body")).await;
+ assert_eq!(small, "short body");
+}
+
const FIXTURE_BACKEND: &str = "fixture-managed-provider";
fn install_fixture_backend_classifier() {