use std::fs::{self, File};
use std::io::Read;
use std::path::{Path, PathBuf};
use eyre::{Result, WrapErr, eyre};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ArchiveKind {
Zip,
Tar,
}
impl ArchiveKind {
pub fn from_dist_type(kind: &str) -> Option<Self> {
match kind {
"zip" => Some(Self::Zip),
"tar" => Some(Self::Tar),
_ => None,
}
}
pub fn extension(self) -> &'static str {
match self {
Self::Zip => "zip",
Self::Tar => "tar",
}
}
}
pub fn extract_dist(
kind: ArchiveKind,
archive: &Path,
into: &Path,
strip_prefix: Option<&str>,
) -> Result<()> {
let detected: String;
let strip = if let Some(s) = strip_prefix {
s
} else {
detected = match kind {
ArchiveKind::Zip => detect_zip_top_level(archive),
ArchiveKind::Tar => detect_tar_top_level(archive),
}
.wrap_err_with(|| format!("detecting top-level dir in {}", archive.display()))?;
detected.as_str()
};
match kind {
ArchiveKind::Zip => extract_zip(archive, into, strip),
ArchiveKind::Tar => extract_tar(archive, into, strip),
}
}
#[tracing::instrument(skip_all, fields(into = %into.display()))]
pub fn extract_zip(zip_path: &Path, into: &Path, strip_prefix: &str) -> Result<()> {
let f = File::open(zip_path).wrap_err_with(|| format!("opening {}", zip_path.display()))?;
let mut archive =
zip::ZipArchive::new(f).wrap_err_with(|| format!("reading zip {}", zip_path.display()))?;
for i in 0..archive.len() {
let mut entry = archive
.by_index(i)
.wrap_err_with(|| format!("reading zip entry {i}"))?;
let Some(raw) = entry.enclosed_name() else {
continue;
};
let Some(rewritten) = rewrite_archive_path(&raw, strip_prefix) else {
continue;
};
let dest = into.join(&rewritten);
if entry.is_dir() {
fs::create_dir_all(&dest).wrap_err_with(|| format!("creating {}", dest.display()))?;
continue;
}
if let Some(parent) = dest.parent() {
fs::create_dir_all(parent)
.wrap_err_with(|| format!("creating {}", parent.display()))?;
}
let mut out =
File::create(&dest).wrap_err_with(|| format!("creating {}", dest.display()))?;
std::io::copy(&mut entry, &mut out)
.wrap_err_with(|| format!("writing {}", dest.display()))?;
#[cfg(unix)]
if let Some(mode) = entry.unix_mode() {
use std::os::unix::fs::PermissionsExt;
let _ = fs::set_permissions(&dest, fs::Permissions::from_mode(mode));
}
}
Ok(())
}
pub fn detect_zip_top_level(zip_path: &Path) -> Result<String> {
let f = File::open(zip_path).wrap_err_with(|| format!("opening {}", zip_path.display()))?;
let mut archive =
zip::ZipArchive::new(f).wrap_err_with(|| format!("reading zip {}", zip_path.display()))?;
let mut top: Option<String> = None;
for i in 0..archive.len() {
let entry = archive
.by_index(i)
.wrap_err_with(|| format!("reading zip entry {i}"))?;
let Some(raw) = entry.enclosed_name() else {
continue;
};
let Some(first) = raw.components().next() else {
continue;
};
let std::path::Component::Normal(os) = first else {
continue;
};
let Some(s) = os.to_str() else {
return Ok(String::new());
};
match &top {
None => top = Some(s.to_owned()),
Some(existing) if existing == s => {}
Some(_) => return Ok(String::new()),
}
}
Ok(top.unwrap_or_default())
}
fn rewrite_archive_path(path: &Path, strip_prefix: &str) -> Option<PathBuf> {
let rewritten = if strip_prefix.is_empty() {
path.to_path_buf()
} else {
match path.strip_prefix(strip_prefix) {
Ok(rest) => rest.to_path_buf(),
Err(_) => path.to_path_buf(),
}
};
if rewritten.as_os_str().is_empty() {
None
} else {
Some(rewritten)
}
}
fn open_tar_reader(archive: &Path) -> Result<Box<dyn Read>> {
let mut f = File::open(archive).wrap_err_with(|| format!("opening {}", archive.display()))?;
let mut magic = [0u8; 4];
let mut filled = 0;
while filled < magic.len() {
let n = f
.read(&mut magic[filled..])
.wrap_err_with(|| format!("reading {}", archive.display()))?;
if n == 0 {
break;
}
filled += n;
}
std::io::Seek::seek(&mut f, std::io::SeekFrom::Start(0))
.wrap_err_with(|| format!("rewinding {}", archive.display()))?;
let magic = &magic[..filled];
if magic.starts_with(&[0x1f, 0x8b]) {
Ok(Box::new(flate2::read::GzDecoder::new(f)))
} else if magic == [0x28, 0xb5, 0x2f, 0xfd] {
let decoder = zstd::stream::read::Decoder::new(f)
.wrap_err_with(|| format!("reading zstd stream {}", archive.display()))?;
Ok(Box::new(decoder))
} else if magic.starts_with(b"BZh") {
Err(eyre!(
"{} is a bzip2 tarball, which this installer does not extract",
archive.display()
))
} else {
Ok(Box::new(f))
}
}
#[tracing::instrument(skip_all, fields(into = %into.display()))]
pub fn extract_tar(archive: &Path, into: &Path, strip_prefix: &str) -> Result<()> {
let mut tar = tar::Archive::new(open_tar_reader(archive)?);
tar.set_preserve_permissions(true);
tar.set_preserve_mtime(true);
for entry in tar
.entries()
.wrap_err_with(|| format!("reading entries from {}", archive.display()))?
{
let mut entry = entry.wrap_err("reading archive entry")?;
let path = entry.path().wrap_err("reading entry path")?.into_owned();
let kind = entry.header().entry_type();
if kind.is_pax_global_extensions() || kind.is_pax_local_extensions() {
continue;
}
let Some(rewritten) = rewrite_archive_path(&path, strip_prefix) else {
continue;
};
if !is_safe_archive_path(&rewritten) {
return Err(eyre!(
"archive entry {} escapes the extraction root",
path.display()
));
}
let dest = into.join(&rewritten);
if let Some(parent) = dest.parent() {
fs::create_dir_all(parent)
.wrap_err_with(|| format!("creating {}", parent.display()))?;
}
if kind.is_hard_link() {
let link_name = entry
.link_name()
.wrap_err("reading hardlink target")?
.ok_or_else(|| eyre!("hardlink entry {} has no target", path.display()))?
.into_owned();
let target_rel = rewrite_archive_path(&link_name, strip_prefix)
.filter(|p| is_safe_archive_path(p))
.ok_or_else(|| {
eyre!(
"hardlink target {} escapes the extraction root",
link_name.display()
)
})?;
let target = into.join(target_rel);
let _ = fs::remove_file(&dest);
fs::hard_link(&target, &dest)
.wrap_err_with(|| format!("linking {} → {}", dest.display(), target.display()))?;
continue;
}
if kind.is_symlink() {
let link_name = entry
.link_name()
.wrap_err("reading symlink target")?
.ok_or_else(|| eyre!("symlink entry {} has no target", path.display()))?
.into_owned();
if symlink_target_escapes(&rewritten, &link_name) {
return Err(eyre!(
"symlink entry {} → {} escapes the extraction root",
path.display(),
link_name.display()
));
}
}
entry
.unpack(&dest)
.wrap_err_with(|| format!("unpacking {} → {}", path.display(), dest.display()))?;
}
Ok(())
}
pub fn detect_tar_top_level(archive: &Path) -> Result<String> {
let mut tar = tar::Archive::new(open_tar_reader(archive)?);
let mut top: Option<String> = None;
for entry in tar
.entries()
.wrap_err_with(|| format!("reading entries from {}", archive.display()))?
{
let entry = entry.wrap_err("reading archive entry")?;
let kind = entry.header().entry_type();
if kind.is_pax_global_extensions() || kind.is_pax_local_extensions() {
continue;
}
let path = entry.path().wrap_err("reading entry path")?;
let mut components = path.components();
let first = loop {
match components.next() {
Some(std::path::Component::CurDir) => {}
other => break other,
}
};
let Some(first) = first else {
continue;
};
let std::path::Component::Normal(os) = first else {
return Ok(String::new());
};
let Some(s) = os.to_str() else {
return Ok(String::new());
};
match &top {
None => top = Some(s.to_owned()),
Some(existing) if existing == s => {}
Some(_) => return Ok(String::new()),
}
}
Ok(top.unwrap_or_default())
}
fn is_safe_archive_path(path: &Path) -> bool {
use std::path::Component;
path.components().all(|c| match c {
Component::Normal(_) | Component::CurDir => true,
Component::ParentDir | Component::RootDir | Component::Prefix(_) => false,
})
}
fn symlink_target_escapes(link_rel: &Path, target: &Path) -> bool {
use std::path::Component;
if target.is_absolute() {
return true;
}
let mut depth: usize = link_rel.parent().map_or(0, |p| {
p.components()
.filter(|c| matches!(c, Component::Normal(_)))
.count()
});
for c in target.components() {
match c {
Component::CurDir => {}
Component::Normal(_) => depth += 1,
Component::ParentDir => {
let Some(next) = depth.checked_sub(1) else {
return true;
};
depth = next;
}
Component::RootDir | Component::Prefix(_) => return true,
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone, Copy)]
enum Codec {
Plain,
Gzip,
Zstd,
}
fn tar_with(entries: &[(&str, &[u8])], codec: Codec) -> Vec<u8> {
let mut builder = tar::Builder::new(Vec::new());
for (name, body) in entries {
let mut header = tar::Header::new_gnu();
header.set_size(body.len() as u64);
header.set_mode(0o644);
header.set_cksum();
builder.append_data(&mut header, name, *body).unwrap();
}
let raw = builder.into_inner().unwrap();
match codec {
Codec::Plain => raw,
Codec::Gzip => {
let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
std::io::Write::write_all(&mut gz, &raw).unwrap();
gz.finish().unwrap()
}
Codec::Zstd => zstd::stream::encode_all(raw.as_slice(), 1).unwrap(),
}
}
#[test]
fn every_tar_codec_extracts_with_the_wrapper_stripped() {
for codec in [Codec::Plain, Codec::Gzip, Codec::Zstd] {
let tmp = tempfile::tempdir().unwrap();
let archive = tmp.path().join("dist.tar");
fs::write(
&archive,
tar_with(
&[
("acme-lib-abc123/composer.json", b"{}"),
("acme-lib-abc123/src/Lib.php", b"<?php"),
],
codec,
),
)
.unwrap();
assert_eq!(detect_tar_top_level(&archive).unwrap(), "acme-lib-abc123");
let into = tmp.path().join("out");
fs::create_dir_all(&into).unwrap();
extract_dist(ArchiveKind::Tar, &archive, &into, None).unwrap();
assert_eq!(fs::read(into.join("src/Lib.php")).unwrap(), b"<?php");
assert!(into.join("composer.json").is_file());
}
}
#[test]
fn tar_symlink_escaping_the_root_is_rejected() {
let tmp = tempfile::tempdir().unwrap();
let mut builder = tar::Builder::new(Vec::new());
let mut header = tar::Header::new_gnu();
header.set_entry_type(tar::EntryType::Symlink);
header.set_size(0);
builder
.append_link(&mut header, "pkg/evil", "../../etc/passwd")
.unwrap();
let archive = tmp.path().join("dist.tar");
fs::write(&archive, builder.into_inner().unwrap()).unwrap();
let into = tmp.path().join("out");
fs::create_dir_all(&into).unwrap();
let err = extract_tar(&archive, &into, "pkg").unwrap_err();
assert!(format!("{err:#}").contains("escapes the extraction root"));
}
#[test]
fn symlink_escape_is_lexical() {
assert!(symlink_target_escapes(Path::new("a"), Path::new("/abs")));
assert!(symlink_target_escapes(Path::new("a"), Path::new("../x")));
assert!(!symlink_target_escapes(
Path::new("bin/x"),
Path::new("../lib/y")
));
}
#[test]
fn dist_types_map_to_archive_kinds() {
assert_eq!(ArchiveKind::from_dist_type("zip"), Some(ArchiveKind::Zip));
assert_eq!(ArchiveKind::from_dist_type("tar"), Some(ArchiveKind::Tar));
assert_eq!(ArchiveKind::from_dist_type("xz"), None);
}
}