diff --git a/Cargo.lock b/Cargo.lock index f9e3b4b..8280456 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -440,9 +440,9 @@ checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" [[package]] name = "calamine" -version = "0.36.1" +version = "0.38.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fa68281b1a76b54a62156474adb06bb380a67e07dd60656e3217152b42183f3" +checksum = "f431b67cba6122a1f7d26b7adafc71bbdf07026095fe811f5efc025d8748e1d8" dependencies = [ "atoi_simd", "byteorder", @@ -450,7 +450,7 @@ dependencies = [ "encoding_rs", "fast-float2", "log", - "quick-xml", + "quick-xml 0.42.0", "serde", "zip", ] @@ -590,6 +590,15 @@ version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" +[[package]] +name = "constant_time_eq" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9eb6feb175df65d0a3ccd3e73484e940d0ac664e8034f0519fd49a93db3071d9" +dependencies = [ + "rustversion", +] + [[package]] name = "convert_case" version = "0.10.0" @@ -2011,7 +2020,7 @@ dependencies = [ "libc", "pdf-extract", "pulldown-cmark", - "quick-xml", + "quick-xml 0.41.0", "rcgen", "regex", "reqwest", @@ -2534,6 +2543,18 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags 2.13.1", + "cfg-if", + "cfg_aliases", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -3251,6 +3272,15 @@ name = "quick-xml" version = "0.41.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" +dependencies = [ + "memchr", +] + +[[package]] +name = "quick-xml" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b1177fdf999d2321d3fb46ff47159d9c1fb9ad66a4879f8c50a0b504615e9b" dependencies = [ "encoding_rs", "memchr", @@ -3498,9 +3528,9 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.45" +version = "0.23.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" dependencies = [ "once_cell", "ring", @@ -3550,7 +3580,7 @@ dependencies = [ "libc", "log", "memchr", - "nix", + "nix 0.29.0", "radix_trie", "rustyline-derive", "unicode-segmentation", @@ -4459,9 +4489,9 @@ dependencies = [ [[package]] name = "typed-path" -version = "0.12.3" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e28f89b80c87b8fb0cf04ab448d5dd0dd0ade2f8891bae878de66a75a28600e" +checksum = "9ce898823d8cd0f028662d2f349694b1af0a5cc9be83f1d213a00d3147445e1e" [[package]] name = "typenum" @@ -4796,7 +4826,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "338e30461b3a2b67d70eb30a6d89f8e0c93a833e07d2ae89085cd070c4a00ac0" dependencies = [ "proc-macro2", - "quick-xml", + "quick-xml 0.41.0", "quote", ] @@ -5269,7 +5299,7 @@ checksum = "a6c2ad15e0e922856ee89afe862b8992334bbe7953adad56cd1199358cb30566" dependencies = [ "bitflags 2.13.1", "libc", - "quick-xml", + "quick-xml 0.41.0", ] [[package]] @@ -5443,6 +5473,20 @@ name = "zeroize" version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e" +dependencies = [ + "zeroize_derive", +] + +[[package]] +name = "zeroize_derive" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c50655cbb0fe3fc43170059e702f1ce5e19b84cec58dc87b037a09935c2f328" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] [[package]] name = "zerotrie" @@ -5479,15 +5523,21 @@ dependencies = [ [[package]] name = "zip" -version = "8.6.0" +version = "9.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d04a6b5381502aa6087c94c669499eb1602eb9c5e8198e534de571f7154809b" +checksum = "e3476fd497ac8af5b1ec7b51acab7bef380f7e8dd8e0487595bafd839417a302" dependencies = [ + "constant_time_eq", "crc32fast", "flate2", + "getrandom 0.4.3", "indexmap 2.14.0", + "libc", "memchr", + "nix 0.31.3", "typed-path", + "windows-sys 0.61.2", + "zeroize", "zopfli", ] diff --git a/Cargo.toml b/Cargo.toml index 1856965..9003b07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -129,10 +129,18 @@ tree-sitter-javascript = { version = "=0.25.0", optional = true } # Documents: Read returns markdown for these. # PDF text. It can panic on a malformed file, so every call is caught. pdf-extract = "=0.12.1" -# Spreadsheets: xlsx, xls, ods. -calamine = { version = "=0.36.1", default-features = false } -# The archive under docx, pptx, odt (and the bomb check before calamine). -zip = { version = "=8.6.0", default-features = false, features = ["deflate"] } +# Spreadsheets: xlsx, xls, ods. 0.38.0 (MIT, MSRV 1.88): the first calamine +# on zip 9, so the spreadsheet reader leaves the zip 8 the advisories below +# name; 0.36.1 required zip ^8.6. +calamine = { version = "=0.38.0", default-features = false } +# The archive under docx, pptx, odt (and the bomb check before calamine), and +# the tool archives ext_tools unpacks. 9.5.1 (MIT, MSRV 1.88, the latest +# stable): zip 8.6.0 had GitHub advisories for a parser differential between +# its two headers (critical), extract() writing outside the target through a +# symlink (high), unbounded symlink targets and Shrink/Reduce/Implode sizes +# (high, denial of service) and an AES HMAC check skipped at early EOF; all +# fixed by 9.0.5. +zip = { version = "=9.5.1", default-features = false, features = ["deflate"] } # The XML inside those archives. 0.41 is already in the lock; not bumped. quick-xml = { version = "=0.41.0", default-features = false } # Readability for an HTML article, as markdown. diff --git a/src/ext_tools/tests.rs b/src/ext_tools/tests.rs index 08e2ed8..86718da 100644 --- a/src/ext_tools/tests.rs +++ b/src/ext_tools/tests.rs @@ -191,6 +191,46 @@ fn a_zip_that_reaches_outside_is_refused_and_writes_nothing_outside() { } } +/// **A ZIP WHOSE TWO HEADERS DISAGREE ON A NAME WRITES NOTHING OUTSIDE**: +/// the central directory says `fine.txt`, the entry's own local header says +/// `../e.txt` (same length). That disagreement is the zip 8.6.0 critical +/// advisory ("parser differential via dual-header inconsistencies"): checks +/// read one name, extraction another. Whatever the reader does with it, +/// `e.txt` must not appear beside the target folder. Control: the same +/// archive, consistent, unpacks. +#[test] +fn a_zip_whose_headers_disagree_on_a_name_writes_nothing_outside() { + let dir = tempfile::tempdir().expect("dir"); + let plain = zip_of(&[("fine.txt", b"x")]); + let mut cloaked = plain.clone(); + // The first occurrence is the local header's (it precedes the data and + // the central directory). + let local = cloaked + .windows(8) + .position(|w| w == b"fine.txt") + .expect("the local name"); + cloaked[local..local + 8].copy_from_slice(b"../e.txt"); + let central = cloaked + .windows(8) + .position(|w| w == b"fine.txt") + .expect("the central name is unchanged"); + assert!(central > local, "only the local header was changed"); + + let into = dir.path().join("cloaked"); + let archive = dir.path().join("a.zip"); + std::fs::write(&archive, &cloaked).expect("seed"); + let result = unpack::unpack(&archive, "a.zip", &into); + assert!( + !dir.path().join("e.txt").exists(), + "the local header's name escaped the folder ({result:?})" + ); + + let control = dir.path().join("plain"); + std::fs::write(&archive, &plain).expect("seed"); + unpack::unpack(&archive, "a.zip", &control).expect("the plain archive unpacks"); + assert_eq!(std::fs::read(control.join("fine.txt")).expect("file"), b"x"); +} + /// **A TAR.GZ THAT REACHES OUTSIDE, OR HOLDS A LINK, IS REFUSED.** #[test] fn a_tar_gz_with_dotdot_or_a_link_is_refused() { diff --git a/src/ext_tools/unpack.rs b/src/ext_tools/unpack.rs index 7bf0b9b..674661b 100644 --- a/src/ext_tools/unpack.rs +++ b/src/ext_tools/unpack.rs @@ -155,7 +155,13 @@ fn unzip(file: std::fs::File, into: &Path, budget: &mut Budget) -> Result<(), St let mut entry = archive .by_index(index) .map_err(|e| format!("entry {index} could not be read: {e}"))?; - let name = entry.name().to_string(); + // zip 9 refuses a name it cannot decode instead of guessing one; so + // does this, since a name read two ways is how an archive hides a + // path (the parser-differential advisory that moved us off zip 8). + let name = entry + .name() + .map_err(|e| format!("entry {index} has a name that cannot be read: {e}"))? + .to_string(); // Both rules: ours, and the crate's own. let to = destination(into, &name)?; if entry.enclosed_name().is_none() { diff --git a/src/tools/documents.rs b/src/tools/documents.rs index 86ebc5d..9cfab8e 100644 --- a/src/tools/documents.rs +++ b/src/tools/documents.rs @@ -358,8 +358,15 @@ impl<'a> Archive<'a> { Ok(()) } + /// The entries' names. A name zip 9 cannot decode is left out: callers + /// look for known parts (`word/document.xml`, `ppt/slides/...`), and a + /// garbled name is none of them. fn names(&self) -> Vec { - self.zip.file_names().map(str::to_string).collect() + self.zip + .file_names() + .filter_map(Result::ok) + .map(|n| n.to_string()) + .collect() } }