pub fn validate_path(path: &Path, allowed_base: &Path) -> Result<PathBuf>Expand description
Validate file path and prevent path traversal attacks
Performs comprehensive path validation:
- Canonicalizes path (resolves symlinks, relative components)
- Validates against allowed base directory
- Checks component lengths
- Validates file name characters
§Security Considerations
- Prevents path traversal (../ attacks)
- Protects against symlink attacks
- Validates path components
- Ensures path stays within allowed directory
§Errors
Returns error if:
- Path does not exist (canonicalize fails)
- Path escapes allowed base directory
- Path components exceed length limits
- Path contains invalid characters
§Examples
use backup_suite::core::validation::validate_path;
use std::path::PathBuf;
use std::env;
let current_dir = env::current_dir().unwrap();
let safe_path = current_dir.join("backup/data.txt");
// Valid path within current directory
assert!(validate_path(&safe_path, ¤t_dir).is_ok());
// Invalid: path traversal attempt
let malicious = PathBuf::from("/tmp/../../etc/passwd");
assert!(validate_path(&malicious, ¤t_dir).is_err());