@sysid/sandbox-runtime-improved 0.0.42-sysid.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +676 -0
  3. package/dist/cli.d.ts +3 -0
  4. package/dist/cli.d.ts.map +1 -0
  5. package/dist/cli.js +166 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/index.d.ts +11 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +9 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/sandbox/generate-seccomp-filter.d.ts +71 -0
  12. package/dist/sandbox/generate-seccomp-filter.d.ts.map +1 -0
  13. package/dist/sandbox/generate-seccomp-filter.js +263 -0
  14. package/dist/sandbox/generate-seccomp-filter.js.map +1 -0
  15. package/dist/sandbox/http-proxy.d.ts +19 -0
  16. package/dist/sandbox/http-proxy.d.ts.map +1 -0
  17. package/dist/sandbox/http-proxy.js +295 -0
  18. package/dist/sandbox/http-proxy.js.map +1 -0
  19. package/dist/sandbox/linux-sandbox-utils.d.ts +158 -0
  20. package/dist/sandbox/linux-sandbox-utils.d.ts.map +1 -0
  21. package/dist/sandbox/linux-sandbox-utils.js +875 -0
  22. package/dist/sandbox/linux-sandbox-utils.js.map +1 -0
  23. package/dist/sandbox/macos-sandbox-utils.d.ts +41 -0
  24. package/dist/sandbox/macos-sandbox-utils.d.ts.map +1 -0
  25. package/dist/sandbox/macos-sandbox-utils.js +672 -0
  26. package/dist/sandbox/macos-sandbox-utils.js.map +1 -0
  27. package/dist/sandbox/sandbox-config.d.ts +307 -0
  28. package/dist/sandbox/sandbox-config.d.ts.map +1 -0
  29. package/dist/sandbox/sandbox-config.js +195 -0
  30. package/dist/sandbox/sandbox-config.js.map +1 -0
  31. package/dist/sandbox/sandbox-manager.d.ts +42 -0
  32. package/dist/sandbox/sandbox-manager.d.ts.map +1 -0
  33. package/dist/sandbox/sandbox-manager.js +796 -0
  34. package/dist/sandbox/sandbox-manager.js.map +1 -0
  35. package/dist/sandbox/sandbox-schemas.d.ts +57 -0
  36. package/dist/sandbox/sandbox-schemas.d.ts.map +1 -0
  37. package/dist/sandbox/sandbox-schemas.js +3 -0
  38. package/dist/sandbox/sandbox-schemas.js.map +1 -0
  39. package/dist/sandbox/sandbox-utils.d.ts +116 -0
  40. package/dist/sandbox/sandbox-utils.d.ts.map +1 -0
  41. package/dist/sandbox/sandbox-utils.js +463 -0
  42. package/dist/sandbox/sandbox-utils.js.map +1 -0
  43. package/dist/sandbox/sandbox-violation-store.d.ts +19 -0
  44. package/dist/sandbox/sandbox-violation-store.d.ts.map +1 -0
  45. package/dist/sandbox/sandbox-violation-store.js +54 -0
  46. package/dist/sandbox/sandbox-violation-store.js.map +1 -0
  47. package/dist/sandbox/socks-proxy.d.ts +13 -0
  48. package/dist/sandbox/socks-proxy.d.ts.map +1 -0
  49. package/dist/sandbox/socks-proxy.js +95 -0
  50. package/dist/sandbox/socks-proxy.js.map +1 -0
  51. package/dist/utils/config-loader.d.ts +11 -0
  52. package/dist/utils/config-loader.d.ts.map +1 -0
  53. package/dist/utils/config-loader.js +60 -0
  54. package/dist/utils/config-loader.js.map +1 -0
  55. package/dist/utils/debug.d.ts +7 -0
  56. package/dist/utils/debug.d.ts.map +1 -0
  57. package/dist/utils/debug.js +25 -0
  58. package/dist/utils/debug.js.map +1 -0
  59. package/dist/utils/platform.d.ts +15 -0
  60. package/dist/utils/platform.d.ts.map +1 -0
  61. package/dist/utils/platform.js +49 -0
  62. package/dist/utils/platform.js.map +1 -0
  63. package/dist/utils/ripgrep.d.ts +22 -0
  64. package/dist/utils/ripgrep.d.ts.map +1 -0
  65. package/dist/utils/ripgrep.js +45 -0
  66. package/dist/utils/ripgrep.js.map +1 -0
  67. package/dist/utils/which.d.ts +9 -0
  68. package/dist/utils/which.d.ts.map +1 -0
  69. package/dist/utils/which.js +25 -0
  70. package/dist/utils/which.js.map +1 -0
  71. package/dist/vendor/seccomp/arm64/apply-seccomp +0 -0
  72. package/dist/vendor/seccomp/arm64/unix-block.bpf +0 -0
  73. package/dist/vendor/seccomp/x64/apply-seccomp +0 -0
  74. package/dist/vendor/seccomp/x64/unix-block.bpf +0 -0
  75. package/dist/vendor/seccomp-src/apply-seccomp.c +98 -0
  76. package/dist/vendor/seccomp-src/seccomp-unix-block.c +97 -0
  77. package/package.json +88 -0
  78. package/vendor/seccomp/arm64/apply-seccomp +0 -0
  79. package/vendor/seccomp/arm64/unix-block.bpf +0 -0
  80. package/vendor/seccomp/x64/apply-seccomp +0 -0
  81. package/vendor/seccomp/x64/unix-block.bpf +0 -0
  82. package/vendor/seccomp-src/apply-seccomp.c +98 -0
  83. package/vendor/seccomp-src/seccomp-unix-block.c +97 -0
package/README.md ADDED
@@ -0,0 +1,676 @@
1
+ # Anthropic Sandbox Runtime (srt)
2
+
3
+ A lightweight sandboxing tool for enforcing filesystem and network restrictions on arbitrary processes at the OS level, without requiring a container.
4
+
5
+ `srt` uses native OS sandboxing primitives (`sandbox-exec` on macOS, `bubblewrap` on Linux) and proxy-based network filtering. It can be used to sandbox the behaviour of agents, local MCP servers, bash commands and arbitrary processes.
6
+
7
+ > **Beta Research Preview**
8
+ >
9
+ > The Sandbox Runtime is a research preview developed for [Claude Code](https://www.claude.com/product/claude-code) to enable safer AI agents. It's being made available as an early open source preview to help the broader ecosystem build more secure agentic systems. As this is an early research preview, APIs and configuration formats may evolve. We welcome feedback and contributions to make AI agents safer by default!
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install -g @anthropic-ai/sandbox-runtime
15
+ ```
16
+
17
+ ## Basic Usage
18
+
19
+ ```bash
20
+ # Network restrictions
21
+ $ srt "curl anthropic.com"
22
+ Running: curl anthropic.com
23
+ <html>...</html> # Request succeeds
24
+
25
+ $ srt "curl example.com"
26
+ Running: curl example.com
27
+ Connection blocked by network allowlist # Request blocked
28
+
29
+ # Filesystem restrictions
30
+ $ srt "cat README.md"
31
+ Running: cat README.md
32
+ # Anthropic Sandb... # Current directory access allowed
33
+
34
+ $ srt "cat ~/.ssh/id_rsa"
35
+ Running: cat ~/.ssh/id_rsa
36
+ cat: /Users/ollie/.ssh/id_rsa: Operation not permitted # Specific file blocked
37
+ ```
38
+
39
+ ## Overview
40
+
41
+ This package provides a standalone sandbox implementation that can be used as both a CLI tool and a library. It's designed with a **secure-by-default** philosophy tailored for common developer use cases: processes start with minimal access, and you explicitly poke only the holes you need.
42
+
43
+ **Key capabilities:**
44
+
45
+ - **Network restrictions**: Control which hosts/domains can be accessed via HTTP/HTTPS and other protocols
46
+ - **Filesystem restrictions**: Control which files/directories can be read/written
47
+ - **Unix socket restrictions**: Control access to local IPC sockets
48
+ - **Violation monitoring**: On macOS, tap into the system's sandbox violation log store for real-time alerts
49
+
50
+ ### Example Use Case: Sandboxing MCP Servers
51
+
52
+ A key use case is sandboxing Model Context Protocol (MCP) servers to restrict their capabilities. For example, to sandbox the filesystem MCP server:
53
+
54
+ **Without sandboxing** (`.mcp.json`):
55
+
56
+ ```json
57
+ {
58
+ "mcpServers": {
59
+ "filesystem": {
60
+ "command": "npx",
61
+ "args": ["-y", "@modelcontextprotocol/server-filesystem"]
62
+ }
63
+ }
64
+ }
65
+ ```
66
+
67
+ **With sandboxing** (`.mcp.json`):
68
+
69
+ ```json
70
+ {
71
+ "mcpServers": {
72
+ "filesystem": {
73
+ "command": "srt",
74
+ "args": ["npx", "-y", "@modelcontextprotocol/server-filesystem"]
75
+ }
76
+ }
77
+ }
78
+ ```
79
+
80
+ Then configure restrictions in `~/.srt-settings.json`:
81
+
82
+ ```json
83
+ {
84
+ "filesystem": {
85
+ "denyRead": [],
86
+ "allowWrite": ["."],
87
+ "denyWrite": ["~/sensitive-folder"]
88
+ },
89
+ "network": {
90
+ "allowedDomains": [],
91
+ "deniedDomains": []
92
+ }
93
+ }
94
+ ```
95
+
96
+ Now the MCP server will be blocked from writing to the denied path:
97
+
98
+ ```
99
+ > Write a file to ~/sensitive-folder
100
+ ✗ Error: EPERM: operation not permitted, open '/Users/ollie/sensitive-folder/test.txt'
101
+ ```
102
+
103
+ ## How It Works
104
+
105
+ The sandbox uses OS-level primitives to enforce restrictions that apply to the entire process tree:
106
+
107
+ - **macOS**: Uses `sandbox-exec` with dynamically generated [Seatbelt profiles](https://reverse.put.as/wp-content/uploads/2011/09/Apple-Sandbox-Guide-v1.0.pdf)
108
+ - **Linux**: Uses [bubblewrap](https://github.com/containers/bubblewrap) for containerization with network namespace isolation
109
+
110
+ ![0d1c612947c798aef48e6ab4beb7e8544da9d41a-4096x2305](https://github.com/user-attachments/assets/76c838a9-19ef-4d0b-90bb-cbe1917b3551)
111
+
112
+ ### Dual Isolation Model
113
+
114
+ Both filesystem and network isolation are required for effective sandboxing. Without file isolation, a compromised process could exfiltrate SSH keys or other sensitive files. Without network isolation, a process could escape the sandbox and gain unrestricted network access.
115
+
116
+ **Filesystem Isolation** enforces read and write restrictions:
117
+
118
+ - **Read** (deny-then-allow pattern): By default, read access is allowed everywhere. You can deny broad regions (e.g., `/Users`) and then re-allow specific paths within them (e.g., `.`). `allowRead` takes precedence over `denyRead` — the opposite of write, where `denyWrite` takes precedence over `allowWrite`.
119
+ - **Write** (allow-only pattern): By default, write access is denied everywhere. You must explicitly allow paths (e.g., `.`, `/tmp`). An empty allow list means no write access.
120
+
121
+ **Network Isolation** (allow-only pattern): By default, all network access is denied. You must explicitly allow domains. An empty allowedDomains list means no network access. Network traffic is routed through proxy servers running on the host:
122
+
123
+ - **Linux**: Requests are routed via the filesystem over a Unix domain socket. The network namespace of the sandboxed process is removed entirely, so all network traffic must go through the proxies running on the host (listening on Unix sockets that are bind-mounted into the sandbox)
124
+
125
+ - **macOS**: The Seatbelt profile allows communication only to a specific localhost port. The proxies listen on this port, creating a controlled channel for all network access
126
+
127
+ Both HTTP/HTTPS (via HTTP proxy) and other TCP traffic (via SOCKS5 proxy) are mediated by these proxies, which enforce your domain allowlists and denylists.
128
+
129
+ For more details on sandboxing in Claude Code, see:
130
+
131
+ - [Claude Code Sandboxing Documentation](https://docs.claude.com/en/docs/claude-code/sandboxing)
132
+ - [Beyond Permission Prompts: Making Claude Code More Secure and Autonomous](https://www.anthropic.com/engineering/claude-code-sandboxing)
133
+
134
+ ## Architecture
135
+
136
+ ```
137
+ src/
138
+ ├── index.ts # Library exports
139
+ ├── cli.ts # CLI entrypoint (srt command)
140
+ ├── utils/ # Shared utilities
141
+ │ ├── debug.ts # Debug logging
142
+ │ ├── settings.ts # Settings reader (permissions + sandbox config)
143
+ │ ├── platform.ts # Platform detection
144
+ │ └── exec.ts # Command execution utilities
145
+ └── sandbox/ # Sandbox implementation
146
+ ├── sandbox-manager.ts # Main sandbox manager
147
+ ├── sandbox-schemas.ts # Zod schemas for validation
148
+ ├── sandbox-violation-store.ts # Violation tracking
149
+ ├── sandbox-utils.ts # Shared sandbox utilities
150
+ ├── http-proxy.ts # HTTP/HTTPS proxy for network filtering
151
+ ├── socks-proxy.ts # SOCKS5 proxy for network filtering
152
+ ├── linux-sandbox-utils.ts # Linux bubblewrap sandboxing
153
+ └── macos-sandbox-utils.ts # macOS sandbox-exec sandboxing
154
+ ```
155
+
156
+ ## Usage
157
+
158
+ ### As a CLI tool
159
+
160
+ The `srt` command (Anthropic Sandbox Runtime) wraps any command with security boundaries:
161
+
162
+ ```bash
163
+ # Run a command in the sandbox
164
+ srt echo "hello world"
165
+
166
+ # With debug logging
167
+ srt --debug curl https://example.com
168
+
169
+ # Specify custom settings file
170
+ srt --settings /path/to/srt-settings.json npm install
171
+ ```
172
+
173
+ ### As a library
174
+
175
+ ```typescript
176
+ import {
177
+ SandboxManager,
178
+ type SandboxRuntimeConfig,
179
+ } from '@anthropic-ai/sandbox-runtime'
180
+ import { spawn } from 'child_process'
181
+
182
+ // Define your sandbox configuration
183
+ const config: SandboxRuntimeConfig = {
184
+ network: {
185
+ allowedDomains: ['example.com', 'api.github.com'],
186
+ deniedDomains: [],
187
+ },
188
+ filesystem: {
189
+ denyRead: ['~/.ssh'],
190
+ allowWrite: ['.', '/tmp'],
191
+ denyWrite: ['.env'],
192
+ },
193
+ }
194
+
195
+ // Initialize the sandbox (starts proxy servers, etc.)
196
+ await SandboxManager.initialize(config)
197
+
198
+ // Wrap a command with sandbox restrictions
199
+ const sandboxedCommand = await SandboxManager.wrapWithSandbox(
200
+ 'curl https://example.com',
201
+ )
202
+
203
+ // Execute the sandboxed command
204
+ const child = spawn(sandboxedCommand, { shell: true, stdio: 'inherit' })
205
+
206
+ // Handle exit and cleanup after child process completes
207
+ child.on('exit', async code => {
208
+ console.log(`Command exited with code ${code}`)
209
+ // Cleanup when done (optional, happens automatically on process exit)
210
+ await SandboxManager.reset()
211
+ })
212
+ ```
213
+
214
+ #### Available exports
215
+
216
+ ```typescript
217
+ // Main sandbox manager
218
+ export { SandboxManager } from '@anthropic-ai/sandbox-runtime'
219
+
220
+ // Violation tracking
221
+ export { SandboxViolationStore } from '@anthropic-ai/sandbox-runtime'
222
+
223
+ // TypeScript types
224
+ export type {
225
+ SandboxRuntimeConfig,
226
+ NetworkConfig,
227
+ FilesystemConfig,
228
+ IgnoreViolationsConfig,
229
+ SandboxAskCallback,
230
+ FsReadRestrictionConfig,
231
+ FsWriteRestrictionConfig,
232
+ NetworkRestrictionConfig,
233
+ } from '@anthropic-ai/sandbox-runtime'
234
+ ```
235
+
236
+ ## Configuration
237
+
238
+ ### Settings File Location
239
+
240
+ By default, the sandbox runtime looks for configuration at `~/.srt-settings.json`. You can specify a custom path using the `--settings` flag:
241
+
242
+ ```bash
243
+ srt --settings /path/to/srt-settings.json <command>
244
+ ```
245
+
246
+ ### Complete Configuration Example
247
+
248
+ ```json
249
+ {
250
+ "network": {
251
+ "allowedDomains": [
252
+ "github.com",
253
+ "*.github.com",
254
+ "lfs.github.com",
255
+ "api.github.com",
256
+ "npmjs.org",
257
+ "*.npmjs.org"
258
+ ],
259
+ "deniedDomains": ["malicious.com"],
260
+ "allowUnixSockets": ["/var/run/docker.sock"],
261
+ "allowLocalBinding": false
262
+ },
263
+ "filesystem": {
264
+ "denyRead": ["~/.ssh"],
265
+ "allowRead": [],
266
+ "allowWrite": [".", "src/", "test/", "/tmp"],
267
+ "denyWrite": [".env", "config/production.json"]
268
+ },
269
+ "ignoreViolations": {
270
+ "*": ["/usr/bin", "/System"],
271
+ "git push": ["/usr/bin/nc"],
272
+ "npm": ["/private/tmp"]
273
+ },
274
+ "enableWeakerNestedSandbox": false,
275
+ "enableWeakerNetworkIsolation": false
276
+ }
277
+ ```
278
+
279
+ ### Configuration Options
280
+
281
+ #### Network Configuration
282
+
283
+ Uses an **allow-only pattern** - all network access is denied by default.
284
+
285
+ - `network.allowedDomains` - Array of allowed domains (supports wildcards like `*.example.com`). Empty array = no network access.
286
+ - `network.deniedDomains` - Array of denied domains (checked first, takes precedence over allowedDomains)
287
+ - `network.allowLocalBinding` - Allow binding to local ports (boolean, default: false)
288
+
289
+ **Unix Socket Settings** (platform-specific behavior):
290
+
291
+ | Setting | macOS | Linux |
292
+ |---------|-------|-------|
293
+ | `allowUnixSockets: string[]` | Allowlist of socket paths | *Ignored* (seccomp can't filter by path) |
294
+ | `allowAllUnixSockets: boolean` | Allow all sockets | Disable seccomp blocking |
295
+
296
+ Unix sockets are **blocked by default** on both platforms.
297
+
298
+ - **macOS**: Use `allowUnixSockets` to allow specific paths (e.g., `["/var/run/docker.sock"]`), or `allowAllUnixSockets: true` to allow all.
299
+ - **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.
300
+
301
+ #### Filesystem Configuration
302
+
303
+ Uses two different patterns:
304
+
305
+ **Read restrictions** (deny-then-allow pattern) - all reads allowed by default:
306
+
307
+ - `filesystem.denyRead` - Array of paths to deny read access. Empty array = full read access.
308
+ - `filesystem.allowRead` - Array of paths to re-allow read access within denied regions (takes precedence over denyRead). **Note:** this is the opposite of write, where `denyWrite` takes precedence over `allowWrite`.
309
+
310
+ **Write restrictions** (allow-only pattern) - all writes denied by default:
311
+
312
+ - `filesystem.allowWrite` - Array of paths to allow write access. Empty array = no write access.
313
+ - `filesystem.denyWrite` - Array of paths to deny write access within allowed paths (takes precedence over allowWrite)
314
+
315
+ **Path Syntax (macOS):**
316
+
317
+ Paths support git-style glob patterns on macOS, similar to `.gitignore` syntax:
318
+
319
+ - `*` - Matches any characters except `/` (e.g., `*.ts` matches `foo.ts` but not `foo/bar.ts`)
320
+ - `**` - Matches any characters including `/` (e.g., `src/**/*.ts` matches all `.ts` files in `src/`)
321
+ - `?` - Matches any single character except `/` (e.g., `file?.txt` matches `file1.txt`)
322
+ - `[abc]` - Matches any character in the set (e.g., `file[0-9].txt` matches `file3.txt`)
323
+
324
+ Examples:
325
+
326
+ - `"allowWrite": ["src/"]` - Allow write to entire `src/` directory
327
+ - `"allowWrite": ["src/**/*.ts"]` - Allow write to all `.ts` files in `src/` and subdirectories
328
+ - `"denyRead": ["~/.ssh"]` - Deny read to SSH directory
329
+ - `"denyRead": ["/Users"], "allowRead": ["."]` - Deny read to all of `/Users`, but re-allow the current directory
330
+ - `"denyWrite": [".env"]` - Deny write to `.env` file (even if current directory is allowed)
331
+
332
+ **Path Syntax (Linux):**
333
+
334
+ **Linux currently does not support glob matching.** Use literal paths only:
335
+
336
+ - `"allowWrite": ["src/"]` - Allow write to `src/` directory
337
+ - `"denyRead": ["/home/user/.ssh"]` - Deny read to SSH directory
338
+ - `"denyRead": ["/home"], "allowRead": ["."]` - Deny read to all of `/home`, but re-allow the current directory
339
+
340
+ **All platforms:**
341
+
342
+ - Paths can be absolute (e.g., `/home/user/.ssh`) or relative to the current working directory (e.g., `./src`)
343
+ - `~` expands to the user's home directory
344
+
345
+ #### Other Configuration
346
+
347
+ - `ignoreViolations` - Object mapping command patterns to arrays of paths where violations should be ignored
348
+ - `enableWeakerNestedSandbox` - Enable weaker sandbox mode for Docker environments (boolean, default: false)
349
+ - `enableWeakerNetworkIsolation` - Allow access to `com.apple.trustd.agent` in the macOS sandbox (boolean, default: false). This is needed for Go programs (`gh`, `gcloud`, `terraform`, `kubectl`, etc.) to verify TLS certificates when using `httpProxyPort` with a MITM proxy and custom CA. **Security warning:** enabling this opens a potential data exfiltration vector through the trustd service.
350
+
351
+ ### Common Configuration Recipes
352
+
353
+ **Allow GitHub access** (all necessary endpoints):
354
+
355
+ ```json
356
+ {
357
+ "network": {
358
+ "allowedDomains": [
359
+ "github.com",
360
+ "*.github.com",
361
+ "lfs.github.com",
362
+ "api.github.com"
363
+ ],
364
+ "deniedDomains": []
365
+ },
366
+ "filesystem": {
367
+ "denyRead": [],
368
+ "allowWrite": ["."],
369
+ "denyWrite": []
370
+ }
371
+ }
372
+ ```
373
+
374
+ **Restrict to specific directories:**
375
+
376
+ ```json
377
+ {
378
+ "network": {
379
+ "allowedDomains": [],
380
+ "deniedDomains": []
381
+ },
382
+ "filesystem": {
383
+ "denyRead": ["~/.ssh"],
384
+ "allowWrite": [".", "src/", "test/"],
385
+ "denyWrite": [".env", "secrets/"]
386
+ }
387
+ }
388
+ ```
389
+
390
+ **Workspace-only filesystem access** (deny reads outside the workspace):
391
+
392
+ ```json
393
+ {
394
+ "network": {
395
+ "allowedDomains": [],
396
+ "deniedDomains": []
397
+ },
398
+ "filesystem": {
399
+ "denyRead": ["/Users"],
400
+ "allowRead": ["."],
401
+ "allowWrite": ["."],
402
+ "denyWrite": []
403
+ }
404
+ }
405
+ ```
406
+
407
+ This denies reading anything under `/Users` (or `/home` on Linux), then re-allows the current working directory. System paths (`/usr`, `/lib`, etc.) remain readable.
408
+
409
+ ### Common Issues and Tips
410
+
411
+ **Running Jest:** Use `--no-watchman` flag to avoid sandbox violations:
412
+
413
+ ```bash
414
+ srt "jest --no-watchman"
415
+ ```
416
+
417
+ Watchman accesses files outside the sandbox boundaries, which will trigger permission errors. Disabling it allows Jest to run with the built-in file watcher instead.
418
+
419
+ ## Platform Support
420
+
421
+ - **macOS**: Uses `sandbox-exec` with custom profiles (no additional dependencies)
422
+ - **Linux**: Uses `bubblewrap` (bwrap) for containerization
423
+ - **Windows**: Not yet supported
424
+
425
+ ### Platform-Specific Dependencies
426
+
427
+ **Linux requires:**
428
+
429
+ - `bubblewrap` - Container runtime
430
+ - Ubuntu/Debian: `apt-get install bubblewrap`
431
+ - Fedora: `dnf install bubblewrap`
432
+ - Arch: `pacman -S bubblewrap`
433
+ - `socat` - Socket relay for proxy bridging
434
+ - Ubuntu/Debian: `apt-get install socat`
435
+ - Fedora: `dnf install socat`
436
+ - Arch: `pacman -S socat`
437
+ - `ripgrep` - Fast search tool for deny path detection
438
+ - Ubuntu/Debian: `apt-get install ripgrep`
439
+ - Fedora: `dnf install ripgrep`
440
+ - Arch: `pacman -S ripgrep`
441
+
442
+ **Optional Linux dependencies (for seccomp fallback):**
443
+
444
+ The package includes pre-generated seccomp BPF filters for x86-64 and arm architectures. These dependencies are only needed if you are on a different architecture where pre-generated filters are not available:
445
+
446
+ - `gcc` or `clang` - C compiler
447
+ - `libseccomp-dev` - Seccomp library development files
448
+ - Ubuntu/Debian: `apt-get install gcc libseccomp-dev`
449
+ - Fedora: `dnf install gcc libseccomp-devel`
450
+ - Arch: `pacman -S gcc libseccomp`
451
+
452
+ **macOS requires:**
453
+
454
+ - `ripgrep` - Fast search tool for deny path detection
455
+ - Install via Homebrew: `brew install ripgrep`
456
+ - Or download from: https://github.com/BurntSushi/ripgrep/releases
457
+
458
+ ## Development
459
+
460
+ ```bash
461
+ # Install dependencies
462
+ npm install
463
+
464
+ # Build the project
465
+ npm run build
466
+
467
+ # Build seccomp binaries (requires Docker)
468
+ npm run build:seccomp
469
+
470
+ # Run tests
471
+ npm test
472
+
473
+ # Run integration tests
474
+ npm run test:integration
475
+
476
+ # Type checking
477
+ npm run typecheck
478
+
479
+ # Lint code
480
+ npm run lint
481
+
482
+ # Format code
483
+ npm run format
484
+ ```
485
+
486
+ ### Building Seccomp Binaries
487
+
488
+ The pre-generated BPF filters are included in the repository, but you can rebuild them if needed:
489
+
490
+ ```bash
491
+ npm run build:seccomp
492
+ ```
493
+
494
+ This script uses Docker to cross-compile seccomp binaries for multiple architectures:
495
+
496
+ - x64 (x86-64)
497
+ - arm64 (aarch64)
498
+
499
+ The script builds static generator binaries, generates the BPF filters (~104 bytes each), and stores them in `vendor/seccomp/x64/` and `vendor/seccomp/arm64/`. The generator binaries are removed to keep the package size small.
500
+
501
+ ## Implementation Details
502
+
503
+ ### Network Isolation Architecture
504
+
505
+ The sandbox runs HTTP and SOCKS5 proxy servers on the host machine that filter all network requests based on permission rules:
506
+
507
+ 1. **HTTP/HTTPS Traffic**: An HTTP proxy server intercepts requests and validates them against allowed/denied domains
508
+ 2. **Other Network Traffic**: A SOCKS5 proxy handles all other TCP connections (SSH, database connections, etc.)
509
+ 3. **Permission Enforcement**: The proxies enforce the `permissions` rules from your configuration
510
+
511
+ **Platform-specific proxy communication:**
512
+
513
+ - **Linux**: Requests are routed via the filesystem over Unix domain sockets (using `socat` for bridging). The network namespace is removed from the bubblewrap container, ensuring all network traffic must go through the proxies.
514
+
515
+ - **macOS**: The Seatbelt profile allows communication only to specific localhost ports where the proxies listen. All other network access is blocked.
516
+
517
+ ### Filesystem Isolation
518
+
519
+ Filesystem restrictions are enforced at the OS level:
520
+
521
+ - **macOS**: Uses `sandbox-exec` with dynamically generated Seatbelt profiles that specify allowed read/write paths
522
+ - **Linux**: Uses `bubblewrap` with bind mounts, marking directories as read-only or read-write based on configuration
523
+
524
+ **Default filesystem permissions:**
525
+
526
+ - **Read** (deny-then-allow): Allowed everywhere by default. You can deny broad regions, then re-allow specific paths within them. `allowRead` takes precedence over `denyRead`.
527
+
528
+ - Example: `denyRead: ["~/.ssh"]` to block access to SSH keys
529
+ - Example: `denyRead: ["/Users"], allowRead: ["."]` to block all of `/Users` except the workspace
530
+ - Empty `denyRead: []` = full read access (nothing denied)
531
+
532
+ - **Write** (allow-only): Denied everywhere by default. You must explicitly allow paths.
533
+ - Example: `allowWrite: [".", "/tmp"]` to allow writes to current directory and /tmp
534
+ - Empty `allowWrite: []` = no write access (nothing allowed)
535
+ - `denyWrite` creates exceptions within allowed paths (deny takes precedence)
536
+
537
+ **Precedence is intentionally opposite for reads vs writes:** `allowRead` overrides `denyRead`, while `denyWrite` overrides `allowWrite`. This lets you carve out readable regions within denied areas, and carve out protected regions within writable areas.
538
+
539
+ ### Mandatory Deny Paths (Auto-Protected Files)
540
+
541
+ Certain sensitive files and directories are **always blocked from writes**, even if they fall within an allowed write path. This provides defense-in-depth against sandbox escapes and configuration tampering.
542
+
543
+ **Always-blocked files:**
544
+
545
+ - Shell config files: `.bashrc`, `.bash_profile`, `.zshrc`, `.zprofile`, `.profile`
546
+ - Git config files: `.gitconfig`, `.gitmodules`
547
+ - Other sensitive files: `.ripgreprc`, `.mcp.json`
548
+
549
+ **Always-blocked directories:**
550
+
551
+ - IDE directories: `.vscode/`, `.idea/`
552
+ - Claude config directories: `.claude/commands/`, `.claude/agents/`
553
+ - Git hooks and config: `.git/hooks/`, `.git/config`
554
+
555
+ These paths are blocked automatically - you don't need to add them to `denyWrite`. For example, even with `allowWrite: ["."]`, writing to `.bashrc` or `.git/hooks/pre-commit` will fail:
556
+
557
+ ```bash
558
+ $ srt 'echo "malicious" >> .bashrc'
559
+ /bin/bash: .bashrc: Operation not permitted
560
+
561
+ $ srt 'echo "bad" > .git/hooks/pre-commit'
562
+ /bin/bash: .git/hooks/pre-commit: Operation not permitted
563
+ ```
564
+
565
+ **Note (Linux):** On Linux, mandatory deny paths only block files that already exist. Non-existent files in these patterns cannot be blocked by bubblewrap's bind-mount approach. macOS uses glob patterns which block both existing and new files.
566
+
567
+ **Linux search depth:** On Linux, the sandbox uses `ripgrep` to scan for dangerous files in subdirectories within allowed write paths. By default, it searches up to 3 levels deep for performance. You can configure this with `mandatoryDenySearchDepth`:
568
+
569
+ ```json
570
+ {
571
+ "mandatoryDenySearchDepth": 5,
572
+ "filesystem": {
573
+ "allowWrite": ["."]
574
+ }
575
+ }
576
+ ```
577
+
578
+ - Default: `3` (searches up to 3 levels deep)
579
+ - Range: `1` to `10`
580
+ - Higher values provide more protection but slower performance
581
+ - Files in CWD (depth 0) are always protected regardless of this setting
582
+
583
+ ### Unix Socket Restrictions (Linux)
584
+
585
+ On Linux, the sandbox uses **seccomp BPF (Berkeley Packet Filter)** to block Unix domain socket creation at the syscall level. This provides an additional layer of security to prevent processes from creating new Unix domain sockets for local IPC (unless explicitly allowed).
586
+
587
+ **How it works:**
588
+
589
+ 1. **Pre-generated BPF filters**: The package includes pre-compiled BPF filters for different architectures (x64, ARM64). These are ~104 bytes each and stored in `vendor/seccomp/`. The filters are architecture-specific but libc-independent, so they work with both glibc and musl.
590
+
591
+ 2. **Runtime detection**: The sandbox automatically detects your system's architecture and loads the appropriate pre-generated BPF filter.
592
+
593
+ 3. **Syscall filtering**: The BPF filter intercepts the `socket()` syscall and blocks creation of `AF_UNIX` sockets by returning `EPERM`. This prevents sandboxed code from creating new Unix domain sockets.
594
+
595
+ 4. **Two-stage application using apply-seccomp binary**:
596
+ - Outer bwrap creates the sandbox with filesystem, network, and PID namespace restrictions
597
+ - Network bridging processes (socat) start inside the sandbox (need Unix sockets)
598
+ - apply-seccomp binary applies the seccomp filter via `prctl()`
599
+ - apply-seccomp execs the user command with seccomp active
600
+ - User command runs with all sandbox restrictions plus Unix socket creation blocking
601
+
602
+ **Security limitations**: The filter only blocks `socket(AF_UNIX, ...)` syscalls. It does not prevent operations on Unix socket file descriptors inherited from parent processes or passed via `SCM_RIGHTS`. For most sandboxing scenarios, blocking socket creation is sufficient to prevent unauthorized IPC.
603
+
604
+ **Zero runtime dependencies**: Pre-built static apply-seccomp binaries and pre-generated BPF filters are included for x64 and arm64 architectures. No compilation tools or external dependencies required at runtime.
605
+
606
+ **Architecture support**: x64 and arm64 are fully supported with pre-built binaries. Other architectures are not currently supported. To use sandboxing without Unix socket blocking on unsupported architectures, set `allowAllUnixSockets: true` in your configuration.
607
+
608
+ ### Violation Detection and Monitoring
609
+
610
+ When a sandboxed process attempts to access a restricted resource:
611
+
612
+ 1. **Blocks the operation** at the OS level (returns `EPERM` error)
613
+ 2. **Logs the violation** (platform-specific mechanisms)
614
+ 3. **Notifies the user** (in Claude Code, this triggers a permission prompt)
615
+
616
+ **macOS**: The sandbox runtime taps into macOS's system sandbox violation log store. This provides real-time notifications with detailed information about what was attempted and why it was blocked. This is the same mechanism Claude Code uses for violation detection.
617
+
618
+ ```bash
619
+ # View sandbox violations in real-time
620
+ log stream --predicate 'process == "sandbox-exec"' --style syslog
621
+ ```
622
+
623
+ **Linux**: Bubblewrap doesn't provide built-in violation reporting. Use `strace` to trace system calls and identify blocked operations:
624
+
625
+ ```bash
626
+ # Trace all denied operations
627
+ strace -f srt <your-command> 2>&1 | grep EPERM
628
+
629
+ # Trace specific file operations
630
+ strace -f -e trace=open,openat,stat,access srt <your-command> 2>&1 | grep EPERM
631
+
632
+ # Trace network operations
633
+ strace -f -e trace=network srt <your-command> 2>&1 | grep EPERM
634
+ ```
635
+
636
+ ### Advanced: Bring Your Own Proxy
637
+
638
+ For more sophisticated network filtering, you can configure the sandbox to use your own proxy instead of the built-in ones. This enables:
639
+
640
+ - **Traffic inspection**: Use tools like [mitmproxy](https://mitmproxy.org/) to inspect and modify traffic
641
+ - **Custom filtering logic**: Implement complex rules beyond simple domain allowlists
642
+ - **Audit logging**: Log all network requests for compliance or debugging
643
+
644
+ **Example with mitmproxy:**
645
+
646
+ ```bash
647
+ # Start mitmproxy with custom filtering script
648
+ mitmproxy -s custom_filter.py --listen-port 8888
649
+ ```
650
+
651
+ Note: Custom proxy configuration is not yet supported in the new configuration format. This feature will be added in a future release.
652
+
653
+ **Important security consideration:** Even with domain allowlists, exfiltration vectors may exist. For example, allowing `github.com` lets a process push to any repository. With a custom MITM proxy and proper certificate setup, you can inspect and filter specific API calls to prevent this.
654
+
655
+ ### Security Limitations
656
+
657
+ - Network Sandboxing Limitations: The network filtering system operates by restricting the domains that processes are allowed to connect to. It does not otherwise inspect the traffic passing through the proxy and users are responsible for ensuring they only allow trusted domains in their policy.
658
+
659
+ <Warning>
660
+ Users should be aware of potential risks that come from allowing broad domains like `github.com` that may allow for data exfiltration. Also, in some cases it may be possible to bypass the network filtering through [domain fronting](https://en.wikipedia.org/wiki/Domain_fronting).
661
+ </Warning>
662
+
663
+ - Privilege Escalation via Unix Sockets: The `allowUnixSockets` configuration can inadvertently grant access to powerful system services that could lead to sandbox bypasses. For example, if it is used to allow access to `/var/run/docker.sock` this would effectively grant access to the host system through exploiting the docker socket. Users are encouraged to carefully consider any unix sockets that they allow through the sandbox.
664
+ - Filesystem Permission Escalation: Overly broad filesystem write permissions can enable privilege escalation attacks. Allowing writes to directories containing executables in `$PATH`, system configuration directories, or user shell configuration files (`.bashrc`, `.zshrc`) can lead to code execution in different security contexts when other users or system processes access these files.
665
+ - Linux Sandbox Strength: The Linux implementation provides strong filesystem and network isolation but includes an `enableWeakerNestedSandbox` mode that enables it to work inside of Docker environments without privileged namespaces. This option considerably weakens security and should only be used incases where additional isolation is otherwise enforced.
666
+ - Weaker Network Isolation (macOS): The `enableWeakerNetworkIsolation` option re-enables access to `com.apple.trustd.agent`, which is needed for Go programs to verify TLS certificates via the macOS Security framework. This opens a potential data exfiltration vector through the trustd service and should only be enabled when Go TLS verification is required (e.g., when using `httpProxyPort` with a MITM proxy and custom CA).
667
+
668
+ ### Known Limitations and Future Work
669
+
670
+ **Linux proxy bypass**: Currently uses environment variables (`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`) to direct traffic through proxies. This works for most applications but may be ignored by programs that don't respect these variables, leading to them being unable to connect to the internet.
671
+
672
+ **Future improvements:**
673
+
674
+ - **Proxychains support**: Add support for `proxychains` with `LD_PRELOAD` on Linux to intercept network calls at a lower level, making bypass more difficult
675
+
676
+ - **Linux violation monitoring**: Implement automatic `strace`-based violation detection for Linux, integrated with the violation store. Currently, Linux users must manually run `strace` to see violations, unlike macOS which has automatic violation monitoring via the system log store
package/dist/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}