use std::path::{Path, PathBuf};
pub const MEMBER_CAP: u64 = 100 * 1024 * 1024;
pub const TAR_LIST_CAP: u64 = 100 * 1024 * 1024;
pub const ZIP_LIST_CAP: u64 = 8 * 1024 * 1024;
pub fn is_list_cap_refusal(rendered: &str) -> bool {
rendered.contains("too large to list") || rendered.contains("past the listing cap")
}
struct CappedRead<R> {
inner: R,
remaining: u64,
}
impl<R: std::io::Read> std::io::Read for CappedRead<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
if self.remaining == 0 {
return Err(std::io::Error::other(
"archive expands past the listing cap",
));
}
let take = buf
.len()
.min(self.remaining.min(usize::MAX as u64) as usize);
let n = self.inner.read(&mut buf[..take])?;
self.remaining -= n as u64;
Ok(n)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ArchiveKind {
Zip,
Tar,
TarGz,
TarXz,
}
impl ArchiveKind {
pub fn label(self) -> &'static str {
match self {
ArchiveKind::Zip => "ZIP",
ArchiveKind::Tar => "TAR",
ArchiveKind::TarGz => "TAR.GZ",
ArchiveKind::TarXz => "TAR.XZ",
}
}
}
fn xz_tar_bytes(path: &Path, cap: u64) -> std::io::Result<Vec<u8>> {
let past_cap = |size: String| {
std::io::Error::other(format!("archive expands past the listing cap ({size})"))
};
let bytes = std::fs::read(path)?;
let declared = crate::lsp::install::xz_declared_size(&bytes)
.ok_or_else(|| std::io::Error::other("the xz index cannot be read"))?;
if declared > cap {
return Err(past_cap(format!("{declared} bytes")));
}
let mut sink = crate::lsp::install::Capped {
buf: Vec::new(),
cap,
};
if let Err(e) = lzma_rs::xz_decompress(&mut std::io::Cursor::new(&bytes), &mut sink) {
let e = format!("{e:?}");
return Err(if e.contains("ceiling") {
past_cap(String::from("more than its index declares"))
} else {
std::io::Error::other(format!("xz: {e}"))
});
}
let tar = sink.buf;
if !tar.len().is_multiple_of(512) {
return Err(std::io::Error::other("not a tar archive"));
}
Ok(tar)
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArchiveEntry {
pub path: String,
pub size: u64,
pub dir: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArchiveView {
pub kind: ArchiveKind,
pub source_byte_size: u64,
pub entries: Vec<ArchiveEntry>,
pub selected: usize,
pub scroll: usize,
pub rows_top: u16,
pub rows_visible: u16,
}
pub fn kind_from_ext(path: &Path) -> Option<ArchiveKind> {
let name = path.file_name()?.to_str()?.to_ascii_lowercase();
if name.ends_with(".tar.gz") || name.ends_with(".tgz") {
return Some(ArchiveKind::TarGz);
}
if name.ends_with(".tar.xz") || name.ends_with(".txz") {
return Some(ArchiveKind::TarXz);
}
if name.ends_with(".tar") {
return Some(ArchiveKind::Tar);
}
const ZIP_EXTS: &[&str] = &[
".zip", ".jar", ".whl", ".apk", ".aar", ".war", ".ear", ".nupkg", ".vsix", ".epub",
];
if ZIP_EXTS.iter().any(|ext| name.ends_with(ext)) {
return Some(ArchiveKind::Zip);
}
None
}
pub fn list(path: &Path, kind: ArchiveKind) -> std::io::Result<ArchiveView> {
let meta = std::fs::metadata(path)?;
let mut entries: Vec<ArchiveEntry> = Vec::new();
match kind {
ArchiveKind::Zip => {
if meta.len() > ZIP_LIST_CAP {
return Err(std::io::Error::other(format!(
"zip too large to list ({} bytes)",
meta.len()
)));
}
let f = std::fs::File::open(path)?;
let mut z = zip::ZipArchive::new(f).map_err(std::io::Error::other)?;
for i in 0..z.len() {
let e = z.by_index_raw(i).map_err(std::io::Error::other)?;
entries.push(ArchiveEntry {
path: zip_member_name(&e),
size: e.size(),
dir: e.is_dir(),
});
}
}
ArchiveKind::Tar | ArchiveKind::TarGz | ArchiveKind::TarXz => {
if meta.len() > TAR_LIST_CAP {
return Err(std::io::Error::other(format!(
"tar archive too large to list ({} bytes)",
meta.len()
)));
}
let f = std::fs::File::open(path)?;
let read: Box<dyn std::io::Read> = match kind {
ArchiveKind::TarGz => Box::new(CappedRead {
inner: flate2::read::GzDecoder::new(f),
remaining: TAR_LIST_CAP,
}),
ArchiveKind::TarXz => {
Box::new(std::io::Cursor::new(xz_tar_bytes(path, TAR_LIST_CAP)?))
}
_ => Box::new(f),
};
let mut a = tar::Archive::new(read);
for entry in a.entries()? {
let e = entry?;
let hdr = e.header();
entries.push(ArchiveEntry {
path: e.path()?.to_string_lossy().into_owned(),
size: hdr.size().unwrap_or(0),
dir: hdr.entry_type().is_dir(),
});
}
}
}
entries.sort_by(|a, b| a.path.cmp(&b.path));
Ok(ArchiveView {
kind,
source_byte_size: meta.len(),
entries,
selected: 0,
scroll: 0,
rows_top: 0,
rows_visible: 0,
})
}
pub fn extract_member(
path: &Path,
kind: ArchiveKind,
member: &str,
dest_dir: &Path,
) -> std::io::Result<PathBuf> {
let dest = contained_join(dest_dir, member)
.ok_or_else(|| std::io::Error::other(format!("refusing unsafe member path: {member}")))?;
std::fs::create_dir_all(dest_dir)?;
prepare_no_follow(dest_dir, &dest)?;
match kind {
ArchiveKind::Zip => {
let f = std::fs::File::open(path)?;
let mut z = zip::ZipArchive::new(f).map_err(std::io::Error::other)?;
let index = (0..z.len())
.find(|&i| {
z.by_index_raw(i)
.is_ok_and(|e| zip_member_name(&e) == member)
})
.ok_or_else(|| std::io::Error::other(format!("no such member: {member}")))?;
let mut e = z.by_index(index).map_err(std::io::Error::other)?;
if e.size() > MEMBER_CAP {
return Err(std::io::Error::other(format!(
"member too large ({} bytes)",
e.size()
)));
}
write_out(&dest, &mut e)?;
}
ArchiveKind::Tar | ArchiveKind::TarGz | ArchiveKind::TarXz => {
let f = std::fs::File::open(path)?;
let read: Box<dyn std::io::Read> = match kind {
ArchiveKind::TarGz => Box::new(flate2::read::GzDecoder::new(f)),
ArchiveKind::TarXz => {
Box::new(std::io::Cursor::new(xz_tar_bytes(path, TAR_LIST_CAP)?))
}
_ => Box::new(f),
};
let mut a = tar::Archive::new(read);
for entry in a.entries()? {
let mut e = entry?;
if e.path()?.to_string_lossy() == member {
if e.header().size().unwrap_or(0) > MEMBER_CAP {
return Err(std::io::Error::other("member too large"));
}
write_out(&dest, &mut e)?;
return Ok(dest);
}
}
return Err(std::io::Error::other(format!("no such member: {member}")));
}
}
Ok(dest)
}
fn zip_member_name<R: std::io::Read>(e: &zip::read::ZipFile<'_, R>) -> String {
let raw = e.name_raw();
match std::str::from_utf8(raw) {
Ok(name) if !raw.is_ascii() => name.to_string(),
_ => e.name().to_string(),
}
}
fn write_out(dest: &Path, read: &mut dyn std::io::Read) -> std::io::Result<()> {
write_out_capped(dest, read, MEMBER_CAP)
}
fn prepare_no_follow(root: &Path, dest: &Path) -> std::io::Result<()> {
let rel = dest.strip_prefix(root).map_err(std::io::Error::other)?;
let mut cur = root.to_path_buf();
let parts: Vec<_> = rel.components().collect();
for c in &parts[..parts.len().saturating_sub(1)] {
cur.push(c);
match std::fs::symlink_metadata(&cur) {
Ok(m) if m.file_type().is_symlink() => {
return Err(std::io::Error::other(format!(
"refusing to traverse a symlink inside the extraction root: {}",
cur.display()
)));
}
Ok(m) if m.is_dir() => {}
Ok(_) => {
return Err(std::io::Error::other(format!(
"a member path component is a file: {}",
cur.display()
)));
}
Err(_) => std::fs::create_dir(&cur)?,
}
}
match std::fs::symlink_metadata(dest) {
Ok(m) if m.file_type().is_symlink() => Err(std::io::Error::other(
"refusing to overwrite a symlink at the extraction target",
)),
Ok(m) if m.is_file() => Ok(()),
Ok(_) => Err(std::io::Error::other("extraction target is a directory")),
Err(_) => Ok(()),
}
}
fn write_out_capped(dest: &Path, read: &mut dyn std::io::Read, cap: u64) -> std::io::Result<()> {
let name = dest
.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_default();
let tmp = dest.with_file_name(format!(".{name}.croft-extract-{}", std::process::id()));
let result = (|| {
let mut out = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&tmp)?;
let mut limited = std::io::Read::take(read, cap + 1);
let copied = std::io::copy(&mut limited, &mut out)?;
if copied > cap {
return Err(std::io::Error::other(
"member stream exceeded the size cap; partial output removed",
));
}
std::fs::rename(&tmp, dest)
})();
if result.is_err() {
let _ = std::fs::remove_file(&tmp);
}
result
}
pub fn contained_join(dir: &Path, member: &str) -> Option<PathBuf> {
use std::path::Component;
let rel = Path::new(member);
let mut out = dir.to_path_buf();
let mut depth = 0usize;
for c in rel.components() {
match c {
Component::Normal(seg) => {
out.push(seg);
depth += 1;
}
Component::CurDir => {}
Component::ParentDir => {
if depth == 0 {
return None;
}
out.pop();
depth -= 1;
}
Component::RootDir | Component::Prefix(_) => return None,
}
}
(depth > 0).then_some(out)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_zip_over_the_list_cap_refuses_to_list() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("many.zip");
write_zip_with_entries(&p, (ZIP_LIST_CAP / 80) as usize);
let size = std::fs::metadata(&p).unwrap().len();
assert!(size > ZIP_LIST_CAP, "fixture: {size} bytes clears the cap");
assert!(
size < 50 * 1024 * 1024,
"and stays under the editor's general size cap, so only this \
guard could have caught it"
);
let err = list(&p, ArchiveKind::Zip).unwrap_err();
assert!(
err.to_string().contains("too large to list"),
"says why: {err}"
);
}
#[test]
fn a_zip_over_the_list_cap_opens_as_hex_and_says_why() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("many.zip");
write_zip_with_entries(&p, (ZIP_LIST_CAP / 80) as usize);
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
assert!(
e.hex.is_some(),
"the file is still viewable, just not as an archive"
);
assert!(e.archive.is_none(), "and not as the archive it refused");
assert!(
e.status.contains("too large to list"),
"and the status says why it is hex: {}",
e.status
);
}
#[test]
fn a_zip_exactly_at_the_list_cap_is_not_refused_by_size() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("atcap.zip");
let f = std::fs::File::create(&p).unwrap();
f.set_len(ZIP_LIST_CAP).unwrap();
drop(f);
let err = list(&p, ArchiveKind::Zip).unwrap_err();
assert!(
!err.to_string().contains("too large to list"),
"at the cap the size gate must not fire, leaving the parse to \
reject it: {err}"
);
}
#[test]
fn an_extensionless_targz_past_the_decoded_cap_says_why_in_the_hex_viewer() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("noext");
{
let f = std::fs::File::create(&p).unwrap();
let gz = flate2::write::GzEncoder::new(f, flate2::Compression::fast());
let mut t = tar::Builder::new(gz);
let zeros = vec![0u8; (TAR_LIST_CAP / 4) as usize];
for i in 0..5 {
let mut hdr = tar::Header::new_gnu();
hdr.set_size(zeros.len() as u64);
hdr.set_cksum();
t.append_data(&mut hdr, format!("z{i}.bin"), &zeros[..])
.unwrap();
}
t.finish().unwrap();
}
assert!(
std::fs::metadata(&p).unwrap().len() < 50 * 1024 * 1024,
"fixture: under the editor's general cap, so magic routing runs"
);
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
assert!(e.hex.is_some(), "still viewable, just not as an archive");
assert!(
crate::archive::is_list_cap_refusal(&e.status),
"and the reason reaches the reader on this route too: {}",
e.status
);
}
#[test]
fn an_oversized_raw_tar_says_why_in_the_hex_viewer() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("big.tar");
{
let mut header = vec![0u8; 512];
header[..5].copy_from_slice(b"a.bin");
header[257..262].copy_from_slice(b"ustar");
std::fs::write(&p, &header).unwrap();
let f = std::fs::OpenOptions::new().write(true).open(&p).unwrap();
f.set_len(TAR_LIST_CAP + 1).unwrap();
}
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
assert!(e.hex.is_some(), "still viewable, just not as an archive");
assert!(
crate::archive::is_list_cap_refusal(&e.status),
"the raw cap's refusal reaches the reader: {}",
e.status
);
}
#[test]
fn a_stale_refusal_reason_does_not_explain_the_same_file_later() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("shrinking.zip");
write_zip_with_entries(&p, (ZIP_LIST_CAP / 80) as usize);
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
assert!(
e.hex.is_some() && crate::archive::is_list_cap_refusal(&e.status),
"staging: over the cap, hex with the reason: {}",
e.status
);
e.open(&p).unwrap();
write_zip_with_entries(&p, 10);
e.open(&p).unwrap();
assert!(e.archive.is_some(), "staging: it lists now");
e.open_hex(&p).unwrap();
assert!(
!crate::archive::is_list_cap_refusal(&e.status),
"a reason from when it was over the cap must not explain it \
now that it is not: {}",
e.status
);
}
#[test]
fn a_refusal_reason_does_not_explain_a_later_unrelated_file() {
let dir = tempfile::tempdir().unwrap();
let armed = dir.path().join("armed.zip");
{
use std::io::Write as _;
let mut f = std::fs::File::create(&armed).unwrap();
f.write_all(&b"plain text head, not binary\n".repeat(200))
.unwrap();
f.set_len(60 * 1024 * 1024).unwrap();
}
let mut e = crate::widgets::editor::Editor::new();
assert!(
e.open(&armed).is_err(),
"precondition: this open must reach no viewer, so the note it \
armed is left behind rather than consumed"
);
let plain = dir.path().join("plain.bin");
std::fs::write(&plain, [0u8, 1, 2, 3, 0, 255]).unwrap();
e.open_hex(&plain).unwrap();
assert!(
!e.status.contains("too large to list"),
"a note from another file must not explain this one: {}",
e.status
);
}
#[test]
fn an_extensionless_zip_over_the_list_cap_also_refuses() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("noext");
write_zip_with_entries(&p, (ZIP_LIST_CAP / 80) as usize);
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
assert!(e.hex.is_some(), "sniffed as a zip, refused, still viewable");
assert!(e.archive.is_none());
}
#[test]
fn an_ordinary_zip_under_the_list_cap_still_lists() {
let dir = tempfile::tempdir().unwrap();
let p = dir.path().join("ordinary.zip");
write_zip_with_entries(&p, 38_000);
let size = std::fs::metadata(&p).unwrap().len();
assert!(
size < ZIP_LIST_CAP,
"fixture: {size} bytes is under the cap"
);
let view = list(&p, ArchiveKind::Zip).unwrap();
assert_eq!(view.entries.len(), 38_000);
}
fn write_zip_with_entries(path: &std::path::Path, n: usize) {
let f = std::fs::File::create(path).unwrap();
let mut z = zip::ZipWriter::new(f);
let o = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
for i in 0..n {
z.start_file(i.to_string(), o).unwrap();
}
z.finish().unwrap();
}
use std::io::Write as _;
fn make_zip(p: &Path) {
let f = std::fs::File::create(p).unwrap();
let mut z = zip::ZipWriter::new(f);
let opts = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
z.add_directory("docs/", opts).unwrap();
z.start_file("docs/readme.txt", opts).unwrap();
z.write_all(b"hello from zip").unwrap();
z.start_file("evil.txt", opts).unwrap();
z.write_all(b"ok").unwrap();
z.finish().unwrap();
}
fn make_tar_gz(p: &Path) {
let f = std::fs::File::create(p).unwrap();
let gz = flate2::write::GzEncoder::new(f, flate2::Compression::fast());
let mut t = tar::Builder::new(gz);
let data = b"tar payload";
let mut hdr = tar::Header::new_gnu();
hdr.set_size(data.len() as u64);
hdr.set_mode(0o644);
hdr.set_cksum();
t.append_data(&mut hdr, "inner/file.bin", &data[..])
.unwrap();
t.into_inner().unwrap().finish().unwrap();
}
#[test]
fn lists_zip_and_targz_members_sorted_without_payload_reads() {
let tmp = tempfile::tempdir().unwrap();
let zp = tmp.path().join("a.zip");
make_zip(&zp);
let v = list(&zp, ArchiveKind::Zip).unwrap();
let names: Vec<&str> = v.entries.iter().map(|e| e.path.as_str()).collect();
assert_eq!(names, vec!["docs/", "docs/readme.txt", "evil.txt"]);
assert!(v.entries[0].dir);
assert_eq!(v.entries[1].size, 14);
let tp = tmp.path().join("a.tar.gz");
make_tar_gz(&tp);
let v = list(&tp, ArchiveKind::TarGz).unwrap();
assert_eq!(v.entries.len(), 1);
assert_eq!(v.entries[0].path, "inner/file.bin");
assert_eq!(v.entries[0].size, 11);
}
#[test]
fn extracts_a_member_and_refuses_traversal() {
let tmp = tempfile::tempdir().unwrap();
let zp = tmp.path().join("a.zip");
make_zip(&zp);
let dest = tmp.path().join("out");
let got = extract_member(&zp, ArchiveKind::Zip, "docs/readme.txt", &dest).unwrap();
assert_eq!(std::fs::read_to_string(&got).unwrap(), "hello from zip");
assert!(got.starts_with(&dest));
let tp = tmp.path().join("a.tar.gz");
make_tar_gz(&tp);
let got = extract_member(&tp, ArchiveKind::TarGz, "inner/file.bin", &dest).unwrap();
assert_eq!(std::fs::read(&got).unwrap(), b"tar payload");
assert!(extract_member(&zp, ArchiveKind::Zip, "../escape.txt", &dest).is_err());
assert!(extract_member(&zp, ArchiveKind::Zip, "/abs.txt", &dest).is_err());
assert!(!dest.join("..").join("escape.txt").exists());
}
fn info_zip_style(p: &Path, name: &str, body: &[u8], patch: Option<(u8, u8)>) {
let mut out = std::io::Cursor::new(Vec::new());
let mut z = zip::ZipWriter::new(&mut out);
let o = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
z.start_file(name, o).unwrap();
z.write_all(body).unwrap();
z.finish().unwrap();
let mut bytes = out.into_inner();
for i in 0..bytes.len().saturating_sub(10) {
let flags_at = match &bytes[i..i + 4] {
b"PK\x03\x04" => i + 6,
b"PK\x01\x02" => i + 8,
_ => continue,
};
bytes[flags_at + 1] &= !0x08;
}
if let Some((from, to)) = patch {
for b in bytes.iter_mut().filter(|b| **b == from) {
*b = to;
}
}
std::fs::write(p, bytes).unwrap();
}
#[test]
fn a_utf8_name_without_the_utf8_flag_lists_and_extracts_as_utf8() {
let tmp = tempfile::tempdir().unwrap();
let zp = tmp.path().join("w.zip");
let name = "src/ünï cödé/файл.txt";
info_zip_style(&zp, name, b"x\n", None);
let v = list(&zp, ArchiveKind::Zip).unwrap();
assert_eq!(v.entries[0].path, name);
let dest = tmp.path().join("out");
let got = extract_member(&zp, ArchiveKind::Zip, name, &dest).unwrap();
assert_eq!(got, dest.join(name));
assert_eq!(std::fs::read(&got).unwrap(), b"x\n");
}
#[test]
fn a_cp437_name_still_reads_as_cp437() {
let tmp = tempfile::tempdir().unwrap();
let zp = tmp.path().join("dos.zip");
info_zip_style(&zp, "caf~.txt", b"menu", Some((b'~', 0x82)));
let v = list(&zp, ArchiveKind::Zip).unwrap();
assert_eq!(v.entries[0].path, "café.txt");
let dest = tmp.path().join("out");
let got = extract_member(&zp, ArchiveKind::Zip, "café.txt", &dest).unwrap();
assert_eq!(std::fs::read(&got).unwrap(), b"menu");
}
#[test]
fn contained_join_is_strictly_lexical() {
let d = Path::new("/safe/dir");
assert_eq!(
contained_join(d, "a/b.txt").unwrap(),
Path::new("/safe/dir/a/b.txt")
);
assert_eq!(
contained_join(d, "a/../b.txt").unwrap(),
Path::new("/safe/dir/b.txt")
);
assert!(contained_join(d, "../evil").is_none());
assert!(contained_join(d, "a/../../evil").is_none());
assert!(contained_join(d, "/etc/passwd").is_none());
assert!(contained_join(d, ".").is_none(), "no file at all");
}
#[test]
fn write_out_caps_the_actual_stream_not_the_declared_size() {
let tmp = tempfile::tempdir().unwrap();
let dest = tmp.path().join("x.bin");
let mut endless = std::io::repeat(7u8);
let err = write_out_capped(&dest, &mut endless, 1024).unwrap_err();
assert!(err.to_string().contains("size cap"));
assert!(!dest.exists(), "partial output removed");
let mut small = &b"ok"[..];
write_out_capped(&dest, &mut small, 1024).unwrap();
assert_eq!(std::fs::read(&dest).unwrap(), b"ok");
}
#[test]
fn a_failed_extraction_keeps_the_file_already_there() {
struct Broken;
impl std::io::Read for Broken {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
buf[0] = b'X';
static CALLS: std::sync::atomic::AtomicUsize =
std::sync::atomic::AtomicUsize::new(0);
if CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed) == 0 {
Ok(1)
} else {
Err(std::io::Error::other("Invalid checksum"))
}
}
}
let tmp = tempfile::tempdir().unwrap();
let dest = tmp.path().join("notes.txt");
std::fs::write(&dest, "PRECIOUS").unwrap();
prepare_no_follow(tmp.path(), &dest).unwrap();
assert!(write_out_capped(&dest, &mut Broken, 1024).is_err());
assert_eq!(std::fs::read_to_string(&dest).unwrap(), "PRECIOUS");
let left: Vec<_> = std::fs::read_dir(tmp.path()).unwrap().flatten().collect();
assert_eq!(left.len(), 1, "no partial file left behind");
}
#[test]
fn oversized_tar_refuses_to_list() {
let tmp = tempfile::tempdir().unwrap();
let p = tmp.path().join("big.tar");
let f = std::fs::File::create(&p).unwrap();
f.set_len(TAR_LIST_CAP + 1).unwrap();
drop(f);
let err = list(&p, ArchiveKind::Tar).unwrap_err();
assert!(err.to_string().contains("too large to list"));
}
#[test]
fn targz_expanding_past_the_cap_is_refused_at_list() {
let tmp = tempfile::tempdir().unwrap();
let p = tmp.path().join("bomb.tar.gz");
let f = std::fs::File::create(&p).unwrap();
let gz = flate2::write::GzEncoder::new(f, flate2::Compression::fast());
let mut t = tar::Builder::new(gz);
let zeros = vec![0u8; (TAR_LIST_CAP / 4) as usize];
for i in 0..5 {
let mut hdr = tar::Header::new_gnu();
hdr.set_size(zeros.len() as u64);
hdr.set_cksum();
t.append_data(&mut hdr, format!("z{i}.bin"), &zeros[..])
.unwrap();
}
t.into_inner().unwrap().finish().unwrap();
assert!(
std::fs::metadata(&p).unwrap().len() < TAR_LIST_CAP,
"the compressed file itself is small"
);
let err = list(&p, ArchiveKind::TarGz).unwrap_err();
assert!(err.to_string().contains("listing cap"), "{err}");
}
#[cfg(unix)]
#[test]
fn extraction_refuses_symlinked_components_and_targets() {
let tmp = tempfile::tempdir().unwrap();
let zp = tmp.path().join("a.zip");
make_zip(&zp);
let dest = tmp.path().join("out");
let outside = tmp.path().join("outside");
std::fs::create_dir_all(&dest).unwrap();
std::fs::create_dir_all(&outside).unwrap();
std::os::unix::fs::symlink(&outside, dest.join("docs")).unwrap();
let err = extract_member(&zp, ArchiveKind::Zip, "docs/readme.txt", &dest).unwrap_err();
assert!(err.to_string().contains("symlink"), "{err}");
assert!(!outside.join("readme.txt").exists(), "nothing escaped");
std::fs::remove_file(dest.join("docs")).unwrap();
std::os::unix::fs::symlink(outside.join("evil.txt"), dest.join("evil.txt")).unwrap();
let err = extract_member(&zp, ArchiveKind::Zip, "evil.txt", &dest).unwrap_err();
assert!(err.to_string().contains("symlink"), "{err}");
assert!(!outside.join("evil.txt").exists());
}
fn make_tar_xz(p: &Path) {
let mut t = tar::Builder::new(Vec::new());
let mut dir = tar::Header::new_ustar();
dir.set_entry_type(tar::EntryType::Directory);
dir.set_size(0);
dir.set_mode(0o755);
dir.set_cksum();
t.append_data(&mut dir, "proj/", std::io::empty()).unwrap();
for (name, body) in [
("proj/README.md", "hello\n"),
("proj/main.rs", "fn main(){}\n"),
] {
let mut hdr = tar::Header::new_ustar();
hdr.set_size(body.len() as u64);
hdr.set_mode(0o644);
hdr.set_cksum();
t.append_data(&mut hdr, name, body.as_bytes()).unwrap();
}
let tar_bytes = t.into_inner().unwrap();
let mut xz = Vec::new();
lzma_rs::xz_compress(&mut std::io::Cursor::new(&tar_bytes), &mut xz).unwrap();
std::fs::write(p, xz).unwrap();
}
#[test]
fn a_tar_xz_lists_its_members_by_extension_and_by_content() {
let tmp = tempfile::tempdir().unwrap();
for name in ["proj-1.0.tar.xz", "proj-1.0.txz", "release-asset"] {
let p = tmp.path().join(name);
make_tar_xz(&p);
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
let view = e
.archive
.as_ref()
.unwrap_or_else(|| panic!("{name} opens as an archive, not hex"));
assert_eq!(view.kind.label(), "TAR.XZ", "{name}");
let names: Vec<&str> = view.entries.iter().map(|m| m.path.as_str()).collect();
assert_eq!(
names,
vec!["proj/", "proj/README.md", "proj/main.rs"],
"{name}"
);
}
}
#[test]
fn a_tar_xz_member_extracts() {
let tmp = tempfile::tempdir().unwrap();
let p = tmp.path().join("proj-1.0.tar.xz");
make_tar_xz(&p);
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
let kind = e.archive.as_ref().expect("listed").kind;
let out = extract_member(&p, kind, "proj/main.rs", &tmp.path().join("out")).unwrap();
assert_eq!(std::fs::read_to_string(out).unwrap(), "fn main(){}\n");
}
#[test]
fn a_v7_or_empty_tar_xz_lists() {
let tmp = tempfile::tempdir().unwrap();
let mut t = tar::Builder::new(Vec::new());
let body = "old\n";
let mut hdr = tar::Header::new_old();
hdr.set_size(body.len() as u64);
hdr.set_mode(0o644);
hdr.set_cksum();
t.append_data(&mut hdr, "v7.txt", body.as_bytes()).unwrap();
let v7 = t.into_inner().unwrap();
assert_ne!(&v7[257..262], b"ustar");
for (name, tar_bytes, want) in [
("v7.tar.xz", v7, vec!["v7.txt"]),
("empty.tar.xz", Vec::new(), vec![]),
] {
let p = tmp.path().join(name);
let mut xz = Vec::new();
lzma_rs::xz_compress(&mut std::io::Cursor::new(&tar_bytes), &mut xz).unwrap();
std::fs::write(&p, xz).unwrap();
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
let view = e
.archive
.as_ref()
.unwrap_or_else(|| panic!("{name} opens as an archive, not hex"));
assert_eq!(view.kind.label(), "TAR.XZ", "{name}");
let names: Vec<&str> = view.entries.iter().map(|m| m.path.as_str()).collect();
assert_eq!(names, want, "{name}");
}
}
#[test]
fn a_plain_xz_file_is_not_an_archive() {
let tmp = tempfile::tempdir().unwrap();
let p = tmp.path().join("notes.txt.xz");
let mut xz = Vec::new();
lzma_rs::xz_compress(&mut std::io::Cursor::new(b"just some notes\n"), &mut xz).unwrap();
std::fs::write(&p, xz).unwrap();
let mut e = crate::widgets::editor::Editor::new();
e.open(&p).unwrap();
assert!(e.archive.is_none(), "not a tar, so not an archive");
}
#[test]
fn a_tar_xz_past_the_decoded_cap_is_refused_as_a_listing_cap() {
let tmp = tempfile::tempdir().unwrap();
let p = tmp.path().join("proj-1.0.tar.xz");
make_tar_xz(&p);
let err = xz_tar_bytes(&p, 1024).unwrap_err();
assert!(is_list_cap_refusal(&err.to_string()), "{err}");
assert!(xz_tar_bytes(&p, TAR_LIST_CAP).is_ok());
}
#[test]
fn zip_based_packages_route_as_zip_by_extension() {
for name in [
"app.apk",
"lib.aar",
"site.war",
"app.ear",
"pkg.nupkg",
"ext.vsix",
"book.epub",
] {
assert_eq!(
kind_from_ext(Path::new(name)),
Some(ArchiveKind::Zip),
"{name}"
);
}
assert_eq!(kind_from_ext(Path::new("notes.txt.xz")), None);
}
#[test]
fn kind_detection_covers_compound_extensions() {
assert_eq!(
kind_from_ext(Path::new("a.tar.gz")),
Some(ArchiveKind::TarGz)
);
assert_eq!(kind_from_ext(Path::new("a.tgz")), Some(ArchiveKind::TarGz));
assert_eq!(kind_from_ext(Path::new("a.tar")), Some(ArchiveKind::Tar));
assert_eq!(kind_from_ext(Path::new("a.zip")), Some(ArchiveKind::Zip));
assert_eq!(kind_from_ext(Path::new("lib.jar")), Some(ArchiveKind::Zip));
assert_eq!(kind_from_ext(Path::new("pkg.whl")), Some(ArchiveKind::Zip));
assert_eq!(kind_from_ext(Path::new("a.txt")), None);
}
}