pub struct SandboxRoot { /* private fields */ }Expand description
A validated root directory for sandboxed operations.
All paths derived from this root are guaranteed to stay within it.
SandboxRoot canonicalizes the root path at construction time and
validates all joined paths to prevent directory traversal attacks.
§Security Guarantees
- The root path is canonicalized (resolved to absolute, symlinks followed)
- Joined paths cannot escape the root via
..traversal - Absolute paths are rejected unless they’re within the root
- Symlinks can be optionally rejected to prevent escape via symlink
§Example
use xchecker_utils::paths::{SandboxRoot, SandboxConfig};
let root = SandboxRoot::new("/path/to/workspace", SandboxConfig::default())?;
let file = root.join("src/main.rs")?;
println!("Safe path: {}", file.as_path().display());Implementations§
Source§impl SandboxRoot
impl SandboxRoot
Sourcepub fn new(
root: impl AsRef<Path>,
config: SandboxConfig,
) -> Result<Self, SandboxError>
pub fn new( root: impl AsRef<Path>, config: SandboxConfig, ) -> Result<Self, SandboxError>
Create a new sandbox root from a path.
Canonicalizes the path and verifies it exists as a directory.
§Arguments
root- The path to use as the sandbox rootconfig- Configuration for symlink/hardlink handling
§Errors
Returns an error if:
- The path does not exist
- The path is not a directory
- The path cannot be canonicalized
Sourcepub fn new_default(root: impl AsRef<Path>) -> Result<Self, SandboxError>
pub fn new_default(root: impl AsRef<Path>) -> Result<Self, SandboxError>
Create a sandbox root with default (restrictive) configuration.
This is a convenience method equivalent to SandboxRoot::new(root, SandboxConfig::default()).
Sourcepub fn join(&self, rel: impl AsRef<Path>) -> Result<SandboxPath, SandboxError>
pub fn join(&self, rel: impl AsRef<Path>) -> Result<SandboxPath, SandboxError>
Join a relative path, validating it stays within the sandbox.
§Arguments
rel- A relative path to join to the sandbox root
§Errors
Returns an error if:
- The path contains
..traversal components - The path is absolute
- The resolved path escapes the sandbox root
- The path is or contains a symlink (when symlinks are not allowed)
- The path is or contains a hardlink (when hardlinks are not allowed)
Sourcepub fn config(&self) -> &SandboxConfig
pub fn config(&self) -> &SandboxConfig
Get the sandbox configuration.
Trait Implementations§
Source§impl Clone for SandboxRoot
impl Clone for SandboxRoot
Source§fn clone(&self) -> SandboxRoot
fn clone(&self) -> SandboxRoot
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for SandboxRoot
impl RefUnwindSafe for SandboxRoot
impl Send for SandboxRoot
impl Sync for SandboxRoot
impl Unpin for SandboxRoot
impl UnsafeUnpin for SandboxRoot
impl UnwindSafe for SandboxRoot
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more