Skip to main content

Crate zenops_safe_relative_path

Crate zenops_safe_relative_path 

Source
Expand description

Relative paths that statically cannot escape their parent directory.

A SafeRelativePath is a relative path whose string form is guaranteed to contain no .. components. Joining one onto a base directory can therefore never resolve to a sibling, ancestor, or cousin of that base. Putting the type in a function signature pushes the validation out to the boundary — everything downstream of the signature can trust the input without re-checking it.

The crate mirrors the Path / PathBuf split from the standard library:

Two more specialised types build on the same idea:

  • SinglePathComponent — narrower still, a single segment with no separators. Useful for file names and directory entries.
  • srpath! — a macro that validates a string literal at compile time and produces a &'static SafeRelativePath with no run-time cost.

§Example

use zenops_safe_relative_path::SafeRelativePathBuf;

let ok: SafeRelativePathBuf = "config/app.toml".parse().unwrap();
assert_eq!(ok.as_str(), "config/app.toml");

let escaping: Result<SafeRelativePathBuf, _> = "../etc/passwd".parse();
assert!(escaping.is_err());

§Limitations

Safety here is purely lexical — the crate inspects the path string and nothing else. Symlinks are not followed, so a SafeRelativePath joined onto a directory that contains a symlink can still reach outside the base. If symlink containment matters, layer a check on top: canonicalise the joined path and assert it still starts with the base.

Modules§

error

Macros§

srpath
Validate a path literal at compile time and produce a &'static SafeRelativePath.

Structs§

SafeRelativePath
A borrowed relative path that statically cannot escape its parent.
SafeRelativePathBuf
An owned relative path that statically cannot escape its parent.
SinglePathComponent
A path that is exactly one segment — no separators, no traversal.