Skip to main content

Sandbox

Struct Sandbox 

Source
pub struct Sandbox<N: NetworkPolicy = DenyAll> { /* private fields */ }
Expand description

A sandbox for running untrusted code with restricted permissions

All network traffic from sandboxed processes is routed through a local proxy that applies the configured NetworkPolicy for filtering and logging.

When dropped, the sandbox will:

  • Stop the network proxy
  • Stop the IPC server (if enabled)
  • Kill all child processes that were spawned within it
  • Delete the working directory if it was auto-created, unless keep_working_dir() was called

Implementations§

Source§

impl Sandbox<DenyAll>

Source

pub async fn new() -> Result<Self>

Create a new sandbox with default configuration

Uses the global executor from executor-core (initialized with AsyncExecutor if not set). Creates a random working directory in the current directory using four English words connected by hyphens.

By default, all network access is denied (DenyAll policy).

Source

pub async fn with_executor<E: Executor + Clone + 'static>( executor: E, ) -> Result<Self>

Create a new sandbox with a custom executor

Use this when you want to integrate with a specific async runtime (e.g., tokio, async-std) instead of the default executor.

Source§

impl<N: NetworkPolicy + 'static> Sandbox<N>

Source

pub async fn with_config(config: SandboxConfig<N>) -> Result<Self>

Create a sandbox with custom configuration

Uses the global executor from executor-core (initialized with AsyncExecutor if not set).

Source

pub async fn with_config_and_executor<E: Executor + Clone + 'static>( config: SandboxConfig<N>, executor: E, ) -> Result<Self>

Create a sandbox with custom configuration and executor

Use this when you want full control over both the configuration and the async runtime executor.

Source

pub fn keep_working_dir(&mut self) -> &mut Self

Keep the working directory after the sandbox is dropped

By default, auto-created working directories are deleted when the sandbox is dropped. User-provided working directories are preserved by default. Call this method to preserve the working directory for inspection or reuse.

Note: Child processes are always killed when the sandbox is dropped, regardless of this setting.

Source

pub fn proxy_url(&self) -> String

Get the proxy URL for environment variables

This URL should be set as HTTP_PROXY and HTTPS_PROXY for processes that need network access through the sandbox’s proxy.

Source

pub fn command(&self, program: impl Into<String>) -> Command<'_>

Create a command builder for running a program in the sandbox

The command will automatically have HTTP_PROXY and HTTPS_PROXY environment variables set to route traffic through the sandbox’s proxy. If IPC is configured, HEEL_IPC_ENDPOINT will also be set.

Source

pub async fn run_python(&self, script: &str) -> Result<Output>

Run a Python script in the sandbox

The script will be executed using the Python interpreter from the configured virtual environment, or the system Python if no venv is configured.

Source

pub fn config(&self) -> &SandboxConfigData

Get a reference to the sandbox configuration data

Source

pub fn working_dir(&self) -> &Path

Get the path to the working directory

Trait Implementations§

Source§

impl<N: NetworkPolicy> Drop for Sandbox<N>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<N> Freeze for Sandbox<N>

§

impl<N = DenyAll> !RefUnwindSafe for Sandbox<N>

§

impl<N> Send for Sandbox<N>

§

impl<N> Sync for Sandbox<N>

§

impl<N> Unpin for Sandbox<N>

§

impl<N> UnsafeUnpin for Sandbox<N>

§

impl<N = DenyAll> !UnwindSafe for Sandbox<N>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more