Skip to content

Commit

Permalink
Fix calculations avoiding to exceed the 2^18 bytes block limit (#137)
Browse files Browse the repository at this point in the history
* Error out when exceeding maximum block size in MemoryBlockStore

* fix: calculation to not exceed block limit
  • Loading branch information
matheus23 authored Jan 11, 2023
1 parent f02658b commit 6969c08
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
8 changes: 8 additions & 0 deletions wnfs/proptest-regressions/private/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
cc 3d8ef3fd02b50b6a8e666913259bc43b153f42b7ce3f17a1654799b8777284b3 # shrinks to input = _CanIncludeAndStreamContentFromFileArgs { length: 262117 }
cc ec425ece25d3c83eb22e70aef1e39e6585b3f1c6ffdeb8eff41d7ad96d80e886 # shrinks to input = _CanIncludeAndGetContentFromFileArgs { length: 262117 }
8 changes: 6 additions & 2 deletions wnfs/src/common/blockstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::{borrow::Cow, io::Cursor};

use anyhow::Result;
use anyhow::{bail, Result};
use async_trait::async_trait;
use libipld::{
cbor::DagCborCodec,
Expand All @@ -17,7 +17,7 @@ use std::collections::HashMap;

use crate::{
private::{Key, NONCE_SIZE},
utils, AsyncSerialize,
utils, AsyncSerialize, MAX_BLOCK_SIZE,
};

use super::FsError;
Expand Down Expand Up @@ -102,6 +102,10 @@ impl MemoryBlockStore {
impl BlockStore for MemoryBlockStore {
/// Stores an array of bytes in the block store.
async fn put_block(&mut self, bytes: Vec<u8>, codec: IpldCodec) -> Result<Cid> {
if bytes.len() > MAX_BLOCK_SIZE {
bail!(FsError::MaximumBlockSizeExceeded(bytes.len()))
}

let hash = Code::Sha2_256.digest(&bytes);
let cid = Cid::new(Version::V1, codec.into(), hash)?;

Expand Down
3 changes: 3 additions & 0 deletions wnfs/src/common/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub enum FsError {

#[error("Key does not exist in HAMT")]
KeyNotFoundInHamt,

#[error("Maximum block size exceeded: Encountered block with {0} bytes")]
MaximumBlockSizeExceeded(usize),
}

pub fn error<T>(err: impl std::error::Error + Send + Sync + 'static) -> Result<T> {
Expand Down
7 changes: 4 additions & 3 deletions wnfs/src/private/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
encrypted::Encrypted, namefilter::Namefilter, Key, PrivateForest, PrivateNodeHeader,
RevisionKey, NONCE_SIZE,
RevisionKey, AUTHENTICATION_TAG_SIZE, NONCE_SIZE,
};
use crate::{
dagcbor, utils, utils::get_random_bytes, BlockStore, FsError, Hasher, Id, Metadata, NodeType,
Expand All @@ -23,12 +23,13 @@ use std::{collections::BTreeSet, iter, rc::Rc};
//--------------------------------------------------------------------------------------------------

/// The maximum block size is 2 ^ 18 but the first 12 bytes are reserved for the cipher text's initialization vector.
/// This leaves a maximum of (2 ^ 18) - 12 = 262,132 bytes for the actual data.
/// The ciphertext then also contains a 16 byte authentication tag.
/// This leaves a maximum of (2 ^ 18) - 12 - 16 = 262,116 bytes for the actual data.
///
/// More on that [here][priv-file].
///
/// [priv-file]: https://github.com/wnfs-wg/spec/blob/matheus23/file-sharding/spec/private-wnfs.md#314-private-file
pub const MAX_BLOCK_CONTENT_SIZE: usize = MAX_BLOCK_SIZE - NONCE_SIZE;
pub const MAX_BLOCK_CONTENT_SIZE: usize = MAX_BLOCK_SIZE - NONCE_SIZE - AUTHENTICATION_TAG_SIZE;

//--------------------------------------------------------------------------------------------------
// Type Definitions
Expand Down
1 change: 1 addition & 0 deletions wnfs/src/private/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{utils, FsError};
//--------------------------------------------------------------------------------------------------

pub(crate) const NONCE_SIZE: usize = 12;
pub(crate) const AUTHENTICATION_TAG_SIZE: usize = 16;
pub const KEY_BYTE_SIZE: usize = 32;

//--------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 6969c08

Please sign in to comment.