Skip to content

Commit

Permalink
Merge pull request maidsafe#2765 from grumbach/chunk_size_checks
Browse files Browse the repository at this point in the history
feat: protect nodes and clients against oversized chunks and update chunk size to 4MB
  • Loading branch information
grumbach authored Feb 20, 2025
2 parents 94f52c7 + 7c1f7ea commit c35ee9a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
11 changes: 11 additions & 0 deletions ant-node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use ant_evm::payment_vault::verify_data_payment;
use ant_evm::ProofOfPayment;
use ant_networking::NetworkError;
use ant_protocol::storage::GraphEntry;
use ant_protocol::Error as ProtocolError;
use ant_protocol::{
storage::{
try_deserialize_record, try_serialize_record, Chunk, DataTypes, GraphEntryAddress, Pointer,
Expand Down Expand Up @@ -428,6 +429,16 @@ impl Node {
let key = NetworkAddress::from_chunk_address(*chunk.address()).to_record_key();
let pretty_key = PrettyPrintRecordKey::from(&key).into_owned();

// reject if chunk is too large
if chunk.size() > Chunk::MAX_SIZE {
warn!(
"Chunk at {pretty_key:?} is too large: {} bytes, when max size is {} bytes",
chunk.size(),
Chunk::MAX_SIZE
);
return Err(ProtocolError::OversizedChunk(chunk.size(), Chunk::MAX_SIZE).into());
}

let record = Record {
key,
value: try_serialize_record(&chunk, RecordKind::DataOnly(DataTypes::Chunk))?.to_vec(),
Expand Down
4 changes: 4 additions & 0 deletions ant-protocol/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ pub enum Error {
#[error("Chunk does not exist {0:?}")]
ChunkDoesNotExist(NetworkAddress),

// ---------- Chunk errors
#[error("Chunk is too large: {0} bytes, when max size is {1} bytes")]
OversizedChunk(usize, usize),

// ---------- Scratchpad errors
/// The provided String can't be deserialized as a ScratchpadAddress
#[error("Failed to deserialize hex ScratchpadAddress")]
Expand Down
6 changes: 3 additions & 3 deletions ant-protocol/src/storage/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub struct Chunk {
}

impl Chunk {
/// The default maximum size of a chunk is 1MB
pub const DEFAULT_MAX_SIZE: usize = 1024 * 1024;
/// The maximum size of a chunk is 4MB
pub const MAX_SIZE: usize = 4 * 1024 * 1024;

/// Creates a new instance of `Chunk`.
pub fn new(value: Bytes) -> Self {
Expand Down Expand Up @@ -63,7 +63,7 @@ impl Chunk {

/// Returns true if the chunk is too big
pub fn is_too_big(&self) -> bool {
self.size() > Self::DEFAULT_MAX_SIZE
self.size() > Self::MAX_SIZE
}
}

Expand Down
13 changes: 9 additions & 4 deletions autonomi/src/client/data_types/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ impl Client {
) -> Result<(AttoTokens, ChunkAddress), PutError> {
let address = chunk.network_address();

if chunk.size() > Chunk::MAX_SIZE {
return Err(PutError::Serialization(format!(
"Chunk is too large: {} bytes, when max size is {}",
chunk.size(),
Chunk::MAX_SIZE
)));
}

// pay for the chunk storage
let xor_name = *chunk.name();
debug!("Paying for chunk at address: {address:?}");
Expand Down Expand Up @@ -207,10 +215,7 @@ impl Client {

let xor = *addr.xorname();
let store_quote = self
.get_store_quotes(
DataTypes::Chunk,
std::iter::once((xor, Chunk::DEFAULT_MAX_SIZE)),
)
.get_store_quotes(DataTypes::Chunk, std::iter::once((xor, Chunk::MAX_SIZE)))
.await?;
let total_cost = AttoTokens::from_atto(
store_quote
Expand Down

0 comments on commit c35ee9a

Please sign in to comment.