Skip to main content

normalize

Function normalize 

Source
pub fn normalize(path: &Path) -> PathBuf
Expand description

Resolve . and .. lexically, without consulting the filesystem.

A traversal attempt has to be rejected with a message about escaping the root whether or not the target happens to exist, which canonicalize alone cannot do.

A leading .. is kept as a marker so the caller’s containment check can see it — and, critically, a later .. must not pop that marker. ../../etc/passwd popping its own first .. would collapse to etc/passwd, which looks contained, and the read would then resolve to <root>/etc/passwd: not an escape, but silently the wrong file. Depth counts only real segments, so a marker can never be consumed.

pub rather than pub(crate): lanekeep-js’s module loader resolves rule specifiers against the same lexical rule (a different root, a different reason to reject .., the identical algorithm), and sharing this one function is what keeps that algorithm defined once rather than copied at its second call site.