Expand description
Filename and path validation for security hardening.
This library provides validation and sanitization rules inspired by David A. Wheeler’s proposed Linux safename LSM. The goal is to harden systems against attacks that exploit unusual filenames to trick shell scripts, command-line tools, and applications with imperfect input handling.
§API Overview
The library provides two sets of functions:
-
file*functions (is_file_safe,validate_file,sanitize_file): Validate or sanitize a single filename component (no path separators). -
path*functions (is_path_safe,validate_path,sanitize_path): Validate or sanitize full or partial paths by checking each component.
Note: This library does not perform path canonicalization, symlink resolution,
or .. traversal handling. These operations have their own security implications
(TOCTOU races, symlink attacks) and should be handled separately by the caller.
§Default Rules
The default validation blocks:
- Control characters (0x00-0x1F, 0x7F) - prevents terminal escape sequence attacks
- Leading dash (
-) - prevents interpretation as command-line options - Leading tilde (
~) - prevents shell home directory expansion - Leading and trailing spaces - prevents quoting bugs and argument splitting
- Byte 0xFF - invalid UTF-8 leading byte
§Feature Flags
Features are organized into tiers:
-
low(default): Cross-platform safetyblock-colon: Blocks:(PATH injection)block-backslash: Blocks\(path traversal via normalization)
-
require-utf8(default): Requires valid UTF-8 encoding -
medium: Shell safety (includeslow)block-quotes: Blocks"and'block-chaining: Blocks&,;,|(for&&,;,||)block-redirection: Blocks|,>,<block-expansion: Blocks$,%,*,?,`
-
high: Maximum restriction (includesmedium)block-brackets: Blocks(,),[,]block-space: Blocks spaces everywhere (not just leading/trailing)
-
require-ascii: ASCII-only (bytes 0x00-0x7F), mutually exclusive withrequire-utf8
Note: require-utf8 and require-ascii cannot be enabled together (compile-time error).
§Example
use safename::{is_file_safe, is_path_safe, validate_file};
// Filename validation (single component, no slashes)
assert!(is_file_safe("normal_file.txt"));
assert!(!is_file_safe("-rf")); // Leading dash
assert!(!is_file_safe("file\x00name")); // Contains NUL
// Path validation (full or partial paths)
assert!(is_path_safe("/home/user/file.txt"));
assert!(!is_path_safe("/home/-rf")); // Component starts with dash
// Get detailed error information
let result = validate_file(b"~/.bashrc");
assert!(result.is_err());Structs§
- File
Sanitization Options - Configuration for filename sanitization.
- File
Validation Options - Configuration for filename validation.
- Path
Error - Error type for path validation, including the component index.
- Path
Sanitization Options - Configuration for path sanitization.
- Path
Validation Options - Configuration for path validation.
Enums§
- Safe
Name Error - Error type describing why a filename is unsafe.
Constants§
- DEFAULT_
MAX_ FILE_ LEN - Default maximum filename length (NAME_MAX on most Unix systems).
- DEFAULT_
MAX_ PATH_ LEN - Default maximum path length (PATH_MAX on most Unix systems).
- REPLACEMENT_
CHAR - Unicode replacement character (U+FFFD) encoded as UTF-8.
- REPLACEMENT_
UNDERSCORE - ASCII underscore as a single-byte replacement option.
Functions§
- is_
file_ safe - Checks if a filename is safe according to all enabled validation rules.
- is_
path_ safe - Checks if a full path is safe according to all enabled validation rules.
- sanitize_
file - Sanitizes a filename by replacing invalid bytes with underscore.
- sanitize_
file_ with_ options - Sanitizes a filename with custom options.
- sanitize_
path - Sanitizes a full path by sanitizing each component.
- sanitize_
path_ with_ options - Sanitizes a full path with custom options.
- validate_
file - Validates a filename component (what you’d get from
Path::file_name()). - validate_
file_ with_ options - Validates a filename component with custom options.
- validate_
path - Validates a full path by splitting on
/and validating each component. - validate_
path_ with_ options - Validates a full path with custom options.
Type Aliases§
- Path
Result - Result type alias for path validation.
- Safe
Name Result - Result type alias using
SafeNameError.