A simple path traversal checker made with Rust. Useful for APIs that serve dynamic files.
Note: this is a security tool. If you see something wrong, open an issue in GitHub.
The is_path_trav
function receives two paths, one is the base path and the other is the path to check.
To verify if the second is inside the first, path_trav
turn paths into absolute and check if the second route contains the first.
Base : /home/user/data --> /home/user/data
Rel : ./data/folder --> /home/user/data/folder
Relative path is inside base path.
Base : /home/user/data --> /home/user/data
Rel : ./data/../../../etc/passwd --> /etc/passwd
Relative paths isn't inside base path, tries to acces sensitive data
Add path_trav
to your Cargo.toml
toml
[dependencies]
path_trav = "1.0.0"
Then, on your main.rs file ```rust use std::path::Path; use pathtrav::ispath_trav;
fn main() { let important_file = Path::new("./data/../../../etc/passwd");
// with absolute path
let check_abs = is_path_trav(&Path::new("/home/user/data"), &important_file).unwrap();
// with relative path
let check_rel = is_path_trav(&Path::new("./data"), &important_file).unwrap();
}
``
ispathtravreturns
Result
path_trav
is licensed under the GPL-3 license.
🥳 Any PR is welcome! Is a small project, so the guideline is to follow the code style and not make insane pruposes.
Gátomo - GPL-3 License