use anyhow::Context;
use hyper::StatusCode;
use percent_encoding::percent_decode_str;
use std::ffi::OsStr;
use std::path::{Component, Path, PathBuf};
use crate::Result;
pub(crate) trait PathExt {
fn is_hidden(&self) -> bool;
fn contains_symlink(&self, base: &Path) -> Result<bool>;
fn contains_short_name_alias(&self, base: &Path) -> Result<bool>;
}
impl PathExt for Path {
fn is_hidden(&self) -> bool {
self.components()
.filter_map(|cmp| match cmp {
Component::Normal(s) => s.to_str(),
_ => None,
})
.any(|s| s.starts_with('.'))
}
fn contains_symlink(&self, base: &Path) -> Result<bool> {
let mut current = base.to_path_buf();
current.reserve(self.as_os_str().len());
for component in self.components() {
match component {
Component::Normal(c) => {
current.push(c);
let meta = std::fs::symlink_metadata(¤t).with_context(|| {
format!("unable to get metadata for path '{}'", current.display())
})?;
if meta.file_type().is_symlink() {
return Ok(true);
}
}
Component::CurDir => {}
Component::Prefix(_) | Component::RootDir | Component::ParentDir => {
tracing::debug!(
"dir: skipping segment containing invalid prefix, dots or backslashes"
);
}
}
}
Ok(false)
}
fn contains_short_name_alias(&self, base: &Path) -> Result<bool> {
let mut dir = base.to_path_buf();
dir.reserve(self.as_os_str().len());
for component in self.components() {
let Component::Normal(name) = component else {
continue;
};
if name.as_encoded_bytes().contains(&b'~') && !has_dir_entry(&dir, name)? {
return Ok(true);
}
dir.push(name);
}
Ok(false)
}
}
fn has_dir_entry(dir: &Path, name: &OsStr) -> Result<bool> {
let lowercase = name.to_str().map(|name| {
name.chars()
.flat_map(char::to_lowercase)
.collect::<String>()
});
let entries = std::fs::read_dir(dir)
.with_context(|| format!("unable to read directory '{}'", dir.display()))?;
for entry in entries {
let entry =
entry.with_context(|| format!("unable to read entry of '{}'", dir.display()))?;
let entry_name = entry.file_name();
if entry_name == name || is_lowercase_of(&entry_name, lowercase.as_deref()) {
return Ok(true);
}
}
Ok(false)
}
fn is_lowercase_of(name: &OsStr, lowercase: Option<&str>) -> bool {
match (name.to_str(), lowercase) {
(Some(name), Some(lowercase)) => name
.chars()
.flat_map(char::to_lowercase)
.eq(lowercase.chars()),
_ => false,
}
}
#[cfg(unix)]
fn path_from_bytes(bytes: &[u8]) -> PathBuf {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
OsStr::from_bytes(bytes).into()
}
#[cfg(windows)]
fn path_from_bytes(bytes: &[u8]) -> PathBuf {
String::from_utf8_lossy(bytes).into_owned().into()
}
fn decode_tail_path(tail: &str) -> PathBuf {
let bytes = percent_decode_str(tail.trim_start_matches('/')).collect::<Vec<_>>();
path_from_bytes(&bytes)
}
pub(crate) fn sanitize_path(base: &Path, tail: &str) -> Result<PathBuf, StatusCode> {
let path_decoded = decode_tail_path(tail);
let mut full_path = base.to_path_buf();
tracing::trace!("dir: base={:?}, route={:?}", full_path, path_decoded);
for component in path_decoded.components() {
match component {
Component::Normal(comp) => {
if Path::new(&comp)
.components()
.all(|c| matches!(c, Component::Normal(_)))
{
full_path.push(comp)
} else {
tracing::debug!("dir: skipping segment with invalid prefix");
}
}
Component::CurDir => {}
Component::Prefix(_) | Component::RootDir | Component::ParentDir => {
tracing::debug!(
"dir: skipping segment containing invalid prefix, dots or backslashes"
);
}
}
}
Ok(full_path)
}
#[doc(hidden)]
#[must_use]
pub fn is_path_within_base(path: &Path, base_path: &Path) -> bool {
let Ok(resolved) = path.canonicalize() else {
return false;
};
if resolved.starts_with(base_path) {
return true;
}
let Ok(base_resolved) = base_path.canonicalize() else {
return false;
};
resolved.starts_with(base_resolved)
}
#[cfg(test)]
mod tests {
use super::{PathExt, is_path_within_base, sanitize_path};
use std::path::{Path, PathBuf};
fn root_dir() -> PathBuf {
PathBuf::from("docker/public/")
}
#[test]
fn test_sanitize_path() {
let base_dir = &PathBuf::from("docker/public");
assert_eq!(
sanitize_path(base_dir, "/index.html").unwrap(),
root_dir().join("index.html")
);
assert_eq!(
sanitize_path(base_dir, "/../foo.html").unwrap(),
root_dir().join("foo.html"),
);
assert_eq!(
sanitize_path(base_dir, "/../W�foo.html").unwrap(),
root_dir().join("W�foo.html"),
);
assert_eq!(
sanitize_path(base_dir, "/%EF%BF%BD/../bar.html").unwrap(),
root_dir().join("�/bar.html"),
);
assert_eq!(
sanitize_path(base_dir, "à Ã/é%20/öüñ").unwrap(),
root_dir().join("à Ã/é /öüñ"),
);
#[cfg(unix)]
let expected_path = root_dir().join("C:\\/foo.html");
#[cfg(windows)]
let expected_path = PathBuf::from("docker/public/\\foo.html");
assert_eq!(
sanitize_path(base_dir, "/C:\\/foo.html").unwrap(),
expected_path
);
}
#[test]
fn is_path_within_base_rejects_outside_symlink() {
let base = PathBuf::from("tests/fixtures/public");
let outside = base.join("readme.md");
assert!(
!is_path_within_base(&outside, &base),
"outside-root symlink must not be treated as within base"
);
}
#[test]
fn is_path_within_base_accepts_inside_file() {
let base = PathBuf::from("tests/fixtures/public");
let inside = base.join("index.htm");
assert!(
is_path_within_base(&inside, &base),
"in-root file must be treated as within base"
);
}
#[test]
fn test_contains_symlink_returns_false() {
let base = PathBuf::from("tests/fixtures/public");
let user_path = PathBuf::from("./index.htm");
match user_path.contains_symlink(&base) {
Ok(contains) => assert!(!contains),
Err(err) => panic!("unexpected error when checking for symlinks: {err}"),
}
}
#[test]
fn test_contains_symlink_returns_true() {
let base = PathBuf::from("tests/fixtures/public");
let user_path = PathBuf::from("./readme.md");
match user_path.contains_symlink(&base) {
Ok(contains) => assert!(contains),
Err(err) => panic!("unexpected error when checking for symlinks: {err}"),
}
}
#[test]
fn test_contains_symlink_returns_error() {
let base = PathBuf::from("tests/fixtures/public");
let user_path = PathBuf::from("./unknown_file.txt");
match user_path.contains_symlink(&base) {
Ok(_) => panic!("expected error when checking for symlinks on non-existent path"),
Err(err) => assert!(
err.to_string().contains("unable to get metadata for path"),
"unexpected error message: {err}"
),
}
}
fn short_name_fixture() -> tempfile::TempDir {
let base = tempfile::tempdir().unwrap();
let root = base.path();
std::fs::create_dir_all(root.join(".git")).unwrap();
std::fs::create_dir_all(root.join("a~b/.env")).unwrap();
std::fs::write(root.join(".git/config"), "config").unwrap();
std::fs::write(root.join("report~1.txt"), "report").unwrap();
base
}
#[test]
fn contains_short_name_alias_detects_alias_of_hidden_dir() {
let base = short_name_fixture();
for path in ["GIT~1/config", "git~1", "a~b/ENV~1"] {
assert!(
Path::new(path)
.contains_short_name_alias(base.path())
.unwrap(),
"{path} must be reported as a short-name alias"
);
}
}
#[test]
fn contains_short_name_alias_accepts_names_with_tilde() {
let base = short_name_fixture();
for path in ["report~1.txt", "REPORT~1.TXT", "a~b", "a~b/.env"] {
assert!(
!Path::new(path)
.contains_short_name_alias(base.path())
.unwrap(),
"{path} is an entry name, not an alias"
);
}
}
#[test]
fn contains_short_name_alias_skips_names_without_tilde() {
let base = short_name_fixture();
assert!(
!Path::new("missing/dir/file.txt")
.contains_short_name_alias(base.path())
.unwrap()
);
}
#[test]
fn contains_short_name_alias_errors_on_missing_dir() {
let base = short_name_fixture();
assert!(
Path::new("missing/x~1")
.contains_short_name_alias(base.path())
.is_err()
);
}
#[cfg(unix)]
#[test]
fn contains_short_name_alias_skips_base_components() {
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt;
let parent = tempfile::tempdir().unwrap();
let base = parent.path().join("r~1");
std::fs::create_dir(&base).unwrap();
std::fs::set_permissions(parent.path(), Permissions::from_mode(0o311)).unwrap();
let result = Path::new("file.txt").contains_short_name_alias(&base);
std::fs::set_permissions(parent.path(), Permissions::from_mode(0o755)).unwrap();
assert!(!result.unwrap());
}
}