crabpot 0.10.8

Native Rust sandbox runtime for AI agents, rewritten from Anthropic's sandbox-runtime
Documentation

crabpot restricts what a process can read, write and reach on the network, using the OS's own sandbox and no container. It is a port of anthropics/sandbox-runtime (srt) to Rust, with no Node runtime, and reads the same settings file.

Platform How it confines a process
macOS sandbox-exec with a generated profile
Linux bubblewrap and seccomp (bwrap must be on PATH), after a setup step on Ubuntu 24.04 and later
Windows a separate sandbox user behind a WFP firewall fence, after a one-time install

On every platform, network traffic goes through a filtering proxy that only lets the allowed domains through.

Install

cargo install crabpot   # the command
cargo add crabpot       # the library

Use it

Write a settings file, upstream's format:

{
  "network": {
    "allowedDomains": ["github.com", "*.github.com"],
    "deniedDomains": []
  },
  "filesystem": {
    "denyRead": ["~/.ssh"],
    "allowWrite": ["."],
    "denyWrite": [".env"]
  }
}

crabpot adds two optional network keys for services. Both take lists of strings, and an empty list asks for nothing on every platform:

  • network.publish, for example ["127.0.0.1:8001", "0.0.0.0:3600", "[::1]:8002"], lets the command bind and accept connections on those ports on macOS and Linux. On macOS the command chooses its bind address as it would unsandboxed. Seatbelt grants each port on every local address, so the address in the entry does not restrict the sandbox rule. allowLocalBinding: true already covers these ports. On Linux the command has a network namespace of its own, so the launcher listens on each address on the host and passes every connection into the sandbox, where a relay dials the same address and port. The command binds that address as it would unsandboxed. An address that is neither loopback nor a wildcard is added to the sandbox's lo. The listeners set SO_REUSEPORT, so a second sandbox can publish the same port during a cutover. The host refuses connections to a published port until a command inside listens on it, so a readiness probe on the host sees the port closed until the command is ready, unless another sandbox is already serving the same address in a cutover, which keeps the port answering. The port then stays open, and once the command stops listening a connection to it is closed. An address the host cannot listen on, such as a port held without SO_REUSEPORT, refuses the sandbox and names it, as does an IPv6 entry with an interface scope such as [fe80::1%2]:8001, or a port of 3128 or 1080, which the sandbox's proxy relay holds. An IPv4-mapped entry such as [::ffff:10.0.0.5]:80 is the IPv4 address it wraps. Windows refuses a nonempty list until publication is supported there.
  • network.allowTcp, for example ["10.0.0.5:3306", "db.example.com:5432"], names hosts and ports the command may dial directly on Linux. crabpot resolves each host once at startup and refuses the sandbox if a name does not resolve. A relay listens on those addresses and ports inside the sandbox and opens authenticated CONNECT tunnels through the proxy. deniedDomains still wins. Name resolution inside the sandbox is unchanged: dial the address, or a name the command already resolves. Windows refuses nonempty lists. On macOS, Seatbelt cannot restrict the grant to the requested host: *:<port> would admit every host on that port, so macOS refuses nonempty lists too.

publish requires IP literals. allowTcp accepts DNS names or IP literals. IPv6 literals use brackets, as in [::1]:8001. Both require a port from 1 to 65535 and reject duplicates, with an error naming the entry. IPv4 literals use four decimal octets without leading zeros. allowTcp turns IPv4-mapped literals into IPv4, dropping their scope. Other IPv6 literals retain numeric scopes, as in [fe80::1%2]:22. Linux refuses such an entry at startup, naming it: a scope is an interface index of the host, which means nothing in the sandbox's network namespace.

Then run a command under it. With no -s, crabpot reads ~/.srt-settings.json, as upstream does.

crabpot -s settings.json -- git pull
crabpot -s settings.json -c 'npm install && npm test'

A sandbox refusal exits 125; anything else is the command's own exit code, with two exceptions that exit 1 for a failure of their own:

  • windows-install and windows-uninstall run no command. They exit 0 when done and 1 when their arguments are refused or the step fails.
  • On Linux, the helper that bwrap runs inside the sandbox exits 1 when it cannot exec the command. Run directly, it also exits 1 on its own bad arguments and on the unsupported relay mode; the wrapper reports those as 125. Its connect mode is a sandboxed ssh's ProxyCommand, not a wrapper exit status: it exits 1 to ssh when the tunnel fails, and ssh picks its own exit code.

--control-fd <fd> (Unix) reads JSON lines from descriptor <fd>, which is 3 or more, while the command runs. Each line is a full config in the -s shape, and only its network.allowedDomains, network.deniedDomains and network.deniedResolvedAddresses are applied; every other key is ignored, as long as the line is a valid config here: a key this platform refuses (such as network.allowTcp on macOS) makes the line not parse. A request or tunnel opened after the line is judged by the new lists, and one already open is not cut. A line that does not parse is ignored with one message on stderr, and the last good lists stay. End of input keeps the last lists, and the command runs on. A descriptor that is not open, or that fails to read before the command starts, exits 125. The command sees /dev/null under that number, not the descriptor. The descriptor may be close-on-exec.

-d prints debug output, -V (--version) prints the version and -h (--help) prints the usage. Like every option, they belong to the command after -- or after the program's name. Or use the library:

use std::path::Path;
use std::process::Command;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // On Linux and Windows the sandbox runs this binary again as a helper.
    crabpot::run_from_args();

    let config = crabpot::Config::from_file(Path::new("settings.json"))?;
    let sandbox = crabpot::Sandbox::start(&config)?;
    let status = sandbox
        .command(Command::new("curl").arg("https://example.com"))?
        .status()?;
    println!("{status}");
    Ok(())
}

Sandbox::start refuses with a typed StartError that names what is missing. The sandbox's grants and proxy last until it drops.

Linux setup

Ubuntu 24.04 and later set kernel.apparmor_restrict_unprivileged_userns = 1, which strips capabilities from the helper's nested user namespace. Without a fix crabpot fails with bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted. Other distributions need nothing here.

The narrow fix is an AppArmor profile that grants userns to the binaries that need it, and leaves the restriction on for everything else. Put this in /etc/apparmor.d/crabpot:

abi <abi/4.0>,
include <tunables/global>
profile crabpot-bwrap /usr/bin/bwrap flags=(unconfined) {
  userns,
}
profile crabpot-helper /path/to/crabpot flags=(unconfined) {
  userns,
}

Set the second path to the binary that runs crabpot, since the sandbox runs that binary again as the helper: crabpot itself, or the program that embeds the library.

  1. Disable Ubuntu's own bwrap-userns-restrict profile. It attaches to the same /usr/bin/bwrap and is what strips the helper's capabilities.

    sudo ln -s /etc/apparmor.d/bwrap-userns-restrict /etc/apparmor.d/disable/
    sudo apparmor_parser -R /etc/apparmor.d/bwrap-userns-restrict
    
  2. Load the profile:

    sudo apparmor_parser -r /etc/apparmor.d/crabpot
    
  3. Leave kernel.apparmor_restrict_unprivileged_userns at 1.

Tested on Ubuntu 26.04 (kernel 7.0, bwrap 0.11.1) as an unprivileged user: the command writes the working directory, an allowed domain passes, another is refused, a nested unshare is refused, and unshare -Ur outside the sandbox stays refused.

The fallback is to turn the restriction off for the whole host:

sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0

That lets every unprivileged process on the host create user namespaces, which is the door most local privilege escalations use.

Windows setup

The Windows sandbox needs a one-time install: the sandbox user, the WFP network filters and the read access the user needs. It is the same install as upstream's srt windows-install (same user, same WFP sublayer, same setup version), so an install by either tool satisfies the other.

crabpot windows-install [--allow-loopback] [--proxy-port-range LOW-HIGH]
                        [--sandbox-user NAME] [--sublayer-guid GUID] [--force]
                        [--profile-exclude PATH] [--skip-profile-grant]
crabpot windows-uninstall [--keep-user] [--sublayer-guid GUID]
  • windows-install asks for elevation itself, with one UAC prompt, and does the work in the elevated process.
  • After the install it makes the profile read grant, as upstream's install does: the sandbox user can then read your profile, except what srt-win keeps out on its own (~\.ssh, ~\.aws and the other key dot-directories, Windows' own per-user secret stores, browser profiles and password managers, your registry hives) and anything --profile-exclude names. What it opens includes package-manager configuration that can hold feed tokens (.npmrc, pip.ini, NuGet.Config, Maven settings.xml, gradle.properties, .gitconfig). The grant runs as you and needs no elevation. Its first pass touches every file it grants, which takes minutes on a large profile, and a new top-level item of the profile stays unreadable until it runs again. crabpot --srt-win profile forget removes the grant.
  • --profile-exclude PATH keeps one more path unreadable. Repeatable, and it reaches srt-win profile grant as --exclude: an exclusion once recorded stays in force.
  • --skip-profile-grant makes no grant. A crabpot under your profile, which is where cargo install puts one, then stays unreadable to the sandbox user.
  • The install also makes no grant when it ran as a service account (LocalSystem, LocalService, NetworkService), when the owner of the session it ran in could not be read, or when that session belongs to another account, where granting would open the installing account's profile instead of the one the sessions in it read. Each prints the account it turned on.
  • The install ends by checking that the sandbox user can read the crabpot executable, which it runs as the runner. The grant is what makes a binary under your profile readable, and a binary in a folder every user can read, such as one under Program Files, needs no grant at all. When the install made no grant, an unreadable runner is a notice naming crabpot --srt-win profile grant and the install still exits 0, since skipping the grant is what you asked for.
  • Sandbox::start refuses an unreadable runner by name (StartError::RunnerUnreadable) before it grants anything, so the binary is named instead of seclogon answering CreateProcessWithLogonW: Access is denied. It reads the executable's own access list for the sandbox account, the group it is in, and Users, Authenticated Users and Everyone, so a folder granted to the sandbox alone counts. Sitting under your profile is not enough on its own: the grant puts an inheritable entry on each folder it reaches, so what it excludes, what it leaves ungranted (your temporary directory) and any top-level item made after it ran are all refused. So is a binary moved in from another folder, which is what cargo install does: it keeps its old access list, a new grant writes nothing for it, and icacls <path to the binary> /reset re-applies the folder's inherited entries. A host can read that same check before it starts anything: crabpot::windows_status() reports it as WindowsStatus::runner_readable, None when the machine could not make the check.
  • --allow-loopback opens every loopback port to the sandbox user, not only the proxy's. Every listener on localhost becomes reachable from the sandbox, including a forwarder that carries traffic off the machine. A session must use the loopback mode the install used, so changing it means running the install again with --force.
  • --proxy-port-range sets the loopback ports left open for the proxy (default 60080-60089). Each sandbox takes two of them, so the default range holds five sandboxes at once, and a session that finds every port of its range taken refuses with StartError::ProxyRangeFull, which names the range and the flag.
  • windows-uninstall removes the user, its credential and the filters, and with them the profile read grant (--keep-user keeps the user and the grant). It then removes any WFP filters left under the sublayer.
  • Both commands are for Windows and refuse on any other platform.

License

MIT OR Apache-2.0, at your option.

crabpot is derived from anthropics/sandbox-runtime, which is Apache-2.0. See NOTICE.