smriti/services/
path_util.rs1use std::path::{Component, Path, PathBuf};
9
10pub fn relative_path_for_storage(p: &Path) -> String {
17 p.to_string_lossy().replace("\\", "/")
18}
19
20pub fn safe_join_relative(root: &Path, stored_relative: &str) -> Result<PathBuf, String> {
25 if stored_relative.is_empty() {
26 return Err("path is empty".to_string());
27 }
28 if stored_relative.contains('\\') {
29 return Err("path must use normalized forward slashes".to_string());
30 }
31
32 let rel = Path::new(stored_relative);
33 let mut clean = PathBuf::new();
34 for component in rel.components() {
35 match component {
36 Component::Normal(part) => clean.push(part),
37 Component::CurDir => {}
38 Component::ParentDir => return Err("path contains parent traversal".to_string()),
39 Component::RootDir | Component::Prefix(_) => {
40 return Err("path must be relative".to_string())
41 }
42 }
43 }
44
45 if clean.as_os_str().is_empty() {
46 return Err("path is empty".to_string());
47 }
48 Ok(crate::services::cache_location::resolve(root, &clean))
50}
51
52pub fn safe_existing_path_under_root(
55 root: &Path,
56 stored_relative: &str,
57) -> Result<PathBuf, String> {
58 let joined = safe_join_relative(root, stored_relative)?;
59 let local =
61 crate::services::cache_location::local_base(root).and_then(|base| base.canonicalize().ok());
62 let root = root
63 .canonicalize()
64 .map_err(|e| format!("failed to canonicalize root: {e}"))?;
65 let resolved = joined
66 .canonicalize()
67 .map_err(|e| format!("failed to canonicalize path: {e}"))?;
68 if !resolved.starts_with(&root) && !local.is_some_and(|l| resolved.starts_with(l)) {
69 return Err("path resolves outside library root".to_string());
70 }
71 Ok(resolved)
72}
73
74#[cfg(test)]
75mod tests {
76 use super::*;
77 use std::path::PathBuf;
78
79 #[test]
80 fn forward_slashes_pass_through() {
81 let s = relative_path_for_storage(&PathBuf::from("subdir/photo.jpg"));
82 assert_eq!(s, "subdir/photo.jpg");
83 }
84
85 #[test]
86 fn backslashes_become_forward_slashes() {
87 let s = relative_path_for_storage(Path::new(r"subdir\photo.jpg"));
90 assert_eq!(s, "subdir/photo.jpg");
91 }
92
93 #[test]
94 fn mixed_separators_normalize() {
95 let s = relative_path_for_storage(Path::new(r"a\b/c\d.jpg"));
96 assert_eq!(s, "a/b/c/d.jpg");
97 }
98
99 #[test]
100 fn safe_join_accepts_normal_relative_paths() {
101 let joined = safe_join_relative(Path::new("/photos"), "2026/IMG_001.jpg").unwrap();
102 assert_eq!(joined, PathBuf::from("/photos/2026/IMG_001.jpg"));
103 }
104
105 #[test]
106 fn safe_join_rejects_escape_paths() {
107 assert!(safe_join_relative(Path::new("/photos"), "../secret").is_err());
108 assert!(safe_join_relative(Path::new("/photos"), "/etc/passwd").is_err());
109 assert!(safe_join_relative(Path::new("/photos"), r"..\secret").is_err());
110 }
111
112 #[test]
113 fn safe_existing_path_rejects_symlink_escape() {
114 let root = tempfile::tempdir().unwrap();
115 let outside = tempfile::tempdir().unwrap();
116 let outside_file = outside.path().join("secret.jpg");
117 std::fs::write(&outside_file, b"secret").unwrap();
118
119 #[cfg(unix)]
120 {
121 std::os::unix::fs::symlink(outside.path(), root.path().join("link")).unwrap();
122 assert!(safe_existing_path_under_root(root.path(), "link/secret.jpg").is_err());
123 }
124
125 #[cfg(windows)]
126 {
127 if std::os::windows::fs::symlink_dir(outside.path(), root.path().join("link")).is_ok() {
128 assert!(safe_existing_path_under_root(root.path(), "link/secret.jpg").is_err());
129 }
130 }
131 }
132}