use std::path::{Component, Path, PathBuf};
use crate::error::{BlobError, Result};
pub(crate) fn sanitize_rel_path(path: &str) -> Result<PathBuf> {
if path.is_empty() {
return Err(BlobError::Protocol("empty entry path".into()));
}
let p = Path::new(path);
let mut out = PathBuf::new();
for comp in p.components() {
match comp {
Component::Normal(c) => out.push(c),
other => {
return Err(BlobError::Protocol(format!(
"unsafe path component {other:?} in entry path {path:?}"
)));
}
}
}
if out.as_os_str().is_empty() {
return Err(BlobError::Protocol(format!("empty entry path {path:?}")));
}
Ok(out)
}
pub(crate) fn sanitize_symlink_target(link_rel: &Path, target: &str) -> Result<()> {
let t = Path::new(target);
if t.is_absolute() || t.components().any(|c| matches!(c, Component::Prefix(_))) {
return Err(BlobError::Protocol(format!(
"absolute symlink target {target:?}"
)));
}
let mut depth: i64 = link_rel.components().count() as i64 - 1;
for comp in t.components() {
match comp {
Component::Normal(_) => depth += 1,
Component::ParentDir => {
depth -= 1;
if depth < 0 {
return Err(BlobError::Protocol(format!(
"symlink target {target:?} escapes the tree root"
)));
}
}
Component::CurDir => {}
other => {
return Err(BlobError::Protocol(format!(
"unsafe symlink target component {other:?}"
)));
}
}
}
Ok(())
}
pub(crate) fn assert_parent_within(canonical_root: &Path, path: &Path) -> Result<()> {
let parent = path.parent().unwrap_or(canonical_root);
let canon = parent.canonicalize()?;
if !canon.starts_with(canonical_root) {
return Err(BlobError::Protocol(format!(
"entry path {path:?} resolves outside the destination root"
)));
}
Ok(())
}
pub(crate) fn validate_query_prefix(prefix: &str) -> Result<()> {
if prefix.is_empty() {
return Err(BlobError::Protocol("empty key prefix".into()));
}
if prefix.starts_with('/') || prefix.ends_with('/') {
return Err(BlobError::Protocol(format!(
"key prefix {prefix:?} must not start or end with '/'"
)));
}
if prefix.split('/').any(|seg| seg == "**") {
return Err(BlobError::Protocol(format!(
"key prefix {prefix:?} must not contain '**' — a blob id is resolved \
positionally, so no server can answer past an unbounded span"
)));
}
zenoh::key_expr::KeyExpr::try_from(prefix)
.map_err(|e| BlobError::Protocol(format!("invalid key prefix {prefix:?}: {e}")))?;
zenoh::key_expr::KeyExpr::try_from(format!("{prefix}/id/**"))
.map_err(|e| BlobError::Protocol(format!("unusable key prefix {prefix:?}: {e}")))?;
Ok(())
}
pub(crate) fn validate_serve_prefix(prefix: &str) -> Result<()> {
validate_query_prefix(prefix)?;
if prefix.split('/').any(|seg| seg.contains('*')) {
return Err(BlobError::Protocol(format!(
"key prefix {prefix:?} must not contain wildcards when serving or publishing"
)));
}
zenoh::key_expr::KeyExpr::try_from(format!("{prefix}/**"))
.map_err(|e| BlobError::Protocol(format!("undeclarable key prefix {prefix:?}: {e}")))?;
Ok(())
}
pub(crate) fn create_dir_confined(root: &Path, rel: &Path) -> Result<std::path::PathBuf> {
let mut cur = root.to_path_buf();
for comp in rel.components() {
cur.push(comp);
match std::fs::symlink_metadata(&cur) {
Ok(meta) if meta.file_type().is_symlink() => {
return Err(BlobError::Protocol(format!(
"refusing to traverse symlink at {cur:?} while materializing {rel:?}"
)));
}
Ok(meta) if meta.is_dir() => {}
Ok(_) => {
return Err(BlobError::Protocol(format!(
"non-directory in the way at {cur:?} while materializing {rel:?}"
)));
}
Err(_) => {
std::fs::create_dir(&cur)?;
}
}
}
Ok(cur)
}
#[cfg(unix)]
pub(crate) fn fsync_dir(dir: &Path) -> std::io::Result<()> {
std::fs::File::open(dir)?.sync_all()
}
#[cfg(not(unix))]
pub(crate) fn fsync_dir(_dir: &Path) -> std::io::Result<()> {
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rel_paths_sanitized() {
assert_eq!(
sanitize_rel_path("a/b/c.txt").unwrap(),
Path::new("a/b/c.txt")
);
assert_eq!(sanitize_rel_path("x").unwrap(), Path::new("x"));
for bad in ["", "/etc/passwd", "../x", "a/../../x", "a/..", ".", "./"] {
assert!(sanitize_rel_path(bad).is_err(), "should reject {bad:?}");
}
}
#[test]
fn key_prefixes_validated() {
for good in [
"demo/blobs",
"v1/h-0011223344ff/@blob/artifact",
"a",
"a/b/c",
] {
validate_query_prefix(good).unwrap_or_else(|e| panic!("{good:?} rejected: {e}"));
validate_serve_prefix(good).unwrap_or_else(|e| panic!("{good:?} rejected: {e}"));
}
for bad in ["", "/leading", "trailing/", "a//b"] {
assert!(
validate_query_prefix(bad).is_err(),
"{bad:?} should be rejected"
);
assert!(
validate_serve_prefix(bad).is_err(),
"{bad:?} should be rejected"
);
}
for wild in ["v1/*/@blob/artifact", "wild/*/card", "v1/host$*/x"] {
validate_query_prefix(wild)
.unwrap_or_else(|e| panic!("{wild:?} must be queryable: {e}"));
assert!(
validate_serve_prefix(wild).is_err(),
"{wild:?} must not be servable"
);
}
for span in ["wild/**", "**", "a/**/b"] {
assert!(
validate_query_prefix(span).is_err(),
"{span:?} must not be queryable"
);
assert!(
validate_serve_prefix(span).is_err(),
"{span:?} must not be servable"
);
assert_eq!(
crate::parse_id(span, &format!("{span}/A/manifest")),
None,
"parse_id must not resolve an id past '**'"
);
}
}
#[cfg(unix)]
#[test]
fn confined_dir_creation_refuses_symlinks() {
let root = tempfile::tempdir().unwrap();
let leaf = create_dir_confined(root.path(), Path::new("a/b/c")).unwrap();
assert!(leaf.is_dir());
std::os::unix::fs::symlink(root.path().join("a"), root.path().join("link")).unwrap();
assert!(create_dir_confined(root.path(), Path::new("link/x")).is_err());
std::fs::write(root.path().join("f"), b"x").unwrap();
assert!(create_dir_confined(root.path(), Path::new("f/child")).is_err());
}
#[test]
fn symlink_targets_checked() {
let link = Path::new("sub/link");
assert!(sanitize_symlink_target(link, "hello.txt").is_ok());
assert!(sanitize_symlink_target(link, "../big.bin").is_ok()); assert!(sanitize_symlink_target(link, "../../evil").is_err()); assert!(sanitize_symlink_target(link, "/etc/passwd").is_err());
let root_link = Path::new("link");
assert!(sanitize_symlink_target(root_link, "file").is_ok());
assert!(sanitize_symlink_target(root_link, "../x").is_err());
}
}