Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/fctl-agent/src/microvm/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ async fn read_chunked(r: &mut (impl AsyncRead + Unpin)) -> Result<Vec<u8>> {
while !read_line(r).await?.is_empty() {}
return Ok(body);
}
if body.len() + size > MAX_BODY {
// Not body.len() + size, which a size near usize::MAX overflows.
if size > MAX_BODY - body.len() {
bail!("the request's body is too long");
}
let start = body.len();
Expand Down Expand Up @@ -366,6 +367,9 @@ mod tests {
let chunked = b"POST /x HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nhel\r\n2\r\nlo\r\n0\r\n\r\n";
assert_eq!(read_request(&mut &chunked[..]).await?.body, b"hello");
assert!(read_request(&mut &b"GET /"[..]).await.is_err());
// A chunk too long to add to the body's length is refused.
let huge = b"POST /x HTTP/1.1\r\nTransfer-Encoding: chunked\r\n\r\n3\r\nhel\r\nffffffffffffffff\r\n";
assert!(read_request(&mut &huge[..]).await.is_err());
Ok(())
}

Expand Down
30 changes: 22 additions & 8 deletions crates/fctl-host/src/agentbin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,27 @@ async fn download(cfg: &HostConfig, arch: &str, cached: &std::path::Path) -> Res
if expected != actual {
bail!("checksum mismatch for {url}: expected {expected}, got {actual}");
}
let tmp = cached.with_extension("tmp");
std::fs::write(&tmp, &bytes)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&tmp, std::fs::Permissions::from_mode(0o755))?;
}
std::fs::rename(&tmp, cached)?;
// Beside it, its whole name kept (with_extension would cut it at the
// version's last dot, the same for every architecture), and its own to
// this download: two at once write a file each.
let mut tmp = cached.as_os_str().to_owned();
tmp.push(format!(
".{}.tmp",
hex::encode(fctl_domain::random_bytes::<4>())
));
let tmp = PathBuf::from(tmp);
let put = || -> std::io::Result<()> {
std::fs::write(&tmp, &bytes)?;
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(&tmp, std::fs::Permissions::from_mode(0o755))?;
}
std::fs::rename(&tmp, cached)
};
// Named for this download alone, nothing else would remove it.
put().inspect_err(|_| {
let _ = std::fs::remove_file(&tmp);
})?;
Ok(())
}
Loading
Loading