diff --git a/crates/engine/src/services.rs b/crates/engine/src/services.rs index 2a641cf41..ebbe4c3d4 100644 --- a/crates/engine/src/services.rs +++ b/crates/engine/src/services.rs @@ -173,19 +173,22 @@ mod tests { } /// Templates and dotLottie files are ZIP archives too: only PhotoCraft reads have the - /// 512 MiB input limit. The length is set without writing the data. + /// 512 MiB input limit. A file over it is refused from its length, which is set without + /// writing the data, so nothing is read; the same read with a smaller limit, and with none, + /// shows where the limit applies. #[test] fn only_photocraft_reads_have_the_input_limit() { let f = std::env::temp_dir().join(format!("ec-services-large-{}.zip", std::process::id())); std::fs::write(&f, b"PK\x03\x04").unwrap(); std::fs::OpenOptions::new().write(true).open(&f).unwrap().set_len(super::PCRAFT_MAX_INPUT + 1).unwrap(); - let path = f.to_string_lossy(); + let path = f.to_string_lossy().to_string(); let error = FsServices.read_pcraft(&path).unwrap_err(); assert!(error.to_string().contains("512 MiB input limit"), "{error}"); - let bytes = FsServices.read_file(&path).unwrap(); - assert_eq!(bytes.len() as u64, super::PCRAFT_MAX_INPUT + 1); - assert!(bytes.starts_with(b"PK\x03\x04")); - drop(bytes); + std::fs::OpenOptions::new().write(true).open(&f).unwrap().set_len(4).unwrap(); + assert_eq!(FsServices.read_file(&path).unwrap(), b"PK\x03\x04"); + assert_eq!(FsServices.read_pcraft(&path).unwrap(), b"PK\x03\x04"); + assert!(super::read_regular_file(&path, Some(3)).is_err(), "one byte over a limit"); + assert_eq!(super::read_regular_file(&path, None).unwrap(), b"PK\x03\x04"); let _ = std::fs::remove_file(f); } diff --git a/crates/media/src/lib.rs b/crates/media/src/lib.rs index 02ade4f9b..7eff0e9cf 100644 --- a/crates/media/src/lib.rs +++ b/crates/media/src/lib.rs @@ -102,18 +102,17 @@ pub(crate) fn read_regular_file_stamped(path: &std::path::Path) -> std::io::Resu if !meta.is_file() { return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, format!("{} is not a file", path.display()))); } - // Only `.pcraft` files are limited before the read; other ZIP-based files are not. // A renamed bundle is checked after its read, before it is parsed. - let pcraft = ext_of(path) == "pcraft"; - if pcraft && meta.len() > pcraft::MAX_INPUT_BYTES { + let limit = input_limit(path); + if limit.is_some_and(|max| meta.len() > max) { return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "PhotoCraft bundle exceeds the 512 MiB input limit")); } let mut bytes = Vec::new(); // A sparse file can claim far more than memory: an error, not an abort. bytes.try_reserve_exact(usize::try_from(meta.len()).unwrap_or(usize::MAX)).map_err(|e| std::io::Error::new(std::io::ErrorKind::OutOfMemory, e))?; - if pcraft { - file.take(pcraft::MAX_INPUT_BYTES.saturating_add(1)).read_to_end(&mut bytes)?; - if bytes.len() as u64 > pcraft::MAX_INPUT_BYTES { + if let Some(max) = limit { + file.take(max.saturating_add(1)).read_to_end(&mut bytes)?; + if bytes.len() as u64 > max { return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "PhotoCraft bundle grew beyond the 512 MiB input limit")); } } else { @@ -122,6 +121,12 @@ pub(crate) fn read_regular_file_stamped(path: &std::path::Path) -> std::io::Resu Ok((bytes, effectcraft_render::reads::FileStamp::of_metadata(&meta))) } +/// The most bytes a read of `path` takes before it fails: `.pcraft` files have the PhotoCraft +/// input limit; other files, ZIP-based templates and dotLottie files included, have none. +fn input_limit(path: &std::path::Path) -> Option { + (ext_of(path) == "pcraft").then_some(pcraft::MAX_INPUT_BYTES) +} + #[cfg(test)] static OPENS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); @@ -175,16 +180,24 @@ mod read_tests { } /// Templates and dotLottie files are ZIP archives too: only `.pcraft` files have the - /// 512 MiB input limit. The length is set without writing the data. + /// 512 MiB input limit. A `.pcraft` file over it is refused from its length, which is set + /// without writing the data, so nothing is read. #[test] fn large_zip_files_other_than_photocraft_are_read_whole() { - let f = std::env::temp_dir().join(format!("ec-read-large-{}.zip", std::process::id())); + let max = crate::pcraft::MAX_INPUT_BYTES; + for name in ["a.zip", "a.lottie", "a.json", "a"] { + assert_eq!(super::input_limit(std::path::Path::new(name)), None, "{name}"); + } + for name in ["a.pcraft", "a.PCraft"] { + assert_eq!(super::input_limit(std::path::Path::new(name)), Some(max), "{name}"); + } + let f = std::env::temp_dir().join(format!("ec-read-large-{}.pcraft", std::process::id())); std::fs::write(&f, b"PK\x03\x04").unwrap(); - std::fs::OpenOptions::new().write(true).open(&f).unwrap().set_len(crate::pcraft::MAX_INPUT_BYTES + 1).unwrap(); - let bytes = super::read_regular_file(&f).unwrap(); - assert_eq!(bytes.len() as u64, crate::pcraft::MAX_INPUT_BYTES + 1); - assert!(bytes.starts_with(b"PK\x03\x04")); - drop(bytes); + std::fs::OpenOptions::new().write(true).open(&f).unwrap().set_len(max + 1).unwrap(); + let error = super::read_regular_file(&f).unwrap_err(); + assert!(error.to_string().contains("512 MiB input limit"), "{error}"); + std::fs::OpenOptions::new().write(true).open(&f).unwrap().set_len(4).unwrap(); + assert_eq!(super::read_regular_file(&f).unwrap(), b"PK\x03\x04"); let _ = std::fs::remove_file(f); } }