use anyhow::Result;
use std::path::{Path, PathBuf};
pub(crate) const SYSTEM_PROTECTED: &[&str] = &[
"/", "/home", "/etc", "/usr", "/var", "/boot", "/nix", "/run", "/sys", "/dev", "/proc",
];
pub(crate) fn check_safe_to_delete(path: &Path, user_protected: &[String]) -> Result<PathBuf> {
let canon = match path.canonicalize() {
Ok(p) => p,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return Ok(path.to_path_buf());
}
Err(e) => anyhow::bail!(
"cannot canonicalize {}: {} — refusing to delete",
path.display(),
e
),
};
if let Ok(meta) = std::fs::symlink_metadata(path) {
if meta.file_type().is_symlink() {
anyhow::bail!(
"refusing to delete symlink {} — use target directly",
path.display()
);
}
}
let canon_str = canon.display().to_string();
for prot in SYSTEM_PROTECTED {
if is_protected_ancestor(&canon_str, prot) {
anyhow::bail!(
"refusing to delete protected path {} (under system root {})",
canon.display(),
prot
);
}
}
for user_prot in user_protected {
let prot_canon = match Path::new(user_prot).canonicalize() {
Ok(p) => p.display().to_string(),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
Err(e) => anyhow::bail!(
"cannot canonicalize user-protected path {}: {} — refusing",
user_prot,
e
),
};
if is_protected_ancestor(&canon_str, &prot_canon) {
anyhow::bail!(
"refusing to delete protected path {} (under user-protected path {})",
canon.display(),
user_prot
);
}
}
Ok(canon)
}
pub(crate) const SAFE_TMP_ROOTS: &[&str] = &["/tmp"];
pub(crate) fn check_safe_tmp_root(path: &Path) -> Result<PathBuf> {
let metadata = std::fs::symlink_metadata(path).map_err(|e| {
anyhow::anyhow!(
"cannot inspect configured tmp root {}: {} — refusing to scan",
path.display(),
e
)
})?;
if metadata.file_type().is_symlink() {
anyhow::bail!(
"refusing configured tmp root {} because it is a symlink",
path.display()
);
}
if !metadata.is_dir() {
anyhow::bail!(
"refusing configured tmp root {} because it is not a directory",
path.display()
);
}
let canon = path.canonicalize().map_err(|e| {
anyhow::anyhow!(
"cannot canonicalize configured tmp root {}: {} — refusing to scan",
path.display(),
e
)
})?;
if !SAFE_TMP_ROOTS
.iter()
.any(|root| canon.starts_with(Path::new(root)))
{
anyhow::bail!(
"refusing configured tmp root {}: it must be /tmp (or a descendant)",
canon.display()
);
}
Ok(canon)
}
pub(crate) fn check_safe_to_delete_guard(
path: &Path,
user_protected: &[String],
) -> Result<PathBuf> {
let canon = match path.canonicalize() {
Ok(p) => p,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return Ok(path.to_path_buf());
}
Err(e) => anyhow::bail!(
"cannot canonicalize {}: {} — refusing to delete",
path.display(),
e
),
};
if let Ok(meta) = std::fs::symlink_metadata(path) {
if meta.file_type().is_symlink() {
anyhow::bail!(
"refusing to delete symlink {} — use target directly",
path.display()
);
}
}
let canon_str = canon.display().to_string();
for prot in SYSTEM_PROTECTED {
if canon_str == *prot {
anyhow::bail!(
"refusing guard cleanup of protected system path {}",
canon.display()
);
}
}
for user_prot in user_protected {
let prot_canon = match Path::new(user_prot).canonicalize() {
Ok(p) => p.display().to_string(),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
Err(e) => anyhow::bail!(
"cannot canonicalize user-protected path {}: {} — refusing",
user_prot,
e
),
};
if is_protected_ancestor(&canon_str, &prot_canon) {
anyhow::bail!(
"refusing to delete protected path {} (under user-protected path {})",
canon.display(),
user_prot
);
}
}
Ok(canon)
}
pub(crate) fn check_safe_to_delete_tmp_entry(
path: &Path,
tmp_root: &Path,
user_protected: &[String],
) -> Result<PathBuf> {
let canon = check_safe_to_delete_guard(path, user_protected)?;
if !canon.starts_with(tmp_root) {
anyhow::bail!(
"refusing to delete tmp entry {}: resolved path is outside validated tmp root {}",
canon.display(),
tmp_root.display()
);
}
Ok(canon)
}
pub(crate) fn is_protected_ancestor(path: &str, protected: &str) -> bool {
if path == protected {
return true;
}
if protected == "/" {
return path == "/";
}
let prefix = if protected.ends_with('/') {
protected.to_string()
} else {
format!("{}/", protected)
};
path.starts_with(&prefix)
}