use std::path::Path;
use super::types::ArchiveError;
#[cfg(debug_assertions)]
#[must_use]
pub fn sanitize_path_for_error(path: &Path) -> String {
path.display().to_string()
}
#[cfg(not(debug_assertions))]
#[must_use]
pub fn sanitize_path_for_error(path: &Path) -> String {
path.file_name().map_or_else(
|| "<unknown>".to_string(),
|n| n.to_string_lossy().into_owned(),
)
}
#[must_use]
pub fn format_entry_path_for_error(path: &Path) -> String {
crate::path_text(path)
}
#[cfg(debug_assertions)]
#[must_use]
pub fn sanitize_io_error_for_error(e: &std::io::Error) -> String {
e.to_string()
}
#[cfg(not(debug_assertions))]
#[must_use]
pub fn sanitize_io_error_for_error(e: &std::io::Error) -> String {
e.get_ref()
.and_then(|inner| inner.downcast_ref::<super::IoContext>())
.map_or_else(|| e.kind().to_string(), |ctx| ctx.context.to_string())
}
impl ArchiveError {
#[must_use]
pub fn redacted_path(&self) -> Option<String> {
match self {
Self::PathTraversal { path }
| Self::SymlinkEscape { path }
| Self::HardlinkEscape { path }
| Self::InvalidPermissions { path, .. } => Some(format_entry_path_for_error(path)),
Self::SourceNotFound { path }
| Self::SourceNotAccessible { path, .. }
| Self::SourceChanged { path }
| Self::OutputExists { path }
| Self::OutputReservationLeft { path, .. }
| Self::DestinationNotAccessible { path, .. }
| Self::UnknownFormat { path } => Some(sanitize_path_for_error(path)),
Self::DestinationOccupied { destination, .. } => {
destination.as_deref().map(sanitize_path_for_error)
}
Self::PartialExtraction(partial) => partial.cause().redacted_path(),
Self::Io(_)
| Self::InvalidArchive(_)
| Self::ZipBomb(_)
| Self::ZipInconsistency(_)
| Self::TarAmbiguity(_)
| Self::TarTrailer(_)
| Self::QuotaExceeded { .. }
| Self::SecurityViolation { .. }
| Self::InvalidCompressionLevel { .. }
| Self::InvalidConfiguration { .. }
| Self::UnmatchedInclude { .. }
| Self::Cancelled
| Self::ReplaceFailed(_)
| Self::ReaderFormat(_) => None,
}
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
use super::super::types::ZipBombDetails;
use super::super::types::ZipBombScope;
use super::super::violation::Violation;
use super::*;
use crate::ExtractionReport;
use crate::security::Compressed;
use crate::security::Uncompressed;
use std::path::PathBuf;
#[test]
#[cfg(not(debug_assertions))]
fn host_paths_of_the_new_variants_are_redacted_in_release() {
use crate::ReplaceFailure;
use crate::Restore;
use crate::StagingLeftover;
let occupied = ArchiveError::DestinationOccupied {
destination: Some(PathBuf::from("/srv/internal/dest")),
conflicts: crate::OccupiedPaths::one(crate::OccupiedPath::new(
PathBuf::from("a/b"),
None,
)),
by: crate::OccupiedBy::Existing,
};
assert_eq!(occupied.redacted_path().as_deref(), Some("dest"));
assert!(!occupied.into_ffi_message().description.contains("internal"));
let no_destination = ArchiveError::DestinationOccupied {
destination: None,
conflicts: crate::OccupiedPaths::one(crate::OccupiedPath::new(
PathBuf::from("a/b"),
None,
)),
by: crate::OccupiedBy::Existing,
};
assert_eq!(no_destination.redacted_path(), None);
let unreachable = ArchiveError::DestinationNotAccessible {
path: PathBuf::from("/srv/internal/dest"),
step: crate::DestinationStep::Resolve,
error: std::io::Error::other("x"),
};
assert_eq!(unreachable.redacted_path().as_deref(), Some("dest"));
assert!(
!unreachable
.into_ffi_message()
.description
.contains("internal")
);
let left = ArchiveError::OutputReservationLeft {
path: PathBuf::from("/srv/internal/out.tar"),
error: std::io::Error::other("x"),
};
assert!(!left.into_ffi_message().description.contains("internal"));
let mut failure = ReplaceFailure::new(
std::io::Error::other("x"),
Restore::Failed {
backup: PathBuf::from("/srv/internal/backup"),
error: std::io::Error::other("y"),
},
None,
);
failure.leftover = Some(StagingLeftover {
path: PathBuf::from("/srv/internal/stage"),
});
let text = ArchiveError::ReplaceFailed(failure)
.into_ffi_message()
.description;
assert!(!text.contains("internal"), "{text}");
}
#[test]
#[cfg(debug_assertions)]
fn test_sanitize_path_for_error_keeps_full_path_in_debug() {
let path = PathBuf::from("/srv/internal/app/x.txt");
assert_eq!(sanitize_path_for_error(&path), "/srv/internal/app/x.txt");
}
#[test]
#[cfg(not(debug_assertions))]
fn test_sanitize_path_for_error_strips_directory_in_release() {
let path = PathBuf::from("/srv/internal/app/x.txt");
assert_eq!(sanitize_path_for_error(&path), "x.txt");
}
#[test]
fn test_format_entry_path_for_error_never_redacts() {
let path = PathBuf::from("../../etc/passwd");
assert_eq!(format_entry_path_for_error(&path), "../../etc/passwd");
}
#[test]
#[cfg(debug_assertions)]
fn test_sanitize_io_error_for_error_keeps_message_in_debug() {
let err = std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"directory is not writable: /srv/internal/app/private-output",
);
assert_eq!(
sanitize_io_error_for_error(&err),
"directory is not writable: /srv/internal/app/private-output"
);
}
#[test]
#[cfg(not(debug_assertions))]
fn test_sanitize_io_error_for_error_redacts_message_in_release() {
let err = std::io::Error::new(
std::io::ErrorKind::PermissionDenied,
"directory is not writable: /srv/internal/app/private-output",
);
let msg = sanitize_io_error_for_error(&err);
assert!(!msg.contains("/srv/internal/app"));
assert!(msg.contains("permission denied"));
}
#[test]
#[cfg(not(debug_assertions))]
fn test_sanitize_io_error_for_error_surfaces_io_context_in_release() {
let err = std::io::Error::other(crate::IoContext::new(
"failed to read entry metadata",
"/srv/internal/app/x.txt: permission denied",
));
let msg = sanitize_io_error_for_error(&err);
assert_eq!(msg, "failed to read entry metadata");
}
#[test]
#[cfg(debug_assertions)]
fn test_sanitize_io_error_for_error_keeps_io_context_detail_in_debug() {
let err = std::io::Error::other(crate::IoContext::new(
"failed to read entry metadata",
"/srv/internal/app/x.txt: permission denied",
));
assert_eq!(
sanitize_io_error_for_error(&err),
"failed to read entry metadata: /srv/internal/app/x.txt: permission denied"
);
}
#[test]
fn test_never_redacted_variants_keep_full_path() {
let attacker_path = PathBuf::from("../../etc/passwd");
let never_redacted = [
ArchiveError::PathTraversal {
path: attacker_path.clone(),
},
ArchiveError::SymlinkEscape {
path: attacker_path.clone(),
},
ArchiveError::HardlinkEscape {
path: attacker_path.clone(),
},
ArchiveError::InvalidPermissions {
path: attacker_path,
mode: crate::RawMode::new(0o777),
},
];
for err in never_redacted {
assert_eq!(
err.redacted_path().as_deref(),
Some("../../etc/passwd"),
"expected full path to survive redaction for {err:?}"
);
}
}
#[test]
fn test_host_path_variants_follow_profile_policy() {
let host_path = PathBuf::from("/srv/internal/app/x.txt");
let host_derived = [
ArchiveError::SourceNotFound {
path: host_path.clone(),
},
ArchiveError::SourceNotAccessible {
path: host_path.clone(),
error: std::io::Error::from(std::io::ErrorKind::PermissionDenied),
},
ArchiveError::OutputExists {
path: host_path.clone(),
},
ArchiveError::UnknownFormat { path: host_path },
];
for err in host_derived {
let redacted = err.redacted_path().expect("variant carries a path");
assert!(
redacted.ends_with("x.txt"),
"expected filename to survive redaction for {err:?}, got {redacted:?}"
);
#[cfg(not(debug_assertions))]
assert!(
!redacted.contains("/srv/internal"),
"expected host directory to be redacted for {err:?}, got {redacted:?}"
);
}
}
#[test]
fn test_variants_without_path_return_none() {
let no_path = [
ArchiveError::ZipBomb(ZipBombDetails::new(
ZipBombScope::Entry,
Compressed(100),
Uncompressed(100_000),
)),
ArchiveError::unclassified("bad header"),
ArchiveError::from(Violation::Encrypted {
reason: crate::EncryptionRefusal::PasswordRequired,
name: None,
}),
ArchiveError::InvalidCompressionLevel {
level: crate::RequestedLevel::Integer(0),
},
ArchiveError::InvalidConfiguration {
setting: crate::ConfigSetting::OutputPath,
problem: crate::ConfigProblem::NotSet,
},
ArchiveError::Io(std::io::Error::other("test")),
];
for err in no_path {
assert_eq!(err.redacted_path(), None, "expected no path for {err:?}");
}
}
#[test]
fn test_partial_extraction_delegates_to_source() {
let err = ArchiveError::PartialExtraction(crate::Partial::new(
ArchiveError::PathTraversal {
path: PathBuf::from("../../etc/passwd"),
},
ExtractionReport::new(),
crate::PartialWrites::Kept,
));
assert_eq!(err.redacted_path().as_deref(), Some("../../etc/passwd"));
}
}