Skip to content

Commit dba92cd

Browse files
committed
fix(zstd): widen the pre-allocation cap so a full-size CHUNK frame fits
A max-size CHUNK frame's raw payload is the 1 MiB chunk plus 72 bytes of frame fields (map_id, file_content_id, chunk_index), which the 0.3.0 cap of 1 MiB + 64 rejected — breaking every exactly-1 MiB chunk read over a zstd-negotiated stream. The cap moves to 1 MiB + 256, still small enough to refuse forged raw_len headers before allocation.
1 parent d223b3c commit dba92cd

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mst2-codec"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
edition = "2021"
55
description = "MST/2 wire codec: ServingDescriptor (MSD2), MTP2/1 metadata pages, TreeFrame v2, Chunk Map (MCM2/MCL2). Pure byte-level parsing and encoding per Mega_ScorpioFS_MST2 specs 03/05/06/07."
66
license = "Apache-2.0"

‎src/zstd1.rs‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@ pub fn compress(payload: &[u8], level: i32) -> CodecResult<Vec<u8>> {
2626
.map_err(|_| CodecError::Unsupported("zstd compression failed"))
2727
}
2828

29-
/// Maximum raw payload the spec 06 frame kinds allow: 1 MiB data + small
30-
/// header overhead (spec 14). Any `raw_len` above this is rejected before
31-
/// allocation to prevent DoS from a forged header claiming a huge size.
32-
pub const MAX_RAW_PAYLOAD: usize = 1_048_576 + 64;
29+
/// Maximum raw payload the spec 06 frame kinds allow: 1 MiB data + the
30+
/// frame's own structural fields (a full CHUNK frame carries the 1 MiB chunk
31+
/// plus map_id/file_content_id/chunk_index — 72 bytes — and a little slack
32+
/// for future header fields; spec 14). Any `raw_len` above this is rejected
33+
/// before allocation to prevent DoS from a forged header claiming a huge
34+
/// size.
35+
pub const MAX_RAW_PAYLOAD: usize = 1_048_576 + 256;
3336

3437
/// Strict single-frame decompression (see module docs). `raw_len` is the
3538
/// header-advertised uncompressed length and bounds the output exactly.
@@ -150,4 +153,19 @@ mod tests {
150153
let skip = vec![0x50, 0x2A, 0x4D, 0x18, 0, 0, 0, 0];
151154
assert!(decompress_strict(&skip, 0).is_err());
152155
}
156+
157+
#[test]
158+
fn full_mib_chunk_payload_fits_under_the_cap() {
159+
// A full 1 MiB CHUNK frame's raw payload is the chunk plus its 72
160+
// bytes of frame fields (map_id + file_content_id + chunk_index):
161+
// the cap must accept it or every max-size chunk read over zstd
162+
// fails (regression for the 0.3.0 +64 estimate).
163+
let payload_len = 1_048_576 + 72;
164+
let payload = vec![0xA5u8; payload_len];
165+
let wire = compress(&payload, 1).unwrap();
166+
assert_eq!(decompress_strict(&wire, payload_len).unwrap(), payload);
167+
// Just above the cap is still refused.
168+
let mut over = vec![0xA5u8; MAX_RAW_PAYLOAD + 1];
169+
assert!(decompress_strict(&compress(&over, 1).unwrap(), over.len()).is_err());
170+
}
153171
}

0 commit comments

Comments
 (0)