|
1 | | -# Secure Exec SDK |
| 1 | +# Secure Exec |
2 | 2 |
|
3 | | -Run sandboxed Node.js code using a driver-based runtime. |
| 3 | +**Secure Node.js Execution Without a Sandbox** |
4 | 4 |
|
5 | | -## Features |
| 5 | +A lightweight library for secure Node.js execution. No containers, no VMs — just npm-compatible sandboxing out of the box. Powered by the same tech as Cloudflare Workers. |
6 | 6 |
|
7 | | -- **Minimal overhead**: TODO |
8 | | -- **Just a library**: TODO |
9 | | -- **Low memory overhead**: TODO |
| 7 | +``` |
| 8 | +npm install secure-exec |
| 9 | +``` |
10 | 10 |
|
11 | | -TODO: |
| 11 | +## Give your AI agent secure code execution |
12 | 12 |
|
13 | | -- **Node runtime**: isolated-vm backed sandbox execution with driver-owned capability wiring. |
14 | | -- **Browser runtime**: Worker-backed execution through `NodeRuntime` + browser driver factories. |
15 | | -- **Driver-based**: Provide a driver to map filesystem, network, and child_process. |
16 | | -- **Permissions**: Gate syscalls with custom allow/deny functions. |
17 | | -- **Opt-in system features**: Disable network/child_process/FS by omission. |
| 13 | +Expose secure-exec as a tool with the Vercel AI SDK. Your agent can execute arbitrary code without risking your infrastructure. |
18 | 14 |
|
19 | | -## Examples |
| 15 | +```typescript |
| 16 | +import { generateText, tool } from "ai"; |
| 17 | +import { anthropic } from "@ai-sdk/anthropic"; |
| 18 | +import { NodeRuntime, createNodeDriver, createNodeRuntimeDriverFactory } from "secure-exec"; |
| 19 | +import { z } from "zod"; |
20 | 20 |
|
21 | | -- Browser playground: `pnpm -C packages/playground dev` |
| 21 | +const runtime = new NodeRuntime({ |
| 22 | + systemDriver: createNodeDriver({ permissions: { fs: true, network: true } }), |
| 23 | + runtimeDriverFactory: createNodeRuntimeDriverFactory(), |
| 24 | + memoryLimit: 64, |
| 25 | + cpuTimeLimitMs: 5000, |
| 26 | +}); |
| 27 | + |
| 28 | +const result = await generateText({ |
| 29 | + model: anthropic("claude-sonnet-4-20250514"), |
| 30 | + tools: { |
| 31 | + execute: tool({ |
| 32 | + description: "Run JavaScript in a secure sandbox", |
| 33 | + parameters: z.object({ code: z.string() }), |
| 34 | + execute: async ({ code }) => { |
| 35 | + const logs: string[] = []; |
| 36 | + const res = await runtime.exec(code, { |
| 37 | + onStdio: (e) => logs.push(e.message), |
| 38 | + }); |
| 39 | + return { exitCode: res.code, output: logs.join("\n") }; |
| 40 | + }, |
| 41 | + }), |
| 42 | + }, |
| 43 | + prompt: "Calculate the first 20 fibonacci numbers", |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +## Why Secure Exec |
| 48 | + |
| 49 | +Give your AI agent the ability to write and run code safely. |
| 50 | + |
| 51 | +- **No infrastructure required** — No Docker daemon, no hypervisor, no orchestrator. Runs anywhere Node.js, Bun, or an HTML5 browser runs. Deploy to Lambda, a VPS, or a static site — your existing deployment works. |
| 52 | +- **Node.js & npm compatibility** — fs, child_process, http, dns, process, os — bridged to real host capabilities, not stubbed. Run Express, Hono, Next.js, and any npm package. [Compatibility matrix →](https://secure-exec.dev/docs/node-compatability) |
| 53 | +- **Built for AI agents** — Give your AI agent the ability to write and run code safely. Works with the Vercel AI SDK, LangChain, and any tool-use framework. |
| 54 | +- **Deny-by-default permissions** — Filesystem, network, child processes, and env vars are all blocked unless explicitly allowed. Permissions are composable functions — grant read but not write, allow fetch but block spawn. |
| 55 | +- **Configurable resource limits** — CPU time budgets and memory caps. Runaway code is terminated deterministically with exit code 124 — no OOM crashes, no infinite loops, no host exhaustion. |
| 56 | +- **Powered by V8 isolates** — The same isolation primitive behind Cloudflare Workers for Platforms and every browser tab. Battle-tested at scale by the infrastructure you already trust. |
| 57 | + |
| 58 | +## Benchmarks |
| 59 | + |
| 60 | +V8 isolates vs. sandboxes. |
| 61 | + |
| 62 | +### Cold start |
| 63 | + |
| 64 | +| Percentile | Secure Exec | Fastest sandbox | |
| 65 | +|------------|-------------|-----------------| |
| 66 | +| p50 | 16.2 ms | 440 ms | |
| 67 | +| p95 | 17.9 ms | 950 ms | |
| 68 | +| p99 | 17.9 ms | 3,150 ms | |
| 69 | + |
| 70 | +**What's measured:** Time from requesting an execution to first code running. Secure Exec spins up a V8 isolate inside the host process — no container, no VM, no network hop. Sandbox baseline: [e2b](https://www.computesdk.com/benchmarks/), the fastest provider on ComputeSDK as of March 18, 2026. Secure Exec numbers: median of 10,000 runs (100 iterations × 100 samples) on Intel i7-12700KF. |
| 71 | + |
| 72 | +### Memory per instance |
| 73 | + |
| 74 | +| Runtime | Memory | |
| 75 | +|--------------------------|-----------| |
| 76 | +| Secure Exec | ~3.4 MB | |
| 77 | +| Sandbox provider minimum | ~256 MB | |
| 78 | + |
| 79 | +**75x smaller.** V8 isolates share the host process and its V8 engine. Each additional execution only adds its own heap and stack. On a 1 GB server, you can run ~210 concurrent Secure Exec executions vs. ~4 sandboxes. |
| 80 | + |
| 81 | +### Cost per execution-second |
| 82 | + |
| 83 | +| Hardware | Secure Exec | vs. cheapest sandbox ($0.000625/s) | |
| 84 | +|--------------|------------------|------------------------------------| |
| 85 | +| AWS ARM | $0.000011/s | 56x cheaper | |
| 86 | +| AWS x86 | $0.000014/s | 45x cheaper | |
| 87 | +| Hetzner ARM | $0.0000016/s | 380x cheaper | |
| 88 | +| Hetzner x86 | $0.0000027/s | 232x cheaper | |
| 89 | + |
| 90 | +Each execution uses ~3.4 MB instead of a 256 MB container minimum, and you run on your own hardware. Sandbox baseline: Cloudflare Containers, billed at $0.0000025/GiB·s with 256 MB minimum. |
| 91 | + |
| 92 | +## Secure Exec vs. Sandboxes |
| 93 | + |
| 94 | +Same isolation guarantees, without the infrastructure overhead. |
| 95 | + |
| 96 | +**Secure Exec:** |
| 97 | +- ✓ Native V8 performance |
| 98 | +- ✓ Granular deny-by-default permissions |
| 99 | +- ✓ Just npm install — no vendor account |
| 100 | +- ✓ No API keys to manage |
| 101 | +- ✓ Run on any cloud or hardware |
| 102 | +- ✓ No egress fees |
| 103 | + |
| 104 | +**Sandbox:** |
| 105 | +- ✓ Native container performance |
| 106 | +- ✗ Coarse-grained permissions |
| 107 | +- ✗ Vendor account required |
| 108 | +- ✗ API keys to manage |
| 109 | +- ✗ Hardware lock-in |
| 110 | +- ✗ Per-GB egress fees |
| 111 | + |
| 112 | +[Full comparison →](https://secure-exec.dev/docs/sandbox-vs-secure-exec) |
| 113 | + |
| 114 | +## FAQ |
| 115 | + |
| 116 | +<details> |
| 117 | +<summary>How does it work?</summary> |
| 118 | + |
| 119 | +Secure Exec runs untrusted code inside [V8 isolates](https://v8.dev/docs/embed) — the same isolation primitive that powers every Chromium tab and Cloudflare Workers. Each execution gets its own heap, its own globals, and a deny-by-default permission boundary. There is no container, no VM, and no Docker daemon — just fast, lightweight isolation using battle-tested web technology. [Architecture →](https://secure-exec.dev/docs/sdk-overview) |
| 120 | +</details> |
| 121 | + |
| 122 | +<details> |
| 123 | +<summary>Does this require Docker, nested virtualization, or a hypervisor?</summary> |
| 124 | + |
| 125 | +No. Secure Exec is a pure npm package — `npm install secure-exec` is all you need. It has zero infrastructure dependencies: no Docker daemon, no hypervisor, no orchestrator, no sidecar. It runs anywhere Node.js or Bun runs. |
| 126 | +</details> |
| 127 | + |
| 128 | +<details> |
| 129 | +<summary>Can it run in serverless environments?</summary> |
| 130 | + |
| 131 | +We are actively validating serverless platforms, but Secure Exec should work everywhere that provides a standard Node.js-like runtime. This includes Vercel Fluid Compute, AWS Lambda, and Google Cloud Run. Cloudflare Workers is not supported because it does not expose the V8 APIs that Secure Exec relies on. |
| 132 | +</details> |
| 133 | + |
| 134 | +<details> |
| 135 | +<summary>When should I use a sandbox vs. Secure Exec?</summary> |
| 136 | + |
| 137 | +Use **Secure Exec** when you need fast, lightweight code execution — AI tool calls, code evaluation, user-submitted scripts — without provisioning infrastructure. Use a **sandbox** (e2b, Modal, Daytona) when you need a full operating-system environment with persistent disk, root access, or GPU passthrough. [Full comparison →](https://secure-exec.dev/docs/sandbox-vs-secure-exec) |
| 138 | +</details> |
| 139 | + |
| 140 | +<details> |
| 141 | +<summary>Can I run npm install in Secure Exec to dynamically install modules?</summary> |
| 142 | + |
| 143 | +Yes. Secure Exec supports dynamic module installation via npm inside the execution environment. |
| 144 | +</details> |
| 145 | + |
| 146 | +<details> |
| 147 | +<summary>Can I use it to run dev servers like Express, Hono, or Next.js?</summary> |
| 148 | + |
| 149 | +Yes. Secure Exec bridges Node.js APIs including http, net, and child_process, so frameworks like Express, Hono, and Next.js work out of the box. |
| 150 | +</details> |
| 151 | + |
| 152 | +<details> |
| 153 | +<summary>Can it be used for long-running tasks?</summary> |
| 154 | + |
| 155 | +Yes. For orchestrating stateful, long-running processes efficiently, we recommend pairing Secure Exec with [Rivet Actors](https://rivet.dev/docs/actors). |
| 156 | +</details> |
| 157 | + |
| 158 | +<details> |
| 159 | +<summary>What are common use cases?</summary> |
| 160 | + |
| 161 | +- AI agent code evaluation and tool use |
| 162 | +- User-facing dev servers (Express, Hono, Next.js) |
| 163 | +- MCP tool-code execution |
| 164 | +- Sandboxed plugin / extension systems |
| 165 | +- Interactive coding playgrounds |
| 166 | +</details> |
| 167 | + |
| 168 | +<details> |
| 169 | +<summary>Does this have Node.js compatibility?</summary> |
| 170 | + |
| 171 | +Yes. Most Node.js core modules work — including fs, child_process, http, dns, process, and os. These are bridged to real host capabilities, not stubbed. [Compatibility matrix →](https://secure-exec.dev/docs/node-compatability) |
| 172 | +</details> |
| 173 | + |
| 174 | +<details> |
| 175 | +<summary>Does this have access to a full operating system?</summary> |
| 176 | + |
| 177 | +Yes. Secure Exec includes a virtual kernel with a system bridge that supports a granular permission model. Filesystem, network, child processes, and environment variables are all available — gated behind deny-by-default permissions. |
| 178 | +</details> |
| 179 | + |
| 180 | +<details> |
| 181 | +<summary>How does Secure Exec compare to WASM-based JavaScript runtimes like QuickJS?</summary> |
| 182 | + |
| 183 | +WASM-based runtimes like [QuickJS](https://bellard.org/quickjs/) (via quickjs-emscripten) compile a separate JS engine to WebAssembly, which means your code runs through an interpreter inside WASM — not native V8. Secure Exec uses native V8 isolates directly, so you get the same JIT-compiled performance as JavaScript running on the host. No interpretation overhead, no WASM translation layer, and full Node.js API compatibility. |
| 184 | +</details> |
| 185 | + |
| 186 | +## Links |
| 187 | + |
| 188 | +- [Documentation](https://secure-exec.dev/docs) |
| 189 | +- [Changelog](https://github.com/rivet-dev/secure-exec/releases) |
| 190 | +- [Discord](https://rivet.dev/discord) |
| 191 | +- [GitHub](https://github.com/rivet-dev/secure-exec) |
22 | 192 |
|
23 | 193 | ## License |
24 | 194 |
|
|
0 commit comments