Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(iroh): handle out of bounds requests for blobs read_at #2729

Merged
merged 3 commits into from
Sep 16, 2024
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
15 changes: 15 additions & 0 deletions iroh/src/client/blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,21 @@ mod tests {
assert_eq!(reader.size(), 1024 * 128);
assert_eq!(reader.response_size, 20);

// out of bounds - too long
let res = client.blobs().read_at(hash, 0, Some(1024 * 128 + 1)).await;
let err = res.unwrap_err();
assert!(err.to_string().contains("out of bound"));

// out of bounds - offset larger than blob
let res = client.blobs().read_at(hash, 1024 * 128 + 1, None).await;
let err = res.unwrap_err();
assert!(err.to_string().contains("out of range"));

// out of bounds - offset + length too large
let res = client.blobs().read_at(hash, 1024 * 127, Some(1025)).await;
let err = res.unwrap_err();
assert!(err.to_string().contains("out of bound"));

Ok(())
}

Expand Down
20 changes: 18 additions & 2 deletions iroh/src/node/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,15 +1230,31 @@ impl<D: BaoStore> Handler<D> {
let entry = db.get(&req.hash).await?;
let entry = entry.ok_or_else(|| anyhow!("Blob not found"))?;
let size = entry.size();

anyhow::ensure!(
req.offset <= size.value(),
"requested offset is out of range: {} > {:?}",
req.offset,
size
);

let len = req.len.unwrap_or((size.value() - req.offset) as usize);

anyhow::ensure!(
req.offset + len as u64 <= size.value(),
"requested range is out of bounds: offset: {}, len: {} > {:?}",
req.offset,
len,
size
);

tx.send(Ok(ReadAtResponse::Entry {
size,
is_complete: entry.is_complete(),
}))
.await?;
let mut reader = entry.data_reader().await?;

let len = req.len.unwrap_or((size.value() - req.offset) as usize);

let (num_chunks, chunk_size) = if len <= max_chunk_size {
(1, len)
} else {
Expand Down
Loading