Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 63 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 12 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions src/ext_tools/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion src/ext_tools/unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 8 additions & 1 deletion src/tools/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
self.zip.file_names().map(str::to_string).collect()
self.zip
.file_names()
.filter_map(Result::ok)
.map(|n| n.to_string())
.collect()
}
}

Expand Down
Loading