fix(mcmat): validate manifests and read resources by index

This commit is contained in:
Dylan M. Taylor committed 2026-11-18 06:33:33 -05:00
1 parent b0f25d6418
commit abde74fa7d
2 files changed
+110 -14

No files matched your search

+76 -14
View File
@@ -3,7 +3,8 @@
//! The clean-room MCMAT v3 source archive. Applications interpret the SBS entry graph.
use craft_zip::{Archive, Compression, Writer};
use serde::Deserialize;
use serde::Deserializer;
use serde::de::{IgnoredAny, MapAccess, Visitor};
use std::collections::{BTreeMap, BTreeSet};
pub const VERSION: u32 = 3;
@@ -62,20 +63,36 @@ impl Material {
return Err(Error::Invalid("duplicate or overlapping archive entries".into()));
}
let json = archive.read(MANIFEST_ENTRY, limits.max_manifest_bytes)?.ok_or_else(|| Error::Invalid("missing material.json".into()))?;
#[derive(Deserialize)]
struct Version {
version: u32,
struct Marker;
impl<'de> Visitor<'de> for Marker {
type Value = (u32, bool);
fn expecting(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("a version object")
}
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
let mut version = None;
let mut extra = false;
while let Some(key) = map.next_key::<String>()? {
if key == "version" {
if version.is_some() {
return Err(serde::de::Error::duplicate_field("version"));
}
version = Some(map.next_value::<u32>()?);
} else {
map.next_value::<IgnoredAny>()?;
extra = true;
}
}
Ok((version.ok_or_else(|| serde::de::Error::missing_field("version"))?, extra))
}
}
let marker: Version = serde_json::from_slice(&json).map_err(|err| Error::Invalid(format!("material.json: {err}")))?;
if marker.version != VERSION {
return Err(Error::Version(marker.version));
let mut deserializer = serde_json::Deserializer::from_slice(&json);
let (version, extra) = deserializer.deserialize_map(Marker).map_err(|err| Error::Invalid(format!("material.json: {err}")))?;
deserializer.end().map_err(|err| Error::Invalid(format!("material.json: {err}")))?;
if version != VERSION {
return Err(Error::Version(version));
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Marker {
version: u32,
}
if !serde_json::from_slice::<Marker>(&json).is_ok_and(|m| m.version == VERSION) {
if extra {
return Err(Error::Invalid("material.json must contain only the version".into()));
}
let source = archive.read(SOURCE_ENTRY, limits.max_entry_bytes)?.ok_or_else(|| Error::Invalid("missing source.sbs".into()))?;
@@ -87,12 +104,15 @@ impl Material {
continue;
}
if name.strip_suffix('/').is_some_and(|dir| dir == "resources" || valid_resource(dir)) {
if archive.entry_sizes(index)? != Some((0, 0)) {
return Err(Error::Invalid("directory entries must have no payload".into()));
}
continue;
}
if !valid_resource(&name) || !seen.insert(name.to_ascii_lowercase()) {
return Err(Error::Invalid(format!("invalid or ambiguous resource path `{name}`")));
}
let data = archive.read(&name, limits.max_entry_bytes)?.ok_or_else(|| Error::Invalid(format!("missing `{name}`")))?;
let data = archive.read_index(index, limits.max_entry_bytes)?.ok_or_else(|| Error::Invalid(format!("missing `{name}`")))?;
resources.insert(name, data);
}
Ok(Self { source, resources })
@@ -151,6 +171,48 @@ mod tests {
}
}
#[test]
fn marker_requires_an_object_and_complete_input() {
for marker in [b"[3]".as_slice(), b"[2]", b"null", b"3", br#"{"version":3} {}"#, br#"{"version":3,"version":3}"#] {
let mut zip = Writer::new();
zip.add(MANIFEST_ENTRY, marker, Compression::Stored).unwrap();
zip.add(SOURCE_ENTRY, b"source", Compression::Stored).unwrap();
assert!(matches!(Material::open(&zip.finish().unwrap(), Limits::default()), Err(Error::Invalid(_))));
}
}
#[test]
fn directory_members_must_be_empty() {
for payload in [b"".as_slice(), b"hidden"] {
let mut zip = Writer::new();
zip.add(MANIFEST_ENTRY, MARKER, Compression::Stored).unwrap();
zip.add(SOURCE_ENTRY, b"source", Compression::Stored).unwrap();
zip.add("resources/", payload, Compression::Stored).unwrap();
assert_eq!(Material::open(&zip.finish().unwrap(), Limits::default()).is_ok(), payload.is_empty());
}
}
#[test]
fn legacy_encoded_resource_names_read_by_index_under_one_budget() {
let mut zip = Writer::new();
zip.add(MANIFEST_ENTRY, MARKER, Compression::Stored).unwrap();
zip.add(SOURCE_ENTRY, b"s", Compression::Stored).unwrap();
zip.add("resources/a.bin", b"data", Compression::Stored).unwrap();
let mut bytes = zip.finish().unwrap();
// Same-length CP437 name in both headers: no UTF-8 flag, 0x82 decodes to é.
let offsets: Vec<_> =
bytes.windows(b"resources/a.bin".len()).enumerate().filter_map(|(i, n)| (n == b"resources/a.bin").then_some(i + 10)).collect();
assert_eq!(offsets.len(), 2);
for offset in offsets {
bytes[offset] = 0x82;
}
let total = MARKER.len() as u64 + 1 + 4;
let limits = Limits { max_total_bytes: total, ..Default::default() };
let material = Material::open(&bytes, limits).unwrap();
assert_eq!(material.resources["resources/é.bin"], b"data");
assert!(Material::open(&bytes, Limits { max_total_bytes: total - 1, ..limits }).is_err());
}
#[test]
fn compresses_unencoded_source_assets() {
let bytes = write(b"<package/>", [("resources/shape.svg", b"<svg/>".as_slice()), ("resources/font.ttf", b"font".as_slice())]).unwrap();
+34
View File
@@ -108,6 +108,15 @@ impl<'a> Archive<'a> {
Ok(Some(file.is_dir()))
}
/// Declared decoded and compressed sizes at `index`, without name lookup.
pub fn entry_sizes(&mut self, index: usize) -> Result<Option<(u64, u64)>, Error> {
if index >= self.zip.len() {
return Ok(None);
}
let file = self.zip.by_index_raw(index).map_err(|e| Error::NotZip(e.to_string()))?;
Ok(Some((file.size(), file.compressed_size())))
}
/// Do two entries share a name? `zip` then keeps one of them, and which one a writer meant
/// is not known: a format that must not guess refuses the archive.
pub fn has_duplicate_names(&self) -> bool {
@@ -178,6 +187,16 @@ impl<'a> Archive<'a> {
/// Only the bytes of a read that succeeds are charged to the total.
pub fn read(&mut self, name: &str, max: u64) -> Result<Option<Vec<u8>>, Error> {
let Some(index) = self.zip.index_for_name(name) else { return Ok(None) };
self.read_index(index, max)
}
/// Read the entry at `index` under the same size, checksum and aggregate budget
/// rules as [`Self::read`], without repeating a decoded-name lookup.
pub fn read_index(&mut self, index: usize, max: u64) -> Result<Option<Vec<u8>>, Error> {
if index >= self.zip.len() {
return Ok(None);
}
let name = self.entry_name(index)?.unwrap_or_default();
let entry = |msg: String| Error::Entry { name: name.to_string(), msg };
let f = self.zip.by_index(index).map_err(|e| entry(e.to_string()))?;
let declared = f.size();
@@ -213,6 +232,21 @@ impl Archive<'static> {
mod ownership_tests {
use super::*;
#[test]
fn indexed_reads_charge_the_same_budget_and_enforce_entry_limits() {
let mut writer = crate::Writer::new();
writer.add("a", b"abc", crate::Compression::Stored).unwrap();
writer.add("b", b"def", crate::Compression::Deflated).unwrap();
let bytes = writer.finish().unwrap();
let mut archive = Archive::open(&bytes, Limits { max_entries: 2, max_total_bytes: 5 }).unwrap();
assert!(archive.read_index(0, 2).is_err());
assert_eq!(archive.read_index(0, 3).unwrap().unwrap(), b"abc");
assert!(archive.read_index(1, 3).is_err());
assert_eq!(archive.read_index(2, 3).unwrap(), None);
assert_eq!(archive.entry_sizes(0).unwrap(), Some((3, 3)));
assert_eq!(archive.entry_sizes(2).unwrap(), None);
}
#[test]
fn owned_storage_lives_until_last_archive_clone_is_dropped() {
let bytes = crate::Writer::new().finish().unwrap();