From 65aac9b1072fad2f15347f01556ff4bf182b35e5 Mon Sep 17 00:00:00 2001 From: TalBarYakar Date: Mon, 5 Oct 2026 15:19:24 +0300 Subject: [PATCH 1/2] change cbor recurse limit --- src/cbor.rs | 35 +++++++++++++++++++++++++++++------ src/lib.rs | 4 +++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/cbor.rs b/src/cbor.rs index 30b701c1..4943d762 100644 --- a/src/cbor.rs +++ b/src/cbor.rs @@ -177,8 +177,19 @@ 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(|_| CborDecodeError::DecodeError)?; + cbor_to_ivalue(cbor, 0, limit_depth) } /// Decodes an [`IValue`] tree from bytes produced by [`encode_compressed`]. @@ -187,8 +198,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 +220,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 +233,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 +438,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!(decode(&bytes).is_err()); + 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 b7f2335e..f805ad05 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; From 678535e4ee084615ca0a31201009421e19c78110 Mon Sep 17 00:00:00 2001 From: TalBarYakar Date: Tue, 6 Oct 2026 15:37:38 +0300 Subject: [PATCH 2/2] Recursion-limit failures now return DepthLimitExceeded --- src/cbor.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/cbor.rs b/src/cbor.rs index 4943d762..78c23684 100644 --- a/src/cbor.rs +++ b/src/cbor.rs @@ -188,7 +188,10 @@ pub fn decode_with_depth_limit(bytes: &[u8], limit_depth: bool) -> Result CborDecodeError::DepthLimitExceeded, + _ => CborDecodeError::DecodeError, + })?; cbor_to_ivalue(cbor, 0, limit_depth) } @@ -446,7 +449,7 @@ mod tests { } let bytes = encode(&value); - assert!(decode(&bytes).is_err()); + assert_eq!(decode(&bytes), Err(CborDecodeError::DepthLimitExceeded)); assert_eq!(decode_with_depth_limit(&bytes, false).unwrap(), value); }