Skip to main content

safe_relative

Function safe_relative 

Source
pub fn safe_relative(path: &str) -> Option<String>
Expand description

A request path made safe to join onto a surface’s root, or none.

The character set is what the producers write and nothing wider. ! is in it because rustdoc names the redirect stub of every macro macro.<name>!.html, and a reference that 404s on its own macros is a reference with holes: it is a legal path character that no browser escapes, it cannot spell ., .. or a separator, and it therefore opens no route out of the directory that the segment checks do not already close. % stays out, so an escaped traversal is refused as text rather than decoded into one.

use majordomus_cli::web::files::safe_relative;
assert_eq!(safe_relative("cli/index.html").as_deref(), Some("cli/index.html"));
assert_eq!(safe_relative("").as_deref(), Some(""));
assert_eq!(safe_relative("cli/").as_deref(), Some("cli/"));
assert_eq!(safe_relative("m/macro.x!.html").as_deref(), Some("m/macro.x!.html"));
assert_eq!(safe_relative("../Cargo.toml"), None);
assert_eq!(safe_relative("a/../../b"), None);
assert_eq!(safe_relative("/etc/passwd"), None);
assert_eq!(safe_relative("a//b"), None);
assert_eq!(safe_relative("a\0b"), None);
assert_eq!(safe_relative("%2e%2e/b"), None);