| Crates.io | softpath |
| lib.rs | softpath |
| version | 0.2.2 |
| created_at | 2025-06-16 16:31:37.182431+00 |
| updated_at | 2025-07-01 18:09:40.283595+00 |
| description | A human-friendly file and directory path manipulation library for Rust. |
| homepage | |
| repository | https://github.com/GhaziAlibi/softpath |
| max_upload_size | |
| id | 1714472 |
| size | 95,509 |
A safe and intuitive path manipulation library for Rust that actually cares about security.
Working with file paths in Rust shouldn't be a security nightmare. We built SoftPath because we got tired of seeing the same path traversal vulnerabilities pop up in codebases over and over again.
What you get:
Add this to your Cargo.toml:
[dependencies]
softpath = "0.2.2"
Here's how easy it is to work with paths safely:
use softpath::prelude::*;
fn main() -> Result<(), softpath::SoftPathError> {
// Create and write to a file
let config_file = "~/config/app.json".into_path()?;
config_file.write_string("{\"version\": 1}")?;
// Copy it somewhere else
let backup = "~/config/backup/app.json".into_path()?;
config_file.copy_to(&backup)?;
// Create directories as needed
"~/data/logs".into_path()?.create_dir_all()?;
// Read it back
let content = backup.read_to_string()?;
println!("Backup content: {}", content);
Ok(())
}
That's it. No ../../../etc/passwd nonsense will get through.
Here's what protects you from the usual path-related disasters:
Path Traversal Protection
No more ../../../etc/passwd attacks. We check every path before doing anything with it.
Symlink Cycle Detection
Prevents infinite symlink loops that could crash your program or eat up resources.
TOCTOU Prevention
We validate paths right before using them, not way earlier when things might have changed.
Destination Validation
Before copying or moving files, we make sure you're not accidentally overwriting something important.
Cross-platform Consistency
Same security behavior whether you're on Windows, Linux, or macOS.
Security doesn't mean slow. We've made sure the safety checks don't kill your performance:
Do this:
// Let SoftPath handle the validation
let user_path = user_input.into_path()?;
user_path.write_string(content)?;
Don't do this:
// Bypasses all our safety checks
std::fs::write(user_input, content);
A few simple rules:
std::fs when you can.into_path() on user input before doing file operationsError handling example:
match sketchy_path.into_path() {
Ok(safe_path) => safe_path.create_dir_all()?,
Err(SoftPathError::PathTraversal(_)) => {
// Someone tried something sneaky
return Err("Nice try, but no".into());
}
Err(e) => return Err(e),
}
Licensed under:
Want to help make this better? Great! Just keep security in mind.
If you're adding new features:
cargo test before submittingIf you find a security issue:
Before submitting a PR:
# Make sure everything still works
cargo test
# Check that security tests pass
cargo test security
# Make sure you didn't break performance
cargo bench
# Check for known vulnerabilities
cargo audit
We're pretty responsive to PRs, especially if they fix bugs or improve security.