Skip to main content

Sandbox

Struct Sandbox 

Source
pub struct Sandbox { /* private fields */ }
Expand description

A sandbox started from a Config. Wrap each command that is to run in it with Sandbox::command, and drop it when they have all ended, since the proxy stops with it.

Implementations§

Source§

impl Sandbox

Source

pub fn start(config: &Config) -> Result<Self, StartError>

Starts a sandbox that applies config.

§Errors

StartError::UnsupportedPlatform off macOS, Linux and Windows, where nothing confines a command yet, StartError::UnresolvedPath when a path in config cannot be made absolute, and StartError::Proxy when the proxy that carries its network cannot start. On macOS and Linux the sandbox first makes the folder it sets TMPDIR to (CLAUDE_CODE_TMPDIR, then CLAUDE_TMPDIR, then /tmp/claude) when it is missing, owner-only, and lets the command write there. StartError::Tmpdir when that path is relative or holds a .., its parent is missing, it is a link or a file, or another user owns it. A /tmp/claude that another user owns is used as it is, when the host names no folder. On Linux the bwrap checks run first: StartError::Missing when there is no executable bwrap on PATH, and StartError::Uid0WithoutSetfcap for uid 0 without CAP_SETFCAP, and StartError::CurrentExe when the path of this binary is unknown. A bwrap older than 0.5.0 is a warning on stderr. StartError::UnspellableDeny names a mandatory write deny the plan cannot spell, which is a Linux answer too. On Linux, StartError::UnresolvedTcp names an network.allowTcp host that returns no address, and StartError::ScopedTcp one that is an IPv6 literal with an interface scope. On Linux, StartError::ScopedPublish names a network.publish entry with an interface scope, and StartError::PublishedProxyPort one with a port the proxy relay holds inside the sandbox. On Windows the status checks of windows_preflight_for run first, then StartError::ProfileGrantOutdated for a profile grant that is neither none nor current, StartError::LoopbackMismatch for a session whose network.allowLocalBinding is not the install’s loopback mode, StartError::ProfileConflicts when a grant meets what the profile read grant keeps out of reach, StartError::RunnerUnreadable when the sandbox user cannot read this binary and so cannot run it as its runner, StartError::GrantBlocked, StartError::ProfileBusy or StartError::GrantFailed when the session’s grants and denies cannot be made, StartError::FenceInactive or StartError::FenceUnverified when the firewall fence is not shown to be active, and StartError::ProxyRangeFull or StartError::ProxyRangeRefused when the proxy has no port of its range to listen on. The grants are held until the sandbox drops, which revokes them.

Source

pub fn replace_network_lists(&self, config: &Config)

Replaces the allowed and denied domains and the denied resolved addresses of the running sandbox with those of config.network, so a host that follows a settings file can change them without restarting a command.

The next request or tunnel is judged by the new lists, and a tunnel that is already open is not cut. Every other key of config is ignored: the filesystem rules, allowTcp, strictAllowlist, mitmProxy and the rest keep the values the sandbox started with.

Source

pub fn command(&self, command: &Command) -> Result<Command, WrapError>

Wraps command into one that runs it inside this sandbox, to spawn in its place.

On Linux, bwrap runs this binary again inside the sandbox as the helper, and the helper runs command. A host must call run_from_args first thing in main, or its own main runs inside the sandbox in place of the command.

The wrapped command keeps command’s program, arguments, working directory and environment changes. It does not keep its standard streams, which a Command cannot be asked for, nor an env_clear.

On Windows, this binary runs again as srt-win exec in front of the command, which it launches as the sandbox user in two hops. A program that resolves to a .exe or .com file runs as itself, with its arguments as given. Any other program, such as a batch file or a cmd.exe builtin, runs through cmd.exe /d /s /c as one line, a word cmd.exe would act on in quotes. A host must call run_from_args first thing in main for that too.

On macOS it gains the sandbox’s own environment: a name the sandbox owns is set over the caller’s, a name the caller owns is left to it, and the variables that point its clients at the proxy are set over them. On Linux it gains the sandbox’s own SANDBOX_RUNTIME and TMPDIR, and the variables that point its clients at the relay and its ssh at the connect mode, set over the caller’s.

On Linux each call also listens on the addresses of network.publish, with SO_REUSEPORT, and relays what connects to the same address inside the command’s network namespace. The listeners last until the sandbox drops, or until the command’s sandbox has ended and the returned Command is gone, whichever comes first.

§Errors

WrapError::Missing when the machine has no sandbox-exec, WrapError::Bwrap when the command cannot be put in a bwrap line, WrapError::Cmd when it runs through cmd.exe and a word of it cannot be put on the line, WrapError::Publish when the host cannot listen on a published address, and WrapError::UnsupportedPlatform off macOS, Linux and Windows.

Trait Implementations§

Source§

impl Debug for Sandbox

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.