Skip to content

is_within

Whether path, taken relative to base, stays inside base: a guard against path traversal.

A relative path is joined to base and both are cleaned lexically (see normalize), so "a/../../b" escapes and "a/../b" does not; an absolute path must itself lie under base. base counts as inside itself. Nothing touches the file system, so this does not see symbolic links: if the directory can contain links an attacker controls, resolve the path with std::fs::canonicalize and compare that instead.

use helpers4::fs::is_within;

Cargo feature fs (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features fs

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["fs"] }
pub fn is_within(base: &Path, path: &Path) -> bool
ParameterTypeDescription
base&PathThe directory that must contain the result.
path&PathThe path to check, relative to base or absolute.

booltrue when the cleaned path is base or lies under it.

use helpers4::fs::is_within;
use std::path::Path;

let uploads = Path::new("/srv/uploads");
assert!(is_within(uploads, Path::new("avatars/me.png")));
assert!(!is_within(uploads, Path::new("../secrets.txt")));
assert!(!is_within(uploads, Path::new("/etc/passwd")));

src/fs/is_within.rs