Resolve a relative path to an absolute path within the root. This method sanitizes the path to prevent directory traversal attacks. # Errors Returns an error if the path would escape the root.
(&self, relative: &str)
| 68 | /// |
| 69 | /// Returns an error if the path would escape the root. |
| 70 | pub fn resolve_path(&self, relative: &str) -> io::Result<PathBuf> { |
| 71 | let path = self.root.join(relative); |
| 72 | |
| 73 | // Normalize the path to resolve .. and . |
| 74 | let normalized = normalize_path(&path); |
| 75 | |
| 76 | // Ensure the normalized path is still under root |
| 77 | if !normalized.starts_with(&self.root) { |
| 78 | return Err(io::Error::new( |
| 79 | io::ErrorKind::InvalidInput, |
| 80 | format!( |
| 81 | "Path '{}' escapes root directory '{}'", |
| 82 | relative, |
| 83 | self.root.display() |
| 84 | ), |
| 85 | )); |
| 86 | } |
| 87 | |
| 88 | Ok(normalized) |
| 89 | } |
| 90 | |
| 91 | /// Check if a path exists. |
| 92 | pub fn exists(&self, path: &str) -> bool { |