cultcache-rs: element_header owns truncation; the walk tests fill skipped bytes with array markers

Mutation survivors of the first run: the unwrap_or(usize::MAX - k) fallbacks were equivalent
mutants, so a truncated header now returns None through the one helper. Child counts and long
lengths (map16/32, array16/32, str/bin/ext 16 and 32 bit) are tested with skipped bytes that
read as containers when a skip stops short. Builds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-11 10:30:10 +02:00
commit f06cd84596
2 changed files with 97 additions and 75 deletions

View file

@ -526,53 +526,12 @@ impl std::error::Error for PayloadTooDeep {}
/// refusing the first container past [`MAX_PAYLOAD_DEPTH`]. Scalars are skipped by length and nothing
/// is allocated per element. A payload that ends early is the decoder's to refuse.
pub fn check_payload_depth(bytes: &[u8]) -> std::result::Result<(), PayloadTooDeep> {
// The big-endian length after the code byte, when the payload is long enough to hold it.
fn length(bytes: &[u8], position: usize, width: usize) -> Option<usize> {
let field = bytes.get(position + 1..position + 1 + width)?;
Some(field.iter().fold(0usize, |length, byte| (length << 8) | usize::from(*byte)))
}
let mut remaining = [0usize; MAX_PAYLOAD_DEPTH];
let mut depth = 0usize;
let mut position = 0usize;
while position < bytes.len() {
let code = bytes[position];
// (bytes to advance, children to read when the element is a container)
let (advance, children) = match code {
0x00..=0x7f | 0xe0..=0xff | 0xc0..=0xc3 => (1, None),
0x80..=0x8f => (1, Some(2 * usize::from(code & 0x0f))),
0x90..=0x9f => (1, Some(usize::from(code & 0x0f))),
0xa0..=0xbf => (1 + usize::from(code & 0x1f), None),
0xc4 | 0xd9 => (2 + length(bytes, position, 1).unwrap_or(usize::MAX - 2), None),
0xc5 | 0xda => (3 + length(bytes, position, 2).unwrap_or(usize::MAX - 3), None),
0xc6 | 0xdb => (5 + length(bytes, position, 4).unwrap_or(usize::MAX - 5), None),
0xc7 => (3 + length(bytes, position, 1).unwrap_or(usize::MAX - 3), None),
0xc8 => (4 + length(bytes, position, 2).unwrap_or(usize::MAX - 4), None),
0xc9 => (6 + length(bytes, position, 4).unwrap_or(usize::MAX - 6), None),
0xca | 0xce | 0xd2 => (5, None),
0xcb | 0xcf | 0xd3 => (9, None),
0xcc | 0xd0 => (2, None),
0xcd | 0xd1 | 0xd4 => (3, None),
0xd5 => (4, None),
0xd6 => (6, None),
0xd7 => (10, None),
0xd8 => (18, None),
0xdc => match length(bytes, position, 2) {
Some(count) => (3, Some(count)),
None => return Ok(()),
},
0xdd => match length(bytes, position, 4) {
Some(count) => (5, Some(count)),
None => return Ok(()),
},
0xde => match length(bytes, position, 2) {
Some(count) => (3, Some(2 * count)),
None => return Ok(()),
},
0xdf => match length(bytes, position, 4) {
Some(count) => (5, Some(count.saturating_mul(2))),
None => return Ok(()),
},
let Some((advance, children)) = element_header(bytes, position) else {
return Ok(());
};
position = position.saturating_add(advance);
if let Some(children) = children {
@ -600,6 +559,42 @@ pub fn check_payload_depth(bytes: &[u8]) -> std::result::Result<(), PayloadTooDe
Ok(())
}
/// The element at `position`: how many bytes to advance past it (its whole length for a scalar, its
/// header for a container) and, for a container, how many child elements follow. `None` when the
/// payload ends inside the header.
fn element_header(bytes: &[u8], position: usize) -> Option<(usize, Option<usize>)> {
// The big-endian length after the code byte.
let length = |width: usize| -> Option<usize> {
let field = bytes.get(position + 1..position + 1 + width)?;
Some(field.iter().fold(0usize, |length, byte| (length << 8) | usize::from(*byte)))
};
let code = bytes[position];
Some(match code {
0x00..=0x7f | 0xe0..=0xff | 0xc0..=0xc3 => (1, None),
0x80..=0x8f => (1, Some(2 * usize::from(code & 0x0f))),
0x90..=0x9f => (1, Some(usize::from(code & 0x0f))),
0xa0..=0xbf => (1 + usize::from(code & 0x1f), None),
0xc4 | 0xd9 => (2usize.saturating_add(length(1)?), None),
0xc5 | 0xda => (3usize.saturating_add(length(2)?), None),
0xc6 | 0xdb => (5usize.saturating_add(length(4)?), None),
0xc7 => (3usize.saturating_add(length(1)?), None),
0xc8 => (4usize.saturating_add(length(2)?), None),
0xc9 => (6usize.saturating_add(length(4)?), None),
0xca | 0xce | 0xd2 => (5, None),
0xcb | 0xcf | 0xd3 => (9, None),
0xcc | 0xd0 => (2, None),
0xcd | 0xd1 | 0xd4 => (3, None),
0xd5 => (4, None),
0xd6 => (6, None),
0xd7 => (10, None),
0xd8 => (18, None),
0xdc => (3, Some(length(2)?)),
0xdd => (5, Some(length(4)?)),
0xde => (3, Some(length(2)?.saturating_mul(2))),
0xdf => (5, Some(length(4)?.saturating_mul(2))),
})
}
/// The one decode of a document payload: the nesting bound first, then the deserializer, whose own
/// depth counter (1024) is only a backstop. A failure keeps its kind and never quotes the bytes.
fn decode_payload<T: serde::de::DeserializeOwned>(payload: &[u8]) -> Result<T> {

View file

@ -743,47 +743,74 @@ fn every_container_header_counts_one_level() {
assert_eq!(check_payload_depth(&nested(&[0x91], 256, &[0x80])), Err(TOO_DEEP));
}
// A scalar skipped by the wrong length would misplace the chain after it, so the chain's depth
// reads the same only when every scalar width is skipped exactly.
// Every scalar and container header is followed by the chain that tests the bound. The skipped bytes
// are filled with the array marker, so a skip that stops short reads them as containers and a skip
// that runs long eats the chain: either way the chain's depth stops reading as 256 and 257.
#[test]
fn every_scalar_width_is_skipped_by_its_length() {
let scalars: Vec<Vec<u8>> = vec![
fn every_element_is_skipped_by_its_length_and_counted_by_its_children() {
const FILL: u8 = 0x91;
let filled = |head: &[u8], bytes: usize| -> Vec<u8> { [head.to_vec(), vec![FILL; bytes]].concat() };
let long = |code: u8, width: usize, length: usize, extra: usize| -> Vec<u8> {
let size = length.to_be_bytes();
filled(&[&[code][..], &size[size.len() - width..]].concat(), length + extra)
};
let mut elements: Vec<Vec<u8>> = vec![
vec![0x00],
vec![0xff],
vec![0xc0],
vec![0xc2],
vec![0xc3],
vec![0xcc, 1],
vec![0xcd, 0, 1],
vec![0xce, 0, 0, 0, 1],
vec![0xcf, 0, 0, 0, 0, 0, 0, 0, 1],
vec![0xd0, 1],
vec![0xd1, 0, 1],
vec![0xd2, 0, 0, 0, 1],
vec![0xd3, 0, 0, 0, 0, 0, 0, 0, 1],
vec![0xca, 0, 0, 0, 0],
vec![0xcb, 0, 0, 0, 0, 0, 0, 0, 0],
vec![0xa2, b'a', b'b'],
vec![0xd9, 3, b'a', b'b', b'c'],
vec![0xda, 0, 3, b'a', b'b', b'c'],
vec![0xdb, 0, 0, 0, 3, b'a', b'b', b'c'],
vec![0xc4, 3, b'a', b'b', b'c'],
vec![0xc5, 0, 3, b'a', b'b', b'c'],
vec![0xc6, 0, 0, 0, 3, b'a', b'b', b'c'],
vec![0xd4, 1, 0],
vec![0xd5, 1, 0, 0],
vec![0xd6, 1, 0, 0, 0, 0],
vec![0xd7, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[vec![0xd8, 1], vec![0; 16]].concat(),
vec![0xc7, 3, 1, b'a', b'b', b'c'],
vec![0xc8, 0, 3, 1, b'a', b'b', b'c'],
vec![0xc9, 0, 0, 0, 3, 1, b'a', b'b', b'c'],
filled(&[0xcc], 1),
filled(&[0xcd], 2),
filled(&[0xce], 4),
filled(&[0xcf], 8),
filled(&[0xd0], 1),
filled(&[0xd1], 2),
filled(&[0xd2], 4),
filled(&[0xd3], 8),
filled(&[0xca], 4),
filled(&[0xcb], 8),
filled(&[0xa3], 3),
filled(&[0xbf], 31),
filled(&[0xd4], 2),
filled(&[0xd5], 3),
filled(&[0xd6], 5),
filled(&[0xd7], 9),
filled(&[0xd8], 17),
];
for (code, width) in [(0xd9, 1), (0xc4, 1), (0xda, 2), (0xc5, 2), (0xdb, 4), (0xc6, 4)] {
for length in [3, 200, 256, 65536] {
if length < (1usize << (8 * width)) {
elements.push(long(code, width, length, 0));
}
}
}
// An ext carries a type byte after its length.
for (code, width) in [(0xc7, 1), (0xc8, 2), (0xc9, 4)] {
for length in [3, 200, 256, 65536] {
if length < (1usize << (8 * width)) {
elements.push(long(code, width, length, 1));
}
}
}
// Containers whose children are scalars, so a wrong child count shifts everything after them.
elements.push(vec![0x92, 0x00, 0x00]);
elements.push(vec![0x93, 0x00, 0x00, 0x00]);
elements.push(vec![0x82, 0x00, 0x00, 0x00, 0x00]);
elements.push(vec![0x83, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
elements.push([vec![0xdc, 0x01, 0x00], vec![0x00; 256]].concat());
elements.push([vec![0xdd, 0x00, 0x00, 0x00, 0x03], vec![0x00; 3]].concat());
elements.push([vec![0xde, 0x00, 0x03], vec![0x00; 6]].concat());
elements.push([vec![0xde, 0x01, 0x00], vec![0x00; 512]].concat());
elements.push([vec![0xdf, 0x00, 0x00, 0x00, 0x03], vec![0x00; 6]].concat());
elements.push(vec![0xdc, 0x00, 0x00]);
elements.push(vec![0xdf, 0x00, 0x00, 0x00, 0x00]);
let with_chain = |levels: usize| {
let mut bytes = vec![0xdd];
bytes.extend_from_slice(&u32::try_from(scalars.len() + 1).unwrap().to_be_bytes());
for scalar in &scalars {
bytes.extend_from_slice(scalar);
bytes.extend_from_slice(&u32::try_from(elements.len() + 1).unwrap().to_be_bytes());
for element in &elements {
bytes.extend_from_slice(element);
}
bytes.extend(nested(&[0x91], levels, &[0xc0]));
bytes