pub fn validate_within_base(path: &Path, base: &Path) -> Result<(), PathError>Expand description
Validate that a path stays within a base directory
Returns error if the path, when canonicalized, escapes the base directory. Also checks parent directories for symlinks that might escape the base.
For non-existent paths, walks up the directory tree to find an existing ancestor and validates that ancestor stays within the base directory.
Returns error if base doesn’t exist and path is absolute outside base.
§Security
This function protects against path traversal attacks through multiple mechanisms:
- Traversal pattern detection (
..components) - Symlink escape detection via ancestor canonicalization
- Secure prefix matching to prevent prefix bypass attacks