afshinm/zerobox
GitHub: afshinm/zerobox
轻量级跨平台进程沙箱,支持细粒度的文件、网络和凭据控制,专为安全运行 AI 生成代码和不可信命令而设计。
Stars: 362 | Forks: 16
轻量级、跨平台的进程沙箱,由 [OpenAI Codex](https://github.com/openai/codex) 的沙箱运行时提供支持。
- **默认拒绝:** 写入、网络和环境变量默认被阻止,除非您明确允许
- **凭据注入:** 传递进程无法看到的 API 密钥。Zerobox 仅为批准的主机注入真实值
- **文件访问控制:** 允许或拒绝对特定路径的读取和写入
- **网络过滤:** 按域允许或拒绝出站流量
- **纯净环境:** 默认仅继承必要的环境变量(PATH、HOME 等)
- **TypeScript SDK:** `import { Sandbox } from "zerobox"`,采用 Deno 风格的 API
- **跨平台:** 支持 macOS 和 Linux。Windows 支持计划中
- **单一二进制文件:** 无需 Docker,无需 VM,开销约 10ms
## 安装
### Shell (macOS / Linux)
```
curl -fsSL https://raw.githubusercontent.com/afshinm/zerobox/main/install.sh | sh
```
### npm
```
npm install -g zerobox
```
### 从源码构建
```
git clone https://github.com/afshinm/zerobox && cd zerobox
./scripts/sync.sh && cargo build --release -p zerobox
```
## 快速开始
运行一个禁止写入且无网络访问权限的命令:
```
zerobox -- node -e "console.log('hello')"
```
允许写入特定目录:
```
zerobox --allow-write=. -- node script.js
```
允许访问特定域名的网络:
```
zerobox --allow-net=api.openai.com -- node agent.js
```
将密钥传递给特定主机,内部进程永远看不到真实值:
```
zerobox --secret OPENAI_API_KEY=sk-proj-123 --secret-host OPENAI_API_KEY=api.openai.com -- node agent.js
```
使用 TypeScript SDK 执行相同操作:
```
import { Sandbox } from "zerobox";
const sandbox = Sandbox.create({
secrets: {
OPENAI_API_KEY: {
value: process.env.OPENAI_API_KEY,
hosts: ["api.openai.com"],
},
},
});
const output = await sandbox.sh`node agent.js`.text();
```
记录文件系统更改并在执行后撤销它们:
```
zerobox --restore --allow-write=. -- npm install
```
或者仅记录而不恢复,稍后检查并撤销:
```
zerobox --snapshot --allow-write=. -- npm install
zerobox snapshot list
zerobox snapshot diff
zerobox snapshot restore
```
## 架构
` | `--allow-read=/tmp,/data` | Restrict readable user data to listed paths. System libraries remain accessible. Default: all reads allowed. |
| `--deny-read ` | `--deny-read=/secret` | Block reading from these paths. Takes precedence over `--allow-read`. |
| `--allow-write [paths]` | `--allow-write=.` | Allow writing to these paths. Without a value, allows writing everywhere. Default: no writes. |
| `--deny-write ` | `--deny-write=./.git` | Block writing to these paths. Takes precedence over `--allow-write`. |
| `--allow-net [domains]` | `--allow-net=example.com` | Allow outbound network. Without a value, allows all domains. Default: no network. |
| `--deny-net ` | `--deny-net=evil.com` | Block network to these domains. Takes precedence over `--allow-net`. |
| `--env ` | `--env NODE_ENV=prod` | Set env var in the sandbox. Can be repeated. |
| `--allow-env [keys]` | `--allow-env=PATH,HOME` | Inherit parent env vars. Without a value, inherits all. Default: only PATH, HOME, USER, SHELL, TERM, LANG. |
| `--deny-env ` | `--deny-env=SECRET` | Drop these parent env vars. Takes precedence over `--allow-env`. |
| `--secret ` | `--secret API_KEY=sk-123` | Pass a secret. The process sees a placeholder; the real value is injected at the proxy for approved hosts. |
| `--secret-host ` | `--secret-host API_KEY=api.openai.com` | Restrict a secret to specific hosts. Without this, the secret is substituted for all hosts. |
| `-A`, `--allow-all` | `-A` | Grant all filesystem and network permissions. Env and secrets still apply. |
| `--no-sandbox` | `--no-sandbox` | Disable the sandbox entirely. |
| `--strict-sandbox` | `--strict-sandbox` | Require full sandbox (bubblewrap). Fail instead of falling back to weaker isolation. |
| `--debug` | `--debug` | Print sandbox config and proxy decisions to stderr. |
| `--snapshot` | `--snapshot` | Record filesystem changes during execution. |
| `--restore` | `--restore` | Record and restore tracked files to pre-execution state after exit. Implies `--snapshot`. |
| `--snapshot-path ` | `--snapshot-path=./src` | Paths to track for snapshots (default: cwd). |
| `--snapshot-exclude ` | `--snapshot-exclude=build` | Exclude patterns from snapshots. |
| `-C ` | `-C /workspace` | Set working directory for the sandboxed command. |
| `-V`, `--version` | `--version` | Print version. |
| `-h`, `--help` | `--help` | Print help. |
### Snapshot 子命令
| Command | Description |
|---------|-------------|
| `zerobox snapshot list` | List recorded sessions. |
| `zerobox snapshot diff ` | Show changes from a session. |
| `zerobox snapshot restore ` | Restore filesystem to a session's baseline. |
| `zerobox snapshot clean --older-than=` | Remove old snapshot sessions. |
## 许可证
Apache-2.0
标签:API安全, JSONLines, JSON输出, LangChain, MacOS, OpenAI, Streamlit, TypeScript SDK, 代码分析, 代码执行安全, 内存规避, 凭证管理, 单二进制文件, 可视化界面, 子域名枚举, 安全防护, 密钥注入, 文件系统控制, 无Docker, 无VM, 沙盒技术, 沙箱, 环境变量, 系统安全, 网络过滤, 访问控制, 轻量级, 进程隔离, 通知系统, 零信任