pub struct PathValidator { /* private fields */ }Expand description
Path validator that ensures paths stay within allowed directories
§Example
use ruvector_security::PathValidator;
use std::path::PathBuf;
// Create validator with allowed directories
let validator = PathValidator::new(vec![PathBuf::from("/data"), PathBuf::from("/tmp")]);
// Paths within allowed directories are valid
// (Note: these paths must exist for validate() to succeed)
// validator.validate("/data/vectors.db");
// Paths outside allowed directories are rejected
assert!(validator.validate("/etc/passwd").is_err());
// Path traversal attempts are rejected
assert!(validator.validate("/data/../etc/passwd").is_err());Implementations§
Source§impl PathValidator
impl PathValidator
Sourcepub const DEFAULT_MAX_PATH_LENGTH: usize = 4_096usize
pub const DEFAULT_MAX_PATH_LENGTH: usize = 4_096usize
Maximum allowed path length (default 4096)
Sourcepub fn new(allowed_dirs: Vec<PathBuf>) -> Self
pub fn new(allowed_dirs: Vec<PathBuf>) -> Self
Create a new path validator with allowed directories
Sourcepub fn allow_symlinks(self, allow: bool) -> Self
pub fn allow_symlinks(self, allow: bool) -> Self
Set whether symlinks are allowed
Sourcepub fn max_path_length(self, length: usize) -> Self
pub fn max_path_length(self, length: usize) -> Self
Set maximum path length
Sourcepub fn validate<P: AsRef<Path>>(&self, path: P) -> SecurityResult<PathBuf>
pub fn validate<P: AsRef<Path>>(&self, path: P) -> SecurityResult<PathBuf>
Validate a path and return the canonical path if valid
§Errors
Returns an error if:
- The path contains path traversal sequences (
..) - The path is outside all allowed directories
- The path is a symlink (unless allowed)
- The path exceeds maximum length
Sourcepub fn validate_new_file<P: AsRef<Path>>(
&self,
path: P,
) -> SecurityResult<PathBuf>
pub fn validate_new_file<P: AsRef<Path>>( &self, path: P, ) -> SecurityResult<PathBuf>
Validate a path for a new file (may not exist yet)
This validates the parent directory exists and is within allowed paths, and that the filename is safe.
Trait Implementations§
Source§impl Clone for PathValidator
impl Clone for PathValidator
Source§fn clone(&self) -> PathValidator
fn clone(&self) -> PathValidator
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for PathValidator
impl Debug for PathValidator
Auto Trait Implementations§
impl Freeze for PathValidator
impl RefUnwindSafe for PathValidator
impl Send for PathValidator
impl Sync for PathValidator
impl Unpin for PathValidator
impl UnwindSafe for PathValidator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more