use crate::security::permissions::SanitizedMode;
use crate::security::quota::EntryCount;
use crate::security::quota::QuotaPermit;
use crate::types::SafePath;
use crate::types::SafeSymlink;
#[derive(Debug, Clone, Copy)]
pub struct EntrySize {
pub uncompressed: u64,
pub compressed: Option<u64>,
}
#[derive(Debug)]
pub struct ValidatedFile {
pub path: SafePath,
pub permit: QuotaPermit,
pub mode: Option<SanitizedMode>,
}
#[derive(Debug)]
pub struct ValidatedSymlink {
pub link: SafePath,
pub symlink: SafeSymlink,
}
#[derive(Debug)]
pub struct ValidatedHardlink {
pub link: SafePath,
pub target: SafePath,
pub counted: EntryCount,
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use std::assert_matches;
use std::path::Path;
use tempfile::TempDir;
use super::*;
use crate::ArchiveError;
use crate::SecurityConfig;
use crate::security::fs_view::HostFs;
use crate::security::validator::ValidatorCore;
use crate::types::DestDir;
fn sizes(uncompressed: u64) -> EntrySize {
EntrySize {
uncompressed,
compressed: None,
}
}
#[test]
fn a_file_carries_its_path_and_sanitized_mode() {
let temp = TempDir::new().unwrap();
let dest = DestDir::new(temp.path().to_path_buf()).unwrap();
let config = SecurityConfig::default().validate().unwrap();
let mut core = ValidatorCore::new(&config);
let ctx = core.context::<HostFs>();
let file = core
.validate_file_in(
Path::new("a.txt"),
sizes(3),
Some(0o4755),
&dest,
&HostFs,
&ctx,
)
.unwrap();
assert_eq!(file.path.as_path(), Path::new("a.txt"));
assert_eq!(
file.mode.unwrap().as_u32() & 0o6000,
0,
"setuid is stripped"
);
}
#[test]
fn a_refused_file_yields_no_proof() {
let temp = TempDir::new().unwrap();
let dest = DestDir::new(temp.path().to_path_buf()).unwrap();
let config = SecurityConfig::default().validate().unwrap();
let mut core = ValidatorCore::new(&config);
let ctx = core.context::<HostFs>();
assert_matches!(
core.validate_file_in(Path::new("../x"), sizes(1), None, &dest, &HostFs, &ctx),
Err(ArchiveError::PathTraversal { .. })
);
}
#[test]
fn a_directory_symlink_and_hardlink_carry_their_validated_parts() {
let temp = TempDir::new().unwrap();
let dest = DestDir::new(temp.path().to_path_buf()).unwrap();
let config = SecurityConfig::default()
.with_allow_symlinks(true)
.with_allow_hardlinks(true)
.validate()
.unwrap();
let mut core = ValidatorCore::new(&config);
let ctx = core.context::<HostFs>();
let directory = core
.validate_directory_in(Path::new("d"), sizes(0), &dest, &HostFs, &ctx)
.unwrap();
assert_eq!(directory.as_path(), Path::new("d"));
let ctx = core.context::<HostFs>();
let symlink = core
.validate_symlink_in(
Path::new("s"),
Path::new("d"),
sizes(1),
&dest,
&HostFs,
&ctx,
)
.unwrap();
assert_eq!(symlink.link.as_path(), Path::new("s"));
assert_eq!(symlink.symlink.target_path(), Path::new("d"));
let ctx = core.context::<HostFs>();
let hardlink = core
.validate_hardlink_in(
Path::new("h"),
Path::new("t"),
sizes(0),
&dest,
&HostFs,
&ctx,
)
.unwrap();
assert_eq!(hardlink.link.as_path(), Path::new("h"));
assert_eq!(hardlink.target.as_path(), Path::new("t"));
}
}