sandbox-runtime 0.1.1

OS-level sandboxing tool for enforcing filesystem and network restrictions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# sandbox-runtime-rs

OS-level sandboxing tool for enforcing filesystem and network restrictions on arbitrary processes without containerization.

## Features

- **Network Isolation**: Proxy-based domain filtering with allowlist/denylist support
- **Filesystem Restrictions**: Deny-only read access, allow-only write access patterns
- **Unix Socket Control**: Platform-specific Unix socket restrictions
- **Violation Monitoring**: Real-time tracking of sandbox policy violations
- **Cross-Platform**: Native support for macOS and Linux

## Platform Support

| Platform | Sandboxing Mechanism | Network Isolation |
|----------|---------------------|-------------------|
| macOS | Seatbelt (`sandbox-exec`) | HTTP/SOCKS5 proxy |
| Linux | Bubblewrap + seccomp | HTTP/SOCKS5 proxy + socat bridges |

## Installation

### Quick Install

Install the latest release with a single command:

```bash
curl -fsSL https://raw.githubusercontent.com/wangyedev/sandbox-runtime-rs/main/install.sh | sh
```

This will:
- Auto-detect your OS (Linux/macOS) and architecture (x86_64/arm64)
- Download the latest release from GitHub
- Install to `/usr/local/bin` (or `~/.local/bin` if sudo is unavailable)

### Building from Source

```bash
# Clone the repository
git clone https://github.com/wangyedev/sandbox-runtime-rs.git
cd sandbox-runtime-rs

# Build in release mode
cargo build --release

# The binary will be at target/release/srt
```

### Installing the Binary

```bash
# Install to ~/.cargo/bin
cargo install --path .

# Or copy manually
cp target/release/srt /usr/local/bin/
```

### Dependencies

**macOS**: No external dependencies (uses built-in `sandbox-exec`)

**Linux**:
- `bubblewrap` (bwrap) - Required for filesystem sandboxing
- `socat` - Required for network proxy bridging
- `ripgrep` (rg) - Recommended for dangerous file detection

```bash
# Debian/Ubuntu
sudo apt install bubblewrap socat ripgrep

# Fedora/RHEL
sudo dnf install bubblewrap socat ripgrep

# Arch Linux
sudo pacman -S bubblewrap socat ripgrep
```

## Quick Start

### Basic Usage

Run a command with network restrictions:

```bash
# Allow only github.com and npmjs.org
srt -c 'curl https://api.github.com/zen'
```

Run with a custom settings file:

```bash
srt -s ~/.my-sandbox-settings.json -c 'npm install'
```

Run with debug logging:

```bash
srt -d -c 'python script.py'
```

### Command-Line Options

```
srt [OPTIONS] [COMMAND]...

Options:
  -d, --debug              Enable debug logging
  -s, --settings <PATH>    Path to settings file (default: ~/.srt-settings.json)
  -c <COMMAND>             Run command string directly (sh -c mode)
  --control-fd <FD>        Read config updates from file descriptor (JSON lines protocol)
  -h, --help               Print help
  -V, --version            Print version

Arguments:
  [COMMAND]...             Command and arguments to run
```

### Examples

```bash
# Run a command with default settings
srt ls -la

# Run a shell command
srt -c 'echo $PATH && pwd'

# Run with custom settings
srt -s /path/to/settings.json npm install

# Debug mode to see what's happening
srt -d -c 'curl https://example.com'
```

## Configuration

Configuration is loaded from `~/.srt-settings.json` by default. Use the `-s` flag to specify a custom path.

### Full Configuration Schema

```json
{
  "network": {
    "allowedDomains": ["github.com", "*.npmjs.org"],
    "deniedDomains": ["evil.com"],
    "allowUnixSockets": ["/var/run/docker.sock"],
    "allowAllUnixSockets": false,
    "allowLocalBinding": true,
    "httpProxyPort": 3128,
    "socksProxyPort": 1080,
    "mitmProxy": {
      "socketPath": "/tmp/mitm.sock",
      "domains": ["api.example.com"]
    }
  },
  "filesystem": {
    "denyRead": ["/etc/shadow", "/private/var/root"],
    "allowWrite": ["/tmp", "./build", "./node_modules"],
    "denyWrite": ["/tmp/secret"],
    "allowGitConfig": false
  },
  "ignoreViolations": {
    "git": ["file-read-data.*\\.git"]
  },
  "enableWeakerNestedSandbox": false,
  "ripgrep": {
    "command": "rg",
    "args": ["--hidden"]
  },
  "mandatoryDenySearchDepth": 3,
  "allowPty": false,
  "seccomp": {
    "bpfPath": "/path/to/filter.bpf",
    "applyPath": "/path/to/apply-seccomp"
  }
}
```

### Configuration Options

#### Network Configuration (`network`)

| Option | Type | Description |
|--------|------|-------------|
| `allowedDomains` | `string[]` | Domains allowed for network access. Supports wildcards (`*.example.com`). |
| `deniedDomains` | `string[]` | Domains explicitly denied. Takes precedence over `allowedDomains`. |
| `allowLocalBinding` | `boolean` | Allow binding to localhost ports. Default: `false`. |
| `httpProxyPort` | `number` | External HTTP proxy port (if using external proxy). |
| `socksProxyPort` | `number` | External SOCKS5 proxy port (if using external proxy). |
| `mitmProxy` | `object` | MITM proxy configuration for traffic inspection. |

**Unix Socket Settings** (platform-specific behavior):

| Setting | macOS | Linux |
|---------|-------|-------|
| `allowUnixSockets: string[]` | Allowlist of socket paths | *Ignored* (seccomp can't filter by path) |
| `allowAllUnixSockets: boolean` | Allow all sockets | Disable seccomp blocking |

Unix sockets are **blocked by default** on both platforms.

- **macOS**: Use `allowUnixSockets` to allow specific paths (e.g., `["/var/run/docker.sock"]`), or `allowAllUnixSockets: true` to allow all.
- **Linux**: Blocking uses seccomp filters (x64/arm64 only). If seccomp isn't available, sockets are unrestricted and a warning is shown. Use `allowAllUnixSockets: true` to explicitly disable blocking.

#### Filesystem Configuration (`filesystem`)

| Option | Type | Description |
|--------|------|-------------|
| `denyRead` | `string[]` | Paths/patterns denied for reading. Supports globs. |
| `allowWrite` | `string[]` | Paths allowed for writing. Default: deny all writes. |
| `denyWrite` | `string[]` | Paths denied for writing. Overrides `allowWrite`. |
| `allowGitConfig` | `boolean` | Allow writes to `.git/config`. Default: `false`. |

#### Other Options

| Option | Type | Description |
|--------|------|-------------|
| `ignoreViolations` | `object` | Map of command patterns to violation regexes to ignore. |
| `enableWeakerNestedSandbox` | `boolean` | Enable weaker nested sandbox mode. |
| `ripgrep` | `object` | Ripgrep configuration for dangerous file discovery. |
| `mandatoryDenySearchDepth` | `number` | Search depth for mandatory deny discovery (Linux). Default: `3`. |
| `allowPty` | `boolean` | Allow pseudo-terminal access (macOS only). Default: `false`. |
| `seccomp` | `object` | Custom seccomp filter configuration (Linux only). |

### Example Configurations

**Development Environment**:

```json
{
  "network": {
    "allowedDomains": [
      "github.com",
      "*.github.com",
      "*.npmjs.org",
      "registry.yarnpkg.com",
      "pypi.org",
      "*.pypi.org"
    ],
    "allowLocalBinding": true
  },
  "filesystem": {
    "allowWrite": [
      "./",
      "/tmp"
    ]
  }
}
```

**Restrictive Production**:

```json
{
  "network": {
    "allowedDomains": ["api.myservice.com"],
    "deniedDomains": ["*.internal.myservice.com"]
  },
  "filesystem": {
    "denyRead": ["/etc/passwd", "/etc/shadow"],
    "allowWrite": ["/var/log/myapp"]
  }
}
```

## Library Usage

Use `sandbox-runtime` as a Rust library in your project:

```toml
[dependencies]
sandbox-runtime = { path = "../sandbox-runtime-rs" }
tokio = { version = "1", features = ["full"] }
```

```rust
use sandbox_runtime::prelude::*;
use sandbox_runtime::config::{NetworkConfig, FilesystemConfig};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create configuration
    let config = SandboxRuntimeConfig {
        network: NetworkConfig {
            allowed_domains: vec!["github.com".to_string()],
            ..Default::default()
        },
        filesystem: FilesystemConfig {
            allow_write: vec!["/tmp".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    // Initialize the sandbox manager
    let manager = SandboxManager::new();
    manager.initialize(config).await?;

    // Wrap a command with sandbox restrictions
    let wrapped_cmd = manager.wrap_with_sandbox("curl https://api.github.com", None, None).await?;

    println!("Wrapped command: {}", wrapped_cmd);

    // Execute and cleanup
    // ... execute wrapped_cmd ...

    manager.reset().await;
    Ok(())
}
```

### Key Types

- `SandboxManager` - Main entry point for sandbox operations
- `SandboxRuntimeConfig` - Complete configuration structure
- `NetworkConfig` - Network restriction settings
- `FilesystemConfig` - Filesystem restriction settings
- `SandboxViolationStore` - In-memory violation tracking

## Architecture

```
sandbox-runtime-rs/
├── src/
│   ├── main.rs              # CLI entry point
│   ├── lib.rs               # Library exports
│   ├── cli.rs               # Command-line argument parsing
│   ├── error.rs             # Error types
│   ├── config/              # Configuration handling
│   │   ├── mod.rs
│   │   ├── schema.rs        # Config types and validation
│   │   └── loader.rs        # File loading
│   ├── manager/             # Sandbox orchestration
│   │   ├── mod.rs           # SandboxManager
│   │   ├── state.rs         # Internal state
│   │   ├── network.rs       # Proxy initialization
│   │   └── filesystem.rs    # FS config processing
│   ├── proxy/               # Network proxy servers
│   │   ├── mod.rs
│   │   ├── filter.rs        # Domain filtering logic
│   │   ├── http.rs          # HTTP/HTTPS proxy
│   │   └── socks5.rs        # SOCKS5 proxy
│   ├── sandbox/             # Platform-specific sandboxing
│   │   ├── mod.rs
│   │   ├── macos/           # macOS Seatbelt implementation
│   │   │   ├── mod.rs
│   │   │   ├── profile.rs   # Seatbelt profile generation
│   │   │   ├── wrapper.rs   # Command wrapping
│   │   │   ├── glob.rs      # Glob-to-regex conversion
│   │   │   └── monitor.rs   # Log monitoring
│   │   └── linux/           # Linux bubblewrap implementation
│   │       ├── mod.rs
│   │       ├── bwrap.rs     # Bubblewrap command generation
│   │       ├── filesystem.rs # Bind mount generation
│   │       ├── bridge.rs    # Socat bridge management
│   │       └── seccomp.rs   # Seccomp filter handling
│   ├── utils/               # Utility functions
│   │   ├── mod.rs
│   │   ├── platform.rs      # Platform detection
│   │   ├── path.rs          # Path normalization
│   │   ├── shell.rs         # Shell quoting
│   │   ├── ripgrep.rs       # Ripgrep integration
│   │   └── debug.rs         # Debug logging
│   └── violation/           # Violation tracking
│       ├── mod.rs
│       └── store.rs         # In-memory violation store
└── Cargo.toml
```

## How It Works

### Network Isolation

1. **Proxy-Based Filtering**: The sandbox starts HTTP and SOCKS5 proxy servers on localhost
2. **Environment Variables**: Commands run with `http_proxy`, `https_proxy`, and `ALL_PROXY` set
3. **Domain Filtering**: Each connection is checked against allowed/denied domain lists
4. **MITM Support**: Optional routing through a MITM proxy for inspection

```
┌─────────────────────────────────────────────────────────────┐
│                     Sandboxed Process                        │
│  ┌─────────────────────────────────────────────────────────┐ │
│  │  curl https://api.github.com                            │ │
│  └───────────────────────┬─────────────────────────────────┘ │
│                          │ HTTP_PROXY=localhost:3128         │
└──────────────────────────┼───────────────────────────────────┘
            ┌──────────────────────────────┐
            │       HTTP/SOCKS5 Proxy      │
            │  ┌────────────────────────┐  │
            │  │    Domain Filter       │  │
            │  │  ┌──────────────────┐  │  │
            │  │  │ allowed: ✓      │  │  │
            │  │  │ denied: ✗       │  │  │
            │  │  │ mitm: → MITM    │  │  │
            │  │  └──────────────────┘  │  │
            │  └────────────────────────┘  │
            └──────────────────────────────┘
                           ▼ (if allowed)
                    Internet
```

### Filesystem Isolation

**macOS (Seatbelt)**:
- Generates a Seatbelt profile (`.sb` file) with SBPL rules
- Uses `sandbox-exec -f profile.sb command` to run
- Supports glob patterns for path matching

**Linux (Bubblewrap)**:
- Creates isolated filesystem namespace with `bwrap`
- Mounts root as read-only, overlays writable paths
- Uses seccomp to block unauthorized Unix socket creation

### Mandatory Deny Paths

The following files/directories are always protected from writes:

**Dangerous Files**:
- `.gitconfig`, `.bashrc`, `.bash_profile`, `.profile`
- `.zshrc`, `.zprofile`, `.zshenv`, `.zlogin`
- `.npmrc`, `.yarnrc`, `.yarnrc.yml`
- `.mcp.json`, `.mcp-settings.json`

**Dangerous Directories**:
- `.git/hooks`, `.git`
- `.vscode`, `.idea`
- `.claude/commands`

## Security Considerations

### Limitations

1. **Proxy Bypass**: Sandboxed processes that don't respect proxy environment variables may bypass network filtering
2. **Root Access**: The sandbox cannot protect against processes running as root
3. **Kernel Exploits**: Sandbox escapes via kernel vulnerabilities are possible
4. **Unix Sockets (Linux)**: Without seccomp, processes may create Unix sockets to bypass network restrictions

### Best Practices

1. Always specify an explicit `allowedDomains` list rather than relying on `deniedDomains` alone
2. Use the most restrictive `allowWrite` paths possible
3. Enable seccomp on Linux when available
4. Review violation logs regularly
5. Keep the sandbox runtime updated

## Development

### Building

```bash
# Debug build
cargo build

# Release build
cargo build --release

# Run tests
cargo test

# Run with logging
RUST_LOG=debug cargo run -- -c 'echo hello'
```

### Testing

```bash
# Run all tests
cargo test

# Run specific test module
cargo test config::

# Run with output
cargo test -- --nocapture
```

### Code Structure

- Platform-specific code uses `#[cfg(target_os = "...")]` attributes
- Configuration uses `serde` for JSON serialization
- Async runtime is `tokio`
- Error handling uses `thiserror` and `anyhow`

## Acknowledgments

This project is a Rust port of [sandbox-runtime](https://github.com/anthropic-experimental/sandbox-runtime), the original TypeScript implementation by Anthropic. The core architecture, sandboxing approach, and configuration schema are derived from that project.

## License

MIT License - see [LICENSE](LICENSE) for details.