Skip to content

Commit 96938b1

Browse files
authored
Merge branch 'main' into pe/fix-release-validation-workspace-import
2 parents 329e46b + a101e10 commit 96938b1

2 files changed

Lines changed: 82 additions & 6 deletions

File tree

‎src/cli/self_cmd.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use semver::Version;
77
use serde::Serialize;
88

99
use crate::infra::archive::extract_tar_gz;
10-
use crate::infra::download::{download_to_file, verify_file_sha256};
10+
use crate::infra::download::{download_to_file, http_agent, verify_file_sha256};
1111
use crate::store::resolve_ocm_home;
1212

1313
use super::{Cli, render};
@@ -404,7 +404,8 @@ impl Cli {
404404
format!("https://api.github.com/repos/{RELEASE_REPO}/releases/latest")
405405
};
406406

407-
let response = ureq::get(&url)
407+
let response = http_agent()
408+
.get(&url)
408409
.header("User-Agent", &format!("ocm/{}", env!("CARGO_PKG_VERSION")))
409410
.header("Accept", "application/vnd.github+json")
410411
.header("X-GitHub-Api-Version", "2022-11-28")

‎src/infra/download.rs‎

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,32 @@ use std::io;
33
use std::io::Read;
44
use std::path::Path;
55
use std::sync::LazyLock;
6+
use std::time::Duration;
67

78
use base64::Engine;
89
use flate2::read::GzDecoder;
910
use serde::de::DeserializeOwned;
1011
use sha2::{Digest, Sha256, Sha512};
1112

12-
static HTTP_AGENT: LazyLock<ureq::Agent> = LazyLock::new(ureq::agent);
13+
const MAX_DOWNLOAD_BYTES: u64 = 512 * 1024 * 1024;
14+
const HTTP_PHASE_TIMEOUT: Duration = Duration::from_secs(30);
15+
16+
// ureq carries a recv-response deadline into body reads. Bound the header wait from the
17+
// completed request-send phase instead, then use recv-body as the per-read stall timeout.
18+
static HTTP_AGENT: LazyLock<ureq::Agent> = LazyLock::new(|| {
19+
ureq::Agent::new_with_config(
20+
ureq::Agent::config_builder()
21+
.timeout_resolve(Some(HTTP_PHASE_TIMEOUT))
22+
.timeout_connect(Some(HTTP_PHASE_TIMEOUT))
23+
.timeout_send_request(Some(HTTP_PHASE_TIMEOUT))
24+
.timeout_recv_body(Some(HTTP_PHASE_TIMEOUT))
25+
.build(),
26+
)
27+
});
28+
29+
pub(crate) fn http_agent() -> &'static ureq::Agent {
30+
&HTTP_AGENT
31+
}
1332

1433
pub fn artifact_file_name_from_url(url: &str) -> Result<String, String> {
1534
let trimmed = url.trim();
@@ -49,10 +68,31 @@ pub fn download_to_file(url: &str, destination: &Path) -> Result<(), String> {
4968
}
5069

5170
let mut file = File::create(destination).map_err(|error| error.to_string())?;
52-
io::copy(&mut reader, &mut file).map_err(|error| error.to_string())?;
71+
copy_capped(&mut reader, &mut file, MAX_DOWNLOAD_BYTES).map_err(|error| error.to_string())?;
5372
Ok(())
5473
}
5574

75+
fn copy_capped<R: io::Read + ?Sized, W: io::Write + ?Sized>(
76+
reader: &mut R,
77+
writer: &mut W,
78+
max_bytes: u64,
79+
) -> io::Result<u64> {
80+
let copied = {
81+
let mut limited = reader.take(max_bytes);
82+
io::copy(&mut limited, writer)?
83+
};
84+
if copied == max_bytes {
85+
let mut extra = [0_u8; 1];
86+
if reader.read(&mut extra)? != 0 {
87+
return Err(io::Error::new(
88+
io::ErrorKind::InvalidData,
89+
format!("download exceeded {max_bytes} bytes"),
90+
));
91+
}
92+
}
93+
Ok(copied)
94+
}
95+
5696
pub fn fetch_json<T: DeserializeOwned>(url: &str) -> Result<T, String> {
5797
let reader = open_url_reader(url, None, true)?;
5898
parse_json_reader(reader, url)
@@ -81,7 +121,7 @@ fn open_url_reader(
81121
return Err("runtime URL is required".to_string());
82122
}
83123

84-
let mut request = HTTP_AGENT.get(trimmed);
124+
let mut request = http_agent().get(trimmed);
85125
if compressed_json {
86126
request = request.header("Accept-Encoding", "gzip");
87127
}
@@ -211,7 +251,8 @@ fn verify_file_sha512_base64(
211251

212252
#[cfg(test)]
213253
mod tests {
214-
use super::artifact_file_name_from_url;
254+
use super::{artifact_file_name_from_url, copy_capped};
255+
use std::io::{self, Read};
215256

216257
#[test]
217258
fn artifact_file_name_rejects_cross_platform_path_components() {
@@ -235,4 +276,38 @@ mod tests {
235276
"openclaw.tar.gz"
236277
);
237278
}
279+
280+
#[test]
281+
fn copy_capped_errors_when_reader_exceeds_max() {
282+
let mut reader = io::repeat(b'a').take(16);
283+
let mut writer = Vec::new();
284+
let error = copy_capped(&mut reader, &mut writer, 8).unwrap_err();
285+
assert!(
286+
error.to_string().contains("exceeded"),
287+
"unexpected error: {error}"
288+
);
289+
assert!(
290+
writer.len() <= 8,
291+
"fail closed must not keep more than the cap: {}",
292+
writer.len()
293+
);
294+
}
295+
296+
#[test]
297+
fn copy_capped_copies_when_reader_is_within_max() {
298+
let mut reader = &b"hello"[..];
299+
let mut writer = Vec::new();
300+
let copied = copy_capped(&mut reader, &mut writer, 8).unwrap();
301+
assert_eq!(copied, 5);
302+
assert_eq!(writer, b"hello");
303+
}
304+
305+
#[test]
306+
fn copy_capped_allows_a_reader_that_hits_the_max_exactly() {
307+
let mut reader = &b"hello"[..];
308+
let mut writer = Vec::new();
309+
let copied = copy_capped(&mut reader, &mut writer, 5).unwrap();
310+
assert_eq!(copied, 5);
311+
assert_eq!(writer, b"hello");
312+
}
238313
}

0 commit comments

Comments
 (0)