use std::collections::HashSet;
use std::path::Path;
use std::path::PathBuf;
use crate::SecurityConfig;
use crate::formats::common::is_extension_allowed;
use crate::inspection::manifest::ArchiveEntry;
use crate::inspection::manifest::ArchiveManifest;
use crate::inspection::manifest::LinkTarget;
use crate::inspection::manifest::ManifestEntryKind;
use crate::security::fs_view::CollisionRecording;
use crate::security::fs_view::LandingLimits;
use crate::security::fs_view::Node;
use crate::security::fs_view::NodeBudget;
use crate::security::fs_view::PathModel;
use crate::security::fs_view::VirtualRoot;
use crate::security::fs_view::VirtualTree;
use crate::types::SafePath;
use crate::types::SafeSymlink;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct LandingPath(PathBuf);
impl LandingPath {
#[must_use]
pub fn as_path(&self) -> &Path {
&self.0
}
#[must_use]
pub fn into_path_buf(self) -> PathBuf {
self.0
}
}
impl AsRef<Path> for LandingPath {
fn as_ref(&self) -> &Path {
&self.0
}
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub enum LandingKind<'a> {
Entry(&'a ArchiveEntry),
ImplicitParent,
}
#[derive(Debug, Clone)]
pub struct Landing<'a> {
path: LandingPath,
kind: LandingKind<'a>,
}
impl<'a> Landing<'a> {
#[must_use]
pub const fn path(&self) -> &LandingPath {
&self.path
}
#[must_use]
pub const fn kind(&self) -> LandingKind<'a> {
self.kind
}
}
impl ArchiveManifest {
pub fn landing_paths<'a>(
&'a self,
config: &SecurityConfig,
) -> impl Iterator<Item = (&'a ArchiveEntry, LandingPath)> + use<'a> {
let root = VirtualRoot::new();
let mut tree = VirtualTree::new(
&root,
PathModel::Host,
NodeBudget::from_config(config),
CollisionRecording::Discard,
);
let landed: Vec<_> = self
.entries()
.iter()
.filter_map(|entry| {
land(entry, config, &root, &mut tree).map(|landing| (entry, landing))
})
.collect();
landed.into_iter()
}
pub fn landings<'a>(
&'a self,
config: &SecurityConfig,
) -> impl Iterator<Item = Landing<'a>> + use<'a> {
let mut seen = HashSet::new();
let mut out = Vec::new();
for (entry, landing) in self.landing_paths(config) {
let ancestors: Vec<_> = landing
.as_path()
.ancestors()
.skip(1)
.filter(|p| !p.as_os_str().is_empty())
.map(Path::to_path_buf)
.collect();
for parent in ancestors.into_iter().rev() {
if seen.insert(parent.clone()) {
out.push(Landing {
path: LandingPath(parent),
kind: LandingKind::ImplicitParent,
});
}
}
seen.insert(landing.as_path().to_path_buf());
out.push(Landing {
path: landing,
kind: LandingKind::Entry(entry),
});
}
out.into_iter()
}
}
fn land(
entry: &ArchiveEntry,
config: &SecurityConfig,
root: &VirtualRoot,
tree: &mut VirtualTree,
) -> Option<LandingPath> {
let is_file = matches!(entry.kind, ManifestEntryKind::File { .. });
if is_file && !is_extension_allowed(&entry.path, config) {
return None;
}
let safe = SafePath::validate_in(&entry.path, root, config, tree).ok()?;
let node = match &entry.kind {
ManifestEntryKind::File { .. } => Node::File,
ManifestEntryKind::Hardlink { .. } => {
if !config.allowed.hardlinks.is_allowed() {
return None;
}
Node::File
}
ManifestEntryKind::Directory => Node::Dir,
ManifestEntryKind::Symlink { target } => {
if !config.allowed.symlinks.is_allowed() {
return None;
}
match target {
LinkTarget::Recorded(t) => {
SafeSymlink::validate_in(&safe, t, root, config, tree).ok()?;
Node::Symlink(t.clone())
}
LinkTarget::Unread => Node::OpaqueSymlink,
LinkTarget::Missing | LinkTarget::Unreadable | LinkTarget::Oversized { .. } => {
return None;
}
}
}
};
let landed = tree
.project_landing(&safe, node, LandingLimits::Enforced)
.ok()?;
Some(LandingPath(landed.into_path_buf()))
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use std::collections::BTreeSet;
use std::io::Cursor;
use std::io::Write as _;
use exarch_testkit::raw_zip::RawEntry;
use exarch_testkit::raw_zip::RawZip;
use super::*;
use crate::extract_archive;
use crate::list_archive;
fn config() -> SecurityConfig {
SecurityConfig::default()
.with_allow_absolute_paths(true)
.with_allow_symlinks(true)
}
fn tar_entry(
builder: &mut tar::Builder<Vec<u8>>,
name: &[u8],
kind: tar::EntryType,
link: &str,
) {
let mut header = tar::Header::new_gnu();
header.set_entry_type(kind);
header.set_mode(0o644);
header.set_size(u64::from(kind == tar::EntryType::Regular));
header.as_old_mut().name[..name.len()].copy_from_slice(name);
if !link.is_empty() {
header.set_link_name(link).unwrap();
}
header.set_cksum();
let data: &[u8] = if kind == tar::EntryType::Regular {
b"x"
} else {
b""
};
builder.append(&header, data).unwrap();
}
fn non_directories(root: &Path) -> BTreeSet<PathBuf> {
let mut found = BTreeSet::new();
let mut stack = vec![root.to_path_buf()];
while let Some(dir) = stack.pop() {
for item in std::fs::read_dir(&dir).unwrap() {
let path = item.unwrap().path();
if std::fs::symlink_metadata(&path).unwrap().is_dir() {
stack.push(path);
} else {
found.insert(path.strip_prefix(root).unwrap().to_path_buf());
}
}
}
found
}
fn every_path(root: &Path) -> BTreeSet<PathBuf> {
let mut found = BTreeSet::new();
let mut stack = vec![root.to_path_buf()];
while let Some(dir) = stack.pop() {
for item in std::fs::read_dir(&dir).unwrap() {
let path = item.unwrap().path();
if std::fs::symlink_metadata(&path).unwrap().is_dir() {
stack.push(path.clone());
}
found.insert(path.strip_prefix(root).unwrap().to_path_buf());
}
}
found
}
fn assert_landing_matches_extraction(file_name: &str, bytes: &[u8]) {
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join(file_name);
std::fs::write(&archive, bytes).unwrap();
let dest = dir.path().join("out");
std::fs::create_dir(&dest).unwrap();
let config = config();
let manifest = list_archive(&archive, &config).unwrap();
let landed: BTreeSet<PathBuf> = manifest
.landing_paths(&config)
.filter(|(entry, _)| !matches!(entry.kind, ManifestEntryKind::Directory))
.map(|(_, landing)| landing.into_path_buf())
.collect();
extract_archive(&archive, &dest, &config).unwrap();
assert_eq!(landed, non_directories(&dest), "{file_name}");
let every: BTreeSet<PathBuf> = manifest
.landings(&config)
.map(|landing| landing.path().as_path().to_path_buf())
.collect();
assert_eq!(every, every_path(&dest), "{file_name}: implicit parents");
let implicit = manifest
.landings(&config)
.filter(|l| matches!(l.kind(), LandingKind::ImplicitParent))
.count();
assert!(implicit > 0, "{file_name}: the fixture implies a parent");
assert!(!landed.is_empty(), "{file_name}");
}
#[test]
fn tar_landing_matches_extraction_for_dot_slash_absolute_and_linked_parents() {
let mut builder = tar::Builder::new(Vec::new());
tar_entry(&mut builder, b"./a.txt", tar::EntryType::Regular, "");
tar_entry(&mut builder, b"/abs.txt", tar::EntryType::Regular, "");
tar_entry(&mut builder, b"d/", tar::EntryType::Directory, "");
tar_entry(&mut builder, b"d/sub.txt", tar::EntryType::Regular, "");
#[cfg(unix)]
{
tar_entry(&mut builder, b"link", tar::EntryType::Symlink, "d");
tar_entry(
&mut builder,
b"link/through.txt",
tar::EntryType::Regular,
"",
);
}
tar_entry(&mut builder, b"x/y/z.txt", tar::EntryType::Regular, "");
assert_landing_matches_extraction("a.tar", &builder.into_inner().unwrap());
}
#[test]
fn a_path_through_a_symlink_lands_at_the_resolved_path() {
let mut builder = tar::Builder::new(Vec::new());
tar_entry(&mut builder, b"d/", tar::EntryType::Directory, "");
tar_entry(&mut builder, b"link", tar::EntryType::Symlink, "d");
tar_entry(&mut builder, b"link/x.txt", tar::EntryType::Regular, "");
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("a.tar");
std::fs::write(&archive, builder.into_inner().unwrap()).unwrap();
let config = config();
let manifest = list_archive(&archive, &config).unwrap();
let landed: Vec<_> = manifest
.landing_paths(&config)
.map(|(_, l)| l.into_path_buf())
.collect();
assert_eq!(
landed,
[
PathBuf::from("d"),
PathBuf::from("link"),
PathBuf::from("d/x.txt")
]
);
}
#[test]
fn zip_landing_matches_extraction_for_backslashes_and_drive_prefixes() {
let entries = [
"a\\b.txt",
"C:\\win.txt",
"/abs.txt",
"plain.txt",
"dir/",
"p/q/r.txt",
]
.iter()
.map(|name| RawEntry::stored(name, if name.ends_with('/') { b"" } else { b"x" }))
.collect();
let bytes = RawZip::new(entries).build().bytes;
assert_landing_matches_extraction("a.zip", &bytes);
}
#[test]
fn sevenz_landing_matches_extraction() {
let mut writer = sevenz_rust2::ArchiveWriter::new(Cursor::new(Vec::new())).unwrap();
for name in ["a.txt", "dir/b.txt", "dir/deeper/c.txt"] {
writer
.push_archive_entry(
sevenz_rust2::ArchiveEntry::new_file(name),
Some(Cursor::new(b"x".to_vec())),
)
.unwrap();
}
let bytes = writer.finish().unwrap().into_inner();
assert_landing_matches_extraction("a.7z", &bytes);
}
#[test]
fn refused_and_skipped_entries_do_not_land() {
let mut builder = tar::Builder::new(Vec::new());
tar_entry(&mut builder, b"ok.txt", tar::EntryType::Regular, "");
tar_entry(&mut builder, b"skip.bin", tar::EntryType::Regular, "");
tar_entry(
&mut builder,
b"esc",
tar::EntryType::Symlink,
"../../outside",
);
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("a.tar");
std::fs::write(&archive, builder.into_inner().unwrap()).unwrap();
let config = config().try_add_allowed_extension("txt").unwrap();
let manifest = list_archive(&archive, &config).unwrap();
let landed: Vec<_> = manifest
.landing_paths(&config)
.map(|(_, l)| l.into_path_buf())
.collect();
assert_eq!(landed, [PathBuf::from("ok.txt")]);
}
#[test]
fn disallowed_links_do_not_land() {
let mut builder = tar::Builder::new(Vec::new());
tar_entry(&mut builder, b"s", tar::EntryType::Symlink, "ok.txt");
tar_entry(&mut builder, b"h", tar::EntryType::Link, "ok.txt");
let _ = builder.get_mut().flush();
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("a.tar");
std::fs::write(&archive, builder.into_inner().unwrap()).unwrap();
let config = config();
let manifest = list_archive(&archive, &config).unwrap();
let strict = SecurityConfig::default();
assert_eq!(manifest.landing_paths(&strict).count(), 0);
}
#[test]
fn implicit_parents_are_reported_once_outermost_first() {
let mut builder = tar::Builder::new(Vec::new());
tar_entry(&mut builder, b"first.txt", tar::EntryType::Regular, "");
tar_entry(&mut builder, b"a/b/c.txt", tar::EntryType::Regular, "");
tar_entry(&mut builder, b"a/d.txt", tar::EntryType::Regular, "");
let dir = tempfile::tempdir().unwrap();
let archive = dir.path().join("a.tar");
std::fs::write(&archive, builder.into_inner().unwrap()).unwrap();
let config = config();
let manifest = list_archive(&archive, &config).unwrap();
let shape: Vec<_> = manifest
.landings(&config)
.map(|l| {
(
l.path().as_path().to_path_buf(),
matches!(l.kind(), LandingKind::ImplicitParent),
)
})
.collect();
assert_eq!(
shape,
[
(PathBuf::from("first.txt"), false),
(PathBuf::from("a"), true),
(PathBuf::from("a/b"), true),
(PathBuf::from("a/b/c.txt"), false),
(PathBuf::from("a/d.txt"), false),
]
);
}
}