use std::path::{Component, Path, PathBuf};
#[derive(Debug)]
pub(crate) struct SymlinkResolution {
pub(crate) path: PathBuf,
pub(crate) error: Option<std::io::Error>,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub(crate) links: Vec<(PathBuf, PathBuf)>,
#[cfg_attr(not(target_os = "linux"), allow(dead_code))]
pub(crate) climbed_out_of: Vec<PathBuf>,
}
pub(crate) fn resolve_through_symlinks(path: &Path) -> Option<SymlinkResolution> {
if path.as_os_str().is_empty() {
return None;
}
let path = match std::path::absolute(path) {
Ok(path) => path,
Err(error) => {
return Some(SymlinkResolution {
path: path.to_path_buf(),
error: Some(error),
links: Vec::new(),
climbed_out_of: Vec::new(),
})
}
};
let mut result = PathBuf::new();
let mut failure = None;
let mut links = Vec::new();
let mut climbed_out_of = Vec::new();
let mut resolve_prefix = |prefix: &Path| {
let resolved = match std::fs::symlink_metadata(prefix) {
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return None,
Err(error) => Err(error),
Ok(metadata) => std::fs::metadata(prefix)
.and_then(|_| std::fs::canonicalize(prefix))
.map(|real| (real, metadata.file_type().is_symlink())),
};
match resolved {
Ok(resolved) => Some(resolved),
Err(error) => {
failure.get_or_insert(error);
None
}
}
};
for component in path.components() {
match component {
Component::Prefix(_) | Component::RootDir => result.push(component.as_os_str()),
Component::CurDir => {}
Component::ParentDir => {
let _ = resolve_prefix(&result.join(".."));
if result.parent().is_some() {
climbed_out_of.push(result.clone());
}
result.pop();
}
Component::Normal(name) => {
result.push(name);
if let Some((real, is_link)) = resolve_prefix(&result) {
let location = std::mem::replace(&mut result, real);
if is_link {
links.push((location, result.clone()));
}
}
}
}
}
Some(SymlinkResolution {
path: result,
error: failure,
links,
climbed_out_of,
})
}
pub(crate) fn is_policy_path_error(error: &std::io::Error) -> bool {
use std::io::ErrorKind;
matches!(
error.kind(),
ErrorKind::NotFound
| ErrorKind::NotADirectory
| ErrorKind::PermissionDenied
| ErrorKind::InvalidFilename
) || error.raw_os_error() == Some(libc::ELOOP)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_path_problems_blame_the_request() {
for errno in [libc::ENOENT, libc::ENOTDIR, libc::ELOOP, libc::EACCES] {
let error = std::io::Error::from_raw_os_error(errno);
assert!(is_policy_path_error(&error), "{error}");
}
for errno in [libc::EIO, libc::ESTALE] {
let error = std::io::Error::from_raw_os_error(errno);
assert!(!is_policy_path_error(&error), "{error}");
}
}
fn temp_root(dir: &tempfile::TempDir) -> PathBuf {
std::fs::canonicalize(dir.path()).expect("tempdir canonicalizes")
}
#[test]
fn a_leaf_symlink_resolves_to_its_target() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
let target = root.join("real.conf");
std::fs::write(&target, b"x").unwrap();
let link = root.join("link.conf");
std::os::unix::fs::symlink(&target, &link).unwrap();
let resolved = resolve_through_symlinks(&link).unwrap();
assert!(resolved.error.is_none(), "{resolved:?}");
assert_eq!(resolved.path, target);
}
#[test]
fn a_symlinked_ancestor_resolves() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
std::fs::create_dir_all(root.join("real/sub")).unwrap();
let file = root.join("real/sub/f.conf");
std::fs::write(&file, b"x").unwrap();
std::os::unix::fs::symlink(root.join("real"), root.join("link")).unwrap();
let resolved = resolve_through_symlinks(&root.join("link/sub/f.conf")).unwrap();
assert!(resolved.error.is_none(), "{resolved:?}");
assert_eq!(resolved.path, file);
}
#[test]
fn dotdot_folds_against_the_resolved_parent() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
std::fs::create_dir(root.join("real")).unwrap();
let sibling = root.join("sibling.conf");
std::fs::write(&sibling, b"x").unwrap();
std::os::unix::fs::symlink(root.join("real"), root.join("link")).unwrap();
let resolved = resolve_through_symlinks(&root.join("link/../sibling.conf")).unwrap();
assert!(resolved.error.is_none(), "{resolved:?}");
assert_eq!(resolved.path, sibling);
}
#[test]
fn a_missing_tail_keeps_the_resolved_ancestors() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
std::fs::create_dir(root.join("real")).unwrap();
std::os::unix::fs::symlink(root.join("real"), root.join("link")).unwrap();
let resolved = resolve_through_symlinks(&root.join("link/absent.conf")).unwrap();
assert!(resolved.error.is_none(), "{resolved:?}");
assert_eq!(resolved.path, root.join("real/absent.conf"));
}
#[test]
fn a_loop_yields_unresolved_text() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
let first = root.join("first");
let second = root.join("second");
std::os::unix::fs::symlink(&second, &first).unwrap();
std::os::unix::fs::symlink(&first, &second).unwrap();
let resolved = resolve_through_symlinks(&first).expect("a non-empty path resolves");
assert!(resolved.error.is_some());
assert!(
std::fs::symlink_metadata(&resolved.path)
.expect("the link node exists")
.file_type()
.is_symlink(),
"a loop leaves the result a symlink: {resolved:?}"
);
}
#[test]
fn missing_prefix_and_parent_segments_do_not_hide_broken_links() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
for (name, target) in [("loop", "loop"), ("dangling", "missing-target")] {
std::os::unix::fs::symlink(target, root.join(name)).unwrap();
let path = root.join("missing/..").join(name).join("../secret");
let resolved = resolve_through_symlinks(&path).unwrap();
assert!(resolved.error.is_some(), "{resolved:?}");
assert_eq!(resolved.path, root.join("secret"));
}
}
#[test]
fn parent_traversal_cannot_treat_a_file_as_a_directory() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
std::fs::write(root.join("file"), b"x").unwrap();
let resolved = resolve_through_symlinks(&root.join("file/../secret")).unwrap();
assert!(resolved.error.is_some(), "{resolved:?}");
assert_eq!(resolved.path, root.join("secret"));
}
#[test]
fn a_link_target_cannot_treat_a_file_as_a_directory() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
std::fs::write(root.join("file"), b"x").unwrap();
std::fs::write(root.join("secret"), b"x").unwrap();
std::os::unix::fs::symlink("file/../secret", root.join("link")).unwrap();
for path in [root.join("link"), root.join("link/child")] {
let resolved = resolve_through_symlinks(&path).unwrap();
assert!(resolved.error.is_some(), "{resolved:?}");
}
}
#[test]
fn an_empty_path_has_no_target() {
assert!(resolve_through_symlinks(Path::new("")).is_none());
}
#[test]
fn a_relative_path_starts_from_the_current_directory() {
let cwd = std::env::current_dir().unwrap();
let name = cwd.file_name().expect("the test runs in a named directory");
let resolved = resolve_through_symlinks(&Path::new("..").join(name)).unwrap();
assert!(resolved.error.is_none(), "{resolved:?}");
assert_eq!(resolved.path, std::fs::canonicalize(&cwd).unwrap());
}
#[test]
fn the_links_on_a_path_are_reported_with_their_real_paths() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
for directory in ["real/sub", "b", "c/x"] {
std::fs::create_dir_all(root.join(directory)).unwrap();
}
std::os::unix::fs::symlink(root.join("real"), root.join("alias")).unwrap();
std::os::unix::fs::symlink("alias", root.join("chain")).unwrap();
std::os::unix::fs::symlink(root.join("c"), root.join("b/inner")).unwrap();
std::os::unix::fs::symlink("../../b/inner/x", root.join("real/sub/outer")).unwrap();
let resolved = resolve_through_symlinks(&root.join("chain/sub/outer/missing")).unwrap();
assert!(resolved.error.is_none(), "{resolved:?}");
assert_eq!(resolved.path, root.join("c/x/missing"));
assert_eq!(
resolved.links,
[
(root.join("chain"), root.join("real")),
(root.join("real/sub/outer"), root.join("c/x")),
]
);
}
#[test]
fn the_directories_a_path_climbs_out_of_are_reported_with_their_real_paths() {
let dir = tempfile::tempdir().unwrap();
let root = temp_root(&dir);
std::fs::create_dir_all(root.join("real/sub")).unwrap();
std::os::unix::fs::symlink(root.join("real"), root.join("alias")).unwrap();
let resolved = resolve_through_symlinks(&root.join("alias/sub/../../other")).unwrap();
assert!(resolved.error.is_none(), "{resolved:?}");
assert_eq!(resolved.path, root.join("other"));
assert_eq!(
resolved.climbed_out_of,
[root.join("real/sub"), root.join("real")]
);
assert!(resolve_through_symlinks(Path::new("/.."))
.unwrap()
.climbed_out_of
.is_empty());
}
}