microsandbox
Lightweight VM sandboxes for running AI agents and untrusted code with hardware-level isolation.
microsandbox is the core Rust library for the microsandbox project. It provides a high-level async API for creating, managing, and interacting with microVM sandboxes — real virtual machines that boot in under 100ms and run standard OCI (Docker) images.
Features
- Hardware isolation — Each sandbox is a real VM with its own Linux kernel, not a container
- Sub-100ms boot — MicroVMs start nearly instantly, no daemon or server required
- OCI image support — Pull and run images from Docker Hub, GHCR, ECR, or any OCI registry
- Command execution — Run commands with streaming or collected output, interactive shells
- Guest filesystem access — Read, write, list, copy files inside a running sandbox
- Named volumes — Persistent storage that survives sandbox restarts, with quotas
- Network policies — Control outbound access: public-only (default), allow-all, or fully airgapped
- DNS filtering — Block specific domains or domain suffixes
- TLS interception — Transparent MITM proxy for HTTPS inspection and secret substitution
- Secrets — Credentials that never enter the VM; placeholder substitution at the network layer
- Port publishing — Expose guest TCP/UDP services on host ports
- Rootfs patches — Modify the filesystem before the VM boots
- Detached mode — Sandboxes can outlive the parent process
- Metrics — CPU, memory, disk I/O, and network I/O per sandbox
Requirements
- Linux with KVM enabled, or macOS with Apple Silicon (M-series)
- Rust 2024 edition
Installation
[]
= "0.4"
Cargo Features
| Feature | Default | Description |
|---|---|---|
prebuilt |
yes | Use pre-built runtime binaries |
net |
yes | Networking: port publishing, policies, TLS, secrets |
To disable networking:
[]
= { = "0.4", = false, = ["prebuilt"] }
Quick Start
use Sandbox;
async
Examples
Command Execution
use Sandbox;
// Collected output.
let output = sandbox.exec.await?;
println!;
println!;
// Streaming output.
let mut handle = sandbox.exec_stream.await?;
while let Some = handle.recv.await
Filesystem Operations
let fs = sandbox.fs;
// Write a file.
fs.write.await?;
// Read it back.
let data = fs.read.await?;
println!;
// List a directory.
for entry in fs.list.await?
Named Volumes
use ;
// Create a 100 MiB named volume.
let data = builder.quota.create.await?;
// Mount it in a sandbox.
let sandbox = builder
.image
.volume
.create
.await?;
sandbox.shell.await?;
sandbox.stop_and_wait.await?;
// Mount the same volume in another sandbox (read-only).
let reader = builder
.image
.volume
.create
.await?;
let output = reader.shell.await?;
println!; // "hello"
Network Policies
use ;
// Default: public internet only (blocks private ranges).
let sandbox = builder
.image
.create
.await?;
// Fully airgapped.
let sandbox = builder
.image
.network
.create
.await?;
// Domain blocking via policy rules (refused at DNS and at TCP egress).
use ;
let mut policy = default;
policy.rules.push;
policy.rules.push;
let sandbox = builder
.image
.network
.create
.await?;
Port Publishing
let sandbox = builder
.image
.port // host:8080 → guest:80
.create
.await?;
Secrets
Secrets use placeholder substitution — the real value never enters the VM. It is only swapped in at the network layer for HTTPS requests to allowed hosts.
let sandbox = builder
.image
.secret_env
.create
.await?;
// Guest sees: API_KEY=$MSB_API_KEY (a placeholder)
// HTTPS to api.openai.com: placeholder is transparently replaced with the real key
// HTTPS to any other host with the placeholder: request is blocked
Rootfs Patches
Modify the filesystem before the VM boots:
let sandbox = builder
.image
.patch
.create
.await?;
Detached Mode
Sandboxes in detached mode survive the parent process:
// Create and detach.
let sandbox = builder
.image
.create_detached
.await?;
// Later, from another process:
let sandbox = start.await?;
let output = sandbox.shell.await?;
Image Sources
use Sandbox;
// OCI image (most common).
builder.image
// Local bind-mounted rootfs.
builder.image
// QCOW2 disk image.
builder.image_with
API Overview
Core Types
| Type | Description |
|---|---|
Sandbox |
Live handle to a running sandbox — lifecycle, execution, filesystem |
SandboxBuilder |
Fluent builder for configuring and creating sandboxes |
SandboxConfig |
Serializable sandbox configuration |
SandboxHandle |
Lightweight metadata handle from the database |
Volume |
Persistent named volume |
VolumeBuilder |
Fluent builder for creating volumes |
Image |
OCI image metadata and inspection |
Execution Types
| Type | Description |
|---|---|
ExecOutput |
Captured stdout/stderr with exit status |
ExecHandle |
Streaming execution handle with event channel |
ExecOptions / ExecOptionsBuilder |
Execution configuration (args, env, cwd, timeout, rlimits) |
ExecEvent |
Stream event: Started, Stdout, Stderr, Exited |
ExecSink |
Writable stdin channel for streaming exec |
ExitStatus |
Exit code and success flag |
Filesystem Types
| Type | Description |
|---|---|
SandboxFs |
Gateway for guest filesystem operations |
FsEntry |
Directory entry (name, kind, size, mode) |
FsMetadata |
File metadata (size, mode, timestamps) |
Configuration Types
| Type | Description |
|---|---|
RootfsSource |
Image source: Oci, Bind, or DiskImage |
VolumeMount |
Mount type: Bind, Named, or Tmpfs |
Patch / PatchBuilder |
Pre-boot filesystem modifications |
NetworkPolicy |
Network access control (requires net feature) |
RegistryAuth |
Docker registry credentials |
PullPolicy |
Image pull strategy: Always, IfMissing, Never |
LogLevel |
Logging verbosity |
Error Handling
All fallible operations return MicrosandboxResult<T>, which uses MicrosandboxError — an enum covering I/O, network, database, configuration, runtime, and timeout errors.
License
Apache-2.0