//! Packages, manifests, limits, password protection and flat documents. use std::io::Write; use crate::crypt::{CryptError, decrypt, encrypt, is_encrypted}; use crate::{Error, Limits, Manifest, Package, PackageWriter, flat, mime, zip_guard}; /// A ZIP of `files`, stored. fn zip_of(files: &[(&str, &[u8])]) -> Vec { let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); let stored = zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Stored); for (name, data) in files { z.start_file(*name, stored).unwrap(); z.write_all(data).unwrap(); } z.finish().unwrap().into_inner() } fn secret() -> Vec { let mut w = PackageWriter::new(mime::TEXT); w.file("content.xml", "text/xml", b"Top secret words."); w.file("styles.xml", "text/xml", b""); w.stored("Pictures/a.png", "image/png", b"\x89PNG not really"); w.finish().unwrap() } #[test] fn a_written_package_reads_back() { let bytes = secret(); // The media type first and stored, at the offset `file` looks for it (section 3.3). assert_eq!(bytes.get(30..38), Some(b"mimetype".as_slice())); assert_eq!(bytes.get(38..38 + mime::TEXT.len()), Some(mime::TEXT.as_bytes())); let mut p = Package::open(&bytes, &Limits::DEFAULT).unwrap(); assert_eq!(p.mimetype().as_deref(), Some(mime::TEXT)); assert_eq!(p.names().collect::>(), ["mimetype", "META-INF/manifest.xml", "content.xml", "styles.xml", "Pictures/a.png"]); assert!(p.contains("styles.xml") && !p.contains("nothing.xml")); assert_eq!(p.read("nothing.xml"), Ok(None)); assert_eq!(p.read("Pictures/a.png").unwrap().as_deref(), Some(b"\x89PNG not really".as_slice())); let m = p.manifest(); assert_eq!(m.version.as_deref(), Some("1.3")); assert_eq!(m.media_type("/"), Some(mime::TEXT)); assert_eq!(m.media_type("Pictures/a.png"), Some("image/png")); assert!(!m.is_encrypted() && !p.is_encrypted()); assert_eq!(Manifest::parse(m.to_xml().as_bytes()), m); } #[test] fn a_manifest_is_read_by_namespace_and_survives_damage() { // Another prefix, a default namespace with plain attributes, and a prefix nothing declares. for xml in [ r#""#, r#""#, r#""#, ] { let m = Manifest::parse(xml.as_bytes()); assert_eq!(m.media_type("/"), Some("a/b"), "{xml}"); assert_eq!(m.entry("x & y.xml").and_then(|e| e.size), Some(12), "{xml}"); } // Cut short: the entries before the cut. let cut = r#""[..], b"\xff\xfe\x00", b"", b"]>"] { assert!(Manifest::parse(junk).entries.is_empty()); } // More entries than a package may hold are dropped. let many = format!("{}", r#""#.repeat(crate::manifest::MAX_ENTRIES + 5)); assert_eq!(Manifest::parse(many.as_bytes()).entries.len(), crate::manifest::MAX_ENTRIES); } #[test] fn a_strict_manifest_refuses_what_a_lenient_one_repairs() { const NS: &str = r#"xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0""#; let good = format!( r#""# ); let m = Manifest::parse_strict(good.as_bytes()).unwrap(); assert_eq!((m.version.as_deref(), m.media_type("/")), (Some("1.3"), Some("a/b"))); // Another prefix for the same namespace is the same manifest. let other = r#""#; assert_eq!(Manifest::parse_strict(other.as_bytes()), Ok(m.clone())); for bad in [ format!(r#""#), format!(r#""#), format!(r#"]>"#), format!(r#""#), format!(r#"trailing"#), format!(r#""#), format!(r#""#), format!(r#""#), r#""#.to_string(), r#""#.to_string(), String::new(), ] { assert!(matches!(Manifest::parse_strict(bad.as_bytes()), Err(Error::Manifest(_))), "{bad}"); let _ = Manifest::parse(bad.as_bytes()); } // The lenient read takes what it can of the first. assert_eq!(Manifest::parse(format!(r#""#).as_bytes()).entries.len(), 1); // Entries: up to the cap, not past it. let many = |n: usize| format!(r#"{}"#, r#""#.repeat(n)); assert_eq!(Manifest::parse_strict(many(crate::manifest::MAX_ENTRIES).as_bytes()).map(|m| m.entries.len()), Ok(crate::manifest::MAX_ENTRIES)); assert!(Manifest::parse_strict(many(crate::manifest::MAX_ENTRIES + 1).as_bytes()).is_err()); // Through a package: missing and present. let mut p_bytes = zip_of(&[("mimetype", mime::SPREADSHEET.as_bytes())]); assert!(matches!(Package::open(&p_bytes, &Limits::DEFAULT).unwrap().manifest_strict(), Err(Error::Manifest(_)))); p_bytes = zip_of(&[("mimetype", mime::SPREADSHEET.as_bytes()), ("META-INF/manifest.xml", good.as_bytes())]); assert_eq!(Package::open(&p_bytes, &Limits::DEFAULT).unwrap().manifest_strict(), Ok(m)); // A written package passes. assert!(Package::open(&secret(), &Limits::DEFAULT).unwrap().manifest_strict().is_ok_and(|m| m.entries.len() == 4)); } #[test] fn a_package_is_held_to_its_limits() { let bytes = zip_of(&[("mimetype", mime::SPREADSHEET.as_bytes()), ("a.xml", &[b'a'; 100]), ("b.xml", &[b'b'; 100])]); // Entries. assert!(matches!(Package::open(&bytes, &Limits { max_entries: 2, ..Limits::DEFAULT }), Err(Error::TooManyEntries(_)))); let bomb = zip_guard::zip64_entry_count_sample(Limits::DEFAULT.max_entries + 1); assert!(matches!(Package::open(&bomb, &Limits::DEFAULT), Err(e) if e.is_limit())); // One file. let mut p = Package::open(&bytes, &Limits { max_entry: 99, ..Limits::DEFAULT }).unwrap(); assert_eq!(p.read("a.xml"), Err(Error::EntryTooLarge("a.xml".into(), 99))); let mut p = Package::open(&bytes, &Limits { max_entry: 100, ..Limits::DEFAULT }).unwrap(); assert_eq!(p.read("a.xml").map(|d| d.map(|d| d.len())), Ok(Some(100))); // All files together; a file read twice counts twice. let mut p = Package::open(&bytes, &Limits { max_total: 250, ..Limits::DEFAULT }).unwrap(); assert!(p.read("a.xml").is_ok() && p.read("b.xml").is_ok()); assert_eq!(p.bytes_read(), 200); assert_eq!(p.read("a.xml"), Err(Error::TotalTooLarge(250))); // Not a ZIP. assert!(matches!(Package::open(b"not a zip", &Limits::DEFAULT), Err(Error::Zip(_)))); } /// A deflated entry whose headers declare `declared` bytes but which inflates to `real.len()`. fn lying_zip(real: &[u8], declared: u32) -> Vec { let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new())); z.start_file("a.xml", zip::write::SimpleFileOptions::default().compression_method(zip::CompressionMethod::Deflated)).unwrap(); z.write_all(real).unwrap(); let mut bytes = z.finish().unwrap().into_inner(); // The uncompressed size, in the central directory header (offset 24) and the local one (22). let want = (real.len() as u32).to_le_bytes(); let central = bytes.windows(4).rposition(|w| w == b"PK\x01\x02").unwrap(); assert_eq!(&bytes[central + 24..central + 28], &want); bytes[central + 24..central + 28].copy_from_slice(&declared.to_le_bytes()); assert_eq!(&bytes[22..26], &want); bytes[22..26].copy_from_slice(&declared.to_le_bytes()); bytes } #[test] fn a_file_that_inflates_past_what_it_declares_is_not_read_past_the_limit() { let bytes = lying_zip(&vec![b'a'; 100_000], 10); let mut p = Package::open(&bytes, &Limits { max_entry: 1000, ..Limits::DEFAULT }).unwrap(); assert_eq!(p.declared_size("a.xml"), Some(10)); // Refused or unreadable, and never more than the limit (and the byte that shows it) inflated. assert!(!matches!(p.read("a.xml"), Ok(Some(_)))); assert!(p.bytes_read() <= 1001, "{}", p.bytes_read()); } #[test] fn a_mimetype_is_short_or_nothing() { let long = "x".repeat(300); let mut p_bytes = zip_of(&[("mimetype", long.as_bytes())]); assert_eq!(Package::open(&p_bytes, &Limits::DEFAULT).unwrap().mimetype(), None); p_bytes = zip_of(&[("mimetype", b" application/vnd.oasis.opendocument.spreadsheet\n")]); assert_eq!(Package::open(&p_bytes, &Limits::DEFAULT).unwrap().mimetype().as_deref(), Some(mime::SPREADSHEET)); p_bytes = zip_of(&[("content.xml", b"")]); assert_eq!(Package::open(&p_bytes, &Limits::DEFAULT).unwrap().mimetype(), None); } #[test] fn an_encrypted_package_opens_with_its_password_only() { let plain = secret(); assert!(!is_encrypted(&plain)); let enc = encrypt(&plain, "pässword").unwrap(); assert!(is_encrypted(&enc)); // Nothing readable is left: the content is encrypted, the manifest says how. let mut p = Package::open(&enc, &Limits::DEFAULT).unwrap(); assert_eq!(p.names().next(), Some("mimetype")); assert!(!enc.windows(10).any(|w| w == b"Top secret")); let manifest = String::from_utf8(p.read("META-INF/manifest.xml").unwrap().unwrap()).unwrap(); for want in [ r#""# ) }; let pkg = |m: String, content: &[u8]| { zip_of(&[("mimetype", b"application/vnd.oasis.opendocument.text"), ("META-INF/manifest.xml", m.as_bytes()), ("content.xml", content)]) }; // Blowfish: said, not guessed at. let blowfish = pkg(manifest("Blowfish CFB", r#"manifest:iteration-count="1024""#), &[0u8; 64]); assert!(is_encrypted(&blowfish)); assert!(matches!(decrypt(&blowfish, "x"), Err(CryptError::Unsupported(w)) if w.contains("Blowfish"))); // An iteration count that would hang, a vector of the wrong size, ragged data: refused. for (extra, data) in [ (r#"manifest:iteration-count="4000000000""#, vec![0u8; 32]), (r#"manifest:iteration-count="1""#, vec![0u8; 33]), (r#"manifest:iteration-count="1""#, vec![]), ] { assert!(decrypt(&pkg(manifest("http://www.w3.org/2001/04/xmlenc#aes256-cbc", extra), &data), "x").is_err(), "{extra}"); } // Junk that isn't a package. assert!(!is_encrypted(b"not a zip")); assert!(decrypt(b"not a zip", "x").is_err()); // A plain package has nothing to decrypt. assert!(matches!(decrypt(&secret(), "x"), Err(CryptError::Unsupported(_)))); } #[test] fn a_package_declaring_too_many_entries_is_refused_before_it_is_decrypted() { let bomb = zip_guard::zip64_entry_count_sample(Limits::DEFAULT.max_entries + 1); assert!(!is_encrypted(&bomb)); assert!(matches!(decrypt(&bomb, "x"), Err(CryptError::Unsupported(e)) if e.contains("declares"))); assert!(matches!(encrypt(&bomb, "x"), Err(CryptError::Unsupported(e)) if e.contains("declares"))); } #[test] fn files_past_the_most_decrypted_are_not_reached() { // The one encrypted file sits after 10,000 others: it is never derived a key for. let manifest = r#""#; let names: Vec = (0..10_000).map(|i| format!("f{i}")).collect(); let mut files: Vec<(&str, &[u8])> = vec![("META-INF/manifest.xml", manifest.as_bytes())]; files.extend(names.iter().map(|n| (n.as_str(), &b""[..]))); files.push(("z.xml", &[0u8; 32])); let last = zip_of(&files); assert_eq!(decrypt(&last, "x"), Err(CryptError::Unsupported("no encrypted file could be read".into()))); // Within reach, the same file is tried, and refused for what it is. let first = zip_of(&[("META-INF/manifest.xml", manifest.as_bytes()), ("z.xml", &[0u8; 32])]); assert_eq!(decrypt(&first, "x"), Err(CryptError::Unsupported("the key isn't 256 bits".into()))); } /// A 1×1 PNG, base64. const PNG_B64: &str = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="; fn flat_doc(mimetype: &str) -> String { format!( r#" Flat title Some words. {PNG_B64} x "# ) } #[test] fn a_flat_document_becomes_a_package_and_back() { let png = { use base64::Engine as _; base64::engine::general_purpose::STANDARD.decode(PNG_B64).unwrap() }; let doc = flat::split(flat_doc(mime::TEXT).as_bytes(), &Limits::DEFAULT).unwrap(); assert_eq!(doc.mimetype.as_deref(), Some(mime::TEXT)); // Each part has what a package's file has; the root's media type stays with the package. assert!(doc.content.contains(""#), "{}", doc.content); assert!(doc.content.contains(r#""#)); let names: Vec<&str> = doc.files.iter().map(|(n, _)| n.as_str()).collect(); assert_eq!( names, [ "Pictures/flat1.png", "Object flat2/content.xml", "Object flat3/content.xml", "Object flat3/styles.xml", "Object flat3/meta.xml", "Object flat3/settings.xml" ] ); assert_eq!(doc.files.first().map(|(_, d)| d.as_slice()), Some(png.as_slice())); assert_eq!(doc.folders, [("Object flat3/".to_string(), mime::CHART.to_string())]); let package = flat::to_package(flat_doc(mime::TEXT).as_bytes(), mime::GRAPHICS, &Limits::DEFAULT).unwrap(); let mut p = Package::open(&package, &Limits::DEFAULT).unwrap(); assert_eq!(p.mimetype().as_deref(), Some(mime::TEXT)); assert_eq!(p.read("Pictures/flat1.png").unwrap().as_deref(), Some(png.as_slice())); assert_eq!(p.manifest().media_type("Object flat3/"), Some(mime::CHART)); // And back: one document of the same media type, the picture, formula and chart inline. let again = String::from_utf8(flat::from_package(&package, &Limits::DEFAULT).unwrap()).unwrap(); assert!(again.contains(&format!("office:mimetype=\"{}\">", mime::TEXT)), "{again}"); assert!(again.contains(&format!("{PNG_B64}")), "{again}"); assert!(again.contains("x"), "{again}"); assert!( again.contains( r#"office:mimetype="application/vnd.oasis.opendocument.chart">"# ), "{again}" ); assert!(!again.contains("xlink:href"), "{again}"); // Taking that apart gives the same files. let twice = flat::split(again.as_bytes(), &Limits::DEFAULT).unwrap(); assert_eq!((twice.files.len(), &twice.folders), (doc.files.len(), &doc.folders)); } #[test] fn a_flat_document_of_another_kind_keeps_its_media_type() { let package = flat::to_package(flat_doc(mime::SPREADSHEET).as_bytes(), mime::TEXT, &Limits::DEFAULT).unwrap(); assert_eq!(Package::open(&package, &Limits::DEFAULT).unwrap().mimetype().as_deref(), Some(mime::SPREADSHEET)); // One that declares none takes the caller's. let bare = ""; let package = flat::to_package(bare.as_bytes(), mime::PRESENTATION, &Limits::DEFAULT).unwrap(); assert_eq!(Package::open(&package, &Limits::DEFAULT).unwrap().mimetype().as_deref(), Some(mime::PRESENTATION)); } #[test] fn hostile_flat_documents_do_not_panic() { assert_eq!(flat::split(b"\xff\xfe", &Limits::DEFAULT), Err(Error::Flat("not UTF-8 text"))); assert_eq!(flat::split(b"", &Limits::DEFAULT), Err(Error::Flat("no office:document"))); assert!(flat::split(b"", &Limits::DEFAULT).is_err()); for xml in [ "!!!not base64", "!!!", "AAAA", ] { let _ = flat::split(xml.as_bytes(), &Limits::DEFAULT); let _ = flat::to_package(xml.as_bytes(), mime::TEXT, &Limits::DEFAULT); } // A picture over the per-file limit is left out; the document still opens. let doc = flat::split(flat_doc(mime::TEXT).as_bytes(), &Limits { max_entry: 10, ..Limits::DEFAULT }).unwrap(); assert!(!doc.files.iter().any(|(n, _)| n.starts_with("Pictures/"))); // More inline objects than are taken out: the first 10,000 become files, the rest go. let many = format!("{}", "".repeat(10_050)); assert_eq!(flat::split(many.as_bytes(), &Limits::DEFAULT).unwrap().files.len(), 10_000); // A package with no media type has no flat form. assert_eq!(flat::from_package(&zip_of(&[("content.xml", b"")]), &Limits::DEFAULT), Err(Error::Flat("the package has no media type"))); } /// Two files of one name: `zip` keeps one, and which one the writer meant is not known. #[test] fn two_files_of_one_name_are_refused() { let bytes = zip_of(&[("mimetype", mime::SPREADSHEET.as_bytes()), ("dupA.xml", b""), ("dupB.xml", b"")]); assert!(Package::open(&bytes, &Limits::DEFAULT).is_ok()); let mut renamed = bytes.clone(); let mut i = 0; while let Some(at) = renamed[i..].windows(4).position(|w| w == b"dupB") { renamed[i + at + 3] = b'A'; i += at + 4; } assert_eq!(Package::open(&renamed, &Limits::DEFAULT).err(), Some(Error::Zip("two files of the package share a name".into()))); } /// Encrypting and decrypting read each file within the per-file limit: a file declaring more is /// refused before it is inflated (encrypting read every file whole; decrypting cut it short). #[test] fn encrypting_and_decrypting_refuse_a_file_past_the_limit() { let over = u32::try_from(Limits::DEFAULT.max_entry + 1).unwrap(); let bytes = lying_zip(b"", over); assert!(crate::crypt::encrypt(&bytes, "pw").is_err()); assert!(crate::crypt::decrypt(&bytes, "pw").is_err()); // The same file declaring its real size encrypts. assert!(crate::crypt::encrypt(&lying_zip(b"", 4), "pw").is_ok()); }