Skip to main content

is_path_within_root

Function is_path_within_root 

Source
pub fn is_path_within_root(path: &Path, root: &Path) -> Result<bool, SsgError>
Expand description

Checks that path resolves to a location inside root.

Complements is_safe_path: that function only rejects paths whose string form contains a .. component, so it cannot catch a path that looks innocuous but resolves elsewhere via a symlink (e.g. content is a symlink to /etc). This function canonicalizes both path and root — resolving all symlinks and .. components — and verifies the former is a descendant of (or equal to) the latter, closing that gap.

root must exist. path must exist (use is_safe_path first for pre-creation checks on paths that don’t exist yet).

§Errors

Returns an SsgError if either path or root cannot be canonicalized (e.g. does not exist, or a broken symlink).

§Examples

use ssg::fs_ops::is_path_within_root;
use tempfile::tempdir;
use std::fs;

let root = tempdir().unwrap();
let inner = root.path().join("content");
fs::create_dir(&inner).unwrap();

assert!(is_path_within_root(&inner, root.path()).unwrap());