Skip to main content

validate_within_base

Function validate_within_base 

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

  1. Traversal pattern detection (.. components)
  2. Symlink escape detection via ancestor canonicalization
  3. Secure prefix matching to prevent prefix bypass attacks