From "A Case for Oxidation: The Rust Programming Language" Sergio Benitez says,
Here is a static file server written in Rocket. It is exactly four lines and it guaranteed to not be vulnerable to directory traversal attacks.
Those four lines are:
#[get("/<path..>")]
fn files(path: PathBuf) -> Option<NamedFile> {
NamedFile::open(Path::new("static/").join(path)).ok()
}
The bottom of this slide says,
FromParam*
implementation forPathBuf
verifies path safety
I get how a type can guarantee safety by validating input (in the same sense that any object can in a constructor or how the input to a function can be wrapped with a validating function.
dangerousThing(validateSafety(input))
Many languages provide this. I also understand how you can make this simpler by putting it into a constructor for a type or class,
class Path {
constructor(path) { this._path = validateSafety(path) }
}
But I'm confused at what (if anything) Rust is doing differently here. Is there anything more to this?