diff --git a/src/cbor.rs b/src/cbor.rs index 30b701c..78c2368 100644 --- a/src/cbor.rs +++ b/src/cbor.rs @@ -177,8 +177,22 @@ fn typed_le_tag(tag: u64, s: &[T]) -> Value { /// Decodes an [`IValue`] tree from CBOR bytes produced by [`encode`]. pub fn decode(bytes: &[u8]) -> Result { - let cbor: Value = ciborium::from_reader(bytes).map_err(|_| CborDecodeError::DecodeError)?; - cbor_to_ivalue(cbor, 0) + decode_with_depth_limit(bytes, true) +} + +/// Decodes an [`IValue`] tree, optionally enforcing the standard nesting limit. +pub fn decode_with_depth_limit(bytes: &[u8], limit_depth: bool) -> Result { + let recursion_limit = if limit_depth { + MAX_DEPTH as usize + } else { + usize::MAX + }; + let cbor: Value = ciborium::de::from_reader_with_recursion_limit(bytes, recursion_limit) + .map_err(|err| match err { + ciborium::de::Error::RecursionLimitExceeded => CborDecodeError::DepthLimitExceeded, + _ => CborDecodeError::DecodeError, + })?; + cbor_to_ivalue(cbor, 0, limit_depth) } /// Decodes an [`IValue`] tree from bytes produced by [`encode_compressed`]. @@ -187,8 +201,8 @@ pub fn decode_compressed(bytes: &[u8]) -> Result { decode(&raw) } -fn cbor_to_ivalue(val: Value, depth: u32) -> Result { - if depth >= MAX_DEPTH { +fn cbor_to_ivalue(val: Value, depth: u32, limit_depth: bool) -> Result { + if limit_depth && depth >= MAX_DEPTH { return Err(CborDecodeError::DepthLimitExceeded); } match val { @@ -209,7 +223,7 @@ fn cbor_to_ivalue(val: Value, depth: u32) -> Result { let hint = arr.len().min(1024); let mut out = IArray::with_capacity(hint).map_err(|_| CborDecodeError::AllocError)?; for v in arr { - let iv = cbor_to_ivalue(v, depth + 1)?; + let iv = cbor_to_ivalue(v, depth + 1, limit_depth)?; out.push(iv).map_err(|_| CborDecodeError::AllocError)?; } Ok(out.into()) @@ -222,7 +236,7 @@ fn cbor_to_ivalue(val: Value, depth: u32) -> Result { Value::Text(s) => s, _ => return Err(CborDecodeError::InvalidValue), }; - let val = cbor_to_ivalue(v, depth + 1)?; + let val = cbor_to_ivalue(v, depth + 1, limit_depth)?; obj.insert(&key, val) .map_err(|_| CborDecodeError::AllocError)?; } @@ -427,6 +441,18 @@ mod tests { assert_eq!(obj.get("b").unwrap().as_string().unwrap().as_str(), "text"); } + #[test] + fn test_optional_depth_limit() { + let mut value = IValue::NULL; + for _ in 0..300 { + value = IArray::try_from(vec![value]).unwrap().into(); + } + + let bytes = encode(&value); + assert_eq!(decode(&bytes), Err(CborDecodeError::DepthLimitExceeded)); + assert_eq!(decode_with_depth_limit(&bytes, false).unwrap(), value); + } + #[test] fn test_compressed_round_trip() { let seed = IValueDeserSeed::new(Some(crate::FPHAConfig::new_with_type( diff --git a/src/lib.rs b/src/lib.rs index b7f2335..f805ad0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,7 +55,9 @@ pub use value::{ pub mod cbor; mod de; mod ser; -pub use cbor::{decode, decode_compressed, encode, encode_compressed, CborDecodeError}; +pub use cbor::{ + decode, decode_compressed, decode_with_depth_limit, encode, encode_compressed, CborDecodeError, +}; pub use de::{from_value, FPHAConfig, IValueDeserSeed}; pub use ser::to_value;