From 1e9d1c1e7702a1962869d0fd8568ad2fb920bb97 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Mon, 27 Jul 2026 05:55:40 +0800 Subject: [PATCH] fix(net): bound an attachment download while it runs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit F-70. The body was read to completion with `bytes()` and only then measured against a 16 MiB ceiling, so a server answering an image request with gigabytes — by fault or by design — was held in memory in full before anything rejected it. Now read in chunks and abandoned the moment the ceiling would be crossed, with a declared Content-Length over the ceiling refused before the body is touched at all. Promoted from a theoretical bound to a real one by the voice work ahead: audio replies are far larger than the stickers this path was built for, so the ceiling becomes reachable in ordinary use rather than only under attack. The fake server for this serves its body with `Connection: close` and no length rather than chunked framing, so the test cannot pass or fail on the fixture's own framing. It counts the blocks it managed to write, which is what distinguishes a download that stopped early from one that was read in full and measured afterwards — confirmed by reverting to `bytes()`, which fails the test. Co-Authored-By: Claude Opus 5 --- src-tauri/src/networking/astrbot_client.rs | 142 ++++++++++++++++++++- 1 file changed, 139 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/networking/astrbot_client.rs b/src-tauri/src/networking/astrbot_client.rs index 31eca9f..aab3971 100644 --- a/src-tauri/src/networking/astrbot_client.rs +++ b/src-tauri/src/networking/astrbot_client.rs @@ -98,6 +98,16 @@ const MAX_SSE_LINE_BYTES: usize = 1024 * 1024; /// message text would grow without limit. A real reply is far below this. const MAX_STREAM_BYTES: u64 = 64 * 1024 * 1024; +/// Ceiling on a single downloaded attachment. +/// +/// Enforced while reading rather than after: the body used to be read to +/// completion with `bytes()` and only then measured, so a server that answered +/// an image request with gigabytes — by fault or by design — was held in memory +/// in full before anything rejected it. Audio replies make this reachable in +/// ordinary use rather than only under attack, since they are far larger than +/// the stickers this path was built for. +const MAX_ATTACHMENT_BYTES: usize = 16 * 1024 * 1024; + /// Splits an SSE byte stream into lines at the byte level. /// /// TCP chunk boundaries fall on arbitrary byte offsets, so decoding each chunk @@ -548,9 +558,29 @@ impl AstrBotClient { .filter(|value| !value.is_empty()) .unwrap_or("application/octet-stream") .to_owned(); - let bytes = response.bytes().await?.to_vec(); - if bytes.is_empty() || bytes.len() > 16 * 1024 * 1024 { - return Ok(Err("attachment size is outside the supported range".into())); + // Rejected on the declared length first, which costs nothing when the + // server is honest. The streaming loop below is what actually holds the + // bound, because Content-Length is a claim and chunked responses make + // none at all. + if let Some(declared) = response.content_length() + && declared > MAX_ATTACHMENT_BYTES as u64 + { + return Ok(Err(format!( + "attachment declares {declared} bytes, over the {MAX_ATTACHMENT_BYTES} byte limit" + ))); + } + let mut response = response; + let mut bytes: Vec = Vec::new(); + while let Some(chunk) = response.chunk().await? { + if bytes.len() + chunk.len() > MAX_ATTACHMENT_BYTES { + return Ok(Err(format!( + "attachment exceeded the {MAX_ATTACHMENT_BYTES} byte limit while downloading" + ))); + } + bytes.extend_from_slice(&chunk); + } + if bytes.is_empty() { + return Ok(Err("attachment response was empty".into())); } // A blob URL is decoded according to the blob's type, so handing the @@ -1526,6 +1556,112 @@ Transfer-Encoding: chunked\r\n\r\n"; } } + /// Serves an attachment response of `chunks` 512 KiB frames, counting how + /// many it managed to write. A client that stops reading once its ceiling is + /// reached makes the later writes fail, so the count is the evidence that the + /// download was bounded while it ran rather than measured after the fact. + async fn spawn_oversized_attachment( + chunks: usize, + declared_length: Option, + ) -> (String, Arc) { + let listener = TcpListener::bind("127.0.0.1:0").await.expect("bind"); + let port = listener.local_addr().expect("addr").port(); + let written = Arc::new(AtomicUsize::new(0)); + let counter = Arc::clone(&written); + + tokio::spawn(async move { + loop { + let Ok((mut socket, _)) = listener.accept().await else { + return; + }; + let counter = Arc::clone(&counter); + tokio::spawn(async move { + if read_request(&mut socket).await.is_none() { + return; + } + // `Connection: close` with no length at all: hyper reads the + // body until EOF. Chosen over chunked framing so the fake + // server has no framing of its own that could fail and be + // mistaken for the guard under test. + let header = match declared_length { + Some(length) => format!( + "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: {length}\r\n\r\n" + ), + None => "HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nConnection: close\r\n\r\n" + .to_owned(), + }; + if socket.write_all(header.as_bytes()).await.is_err() { + return; + } + let block = vec![b'x'; 512 * 1024]; + for _ in 0..chunks { + if socket.write_all(&block).await.is_err() || socket.flush().await.is_err() + { + return; + } + counter.fetch_add(1, Ordering::SeqCst); + } + let _ = socket.shutdown().await; + }); + } + }); + + (format!("http://127.0.0.1:{port}"), written) + } + + /// A body larger than the ceiling is refused without being held in full. + /// + /// F-70. `bytes()` read the whole response before anything measured it, so a + /// server answering with gigabytes was buffered entirely first. + #[tokio::test] + async fn an_oversized_attachment_is_refused_while_it_downloads() { + // 48 MiB offered against a 16 MiB ceiling, chunked so no length is + // declared and only the reading loop can stop it. + let (base_url, written) = spawn_oversized_attachment(96, None).await; + let client = client_for(&base_url); + + let error = client + .download_attachment("huge.png") + .await + .expect_err("must refuse"); + + assert!( + error.to_string().contains("while downloading"), + "expected the streaming guard to trip, got {error:?}" + ); + // Four candidate paths are tried, so a client that read every response in + // full would have let the server write 4 * 96 frames. + let frames = written.load(Ordering::SeqCst); + assert!( + frames < 4 * 96, + "the client read every offered frame: {frames}" + ); + } + + /// A declared length over the ceiling is refused before the body is read. + #[tokio::test] + async fn an_attachment_declaring_an_oversized_length_is_refused_up_front() { + let (base_url, written) = spawn_oversized_attachment(96, Some(96 * 512 * 1024)).await; + let client = client_for(&base_url); + + let error = client + .download_attachment("huge.png") + .await + .expect_err("must refuse"); + + assert!( + error.to_string().contains("declares"), + "expected the declared-length guard, got {error}" + ); + // Nothing of the body should have been consumed, so the server's writes + // block or fail almost immediately. + assert!( + written.load(Ordering::SeqCst) < 4 * 96, + "{}", + written.load(Ordering::SeqCst) + ); + } + fn client_for(base_url: &str) -> AstrBotClient { AstrBotClient::new(AstrBotClientConfig { profile_id: "profile-a".into(),