#![forbid(unsafe_code)]
#![deny(missing_docs)]
#![doc = "Bounded allowlisted extraction of already verified local archives."]
#![doc = ""]
#![doc = "The crate supports regular-file members from tar.gz and zip archives"]
#![doc = "without transport, authenticity, or live-installation policy."]
use flate2::read::GzDecoder;
use sha2::{Digest, Sha256};
use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::fmt;
use std::fs::{self, File};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Component, Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use zip::ZipArchive;
const ROOT_ATTEMPTS: u32 = 32;
const IO_CHUNK_SIZE: usize = 16 * 1024;
const TAR_TRAILING_PADDING_LIMIT: u64 = 1024 * 1024;
const CLEANUP_MAX_PASSES: u32 = 8;
const CLEANUP_MAX_ENTRIES_PER_PASS: usize = 10_000;
const CLEANUP_MAX_DEPTH: usize = 32;
static NEXT_ROOT_ID: AtomicU64 = AtomicU64::new(1);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ArchiveFormat {
TarGz,
Zip,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ArchiveLimits {
max_archive_bytes: u64,
max_entries: usize,
max_path_bytes: usize,
max_member_bytes: u64,
max_total_uncompressed_bytes: u64,
}
impl ArchiveLimits {
pub fn new(
max_archive_bytes: u64,
max_entries: usize,
max_path_bytes: usize,
max_member_bytes: u64,
max_total_uncompressed_bytes: u64,
) -> Result<Self, ExtractionError> {
if max_archive_bytes == 0
|| max_entries == 0
|| max_entries > 100_000
|| max_path_bytes == 0
|| max_path_bytes > 4096
|| max_member_bytes == 0
|| max_total_uncompressed_bytes == 0
|| max_member_bytes > max_total_uncompressed_bytes
{
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPlan));
}
Ok(Self {
max_archive_bytes,
max_entries,
max_path_bytes,
max_member_bytes,
max_total_uncompressed_bytes,
})
}
pub fn strict_default() -> Self {
Self {
max_archive_bytes: 512 * 1024 * 1024,
max_entries: 10_000,
max_path_bytes: 1024,
max_member_bytes: 256 * 1024 * 1024,
max_total_uncompressed_bytes: 512 * 1024 * 1024,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArchiveMember {
source_path: String,
output_name: String,
expected_size: Option<u64>,
expected_sha256: Option<[u8; 32]>,
}
impl ArchiveMember {
pub fn new(
source_path: impl Into<String>,
output_name: impl Into<String>,
expected_size: Option<u64>,
expected_sha256: Option<[u8; 32]>,
) -> Result<Self, ExtractionError> {
let source_path = source_path.into();
let output_name = output_name.into();
validate_archive_path(&source_path, false, 4096)?;
validate_output_name(&output_name, 4096)?;
Ok(Self {
source_path,
output_name,
expected_size,
expected_sha256,
})
}
pub fn source_path(&self) -> &str {
&self.source_path
}
pub fn output_name(&self) -> &str {
&self.output_name
}
}
#[derive(Debug, Clone)]
pub struct ArchivePlan {
archive_path: PathBuf,
format: ArchiveFormat,
members: Vec<ArchiveMember>,
limits: ArchiveLimits,
}
impl ArchivePlan {
pub fn new(
archive_path: impl Into<PathBuf>,
format: ArchiveFormat,
members: Vec<ArchiveMember>,
limits: ArchiveLimits,
) -> Result<Self, ExtractionError> {
if members.is_empty() || members.len() > limits.max_entries {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPlan));
}
let mut sources = HashSet::new();
let mut outputs = HashSet::new();
let mut expected_total = 0u64;
for member in &members {
validate_archive_path(&member.source_path, false, limits.max_path_bytes)?;
validate_output_name(&member.output_name, limits.max_path_bytes)?;
if !sources.insert(member.source_path.clone())
|| !outputs.insert(member.output_name.to_ascii_lowercase())
{
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPlan));
}
if let Some(size) = member.expected_size {
if size > limits.max_member_bytes {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPlan));
}
expected_total = expected_total
.checked_add(size)
.ok_or_else(|| ExtractionError::new(ExtractionErrorKind::InvalidPlan))?;
}
}
if expected_total > limits.max_total_uncompressed_bytes {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPlan));
}
Ok(Self {
archive_path: archive_path.into(),
format,
members,
limits,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExtractionErrorKind {
InvalidPlan,
Io,
MalformedArchive,
InvalidPath,
TooManyEntries,
ArchiveTooLarge,
NonRegularMember,
DuplicateArchiveMember,
MissingMember,
MemberTooLarge,
TotalTooLarge,
SizeMismatch,
DigestMismatch,
CleanupFailed,
}
#[derive(Debug)]
pub struct ExtractionError {
kind: ExtractionErrorKind,
residue_path: Option<PathBuf>,
}
impl ExtractionError {
fn new(kind: ExtractionErrorKind) -> Self {
Self {
kind,
residue_path: None,
}
}
fn with_residue(mut self, path: PathBuf) -> Self {
self.kind = ExtractionErrorKind::CleanupFailed;
self.residue_path = Some(path);
self
}
fn with_empty_residue(mut self, path: PathBuf) -> Self {
if !private_root_is_empty(&path) {
return self.with_residue(path);
}
self.residue_path = Some(path);
self
}
pub fn kind(&self) -> ExtractionErrorKind {
self.kind
}
pub fn residue_path(&self) -> Option<&Path> {
self.residue_path.as_deref()
}
}
impl fmt::Display for ExtractionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "archive extraction failed ({:?})", self.kind)
}
}
impl std::error::Error for ExtractionError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtractedMember {
source_path: String,
output_name: String,
path: PathBuf,
bytes_written: u64,
sha256: [u8; 32],
}
impl ExtractedMember {
pub fn source_path(&self) -> &str {
&self.source_path
}
pub fn output_name(&self) -> &str {
&self.output_name
}
pub fn path(&self) -> &Path {
&self.path
}
pub fn bytes_written(&self) -> u64 {
self.bytes_written
}
pub fn sha256(&self) -> [u8; 32] {
self.sha256
}
}
#[derive(Debug)]
pub struct ExtractedArchive {
guard: Option<DirectoryGuard>,
members: Vec<ExtractedMember>,
handles: Vec<File>,
}
impl ExtractedArchive {
pub fn root(&self) -> &Path {
self.guard.as_ref().expect("active extraction guard").path()
}
pub fn members(&self) -> &[ExtractedMember] {
&self.members
}
pub fn persist(mut self) -> PersistedExtraction {
let guard = self.guard.take().expect("active extraction guard");
let (root, handle) = guard.disarm();
let handles = std::mem::take(&mut self.handles);
PersistedExtraction {
root,
handle,
members: self.members,
handles,
}
}
pub fn cleanup(mut self) -> Result<(), ExtractionError> {
let guard = self.guard.take().expect("active extraction guard");
drop(std::mem::take(&mut self.handles));
guard.cleanup()
}
}
pub struct PersistedExtraction {
root: PathBuf,
handle: File,
members: Vec<ExtractedMember>,
handles: Vec<File>,
}
impl std::fmt::Debug for PersistedExtraction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PersistedExtraction")
.field("root", &self.root)
.field("members", &self.members)
.finish_non_exhaustive()
}
}
impl PersistedExtraction {
pub fn root(&self) -> &Path {
&self.root
}
pub fn members(&self) -> &[ExtractedMember] {
&self.members
}
pub fn cleanup(self) -> Result<(), ExtractionError> {
let Self {
root,
handle,
members: _,
handles,
} = self;
drop(handles);
remove_owned_root_via_handle(root, handle)
}
pub fn into_bound_sources(self) -> Result<BoundExtraction, ExtractionError> {
let Self {
root,
handle: root_handle,
members,
handles,
} = self;
assert_eq!(
members.len(),
handles.len(),
"extraction members and retained handles must stay paired"
);
let mut bound = Vec::with_capacity(members.len());
for (evidence, mut file) in members.into_iter().zip(handles) {
file.flush()
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
file.seek(SeekFrom::Start(0))
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
bound.push(BoundMember {
source_path: evidence.source_path,
output_name: evidence.output_name,
advisory_path: evidence.path,
bytes_written: evidence.bytes_written,
sha256: evidence.sha256,
handle: file,
});
}
Ok(BoundExtraction {
members: bound,
root,
root_handle,
})
}
}
#[derive(Debug)]
pub struct BoundMember {
source_path: String,
output_name: String,
advisory_path: PathBuf,
bytes_written: u64,
sha256: [u8; 32],
handle: File,
}
impl BoundMember {
pub fn source_path(&self) -> &str {
&self.source_path
}
pub fn output_name(&self) -> &str {
&self.output_name
}
pub fn advisory_path(&self) -> &Path {
&self.advisory_path
}
pub fn bytes_written(&self) -> u64 {
self.bytes_written
}
pub fn sha256(&self) -> [u8; 32] {
self.sha256
}
pub fn handle_mut(&mut self) -> &mut File {
&mut self.handle
}
pub fn into_open_object(self) -> File {
self.handle
}
}
#[derive(Debug)]
pub struct BoundExtraction {
members: Vec<BoundMember>,
root: PathBuf,
root_handle: File,
}
impl BoundExtraction {
pub fn members(&self) -> &[BoundMember] {
&self.members
}
pub fn members_mut(&mut self) -> &mut [BoundMember] {
&mut self.members
}
pub fn root(&self) -> &Path {
&self.root
}
pub fn into_members(self) -> (Vec<BoundMember>, DeferredCleanup) {
let Self {
members,
root,
root_handle,
} = self;
(members, DeferredCleanup { root, root_handle })
}
pub fn cleanup(self) -> Result<(), ExtractionError> {
let Self {
members,
root,
root_handle,
} = self;
drop(members);
remove_owned_root_via_handle(root, root_handle)
}
}
pub struct DeferredCleanup {
root: PathBuf,
root_handle: File,
}
impl std::fmt::Debug for DeferredCleanup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DeferredCleanup")
.field("root", &self.root)
.finish_non_exhaustive()
}
}
impl DeferredCleanup {
pub fn root(&self) -> &Path {
&self.root
}
pub fn cleanup(self) -> Result<(), ExtractionError> {
remove_owned_root_via_handle(self.root, self.root_handle)
}
}
pub fn extract(
plan: &ArchivePlan,
output_parent: &Path,
) -> Result<ExtractedArchive, ExtractionError> {
let archive_meta = fs::symlink_metadata(&plan.archive_path)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
if !archive_meta.is_file() || archive_meta.file_type().is_symlink() {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPlan));
}
if archive_meta.len() > plan.limits.max_archive_bytes {
return Err(ExtractionError::new(ExtractionErrorKind::ArchiveTooLarge));
}
let (root, handle) = create_private_root(output_parent)?;
match extract_with_hook_inner(plan, &root, &handle, None) {
Ok((members, handles)) => Ok(ExtractedArchive {
guard: Some(DirectoryGuard::new(root, handle)),
members,
handles,
}),
Err(error) => match remove_owned_root_with_hook(root.clone(), handle, None) {
Ok(()) => Err(error),
Err(_) => Err(error.with_empty_residue(root)),
},
}
}
type MaterializationHook<'a> = Option<&'a dyn Fn(usize, &Path)>;
struct ExtractionScope<'a> {
root_path: &'a Path,
root_handle: &'a File,
write_seq: usize,
hook: MaterializationHook<'a>,
}
impl<'a> ExtractionScope<'a> {
fn run_hook(&self) {
if let Some(hook) = self.hook {
hook(self.write_seq, self.root_path);
}
}
fn create_member(&mut self, output_name: &str) -> Result<File, ExtractionError> {
self.run_hook();
let file = create_private_file_at(self.root_handle, Path::new(output_name))?;
self.write_seq += 1;
Ok(file)
}
fn member_path(&self, output_name: &str) -> PathBuf {
self.root_path.join(output_name)
}
}
#[cfg(test)]
fn extract_with_hook(
plan: &ArchivePlan,
output_parent: &Path,
hook: MaterializationHook<'_>,
) -> Result<ExtractedArchive, ExtractionError> {
let archive_meta = fs::symlink_metadata(&plan.archive_path)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
if !archive_meta.is_file() || archive_meta.file_type().is_symlink() {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPlan));
}
if archive_meta.len() > plan.limits.max_archive_bytes {
return Err(ExtractionError::new(ExtractionErrorKind::ArchiveTooLarge));
}
let (root, handle) = create_private_root(output_parent)?;
match extract_with_hook_inner(plan, &root, &handle, hook) {
Ok((members, handles)) => Ok(ExtractedArchive {
guard: Some(DirectoryGuard::new(root, handle)),
members,
handles,
}),
Err(error) => match remove_owned_root_with_hook(root.clone(), handle, None) {
Ok(()) => Err(error),
Err(_) => Err(error.with_empty_residue(root)),
},
}
}
fn extract_with_hook_inner(
plan: &ArchivePlan,
root: &Path,
root_handle: &File,
hook: MaterializationHook<'_>,
) -> Result<(Vec<ExtractedMember>, Vec<File>), ExtractionError> {
let mut total = 0u64;
let mut seen = HashSet::new();
let mut extracted: HashMap<String, (ExtractedMember, File)> = HashMap::new();
let mut scope = ExtractionScope {
root_path: root,
root_handle,
write_seq: 0,
hook,
};
match plan.format {
ArchiveFormat::TarGz => {
extract_tar_gz(plan, &mut scope, &mut total, &mut seen, &mut extracted)?
}
ArchiveFormat::Zip => extract_zip(plan, &mut scope, &mut total, &mut seen, &mut extracted)?,
}
let mut ordered = Vec::with_capacity(plan.members.len());
let mut handles = Vec::with_capacity(plan.members.len());
for member in &plan.members {
let (evidence, handle) = extracted
.remove(&member.source_path)
.ok_or_else(|| ExtractionError::new(ExtractionErrorKind::MissingMember))?;
ordered.push(evidence);
handles.push(handle);
}
Ok((ordered, handles))
}
fn extract_tar_gz(
plan: &ArchivePlan,
scope: &mut ExtractionScope<'_>,
total: &mut u64,
seen: &mut HashSet<String>,
extracted: &mut HashMap<String, (ExtractedMember, File)>,
) -> Result<(), ExtractionError> {
let input = File::open(&plan.archive_path)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
let decoder = GzDecoder::new(input);
let mut archive = tar::Archive::new(decoder);
let entries = archive
.entries()
.map_err(|_| ExtractionError::new(ExtractionErrorKind::MalformedArchive))?;
for (index, entry) in entries.enumerate() {
if index >= plan.limits.max_entries {
return Err(ExtractionError::new(ExtractionErrorKind::TooManyEntries));
}
let mut entry =
entry.map_err(|_| ExtractionError::new(ExtractionErrorKind::MalformedArchive))?;
if entry.header().path_bytes().contains(&b'\\') {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPath));
}
let path_bytes = entry.path_bytes().into_owned();
let path = std::str::from_utf8(&path_bytes)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::InvalidPath))?;
let entry_type = entry.header().entry_type();
let normalized =
validate_archive_path(path, entry_type.is_dir(), plan.limits.max_path_bytes)?;
if !seen.insert(normalized.clone()) {
return Err(ExtractionError::new(
ExtractionErrorKind::DuplicateArchiveMember,
));
}
let declared = plan
.members
.iter()
.find(|member| member.source_path == normalized);
let is_regular = matches!(entry_type.as_byte(), 0 | b'0');
if declared.is_some() && !is_regular {
return Err(ExtractionError::new(ExtractionErrorKind::NonRegularMember));
}
if !is_regular {
drain_bounded(&mut entry, plan.limits, total)?;
continue;
}
if let Some(member) = declared {
let mut output = scope.create_member(&member.output_name)?;
let path = scope.member_path(&member.output_name);
let (bytes_written, digest) =
copy_bounded(&mut entry, Some(&mut output), plan.limits, total)?;
output
.flush()
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
validate_expected(member, bytes_written, digest)?;
extracted.insert(
normalized.clone(),
(
ExtractedMember {
source_path: normalized,
output_name: member.output_name.clone(),
path,
bytes_written,
sha256: digest,
},
output,
),
);
} else {
drain_bounded(&mut entry, plan.limits, total)?;
}
}
let mut decoder = archive.into_inner();
let mut trailing = 0u64;
let mut buffer = [0u8; IO_CHUNK_SIZE];
loop {
let n = decoder
.read(&mut buffer)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::MalformedArchive))?;
if n == 0 {
break;
}
trailing = trailing
.checked_add(n as u64)
.ok_or_else(|| ExtractionError::new(ExtractionErrorKind::MalformedArchive))?;
if trailing > TAR_TRAILING_PADDING_LIMIT || buffer[..n].iter().any(|byte| *byte != 0) {
return Err(ExtractionError::new(ExtractionErrorKind::MalformedArchive));
}
}
Ok(())
}
fn extract_zip(
plan: &ArchivePlan,
scope: &mut ExtractionScope<'_>,
total: &mut u64,
seen: &mut HashSet<String>,
extracted: &mut HashMap<String, (ExtractedMember, File)>,
) -> Result<(), ExtractionError> {
let input = File::open(&plan.archive_path)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
let mut archive = ZipArchive::new(input)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::MalformedArchive))?;
if archive.len() > plan.limits.max_entries {
return Err(ExtractionError::new(ExtractionErrorKind::TooManyEntries));
}
for index in 0..archive.len() {
let mut entry = archive
.by_index(index)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::MalformedArchive))?;
let path = std::str::from_utf8(entry.name_raw())
.map_err(|_| ExtractionError::new(ExtractionErrorKind::InvalidPath))?;
let normalized = validate_archive_path(path, entry.is_dir(), plan.limits.max_path_bytes)?;
if !seen.insert(normalized.clone()) {
return Err(ExtractionError::new(
ExtractionErrorKind::DuplicateArchiveMember,
));
}
let declared = plan
.members
.iter()
.find(|member| member.source_path == normalized);
let regular = zip_entry_is_regular(&entry);
if declared.is_some() && !regular {
return Err(ExtractionError::new(ExtractionErrorKind::NonRegularMember));
}
if !regular {
drain_bounded(&mut entry, plan.limits, total)?;
continue;
}
if let Some(member) = declared {
let mut output = scope.create_member(&member.output_name)?;
let path = scope.member_path(&member.output_name);
let (bytes_written, digest) =
copy_bounded(&mut entry, Some(&mut output), plan.limits, total)?;
output
.flush()
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
validate_expected(member, bytes_written, digest)?;
extracted.insert(
normalized.clone(),
(
ExtractedMember {
source_path: normalized,
output_name: member.output_name.clone(),
path,
bytes_written,
sha256: digest,
},
output,
),
);
} else {
drain_bounded(&mut entry, plan.limits, total)?;
}
}
Ok(())
}
fn zip_entry_is_regular<R: Read>(entry: &zip::read::ZipFile<'_, R>) -> bool {
if !entry.is_file() || entry.is_symlink() || entry.is_dir() || entry.enclosed_name().is_none() {
return false;
}
entry.unix_mode().is_none_or(|mode| {
let file_type = mode & 0o170000;
file_type == 0 || file_type == 0o100000
})
}
fn copy_bounded<R: Read>(
input: &mut R,
mut output: Option<&mut File>,
limits: ArchiveLimits,
total: &mut u64,
) -> Result<(u64, [u8; 32]), ExtractionError> {
let mut member_bytes = 0u64;
let mut hasher = Sha256::new();
let mut buffer = [0u8; IO_CHUNK_SIZE];
loop {
let n = input
.read(&mut buffer)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::MalformedArchive))?;
if n == 0 {
break;
}
let next_member = member_bytes
.checked_add(n as u64)
.ok_or_else(|| ExtractionError::new(ExtractionErrorKind::MemberTooLarge))?;
let next_total = total
.checked_add(n as u64)
.ok_or_else(|| ExtractionError::new(ExtractionErrorKind::TotalTooLarge))?;
if next_member > limits.max_member_bytes {
return Err(ExtractionError::new(ExtractionErrorKind::MemberTooLarge));
}
if next_total > limits.max_total_uncompressed_bytes {
return Err(ExtractionError::new(ExtractionErrorKind::TotalTooLarge));
}
if let Some(file) = output.as_deref_mut() {
file.write_all(&buffer[..n])
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
}
hasher.update(&buffer[..n]);
member_bytes = next_member;
*total = next_total;
}
let digest: [u8; 32] = hasher.finalize().into();
Ok((member_bytes, digest))
}
fn drain_bounded<R: Read>(
input: &mut R,
limits: ArchiveLimits,
total: &mut u64,
) -> Result<(), ExtractionError> {
copy_bounded(input, None, limits, total).map(|_| ())
}
fn validate_expected(
member: &ArchiveMember,
observed_size: u64,
observed_sha256: [u8; 32],
) -> Result<(), ExtractionError> {
if member
.expected_size
.is_some_and(|expected| expected != observed_size)
{
return Err(ExtractionError::new(ExtractionErrorKind::SizeMismatch));
}
if member
.expected_sha256
.is_some_and(|expected| expected != observed_sha256)
{
return Err(ExtractionError::new(ExtractionErrorKind::DigestMismatch));
}
Ok(())
}
fn validate_archive_path(
path: &str,
directory: bool,
max_path_bytes: usize,
) -> Result<String, ExtractionError> {
if path.is_empty()
|| path.len() > max_path_bytes
|| path.starts_with('/')
|| path.contains('\\')
|| path.contains(':')
|| path.chars().any(|ch| ch.is_control())
{
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPath));
}
let candidate = if directory {
path.strip_suffix('/').unwrap_or(path)
} else {
path
};
if candidate.is_empty() {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPath));
}
let path_obj = Path::new(candidate);
for component in path_obj.components() {
match component {
Component::Normal(_) => {}
_ => return Err(ExtractionError::new(ExtractionErrorKind::InvalidPath)),
}
}
if candidate
.split('/')
.any(|part| part.is_empty() || part == "." || part == "..")
{
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPath));
}
Ok(candidate.to_string())
}
fn validate_output_name(name: &str, max_path_bytes: usize) -> Result<(), ExtractionError> {
if name.is_empty()
|| name.len() > max_path_bytes
|| name == "."
|| name == ".."
|| name.contains('/')
|| name.contains('\\')
|| name.contains(':')
|| name.ends_with('.')
|| name.ends_with(' ')
|| !name.is_ascii()
|| name
.chars()
.any(|ch| ch.is_control() || "<>\"|?*".contains(ch))
{
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPath));
}
let stem = name.split('.').next().unwrap_or(name).to_ascii_uppercase();
let reserved = matches!(
stem.as_str(),
"CON" | "PRN" | "AUX" | "NUL" | "CLOCK$" | "CONIN$" | "CONOUT$"
) || ["COM", "LPT"].iter().any(|prefix| {
stem.strip_prefix(prefix)
.is_some_and(|suffix| suffix.len() == 1 && suffix.as_bytes()[0].is_ascii_digit())
});
if reserved {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPath));
}
Ok(())
}
fn private_root_is_empty(path: &Path) -> bool {
match fs::read_dir(path) {
Ok(mut entries) => entries.next().is_none(),
Err(error) if error.kind() == std::io::ErrorKind::NotFound => true,
Err(_) => false,
}
}
fn open_dir_handle(path: &Path) -> std::io::Result<File> {
#[cfg(windows)]
{
use std::os::windows::fs::OpenOptionsExt;
const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x0200_0000;
std::fs::OpenOptions::new()
.read(true)
.custom_flags(FILE_FLAG_BACKUP_SEMANTICS)
.open(path)
}
#[cfg(not(windows))]
{
File::open(path)
}
}
fn create_private_root(parent: &Path) -> Result<(PathBuf, File), ExtractionError> {
let meta = fs::symlink_metadata(parent)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::InvalidPlan))?;
if !meta.is_dir() || meta.file_type().is_symlink() {
return Err(ExtractionError::new(ExtractionErrorKind::InvalidPlan));
}
let parent_handle =
open_dir_handle(parent).map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
for _ in 0..ROOT_ATTEMPTS {
let id = NEXT_ROOT_ID.fetch_add(1, Ordering::Relaxed);
let file_name = format!(".eggup-extract-{}-{id:016x}", std::process::id());
let mut options = fs_at::OpenOptions::default();
#[cfg(unix)]
{
use fs_at::os::unix::OpenOptionsExt;
options.mode(0o700);
}
match options.mkdir_at(&parent_handle, &file_name) {
Ok(root_handle) => {
let path = parent.join(&file_name);
return Ok((path, root_handle));
}
Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue,
Err(_) => return Err(ExtractionError::new(ExtractionErrorKind::Io)),
}
}
Err(ExtractionError::new(ExtractionErrorKind::Io))
}
fn create_private_file_at(root: &File, name: &Path) -> Result<File, ExtractionError> {
let mut options = fs_at::OpenOptions::default();
options
.read(true)
.write(fs_at::OpenOptionsWriteMode::Write)
.create_new(true)
.follow(false);
#[cfg(unix)]
{
use fs_at::os::unix::OpenOptionsExt;
options.mode(0o600);
}
options
.open_at(root, name)
.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))
}
fn empty_dir_contents(dir: &mut File) -> Result<(), ExtractionError> {
empty_dir_contents_at_depth(dir, 0)
}
fn empty_dir_contents_at_depth(dir: &mut File, depth: usize) -> Result<(), ExtractionError> {
if depth > CLEANUP_MAX_DEPTH {
return Err(ExtractionError::new(ExtractionErrorKind::Io));
}
for _ in 0..CLEANUP_MAX_PASSES {
let names: Vec<std::ffi::OsString> = {
let iter =
fs_at::read_dir(dir).map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
let mut names = Vec::new();
for entry in iter {
let entry = entry.map_err(|_| ExtractionError::new(ExtractionErrorKind::Io))?;
let name = entry.name();
if name == OsStr::new(".") || name == OsStr::new("..") {
continue;
}
names.push(name.to_os_string());
if names.len() > CLEANUP_MAX_ENTRIES_PER_PASS {
return Err(ExtractionError::new(ExtractionErrorKind::Io));
}
}
names
};
if names.is_empty() {
return Ok(());
}
let mut deleted_any = false;
for name in &names {
let name_path = Path::new(name);
match fs_at::OpenOptions::default().open_dir_at(dir, name_path) {
Ok(mut child) => {
let is_dir = child
.metadata()
.map(|metadata| metadata.is_dir())
.unwrap_or(false);
let is_symlink = child
.metadata()
.map(|metadata| metadata.is_symlink())
.unwrap_or(false);
if is_dir && !is_symlink {
empty_dir_contents_at_depth(&mut child, depth + 1)?;
drop(child);
match fs_at::OpenOptions::default().rmdir_at(dir, name_path) {
Ok(()) => deleted_any = true,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
deleted_any = true;
}
Err(_) => {
return Err(ExtractionError::new(ExtractionErrorKind::Io));
}
}
} else {
drop(child);
match fs_at::OpenOptions::default().unlink_at(dir, name_path) {
Ok(()) => deleted_any = true,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
deleted_any = true;
}
Err(_) => {
return Err(ExtractionError::new(ExtractionErrorKind::Io));
}
}
}
}
Err(error) => {
if error.kind() == std::io::ErrorKind::NotFound {
deleted_any = true;
continue;
}
match fs_at::OpenOptions::default().unlink_at(dir, name_path) {
Ok(()) => deleted_any = true,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
deleted_any = true;
}
Err(_) => {
return Err(ExtractionError::new(ExtractionErrorKind::Io));
}
}
}
}
}
if !deleted_any {
return Err(ExtractionError::new(ExtractionErrorKind::Io));
}
}
Err(ExtractionError::new(ExtractionErrorKind::Io))
}
fn remove_owned_root_via_handle(path: PathBuf, handle: File) -> Result<(), ExtractionError> {
remove_owned_root_with_hook(path, handle, None)
}
fn remove_owned_root_with_hook(
path: PathBuf,
mut handle: File,
hook: Option<&dyn Fn(&Path)>,
) -> Result<(), ExtractionError> {
let empty_result = empty_dir_contents(&mut handle);
if let Some(hook) = hook {
hook(&path);
}
let _ = empty_result;
Err(ExtractionError::new(ExtractionErrorKind::CleanupFailed).with_residue(path))
}
struct DirectoryGuard {
path: PathBuf,
handle: Option<File>,
active: bool,
}
impl std::fmt::Debug for DirectoryGuard {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DirectoryGuard")
.field("path", &self.path)
.field("active", &self.active)
.finish_non_exhaustive()
}
}
impl DirectoryGuard {
fn new(path: PathBuf, handle: File) -> Self {
Self {
path,
handle: Some(handle),
active: true,
}
}
fn path(&self) -> &Path {
&self.path
}
fn disarm(mut self) -> (PathBuf, File) {
self.active = false;
let handle = self.handle.take().expect("active extraction guard");
(self.path.clone(), handle)
}
fn cleanup(mut self) -> Result<(), ExtractionError> {
self.active = false;
let handle = self.handle.take().expect("active extraction guard");
remove_owned_root_via_handle(self.path.clone(), handle)
}
}
impl Drop for DirectoryGuard {
fn drop(&mut self) {
if self.active {
if let Some(handle) = self.handle.as_mut() {
let _ = empty_dir_contents(handle);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use eggup_core::{
ArtifactMember, ArtifactSet, BoundSources, InstallPlan, MemberId, ProductId, ReleaseId,
};
use flate2::write::GzEncoder;
use flate2::Compression;
use std::io::Cursor;
use std::sync::atomic::{AtomicU64, Ordering as AtomicOrdering};
use tar::{Builder as TarBuilder, EntryType as TarEntryType, Header};
use zip::write::SimpleFileOptions;
use zip::{CompressionMethod, ZipWriter};
static NEXT_TEST_DIR: AtomicU64 = AtomicU64::new(1);
struct TestDir(PathBuf);
impl TestDir {
fn new() -> Self {
let id = NEXT_TEST_DIR.fetch_add(1, AtomicOrdering::Relaxed);
let path = std::env::temp_dir().join(format!(
"eggup-archive-test-{}-{id:016x}",
std::process::id()
));
fs::create_dir(&path).unwrap();
Self(path)
}
fn path(&self) -> &Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}
struct TarFixture<'a> {
path: &'a str,
kind: u8,
body: &'a [u8],
link: Option<&'a str>,
}
fn limits() -> ArchiveLimits {
ArchiveLimits::new(2 * 1024 * 1024, 32, 256, 1024, 2048).unwrap()
}
fn member(path: &str, output: &str, bytes: &[u8]) -> ArchiveMember {
ArchiveMember::new(
path,
output,
Some(bytes.len() as u64),
Some(Sha256::digest(bytes).into()),
)
.unwrap()
}
fn make_plan(
archive: PathBuf,
format: ArchiveFormat,
members: Vec<ArchiveMember>,
limits: ArchiveLimits,
) -> ArchivePlan {
ArchivePlan::new(archive, format, members, limits).unwrap()
}
fn tar_file(dir: &Path, name: &str, entries: &[TarFixture<'_>]) -> PathBuf {
let archive_path = dir.join(name);
let gzip = GzEncoder::new(Vec::new(), Compression::default());
let mut builder = TarBuilder::new(gzip);
for entry in entries {
let mut header = Header::new_gnu();
header.set_entry_type(TarEntryType::new(entry.kind));
header.set_size(entry.body.len() as u64);
header.set_mode(0o644);
header.set_path(entry.path).unwrap();
if let Some(link) = entry.link {
header.set_link_name(link).unwrap();
}
header.set_cksum();
builder.append(&header, entry.body).unwrap();
}
let gzip = builder.into_inner().unwrap();
let bytes = gzip.finish().unwrap();
fs::write(&archive_path, bytes).unwrap();
archive_path
}
fn zip_file(dir: &Path, name: &str, entries: &[(&str, &[u8])]) -> PathBuf {
let path = dir.join(name);
let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
let options = SimpleFileOptions::default().compression_method(CompressionMethod::Deflated);
for (entry_name, body) in entries {
writer.start_file(*entry_name, options).unwrap();
writer.write_all(body).unwrap();
}
let bytes = writer.finish().unwrap().into_inner();
fs::write(&path, bytes).unwrap();
path
}
fn tar_file_raw_path(dir: &Path, name: &str) -> PathBuf {
let archive_path = dir.join("hostile.tar.gz");
let gzip = GzEncoder::new(Vec::new(), Compression::default());
let mut builder = TarBuilder::new(gzip);
let mut header = Header::new_gnu();
header.set_entry_type(TarEntryType::new(b'0'));
header.set_size(1);
header.set_mode(0o644);
{
let raw = header.as_mut_bytes();
raw[0..100].fill(0);
let bytes = name.as_bytes();
assert!(bytes.len() <= 100, "probe name must fit the raw field");
raw[0..bytes.len()].copy_from_slice(bytes);
raw[345..500].fill(0);
}
header.set_cksum();
builder.append(&header, &b"x"[..]).unwrap();
let gzip = builder.into_inner().unwrap();
fs::write(&archive_path, gzip.finish().unwrap()).unwrap();
archive_path
}
fn names_in(dir: &Path) -> Vec<String> {
let mut out: Vec<String> = fs::read_dir(dir)
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
.collect();
out.sort();
out
}
fn assert_no_escape_or_residue(dir: &Path, archive_name: &str, context: &str) {
for name in names_in(dir) {
if name == archive_name {
continue;
}
assert!(
name.starts_with(".eggup-extract-"),
"{context}: unexpected entry {name:?} escaped into the parent directory"
);
let path = dir.join(&name);
let meta = fs::symlink_metadata(&path).unwrap();
assert!(
meta.is_dir() && !meta.file_type().is_symlink(),
"{context}: owned root {name:?} is not a real directory"
);
let mut inside: Vec<String> = fs::read_dir(&path)
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
.collect();
inside.sort();
assert!(
inside.is_empty(),
"{context}: owned root {name:?} retained a partial extraction {inside:?}"
);
}
}
const HOSTILE_ENTRY_NAMES: [&str; 8] = [
"../escape",
"a/../../escape",
"/etc/passwd",
"..\\..\\evil.exe",
"a\\b",
"a//b",
"./app",
"C:/Windows/x",
];
#[test]
fn hostile_undeclared_tar_entry_names_fail_closed() {
for name in HOSTILE_ENTRY_NAMES {
let dir = TestDir::new();
let archive = tar_file_raw_path(dir.path(), name);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![member("app", "app", b"v1")],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::InvalidPath,
"tar entry {name:?} must be rejected on its raw entry bytes"
);
assert_no_escape_or_residue(dir.path(), "hostile.tar.gz", &format!("tar {name:?}"));
}
}
#[test]
fn hostile_undeclared_zip_entry_names_fail_closed() {
for name in HOSTILE_ENTRY_NAMES {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "hostile.zip", &[(name, b"x")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"v1")],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::InvalidPath,
"zip entry {name:?} must be rejected on its raw entry bytes"
);
assert_no_escape_or_residue(dir.path(), "hostile.zip", &format!("zip {name:?}"));
}
}
#[test]
fn hostile_entry_after_a_valid_member_leaves_no_partial_extraction() {
for name in ["../escape", "/etc/passwd", "a\\b"] {
let dir = TestDir::new();
let archive_path = dir.path().join("mixed.tar.gz");
let gzip = GzEncoder::new(Vec::new(), Compression::default());
let mut builder = TarBuilder::new(gzip);
let mut good = Header::new_gnu();
good.set_entry_type(TarEntryType::new(b'0'));
good.set_size(2);
good.set_mode(0o644);
good.set_path("app").unwrap();
good.set_cksum();
builder.append(&good, &b"v1"[..]).unwrap();
let mut bad = Header::new_gnu();
bad.set_entry_type(TarEntryType::new(b'0'));
bad.set_size(1);
bad.set_mode(0o644);
{
let raw = bad.as_mut_bytes();
raw[0..100].fill(0);
let bytes = name.as_bytes();
raw[0..bytes.len()].copy_from_slice(bytes);
raw[345..500].fill(0);
}
bad.set_cksum();
builder.append(&bad, &b"x"[..]).unwrap();
let gzip = builder.into_inner().unwrap();
fs::write(&archive_path, gzip.finish().unwrap()).unwrap();
let plan = make_plan(
archive_path,
ArchiveFormat::TarGz,
vec![member("app", "app", b"v1")],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::InvalidPath,
"hostile entry {name:?} after a valid member must still fail closed"
);
assert_no_escape_or_residue(dir.path(), "mixed.tar.gz", &format!("mixed tar {name:?}"));
}
}
fn extract_error(plan: &ArchivePlan, dir: &Path) -> ExtractionErrorKind {
extract(plan, dir).unwrap_err().kind()
}
#[test]
fn valid_tar_gz_extracts_allowlisted_members_in_declaration_order() {
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"bundle.tar.gz",
&[
TarFixture {
path: "bin/helper",
kind: b'0',
body: b"helper-v1",
link: None,
},
TarFixture {
path: "bin/main",
kind: b'0',
body: b"main-v1",
link: None,
},
TarFixture {
path: "notes.txt",
kind: b'0',
body: b"ignored",
link: None,
},
],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![
member("bin/main", "main", b"main-v1"),
member("bin/helper", "helper", b"helper-v1"),
],
limits(),
);
let extracted = extract(&plan, dir.path()).unwrap();
assert_eq!(extracted.members()[0].source_path(), "bin/main");
assert_eq!(extracted.members()[1].source_path(), "bin/helper");
assert_eq!(fs::read(extracted.members()[0].path()).unwrap(), b"main-v1");
assert!(!extracted.root().join("notes.txt").exists());
assert_eq!(extracted.members()[0].bytes_written(), 7);
}
#[test]
fn valid_zip_extracts_members_and_returns_digest_evidence() {
let dir = TestDir::new();
let archive = zip_file(
dir.path(),
"bundle.zip",
&[("app.exe", b"windows-bin"), ("helper.dll", b"helper")],
);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![
member("app.exe", "app.exe", b"windows-bin"),
member("helper.dll", "helper.dll", b"helper"),
],
limits(),
);
let extracted = extract(&plan, dir.path()).unwrap();
assert_eq!(
fs::read(extracted.members()[0].path()).unwrap(),
b"windows-bin"
);
assert_eq!(fs::read(extracted.members()[1].path()).unwrap(), b"helper");
let expected_digest: [u8; 32] = Sha256::digest(b"windows-bin").into();
assert_eq!(extracted.members()[0].sha256(), expected_digest);
}
#[cfg(unix)]
#[test]
fn extraction_root_and_files_are_owner_private() {
use std::os::unix::fs::PermissionsExt;
let dir = TestDir::new();
let archive = zip_file(dir.path(), "bundle.zip", &[("app", b"binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"binary")],
limits(),
);
let extracted = extract(&plan, dir.path()).unwrap();
assert_eq!(
fs::metadata(extracted.root()).unwrap().permissions().mode() & 0o777,
0o700
);
assert_eq!(
fs::metadata(extracted.members()[0].path())
.unwrap()
.permissions()
.mode()
& 0o777,
0o600
);
}
#[test]
fn archive_paths_reject_traversal_absolute_prefix_alias_and_overlong_names() {
for path in [
"",
"../escape",
"a/../../escape",
"/etc/passwd",
"C:/Windows/x",
"a\\b",
"a//b",
"./app",
] {
assert_eq!(
ArchiveMember::new(path, "app", None, None)
.unwrap_err()
.kind(),
ExtractionErrorKind::InvalidPath,
"path {path:?}"
);
}
assert_eq!(
ArchiveMember::new("a".repeat(5000), "app", None, None)
.unwrap_err()
.kind(),
ExtractionErrorKind::InvalidPath
);
}
#[test]
fn output_names_reject_windows_reserved_or_ambiguous_names() {
for name in ["../app", "a/b", "a\\b", "CON", "nul.txt", "app.", "app "] {
assert_eq!(
ArchiveMember::new("app", name, None, None)
.unwrap_err()
.kind(),
ExtractionErrorKind::InvalidPath,
"name {name:?}"
);
}
assert_eq!(
ArchivePlan::new(
"archive.zip",
ArchiveFormat::Zip,
vec![
ArchiveMember::new("one", "App", None, None).unwrap(),
ArchiveMember::new("two", "app", None, None).unwrap()
],
limits(),
)
.unwrap_err()
.kind(),
ExtractionErrorKind::InvalidPlan
);
}
#[test]
fn output_names_reject_every_windows_device_name_not_just_1_to_9() {
for name in [
"COM0",
"LPT0",
"com0.txt",
"lpt9",
"LPT0.bin",
"CONIN$",
"CONOUT$",
"conin$.txt",
"CLOCK$",
] {
assert_eq!(
ArchiveMember::new("app", name, None, None)
.unwrap_err()
.kind(),
ExtractionErrorKind::InvalidPath,
"name {name:?} must be rejected as a reserved Windows device name"
);
}
for name in ["COM1", "LPT9"] {
assert_eq!(
ArchiveMember::new("app", name, None, None)
.unwrap_err()
.kind(),
ExtractionErrorKind::InvalidPath,
"name {name:?}"
);
}
for name in ["console", "com10", "auxiliary", "clock"] {
assert!(
ArchiveMember::new("app", name, None, None).is_ok(),
"name {name:?} must remain a usable output name"
);
}
}
#[test]
fn residue_path_is_only_reported_when_the_root_is_actually_empty() {
use crate::{ExtractionError, ExtractionErrorKind};
let dir = TestDir::new();
let empty = dir.path().join("empty");
fs::create_dir(&empty).unwrap();
let err = ExtractionError::new(ExtractionErrorKind::InvalidPath).with_empty_residue(empty);
assert_eq!(err.kind(), ExtractionErrorKind::InvalidPath);
assert!(err.residue_path().is_some());
let full = dir.path().join("full");
fs::create_dir(&full).unwrap();
fs::write(full.join("leftover"), b"x").unwrap();
let err = ExtractionError::new(ExtractionErrorKind::InvalidPath).with_empty_residue(full);
assert_eq!(err.kind(), ExtractionErrorKind::CleanupFailed);
assert!(err.residue_path().is_some());
}
#[test]
fn plan_rejects_duplicate_members_and_nonpositive_or_inconsistent_limits() {
let duplicate = ArchiveMember::new("bin/app", "app", None, None).unwrap();
assert_eq!(
ArchivePlan::new(
"x",
ArchiveFormat::Zip,
vec![duplicate.clone(), duplicate],
limits()
)
.unwrap_err()
.kind(),
ExtractionErrorKind::InvalidPlan
);
assert!(ArchiveLimits::new(0, 1, 10, 10, 10).is_err());
assert!(ArchiveLimits::new(10, 0, 10, 10, 10).is_err());
assert!(ArchiveLimits::new(10, 1, 0, 10, 10).is_err());
assert!(ArchiveLimits::new(10, 1, 10, 11, 10).is_err());
}
#[test]
fn missing_and_duplicate_archive_members_fail_closed() {
let dir = TestDir::new();
let missing = tar_file(dir.path(), "missing.tar.gz", &[]);
let plan = make_plan(
missing,
ArchiveFormat::TarGz,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::MissingMember
);
let duplicate = tar_file(
dir.path(),
"duplicate.tar.gz",
&[
TarFixture {
path: "app",
kind: b'0',
body: b"first",
link: None,
},
TarFixture {
path: "app",
kind: b'0',
body: b"second",
link: None,
},
],
);
let plan = make_plan(
duplicate,
ArchiveFormat::TarGz,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::DuplicateArchiveMember
);
}
#[test]
fn declared_tar_links_and_special_files_are_rejected() {
for (name, kind, link) in [
("symlink", b'2', Some("target")),
("hardlink", b'1', Some("target")),
("fifo", b'6', None),
("device", b'3', None),
] {
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"bad.tar.gz",
&[TarFixture {
path: name,
kind,
body: b"",
link,
}],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![ArchiveMember::new(name, "output", None, None).unwrap()],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::NonRegularMember,
"{name}"
);
}
}
#[test]
fn declared_zip_symlink_is_rejected() {
let dir = TestDir::new();
let path = dir.path().join("symlink.zip");
let mut writer = ZipWriter::new(Cursor::new(Vec::new()));
writer
.add_symlink("app", "target", SimpleFileOptions::default())
.unwrap();
fs::write(path, writer.finish().unwrap().into_inner()).unwrap();
let plan = make_plan(
dir.path().join("symlink.zip"),
ArchiveFormat::Zip,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::NonRegularMember
);
}
#[test]
fn per_member_and_aggregate_decompression_limits_are_enforced() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "large.zip", &[("app", b"123456789")]);
let member_limit = ArchiveLimits::new(1024, 10, 100, 8, 20).unwrap();
let plan = make_plan(
archive.clone(),
ArchiveFormat::Zip,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
member_limit,
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::MemberTooLarge
);
let archive = zip_file(
dir.path(),
"aggregate.zip",
&[("one", b"12345678"), ("two", b"abcdefgh")],
);
let aggregate_limit = ArchiveLimits::new(2048, 10, 100, 8, 12).unwrap();
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![
ArchiveMember::new("one", "one", None, None).unwrap(),
ArchiveMember::new("two", "two", None, None).unwrap(),
],
aggregate_limit,
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::TotalTooLarge
);
}
#[test]
fn entry_count_and_compressed_archive_size_are_bounded() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "entries.zip", &[("app", b"a"), ("extra", b"b")]);
let entry_limit = ArchiveLimits::new(2048, 1, 100, 10, 10).unwrap();
let plan = make_plan(
archive.clone(),
ArchiveFormat::Zip,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
entry_limit,
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::TooManyEntries
);
let bytes = fs::metadata(&archive).unwrap().len();
let archive_limit = ArchiveLimits::new(bytes - 1, 10, 100, 10, 10).unwrap();
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
archive_limit,
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::ArchiveTooLarge
);
}
#[test]
fn exact_size_and_sha256_mismatches_fail() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "verify.zip", &[("app", b"correct")]);
let wrong_size = ArchiveMember::new("app", "app", Some(99), None).unwrap();
let plan = make_plan(
archive.clone(),
ArchiveFormat::Zip,
vec![wrong_size],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::SizeMismatch
);
let wrong_digest = ArchiveMember::new("app", "app", None, Some([0; 32])).unwrap();
let plan = make_plan(archive, ArchiveFormat::Zip, vec![wrong_digest], limits());
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::DigestMismatch
);
}
#[test]
fn corrupt_and_truncated_tar_gz_and_zip_fail() {
let dir = TestDir::new();
let tar = tar_file(
dir.path(),
"ok.tar.gz",
&[TarFixture {
path: "app",
kind: b'0',
body: b"ok",
link: None,
}],
);
let mut bytes = fs::read(&tar).unwrap();
bytes.truncate(bytes.len() - 4);
let broken_tar = dir.path().join("broken.tar.gz");
fs::write(&broken_tar, bytes).unwrap();
let plan = make_plan(
broken_tar,
ArchiveFormat::TarGz,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
limits(),
);
assert_ne!(extract_error(&plan, dir.path()), ExtractionErrorKind::Io);
let zip = zip_file(dir.path(), "ok.zip", &[("app", b"ok")]);
let mut bytes = fs::read(zip).unwrap();
bytes.truncate(bytes.len() / 2);
let broken_zip = dir.path().join("broken.zip");
fs::write(&broken_zip, bytes).unwrap();
let plan = make_plan(
broken_zip,
ArchiveFormat::Zip,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
limits(),
);
assert_eq!(
extract_error(&plan, dir.path()),
ExtractionErrorKind::MalformedArchive
);
}
#[test]
fn failure_cleans_only_its_owned_partial_root_and_drop_cleans_success() {
let dir = TestDir::new();
let sentinel = dir.path().join("foreign");
fs::write(&sentinel, b"keep").unwrap();
let archive = zip_file(dir.path(), "bad.zip", &[("app", b"123456789")]);
let cap = ArchiveLimits::new(2048, 10, 100, 8, 10).unwrap();
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
cap,
);
let error = extract(&plan, dir.path()).unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::MemberTooLarge);
assert_eq!(fs::read(&sentinel).unwrap(), b"keep");
let residue = error.residue_path().expect("empty residue").to_path_buf();
assert!(residue.exists());
assert_eq!(fs::read_dir(&residue).unwrap().count(), 0);
fs::remove_dir(&residue).unwrap();
assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 2);
let archive = zip_file(dir.path(), "good.zip", &[("app", b"ok")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
limits(),
);
let root = {
let output = extract(&plan, dir.path()).unwrap();
output.root().to_path_buf()
};
assert!(root.exists());
assert_eq!(fs::read_dir(&root).unwrap().count(), 0);
fs::remove_dir(&root).unwrap();
}
#[test]
fn persist_transfers_cleanup_responsibility_and_cleanup_is_explicit() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "good.zip", &[("app", b"ok")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![ArchiveMember::new("app", "app", None, None).unwrap()],
limits(),
);
let persisted = extract(&plan, dir.path()).unwrap().persist();
let root = persisted.root().to_path_buf();
assert!(root.exists());
let error = persisted.cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
assert!(root.exists());
assert_eq!(fs::read_dir(&root).unwrap().count(), 0);
fs::remove_dir(&root).unwrap();
}
#[test]
fn cleanup_failure_reports_residue_without_deleting_replacement() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "bundle.zip", &[("app", b"binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"binary")],
limits(),
);
let moved = dir.path().join("moved-root");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
fs::write(root, b"foreign replacement").unwrap();
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
let error = extracted.persist().cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
assert_eq!(fs::read(&root).unwrap(), b"foreign replacement");
fs::remove_file(&root).unwrap();
fs::remove_dir_all(moved).unwrap();
}
#[test]
fn cleanup_preserves_foreign_non_empty_directory_at_original_path() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "bundle.zip", &[("app", b"binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"binary")],
limits(),
);
let moved = dir.path().join("moved-root");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
for i in 0..6 {
fs::write(root.join(format!("foreign-{i}")), b"keep").unwrap();
}
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
let foreign = root.clone();
let error = extracted.persist().cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(foreign.as_path()));
assert!(foreign.exists());
for i in 0..6 {
assert_eq!(
fs::read(foreign.join(format!("foreign-{i}"))).unwrap(),
b"keep"
);
}
assert!(moved.exists());
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir_all(&foreign).unwrap();
fs::remove_dir_all(moved).unwrap();
}
#[test]
fn cleanup_preserves_foreign_empty_directory_at_original_path() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "bundle.zip", &[("app", b"binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"binary")],
limits(),
);
let moved = dir.path().join("moved-root");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
let foreign = root.clone();
let error = extracted.persist().cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(foreign.as_path()));
assert!(foreign.exists());
fs::remove_dir(&foreign).unwrap();
fs::remove_dir_all(moved).unwrap();
}
#[test]
fn drop_cleanup_preserves_foreign_replacement_at_original_path() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "bundle.zip", &[("app", b"binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"binary")],
limits(),
);
let root = {
let moved = dir.path().join("moved-root");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
let foreign = root.to_path_buf();
fs::create_dir(&foreign).unwrap();
fs::write(foreign.join("foreign"), b"keep").unwrap();
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
let foreign = root.clone();
drop(extracted);
assert!(moved.exists());
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
assert!(foreign.exists());
assert_eq!(fs::read(foreign.join("foreign")).unwrap(), b"keep");
moved
};
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)]
#[test]
fn cleanup_preserves_symlink_substitution_at_original_path() {
use std::os::unix::fs::symlink;
let dir = TestDir::new();
let archive = zip_file(dir.path(), "bundle.zip", &[("app", b"binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"binary")],
limits(),
);
let extracted = extract(&plan, dir.path()).unwrap();
let root = extracted.root().to_path_buf();
let moved = dir.path().join("moved-root");
fs::rename(&root, &moved).unwrap();
let target = dir.path().join("link-target");
fs::create_dir(&target).unwrap();
fs::write(target.join("keep"), b"keep").unwrap();
symlink(&target, &root).unwrap();
let error = extracted.persist().cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert!(target.exists());
assert_eq!(fs::read(target.join("keep")).unwrap(), b"keep");
fs::remove_dir_all(moved).unwrap();
fs::remove_dir_all(&target).unwrap();
fs::remove_file(&root).unwrap();
}
#[test]
fn explicit_cleanup_after_replacement_fails_closed_and_persists_identity() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "bundle.zip", &[("app", b"binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"binary")],
limits(),
);
let moved = dir.path().join("moved-root");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
fs::write(root.join("foreign"), b"keep").unwrap();
}),
)
.unwrap();
let persisted = extracted.persist();
let root = persisted.root().to_path_buf();
let foreign = root.clone();
let error = persisted.cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(foreign.as_path()));
assert!(foreign.exists());
assert!(moved.exists());
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir_all(&foreign).unwrap();
fs::remove_dir_all(moved).unwrap();
}
fn owned_root_with_files(parent: &Path, files: &[&str]) -> (PathBuf, File) {
let (root, handle) = create_private_root(parent).unwrap();
for name in files {
fs::write(root.join(name), b"owned").unwrap();
}
(root, handle)
}
#[test]
fn deterministic_race_foreign_non_empty_after_content_deletion_remains_intact() {
let dir = TestDir::new();
let (root, handle) = owned_root_with_files(dir.path(), &["owned"]);
let moved = dir.path().join("moved-owned");
let error = remove_owned_root_with_hook(
root.clone(),
handle,
Some(&|path: &Path| {
fs::rename(path, &moved).unwrap();
fs::create_dir(path).unwrap();
for i in 0..4 {
fs::write(path.join(format!("foreign-{i}")), b"keep").unwrap();
}
}),
)
.unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
for i in 0..4 {
assert_eq!(
fs::read(root.join(format!("foreign-{i}"))).unwrap(),
b"keep"
);
}
assert!(moved.exists());
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir_all(&root).unwrap();
fs::remove_dir(&moved).unwrap();
}
#[test]
fn deterministic_race_foreign_empty_after_content_deletion_remains_intact() {
let dir = TestDir::new();
let (root, handle) = owned_root_with_files(dir.path(), &["owned"]);
let moved = dir.path().join("moved-owned");
let error = remove_owned_root_with_hook(
root.clone(),
handle,
Some(&|path: &Path| {
fs::rename(path, &moved).unwrap();
fs::create_dir(path).unwrap();
}),
)
.unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
assert!(root.exists());
assert_eq!(fs::read_dir(&root).unwrap().count(), 0);
assert!(moved.exists());
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir(&root).unwrap();
fs::remove_dir(&moved).unwrap();
}
#[cfg(unix)]
#[test]
fn deterministic_race_symlink_after_content_deletion_is_not_followed() {
use std::os::unix::fs::symlink;
let dir = TestDir::new();
let (root, handle) = owned_root_with_files(dir.path(), &["owned"]);
let moved = dir.path().join("moved-owned");
let target = dir.path().join("hook-target");
fs::create_dir(&target).unwrap();
fs::write(target.join("keep"), b"keep").unwrap();
let error = remove_owned_root_with_hook(
root.clone(),
handle,
Some(&|path: &Path| {
fs::rename(path, &moved).unwrap();
symlink(&target, path).unwrap();
}),
)
.unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
assert_eq!(fs::read(target.join("keep")).unwrap(), b"keep");
assert!(moved.exists());
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir_all(&moved).unwrap();
fs::remove_dir_all(&target).unwrap();
fs::remove_file(&root).unwrap();
}
#[test]
fn deterministic_race_persisted_cleanup_uses_same_handle_authority() {
let dir = TestDir::new();
let (root, handle) = owned_root_with_files(dir.path(), &["owned"]);
let moved = dir.path().join("moved-owned");
let error = remove_owned_root_with_hook(
root.clone(),
handle,
Some(&|path: &Path| {
fs::rename(path, &moved).unwrap();
fs::create_dir(path).unwrap();
fs::write(path.join("foreign"), b"keep").unwrap();
}),
)
.unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
assert_eq!(fs::read(root.join("foreign")).unwrap(), b"keep");
assert!(moved.exists());
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir_all(&root).unwrap();
fs::remove_dir(&moved).unwrap();
}
#[test]
fn cleanup_remains_bounded_under_concurrent_file_creation() {
let dir = TestDir::new();
let (root, handle) = owned_root_with_files(dir.path(), &["owned"]);
let root_clone = root.clone();
let writer = std::thread::spawn(move || {
for i in 0..200 {
let _ = fs::write(root_clone.join(format!("race-{i}")), b"x");
}
});
let result = remove_owned_root_with_hook(root.clone(), handle, None);
assert_eq!(
result.unwrap_err().kind(),
ExtractionErrorKind::CleanupFailed
);
writer.join().unwrap();
let _ = fs::remove_dir_all(&root);
}
#[cfg(windows)]
#[test]
fn deterministic_race_windows_reparse_after_authority_boundary_is_preserved() {
let dir = TestDir::new();
let (root, handle) = owned_root_with_files(dir.path(), &["owned"]);
let moved = dir.path().join("moved-owned");
let target = dir.path().join("hook-target");
fs::create_dir(&target).unwrap();
fs::write(target.join("keep"), b"keep").unwrap();
let result = remove_owned_root_with_hook(
root.clone(),
handle,
Some(&|path: &Path| {
fs::rename(path, &moved).unwrap();
let _ = std::os::windows::fs::symlink_dir(&target, path);
}),
);
let error = result.unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
assert_eq!(fs::read(target.join("keep")).unwrap(), b"keep");
let _ = fs::remove_dir_all(&root);
let _ = fs::remove_file(&root);
let _ = fs::remove_dir_all(&moved);
let _ = fs::remove_dir(&moved);
let _ = fs::remove_dir_all(&target);
}
#[test]
fn normal_explicit_cleanup_empties_owned_root_and_reports_empty_residue() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "bundle.zip", &[("app", b"binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"binary")],
limits(),
);
let persisted = extract(&plan, dir.path()).unwrap().persist();
let root = persisted.root().to_path_buf();
let member_path = persisted.members()[0].path().to_path_buf();
assert!(member_path.exists());
let error = persisted.cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
assert!(!member_path.exists());
assert!(root.exists());
assert_eq!(fs::read_dir(&root).unwrap().count(), 0);
fs::remove_dir(&root).unwrap();
}
#[test]
fn exclusive_output_creation_preserves_an_existing_file() {
let dir = TestDir::new();
let (root, root_handle) = create_private_root(dir.path()).unwrap();
fs::write(root.join("existing"), b"foreign").unwrap();
assert_eq!(
create_private_file_at(&root_handle, Path::new("existing"))
.unwrap_err()
.kind(),
ExtractionErrorKind::Io
);
assert_eq!(fs::read(root.join("existing")).unwrap(), b"foreign");
drop(root_handle);
fs::remove_dir_all(&root).unwrap();
}
#[test]
fn extracted_file_can_be_prepared_as_an_ordinary_core_artifact() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "release.zip", &[("main", b"new binary")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("main", "main", b"new binary")],
limits(),
);
let bound = extract(&plan, dir.path())
.unwrap()
.persist()
.into_bound_sources()
.unwrap();
assert_eq!(bound.members()[0].bytes_written(), 10);
let install_root = dir.path().join("install");
fs::create_dir(&install_root).unwrap();
let root = bound.root().to_path_buf();
let core_plan = bound_plan(&bound, &install_root);
let (prepared, cleanup) = stage_bound_members(bound, core_plan);
assert_eq!(
fs::read(
prepared
.staged_path(&MemberId::new("main").unwrap())
.unwrap()
)
.unwrap(),
b"new binary"
);
assert!(prepared.stage_root().exists());
let error = cleanup.cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(root.as_path()));
assert!(root.exists());
assert_eq!(fs::read_dir(&root).unwrap().count(), 0);
fs::remove_dir(&root).unwrap();
}
#[test]
fn tar_handle_relative_write_survives_root_rename_before_first_member() {
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"race.tar.gz",
&[TarFixture {
path: "app",
kind: b'0',
body: b"owned-bytes",
link: None,
}],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let moved = dir.path().join("moved-owned-tar");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
for i in 0..3 {
fs::write(root.join(format!("foreign-{i}")), b"keep").unwrap();
}
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
assert_eq!(fs::read(moved.join("app")).unwrap(), b"owned-bytes");
for i in 0..3 {
assert_eq!(
fs::read(root.join(format!("foreign-{i}"))).unwrap(),
b"keep"
);
}
assert!(!root.join("app").exists());
assert_eq!(extracted.members()[0].path(), root.join("app").as_path());
drop(extracted);
fs::remove_dir_all(&moved).unwrap();
fs::remove_dir_all(&root).unwrap();
}
#[test]
fn zip_handle_relative_write_survives_root_rename_before_first_member() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"owned-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let moved = dir.path().join("moved-owned-zip");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
for i in 0..3 {
fs::write(root.join(format!("foreign-{i}")), b"keep").unwrap();
}
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
assert_eq!(fs::read(moved.join("app")).unwrap(), b"owned-bytes");
for i in 0..3 {
assert_eq!(
fs::read(root.join(format!("foreign-{i}"))).unwrap(),
b"keep"
);
}
assert!(!root.join("app").exists());
drop(extracted);
fs::remove_dir_all(&moved).unwrap();
fs::remove_dir_all(&root).unwrap();
}
#[cfg(unix)]
#[test]
fn two_member_replacement_between_writes_stays_in_owned_root() {
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"two.tar.gz",
&[
TarFixture {
path: "one",
kind: b'0',
body: b"first",
link: None,
},
TarFixture {
path: "two",
kind: b'0',
body: b"second",
link: None,
},
],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![
member("one", "one", b"first"),
member("two", "two", b"second"),
],
limits(),
);
let moved = dir.path().join("moved-owned-two");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
if seq == 1 {
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
fs::write(root.join("foreign"), b"keep").unwrap();
}
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
assert_eq!(fs::read(moved.join("one")).unwrap(), b"first");
assert_eq!(fs::read(moved.join("two")).unwrap(), b"second");
assert_eq!(fs::read(root.join("foreign")).unwrap(), b"keep");
assert_eq!(fs::read_dir(&root).unwrap().count(), 1);
assert_eq!(extracted.members().len(), 2);
drop(extracted);
fs::remove_dir_all(&moved).unwrap();
fs::remove_dir_all(&root).unwrap();
}
#[cfg(unix)]
#[test]
fn root_symlink_replacement_is_not_followed_by_materialization() {
use std::os::unix::fs::symlink;
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"owned-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let moved = dir.path().join("moved-owned-link");
let target = dir.path().join("link-target");
fs::create_dir(&target).unwrap();
fs::write(target.join("keep"), b"keep").unwrap();
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
symlink(&target, root).unwrap();
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
assert_eq!(fs::read(moved.join("app")).unwrap(), b"owned-bytes");
assert_eq!(fs::read(target.join("keep")).unwrap(), b"keep");
assert_eq!(fs::read_dir(&target).unwrap().count(), 1);
drop(extracted);
fs::remove_dir_all(&moved).unwrap();
fs::remove_dir_all(&target).unwrap();
fs::remove_file(&root).unwrap();
}
#[cfg(windows)]
#[test]
fn windows_reparse_replacement_is_not_followed_by_materialization() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"owned-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let moved = dir.path().join("moved-owned-reparse");
let target = dir.path().join("reparse-target");
fs::create_dir(&target).unwrap();
fs::write(target.join("keep"), b"keep").unwrap();
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
let _ = std::os::windows::fs::symlink_dir(&target, root);
if !root.exists() {
fs::create_dir(root).unwrap();
fs::write(root.join("foreign"), b"keep").unwrap();
}
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
assert_eq!(fs::read(moved.join("app")).unwrap(), b"owned-bytes");
assert_eq!(fs::read(target.join("keep")).unwrap(), b"keep");
assert_eq!(fs::read_dir(&target).unwrap().count(), 1);
drop(extracted);
let _ = fs::remove_dir_all(&root);
let _ = fs::remove_file(&root);
let _ = fs::remove_dir_all(&moved);
let _ = fs::remove_dir(&moved);
let _ = fs::remove_dir_all(&target);
}
#[test]
fn raced_existing_output_file_in_owned_root_fails_without_clobber() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"new-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"new-bytes")],
limits(),
);
let error = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::write(root.join("app"), b"existing").unwrap();
}),
)
.unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::Io);
if let Some(residue) = error.residue_path() {
if residue.join("app").exists() {
assert_eq!(fs::read(residue.join("app")).unwrap(), b"existing");
}
let _ = fs::remove_dir_all(residue);
}
}
#[cfg(unix)]
#[test]
fn raced_output_symlink_in_owned_root_fails_and_target_untouched() {
use std::os::unix::fs::symlink;
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"new-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"new-bytes")],
limits(),
);
let target = dir.path().join("race-target");
fs::write(&target, b"keep").unwrap();
let error = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
symlink(&target, root.join("app")).unwrap();
}),
)
.unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::Io);
assert_eq!(fs::read(&target).unwrap(), b"keep");
if let Some(residue) = error.residue_path() {
let _ = fs::remove_dir_all(residue);
}
fs::remove_file(&target).unwrap();
}
fn replace_member_entry(path: &Path, foreign: &[u8]) {
fs::remove_file(path).unwrap();
fs::write(path, foreign).unwrap();
}
fn bound_plan(extraction: &BoundExtraction, install_root: &Path) -> InstallPlan {
let artifacts = ArtifactSet::new(
extraction
.members()
.iter()
.map(|member| {
ArtifactMember::new(
MemberId::new(member.output_name()).unwrap(),
member.advisory_path(),
member.output_name(),
)
.unwrap()
})
.collect(),
)
.unwrap();
InstallPlan::new(
ProductId::new("example").unwrap(),
ReleaseId::new("1.0.0").unwrap(),
install_root,
artifacts,
)
.unwrap()
}
fn stage_bound_members(
extraction: BoundExtraction,
plan: InstallPlan,
) -> (eggup_core::PreparedTransaction, DeferredCleanup) {
let (members, cleanup) = extraction.into_members();
let mut sources = BoundSources::new();
for member in members {
let id = MemberId::new(member.output_name()).unwrap();
sources.insert(id, member.into_open_object());
}
(plan.prepare_with_bound_sources(sources).unwrap(), cleanup)
}
fn assert_cleanup_reports_residue(cleanup: DeferredCleanup, residue: &Path, emptied: &Path) {
let error = cleanup.cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(error.residue_path(), Some(residue));
assert_eq!(fs::read_dir(emptied).unwrap().count(), 0);
fs::remove_dir(emptied).unwrap();
}
#[test]
fn bound_tar_member_entry_replacement_after_handoff_stages_owned_bytes() {
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"race.tar.gz",
&[TarFixture {
path: "app",
kind: b'0',
body: b"owned-bytes",
link: None,
}],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let bound = extract(&plan, dir.path())
.unwrap()
.persist()
.into_bound_sources()
.unwrap();
let expected: [u8; 32] = Sha256::digest(b"owned-bytes").into();
assert_eq!(bound.members()[0].bytes_written(), 11);
assert_eq!(bound.members()[0].sha256(), expected);
let install_root = dir.path().join("install");
fs::create_dir(&install_root).unwrap();
let root = bound.root().to_path_buf();
let core_plan = bound_plan(&bound, &install_root);
let advisory = bound.members()[0].advisory_path().to_path_buf();
replace_member_entry(&advisory, b"foreign-bytes");
let (prepared, cleanup) = stage_bound_members(bound, core_plan);
assert_eq!(
fs::read(
prepared
.staged_path(&MemberId::new("app").unwrap())
.unwrap()
)
.unwrap(),
b"owned-bytes"
);
assert_eq!(fs::read(&advisory).unwrap(), b"foreign-bytes");
assert_cleanup_reports_residue(cleanup, &root, &root);
}
#[test]
fn bound_zip_member_entry_replacement_after_handoff_stages_owned_bytes() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"owned-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let bound = extract(&plan, dir.path())
.unwrap()
.persist()
.into_bound_sources()
.unwrap();
let expected: [u8; 32] = Sha256::digest(b"owned-bytes").into();
assert_eq!(bound.members()[0].bytes_written(), 11);
assert_eq!(bound.members()[0].sha256(), expected);
let install_root = dir.path().join("install");
fs::create_dir(&install_root).unwrap();
let root = bound.root().to_path_buf();
let core_plan = bound_plan(&bound, &install_root);
let advisory = bound.members()[0].advisory_path().to_path_buf();
replace_member_entry(&advisory, b"foreign-bytes");
let (prepared, cleanup) = stage_bound_members(bound, core_plan);
assert_eq!(
fs::read(
prepared
.staged_path(&MemberId::new("app").unwrap())
.unwrap()
)
.unwrap(),
b"owned-bytes"
);
assert_eq!(fs::read(&advisory).unwrap(), b"foreign-bytes");
assert_cleanup_reports_residue(cleanup, &root, &root);
}
#[test]
fn bound_two_member_replacement_after_handoff_stages_owned_bytes() {
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"two.tar.gz",
&[
TarFixture {
path: "one",
kind: b'0',
body: b"first",
link: None,
},
TarFixture {
path: "two",
kind: b'0',
body: b"second",
link: None,
},
],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![
member("one", "one", b"first"),
member("two", "two", b"second"),
],
limits(),
);
let bound = extract(&plan, dir.path())
.unwrap()
.persist()
.into_bound_sources()
.unwrap();
let install_root = dir.path().join("install");
fs::create_dir(&install_root).unwrap();
let root = bound.root().to_path_buf();
let core_plan = bound_plan(&bound, &install_root);
let first_advisory = bound.members()[0].advisory_path().to_path_buf();
let second_advisory = bound.members()[1].advisory_path().to_path_buf();
replace_member_entry(&first_advisory, b"foreign-one");
replace_member_entry(&second_advisory, b"foreign-two");
let (prepared, cleanup) = stage_bound_members(bound, core_plan);
assert_eq!(
fs::read(
prepared
.staged_path(&MemberId::new("one").unwrap())
.unwrap()
)
.unwrap(),
b"first"
);
assert_eq!(
fs::read(
prepared
.staged_path(&MemberId::new("two").unwrap())
.unwrap()
)
.unwrap(),
b"second"
);
assert_eq!(fs::read(&first_advisory).unwrap(), b"foreign-one");
assert_eq!(fs::read(&second_advisory).unwrap(), b"foreign-two");
assert_cleanup_reports_residue(cleanup, &root, &root);
}
#[cfg(unix)]
#[test]
fn bound_post_handoff_root_replacement_stages_owned_bytes() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"owned-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let bound = extract(&plan, dir.path())
.unwrap()
.persist()
.into_bound_sources()
.unwrap();
let install_root = dir.path().join("install");
fs::create_dir(&install_root).unwrap();
let root = bound.root().to_path_buf();
let core_plan = bound_plan(&bound, &install_root);
let moved = dir.path().join("moved-owned-post-handoff");
fs::rename(&root, &moved).unwrap();
fs::create_dir(&root).unwrap();
fs::write(root.join("foreign"), b"keep").unwrap();
let (prepared, cleanup) = stage_bound_members(bound, core_plan);
assert_eq!(
fs::read(
prepared
.staged_path(&MemberId::new("app").unwrap())
.unwrap()
)
.unwrap(),
b"owned-bytes"
);
assert_eq!(fs::read(root.join("foreign")).unwrap(), b"keep");
assert_eq!(fs::read_dir(&root).unwrap().count(), 1);
assert_cleanup_reports_residue(cleanup, &root, &moved);
fs::remove_dir_all(&root).unwrap();
}
#[test]
fn bound_tar_root_rename_before_first_write_reads_owned_bytes() {
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"race.tar.gz",
&[TarFixture {
path: "app",
kind: b'0',
body: b"owned-bytes",
link: None,
}],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let moved = dir.path().join("moved-owned-bound-tar");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
fs::write(root.join("foreign"), b"keep").unwrap();
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
let (members, cleanup) = extracted
.persist()
.into_bound_sources()
.unwrap()
.into_members();
assert_eq!(members.len(), 1);
let mut owned = Vec::new();
let mut object = members.into_iter().next().unwrap().into_open_object();
use std::io::Read as _;
object.read_to_end(&mut owned).unwrap();
assert_eq!(owned, b"owned-bytes");
assert_eq!(fs::read(root.join("foreign")).unwrap(), b"keep");
assert!(!root.join("app").exists());
let error = cleanup.cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir(&moved).unwrap();
fs::remove_dir_all(&root).unwrap();
}
#[test]
fn bound_zip_root_rename_before_first_write_reads_owned_bytes() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"owned-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let moved = dir.path().join("moved-owned-bound-zip");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
assert_eq!(seq, 0);
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
fs::write(root.join("foreign"), b"keep").unwrap();
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
let (members, cleanup) = extracted
.persist()
.into_bound_sources()
.unwrap()
.into_members();
assert_eq!(members.len(), 1);
let mut owned = Vec::new();
let mut object = members.into_iter().next().unwrap().into_open_object();
use std::io::Read as _;
object.read_to_end(&mut owned).unwrap();
assert_eq!(owned, b"owned-bytes");
assert_eq!(fs::read(root.join("foreign")).unwrap(), b"keep");
assert!(!root.join("app").exists());
let error = cleanup.cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir(&moved).unwrap();
fs::remove_dir_all(&root).unwrap();
}
#[cfg(unix)]
#[test]
fn bound_two_member_replacement_between_writes_reads_owned_bytes() {
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"two.tar.gz",
&[
TarFixture {
path: "one",
kind: b'0',
body: b"first",
link: None,
},
TarFixture {
path: "two",
kind: b'0',
body: b"second",
link: None,
},
],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![
member("one", "one", b"first"),
member("two", "two", b"second"),
],
limits(),
);
let moved = dir.path().join("moved-owned-bound-two");
let extracted = extract_with_hook(
&plan,
dir.path(),
Some(&|seq: usize, root: &Path| {
if seq == 1 {
fs::rename(root, &moved).unwrap();
fs::create_dir(root).unwrap();
fs::write(root.join("foreign"), b"keep").unwrap();
}
}),
)
.unwrap();
let root = extracted.root().to_path_buf();
let (members, cleanup) = extracted
.persist()
.into_bound_sources()
.unwrap()
.into_members();
assert_eq!(members.len(), 2);
use std::io::Read as _;
let mut bodies = Vec::new();
for member in members {
let mut object = member.into_open_object();
let mut body = Vec::new();
object.read_to_end(&mut body).unwrap();
bodies.push(body);
}
assert_eq!(bodies, vec![b"first".to_vec(), b"second".to_vec()]);
assert_eq!(fs::read(root.join("foreign")).unwrap(), b"keep");
assert_eq!(fs::read_dir(&root).unwrap().count(), 1);
let error = cleanup.cleanup().unwrap_err();
assert_eq!(error.kind(), ExtractionErrorKind::CleanupFailed);
assert_eq!(fs::read_dir(&moved).unwrap().count(), 0);
fs::remove_dir(&moved).unwrap();
fs::remove_dir_all(&root).unwrap();
}
#[test]
fn bound_nonzero_cursor_stages_full_file_from_byte_zero() {
let dir = TestDir::new();
let archive = zip_file(dir.path(), "race.zip", &[("app", b"owned-bytes")]);
let plan = make_plan(
archive,
ArchiveFormat::Zip,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let mut bound = extract(&plan, dir.path())
.unwrap()
.persist()
.into_bound_sources()
.unwrap();
let mut prefix = [0u8; 4];
use std::io::Read as _;
bound.members_mut()[0]
.handle_mut()
.read_exact(&mut prefix)
.unwrap();
assert_eq!(&prefix, b"owne");
let install_root = dir.path().join("install");
fs::create_dir(&install_root).unwrap();
let root = bound.root().to_path_buf();
let core_plan = bound_plan(&bound, &install_root);
let (prepared, cleanup) = stage_bound_members(bound, core_plan);
assert_eq!(
fs::read(
prepared
.staged_path(&MemberId::new("app").unwrap())
.unwrap()
)
.unwrap(),
b"owned-bytes"
);
assert_cleanup_reports_residue(cleanup, &root, &root);
}
#[cfg(unix)]
#[test]
fn bound_sources_preserve_owner_private_mode_and_exact_evidence() {
use std::os::unix::fs::PermissionsExt;
let dir = TestDir::new();
let archive = tar_file(
dir.path(),
"race.tar.gz",
&[TarFixture {
path: "app",
kind: b'0',
body: b"owned-bytes",
link: None,
}],
);
let plan = make_plan(
archive,
ArchiveFormat::TarGz,
vec![member("app", "app", b"owned-bytes")],
limits(),
);
let bound = extract(&plan, dir.path())
.unwrap()
.persist()
.into_bound_sources()
.unwrap();
let expected: [u8; 32] = Sha256::digest(b"owned-bytes").into();
assert_eq!(bound.members()[0].bytes_written(), 11);
assert_eq!(bound.members()[0].sha256(), expected);
assert_eq!(bound.members()[0].source_path(), "app");
assert_eq!(bound.members()[0].output_name(), "app");
let mode = fs::metadata(bound.members()[0].advisory_path())
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o777, 0o600);
let install_root = dir.path().join("install");
fs::create_dir(&install_root).unwrap();
let root = bound.root().to_path_buf();
let core_plan = bound_plan(&bound, &install_root);
let (prepared, cleanup) = stage_bound_members(bound, core_plan);
let staged_mode = fs::metadata(
prepared
.staged_path(&MemberId::new("app").unwrap())
.unwrap(),
)
.unwrap()
.permissions()
.mode();
assert_eq!(staged_mode & 0o777, 0o600);
assert_cleanup_reports_residue(cleanup, &root, &root);
}
}