Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge PR #921

Brandon Thomasce222cd8a604
diff --cc crates/idml/src/import.rs
index 9892559b2,c15f83551..8475e0cfb
--- a/crates/idml/src/import.rs
+++ b/crates/idml/src/import.rs
@@@ -50,6 -60,28 +50,30 @@@ pub struct Imported
pub warnings: Vec<String>,
}
+ /// The most one package part may decompress to. The size an archive claims only bounds the
+ /// reservation, never the read: a small entry can inflate to gigabytes.
+ pub(crate) const MAX_PART_BYTES: u64 = 512 * 1024 * 1024;
+
+ /// Entry `index` of `zip` as (name, decompressed bytes); `None` for a directory. An entry that
+ /// decompresses to more than `limit` bytes is an error rather than an unbounded read.
+ fn read_part(zip: &mut zip::ZipArchive<std::io::Cursor<&[u8]>>, index: usize, limit: u64) -> Result<Option<(String, Vec<u8>)>> {
+ let f = zip.by_index(index).map_err(|e| IdmlError::NotIdml(e.to_string()))?;
- if f.is_dir() {
++ // A folder entry of the archive (the zip crate's own `is_dir`; spelled out so no file-system
++ // probe appears in this crate).
++ if f.name().ends_with('/') {
+ return Ok(None);
+ }
+ let name = f.name().to_string();
+ // The size is what the archive claims: don't reserve more than a sane part up front.
+ let mut buf = Vec::with_capacity((f.size() as usize).min(1 << 24));
+ // Read one byte past the limit to tell "exactly at the limit" from "over it".
+ f.take(limit.saturating_add(1)).read_to_end(&mut buf).map_err(|e| IdmlError::Part { part: name.clone(), msg: e.to_string() })?;
+ if buf.len() as u64 > limit {
+ return Err(IdmlError::Part { part: name, msg: format!("larger than {} MiB uncompressed", limit / (1024 * 1024)) });
+ }
+ Ok(Some((name, buf)))
+ }
+
/// Import with a custom reader for linked files, reporting the problems that did not stop it.
pub fn import_idml_report(bytes: &[u8], read_link: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<Imported> {
let mut zip = zip::ZipArchive::new(std::io::Cursor::new(bytes)).map_err(|e| IdmlError::NotIdml(e.to_string()))?;