Skip to main content

safe_filename

Function safe_filename 

Source
pub fn safe_filename(name: &str) -> Option<String>
Expand description

Strip a filename to its final component and reject traversal attempts.

Returns None if the input is empty, contains only path separators, or resolves to . / .. after extraction.

§Examples

assert_eq!(safe_filename("photo.jpg"), Some("photo.jpg".into()));
assert_eq!(safe_filename("../../../etc/passwd"), Some("passwd".into()));
assert_eq!(safe_filename("sub/dir/file.jpg"), Some("file.jpg".into()));