Skip to main content

PathBoundary

Struct PathBoundary 

Source
pub struct PathBoundary<Marker = ()> { /* private fields */ }
Expand description

A path boundary that serves as the secure foundation for validated path operations.

Represent the trusted filesystem boundary directory for all strict and virtual path operations. All StrictPath/VirtualPath values derived from a PathBoundary are guaranteed to remain within this boundary.

§Examples

let data_dir = PathBoundary::<()>::try_new_create("./data")?;
// Untrusted input from request/CLI/config/etc.
let requested_file = "logs/app.log";
let file = data_dir.strict_join(requested_file)?;
let file_display = file.strictpath_display();
println!("{file_display}");

Implementations§

Source§

impl<Marker> PathBoundary<Marker>

Source

pub fn try_new<P: AsRef<Path>>(restriction_path: P) -> Result<Self>

Creates a new PathBoundary anchored at restriction_path (which must already exist and be a directory).

Create a boundary anchored at an existing directory (must exist and be a directory).

§Errors
  • StrictPathError::InvalidRestriction: Boundary directory is missing, not a directory, or cannot be canonicalized.
§Examples
use strict_path::PathBoundary;
let data_dir = PathBoundary::<()>::try_new("./data")?;
Source

pub fn try_new_create<P: AsRef<Path>>(boundary_dir: P) -> Result<Self>

Creates the directory if missing, then constructs a new PathBoundary.

Ensure the boundary directory exists (create if missing) and construct a new boundary.

§Errors
  • StrictPathError::InvalidRestriction: Directory creation/canonicalization fails.
§Examples
use strict_path::PathBoundary;
let data_dir = PathBoundary::<()>::try_new_create("./data")?;
Source

pub fn strict_join( &self, candidate_path: impl AsRef<Path>, ) -> Result<StrictPath<Marker>>

Join a candidate path to the boundary and return a validated StrictPath.

§Errors
  • StrictPathError::PathResolutionError, StrictPathError::PathEscapesBoundary.
Source

pub fn change_marker<NewMarker>(self) -> PathBoundary<NewMarker>

Consume this boundary and substitute a new marker type.

Mirrors crate::StrictPath::change_marker and crate::VirtualPath::change_marker, enabling marker transformation after authorization checks. Use this when encoding proven authorization into the type system (e.g., after validating a user’s permissions). The consumption makes marker changes explicit during code review.

§Examples
struct ReadOnly;
struct ReadWrite;

let read_only_dir: PathBoundary<ReadOnly> = PathBoundary::try_new_create("./data")?;

// After authorization check...
let write_access_dir: PathBoundary<ReadWrite> = read_only_dir.change_marker();
Source

pub fn into_strictpath(self) -> Result<StrictPath<Marker>>

Consume this boundary and return a StrictPath anchored at the boundary directory.

§Errors
  • StrictPathError::PathResolutionError: Canonicalization fails (directory removed or inaccessible).
  • StrictPathError::PathEscapesBoundary: Guard against race conditions that move the directory.
§Examples
let data_dir: PathBoundary = PathBoundary::try_new_create("./data")?;
let data_path: StrictPath = data_dir.into_strictpath()?;
assert!(data_path.is_dir());
Source

pub fn exists(&self) -> bool

Returns true if the PathBoundary directory exists.

This is always true for a constructed PathBoundary, but we query the filesystem for robustness.

Source

pub fn interop_path(&self) -> &OsStr

Return the boundary directory path as &OsStr for unavoidable third-party AsRef<Path> interop (no allocation).

Source

pub fn strictpath_display(&self) -> Display<'_>

Returns a Display wrapper that shows the PathBoundary directory system path.

Source

pub fn metadata(&self) -> Result<Metadata>

Return filesystem metadata for the boundary directory.

Create a symbolic link at link_path pointing to this boundary’s directory.

Create a hard link at link_path pointing to this boundary’s directory.

Accepts the same link_path: impl AsRef<Path> parameter as strict_symlink and returns io::Result<()>.

Source

pub fn read_dir(&self) -> Result<ReadDir>

Read directory entries under the boundary directory (discovery only).

Source

pub fn strict_read_dir(&self) -> Result<BoundaryReadDir<'_, Marker>>

Iterate directory entries under the boundary, yielding validated StrictPath values.

Unlike read_dir() which returns raw std::fs::DirEntry values requiring manual re-validation, this method yields StrictPath entries directly. Each entry is automatically validated through strict_join() so you can use it immediately for I/O operations without additional validation.

§Examples
use strict_path::PathBoundary;

let data_dir: PathBoundary = PathBoundary::try_new(temp.path())?;

// Auto-validated iteration - no manual re-join needed!
for entry in data_dir.strict_read_dir()? {
    let child = entry?;
    println!("Found: {}", child.strictpath_display());
}
Source

pub fn remove_dir(&self) -> Result<()>

Remove the boundary directory (non-recursive); fails if not empty.

Source

pub fn remove_dir_all(&self) -> Result<()>

Recursively remove the boundary directory and its contents.

Source

pub fn virtualize(self) -> VirtualRoot<Marker>

Convert this boundary into a VirtualRoot for virtual path operations.

Trait Implementations§

Source§

impl<Marker> Clone for PathBoundary<Marker>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Marker> Debug for PathBoundary<Marker>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Marker: Default> FromStr for PathBoundary<Marker>

Source§

fn from_str(path: &str) -> Result<Self, Self::Err>

Parse a PathBoundary from a string path, validating that it already exists as a directory.

WHY VALIDATE-ONLY: When PathBoundary is parsed from untrusted input (serde deserialization of a config file, a CLI flag, an environment variable), the string controls which directory on disk is created. A FromStr that eagerly calls create_dir_all would let an attacker who controls that string touch any directory the process has write access to. from_str intentionally does not create anything; use PathBoundary::try_new_create explicitly when directory creation is the desired side effect.

let data_dir: PathBoundary<()> = p.parse()?;
assert!(data_dir.exists());
Source§

type Err = StrictPathError

The associated error which can be returned from parsing.
Source§

impl<Marker> Hash for PathBoundary<Marker>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Marker> Ord for PathBoundary<Marker>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<Marker> PartialEq<&Path> for PathBoundary<Marker>

Source§

fn eq(&self, other: &&Path) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Marker> PartialEq<Path> for PathBoundary<Marker>

Source§

fn eq(&self, other: &Path) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<M1, M2> PartialEq<PathBoundary<M2>> for PathBoundary<M1>

Source§

fn eq(&self, other: &PathBoundary<M2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<M1, M2> PartialEq<PathBoundary<M2>> for VirtualRoot<M1>

Source§

fn eq(&self, other: &PathBoundary<M2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Marker> PartialEq<PathBuf> for PathBoundary<Marker>

Source§

fn eq(&self, other: &PathBuf) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<M1, M2> PartialEq<VirtualRoot<M2>> for PathBoundary<M1>

Available on crate feature virtual-path only.
Source§

fn eq(&self, other: &VirtualRoot<M2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Marker> PartialOrd for PathBoundary<Marker>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<Marker> Eq for PathBoundary<Marker>

Auto Trait Implementations§

§

impl<Marker> Freeze for PathBoundary<Marker>

§

impl<Marker> RefUnwindSafe for PathBoundary<Marker>
where Marker: RefUnwindSafe,

§

impl<Marker> Send for PathBoundary<Marker>
where Marker: Send,

§

impl<Marker> Sync for PathBoundary<Marker>
where Marker: Sync,

§

impl<Marker> Unpin for PathBoundary<Marker>
where Marker: Unpin,

§

impl<Marker> UnsafeUnpin for PathBoundary<Marker>

§

impl<Marker> UnwindSafe for PathBoundary<Marker>
where Marker: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.