validate_path

Function validate_path 

Source
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:

  1. Canonicalizes path (resolves symlinks, relative components)
  2. Validates against allowed base directory
  3. Checks component lengths
  4. 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, &current_dir).is_ok());

// Invalid: path traversal attempt
let malicious = PathBuf::from("/tmp/../../etc/passwd");
assert!(validate_path(&malicious, &current_dir).is_err());