Skip to content

Commit 7fdad36

Browse files
committed
Bound the serve GET's error body read in time
The error body is only shown in the message, so read it under a 64 KiB cap (or the caller's, if smaller) and the attempt's body timeout; a longer or stalled body is dropped and the status alone is reported. Assisted-by: Claude Code:claude-opus-5-5
1 parent 90d4d40 commit 7fdad36

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

‎crates/socket-patch-core/src/api/client.rs‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,11 +1732,17 @@ impl ApiClient {
17321732
if let Some(err) = classify_auth_error(status, true) {
17331733
return Err((err, hint));
17341734
}
1735-
// The caller's cap bounds an error body too.
1736-
let text = read_capped_typed(resp, max_bytes, noun)
1737-
.await
1738-
.map(|b| String::from_utf8_lossy(&b).into_owned())
1739-
.unwrap_or_default();
1735+
// An error body is only shown, so a small cap and the body
1736+
// timeout bound it; a longer or stalled one is dropped.
1737+
let text = tokio::time::timeout(
1738+
self.vendor_retry.body_timeout,
1739+
read_capped_typed(resp, max_bytes.min(64 * 1024), noun),
1740+
)
1741+
.await
1742+
.ok()
1743+
.and_then(Result::ok)
1744+
.map(|b| String::from_utf8_lossy(&b).into_owned())
1745+
.unwrap_or_default();
17401746
let head = format!("{noun} download failed with status");
17411747
return Err((ApiError::Other(status_error(&head, status, &text)), hint));
17421748
}
@@ -2922,8 +2928,8 @@ mod patch_api_seam_tests {
29222928
.is_err());
29232929
}
29242930

2925-
/// An error body is read under the caller's cap too: its message
2926-
/// shows when it fits, and an oversized one is dropped unread.
2931+
/// An error body is read under the caller's cap (at most 64 KiB): its
2932+
/// message shows when it fits, and an oversized one is dropped unread.
29272933
#[tokio::test]
29282934
async fn trait_download_error_body_is_capped() {
29292935
use wiremock::matchers::{method, path};

0 commit comments

Comments
 (0)