pcraft-format: validate tip inputs and zlib completion with parity tests

This commit is contained in:
Dylan M. Taylor
2026-11-15 05:58:40 -05:00
parent 3401d0af8e
commit dd866b846c
2 changed files with 72 additions and 3 deletions
+14 -3
View File
@@ -3,7 +3,6 @@
//! Extracted from PhotoCraft preset_store.rs at 19aec5eb44131de44361189d64910e7b3e19e57c.
//! Original implementation: Brandon Thomas; subsequent codec changes: David Mann and FinnNk.
//! Group schemas, file naming, content hashes and storage policy remain with the application.
use std::io::Read;
/// Largest accepted tip side.
pub const MAX_SIDE: u32 = 16384;
@@ -21,8 +20,13 @@ pub struct Tip {
}
/// `PCTIP1`, bits (8|16), width, height (u32 LE), then the zlib-deflated samples.
/// Returns an empty vector for invalid dimensions/sample counts or compression failure.
pub fn encode(width: u32, height: u32, data: &[u16]) -> Vec<u8> {
use std::io::Write;
let count = u64::from(width) * u64::from(height);
if width == 0 || height == 0 || width > MAX_SIDE || height > MAX_SIDE || count > MAX_SAMPLES || count != data.len() as u64 {
return Vec::new();
}
let eight = data.iter().all(|v| v % 257 == 0);
let mut out = Vec::with_capacity(TIP_HEADER + data.len() / 4);
out.extend_from_slice(TIP_MAGIC);
@@ -77,8 +81,15 @@ pub fn decode_with_max_side(bytes: &[u8], max_side: u32) -> Result<Tip, String>
b => return Err(format!("unsupported tip depth {b}")),
};
let want = n * bytes_per;
let mut raw = Vec::with_capacity(want as usize);
flate2::read::ZlibDecoder::new(&bytes[TIP_HEADER..]).take(want + 1).read_to_end(&mut raw).map_err(|e| format!("tip data: {e}"))?;
// One byte beyond the declared size detects overlong output, without allocating for
// an attacker-controlled expansion. Require the zlib trailer, not merely enough samples.
let mut raw = Vec::with_capacity(want as usize + 1);
let status = flate2::Decompress::new(true)
.decompress_vec(&bytes[TIP_HEADER..], &mut raw, flate2::FlushDecompress::Finish)
.map_err(|e| format!("tip data: {e}"))?;
if status != flate2::Status::StreamEnd {
return Err("tip data: truncated or oversized zlib stream".into());
}
if raw.len() as u64 != want {
return Err(format!("tip data has {} bytes, expected {want}", raw.len()));
}
+58
View File
@@ -0,0 +1,58 @@
use craft_pcraft_format::tip::{MAX_SAMPLES, MAX_SIDE, decode, decode_with_max_side, encode};
use std::io::Write;
fn fixture(bits: u8, width: u32, height: u32, samples: &[u8]) -> Vec<u8> {
let mut out = b"PCTIP1".to_vec();
out.push(bits);
out.extend(width.to_le_bytes());
out.extend(height.to_le_bytes());
let mut z = flate2::write::ZlibEncoder::new(out, flate2::Compression::new(3));
z.write_all(samples).unwrap();
z.finish().unwrap()
}
#[test]
fn legacy_eight_and_sixteen_bit_fixtures_and_roundtrips() {
for (bits, raw, samples) in [(8, vec![0, 128, 255], vec![0, 32896, 65535]), (16, vec![1, 0, 52, 18, 255, 255], vec![1, 0x1234, 65535])] {
let legacy = fixture(bits, 3, 1, &raw);
let tip = decode(&legacy).unwrap();
assert_eq!((tip.width, tip.height), (3, 1));
assert_eq!(tip.data, samples);
let encoded = encode(3, 1, &samples);
assert_eq!(&encoded[..15], &legacy[..15]);
assert_eq!(decode(&encoded).unwrap(), tip);
}
}
#[test]
fn checks_headers_limits_and_sample_lengths() {
let good = fixture(8, 1, 1, &[255]);
for n in 0..good.len() {
assert!(decode(&good[..n]).is_err(), "truncation at {n}");
}
let mut bad = good.clone();
bad[0] = b'X';
assert!(decode(&bad).is_err());
for (bits, w, h) in [(7, 1, 1), (8, 0, 1), (8, 1, 0), (8, MAX_SIDE + 1, 1), (8, MAX_SIDE, MAX_SIDE)] {
assert!(decode(&fixture(bits, w, h, &[])).is_err());
}
assert!(MAX_SAMPLES < u64::from(MAX_SIDE) * u64::from(MAX_SIDE));
for raw in [vec![], vec![0, 1], vec![0; 10000]] {
assert!(decode(&fixture(8, 1, 1, &raw)).is_err());
}
assert!(decode(&fixture(16, 1, 1, &[1])).is_err());
let mut checksum = good;
*checksum.last_mut().unwrap() ^= 1;
assert!(decode(&checksum).is_err());
assert!(encode(0, 1, &[]).is_empty());
assert!(encode(1, 1, &[]).is_empty());
assert!(encode(MAX_SIDE + 1, 1, &[0]).is_empty());
}
#[test]
fn preview_side_limit_is_checked_before_inflation() {
let good = encode(2, 1, &[0, 65535]);
assert!(decode_with_max_side(&good, 1).is_err());
assert!(decode_with_max_side(&good, 2).is_ok());
assert!(decode_with_max_side(&good, 0).is_err());
}