mirror of
https://github.com/n0-computer/iroh.git
synced 2026-12-03 08:01:40 +00:00
feature: ensure we are not being lied to about total_blobs_size
If e.g. somebody sets total_blobs_size to 1000 but then sends us a huge collection, before it would download it all. Now it will fail as soon as total_blobs_size is exceeded.
This commit is contained in:
+14
-7
@@ -145,9 +145,21 @@ where
|
||||
on_collection(collection.clone()).await?;
|
||||
|
||||
// expect to get blob data in the order they appear in the collection
|
||||
let mut remaining_size = total_blobs_size;
|
||||
for blob in collection.blobs {
|
||||
let blob_reader =
|
||||
let mut blob_reader =
|
||||
handle_blob_response(blob.hash, reader, &mut in_buffer).await?;
|
||||
|
||||
let size = blob_reader.read_size().await?;
|
||||
anyhow::ensure!(
|
||||
size <= MAX_DATA_SIZE,
|
||||
"size too large: {size} > {MAX_DATA_SIZE}"
|
||||
);
|
||||
anyhow::ensure!(
|
||||
size <= remaining_size,
|
||||
"downloaded more than {total_blobs_size}"
|
||||
);
|
||||
remaining_size -= size;
|
||||
let blob_reader =
|
||||
on_blob(blob.hash, blob_reader, Some(blob.name)).await?;
|
||||
reader = blob_reader.into_inner();
|
||||
@@ -215,12 +227,7 @@ async fn handle_blob_response<
|
||||
// next blob in collection will be sent over
|
||||
Res::Found => {
|
||||
assert!(buffer.is_empty());
|
||||
let mut decoder = AsyncSliceDecoder::new(reader, hash, 0, u64::MAX);
|
||||
let size = decoder.read_size().await?;
|
||||
anyhow::ensure!(
|
||||
size <= MAX_DATA_SIZE,
|
||||
"size too large: {size} > {MAX_DATA_SIZE}"
|
||||
);
|
||||
let decoder = AsyncSliceDecoder::new(reader, hash, 0, u64::MAX);
|
||||
Ok(decoder)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user