Path Security
A comprehensive path validation and sanitization library to prevent path traversal attacks in Rust applications.
Features
- Path Traversal Prevention: Validates paths to ensure they don't escape base directories
- Project Name Validation: Ensures project names are safe for filesystem use
- Filename Sanitization: Validates filenames for suspicious patterns
- Cross-Platform: Handles both Unix and Windows path conventions
- Zero Dependencies: Only depends on
anyhowfor error handling - Well Tested: Comprehensive test suite with >95% coverage
Security Guarantees
Basic Path Traversal Protection
- ✅ Blocks
..directory traversal sequences (including encoded variants) - ✅ Rejects absolute paths
- ✅ Prevents null byte injection
- ✅ Blocks environment variable expansion patterns (
$VAR,%VAR%,~) - ✅ Validates against OS reserved names (Windows: CON, PRN, AUX, etc.)
- ✅ Ensures paths resolve within base directory using canonicalization
- ✅ Detects and blocks control characters
- ✅ Enforces reasonable length limits
Encoding Attack Protection
- ✅ URL Encoding: Detects
%2e%2e%2f(.../),%2E,%2F,%5C - ✅ Double URL Encoding: Detects
%252e%252e%252f→%2e%2e%2f→../ - ✅ UTF-8 Overlong Encoding: Blocks
%c0%ae,%c0%af,%c1%9c,%e0%80%ae - ✅ Unicode Percent Encoding: Blocks
%u002e,%u002fsyntax - ✅ HTML Entity Encoding: Detects
../ - ✅ Hex Encoding: Blocks
\x2e\x2fsequences
Unicode Attack Protection
- ✅ Zero-Width Characters: Detects U+200B, U+200C, U+200D, U+FEFF
- ✅ Right-to-Left Override: Blocks U+202E (bidirectional text attack)
- ✅ Homoglyphs: Detects Unicode dots (U+2024-2026) and slashes (U+2044, U+2215, U+2571, U+FF0F)
- ✅ Full-Width Characters: Blocks full-width Unicode variants (U+FF01-FF5E)
- ✅ Combining Characters: Prevents Unicode normalization attacks
Windows-Specific Protection
- ✅ NTFS Alternate Data Streams: Blocks
file.txt::$DATA,file.txt:stream - ✅ UNC Paths: Detects
\\server\share,//server/share - ✅ Extended-Length Paths: Blocks
\\?\C:\,\\.\prefixes - ✅ Device Paths: Prevents access to
\\.\COM1,\\.\pipe\ - ✅ Trailing Dots/Spaces: Blocks Windows filename normalization exploits
- ✅ 8.3 Filename Format: Validates against short name attacks
- ✅ Drive-Relative Paths: Detects
C:../patterns - ✅ Reserved Names with Extensions: Blocks
CON.txt,PRN.log
Path Separator Manipulation
- ✅ Multiple Consecutive Separators: Detects
//,///,\\\\ - ✅ Mixed Separators: Blocks
../\../,.\/ - ✅ Alternative Separators: Detects
;, tab, newline as path separators - ✅ Backslash Normalization: Prevents backslash evasion on Unix
Whitespace Exploitation
- ✅ Leading/Trailing Whitespace: Detects space-padded paths
- ✅ Internal Whitespace: Blocks
.. / ..,(multiple spaces) - ✅ Tab Characters: Prevents tab-based evasion
- ✅ Other Whitespace: Detects form feed, vertical tab
Advanced Traversal Patterns
- ✅ Triple Dots: Blocks
...sequences - ✅ Space-Separated Dots: Detects
. .,. . - ✅ Current Directory Traversal: Blocks
./../../ - ✅ Redundant Patterns: Detects
././../
Special Path Protection
- ✅ Proc Filesystem: Blocks
/proc/self/,/proc/[pid]/ - ✅ Dev Filesystem: Prevents
/dev/null,/dev/randomaccess - ✅ Sys Filesystem: Blocks
/sys/access - ✅ System Directories: Prevents access to
/etc/,/boot/, Windows system paths - ✅ Temp Directories: Validates access to
/tmp/,/var/tmp/
Defense-in-Depth
- ✅ Multi-Phase Validation: 7-phase validation pipeline
- ✅ Canonicalization: Final path resolution validation
- ✅ Fail-Safe Design: Denies ambiguous or suspicious patterns
- ✅ Cross-Platform: Works on Unix, Linux, macOS, and Windows
- ✅ Test Coverage: 95.81% line coverage with comprehensive attack vector tests
Installation
Add this to your Cargo.toml:
[]
= "0.2"
Usage
Path Validation
Validate user-provided paths against a base directory:
use validate_path;
use Path;
Project Name Validation
Ensure project names are filesystem-safe:
use validate_project_name;
Filename Validation
Validate individual filenames:
use validate_filename;
Use Cases
Web Applications
Protect file upload endpoints:
use ;
use Path;
Archive Extraction
Prevent zip slip attacks:
use validate_path;
use Path;
Git Repository Operations
Validate paths when working with repositories:
use validate_path;
use Path;
API Reference
validate_path(path: &Path, base_dir: &Path) -> Result<PathBuf>
Validates a relative path against a base directory and returns the canonical absolute path.
Checks:
- Path is relative (not absolute)
- No
..sequences - No suspicious patterns (
~,$,%, null bytes) - Resolves within base directory after canonicalization
validate_project_name(name: &str) -> Result<String>
Validates a project name for filesystem safety.
Requirements:
- 1-64 characters long
- Only alphanumeric, hyphens, underscores
- Doesn't start/end with hyphen or underscore
- Not a reserved system name
validate_filename(filename: &str) -> Result<String>
Validates an individual filename.
Requirements:
- 1-255 characters long
- No path separators (
/,\) - Not
.or.. - No null bytes or control characters
Testing
Run tests:
Run with coverage:
Achieving 100% Coverage
This library provides 85% coverage through static path validation. For the remaining 15% (symlinks, TOCTOU, etc.), combine with application-level mitigations:
use validate_path;
use OpenOptions;
use OpenOptionsExt;
See REMAINING_15_PERCENT.md for comprehensive details.
Security
If you discover a security vulnerability, please email security@redasgard.com.
License
Licensed under the MIT License. See LICENSE for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Origin
This library was extracted from the Red Asgard security platform, where it's been battle-tested in production handling untrusted code repositories. The library was made standalone to benefit the broader Rust ecosystem with enterprise-grade path security.