Initial Commit
This commit is contained in:
126
openspec/changes/enterprise-server-sandbox/proposal.md
Normal file
126
openspec/changes/enterprise-server-sandbox/proposal.md
Normal file
@@ -0,0 +1,126 @@
|
||||
# OpenSpec Change: enterprise-server-sandbox
|
||||
|
||||
- **Change ID**: enterprise-server-sandbox
|
||||
- **Package**: `@suaveplan/sandbox`
|
||||
- **Category**: server/infra
|
||||
- **Target path**: `server/infra/sandbox`
|
||||
- **Status**: PROPOSED
|
||||
- **Date**: 2026-04-24
|
||||
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/sandbox.md`
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
Deliver `@suaveplan/sandbox` on the genesis branch from scratch at enterprise quality. The package provides an isolated code execution sandbox for running untrusted or user-supplied JavaScript/TypeScript snippets. It supports two backends: a `WorkerSandbox` using Bun's `Worker` API (in-process isolation) and a `ProcessSandbox` using `Bun.spawn` for stronger OS-level isolation. Both expose `run(code, context, options): Promise<SandboxResult>` with configurable resource limits (CPU timeout, memory limit, I/O restrictions), structured output capture, and abort signal support. Genesis adds Zod-validated options, `@suaveplan/error` codes, OTel spans, and comprehensive security-oriented tests.
|
||||
|
||||
---
|
||||
|
||||
## Current-state audit (genesis)
|
||||
|
||||
Does not exist on genesis — fresh build.
|
||||
|
||||
---
|
||||
|
||||
## Available workspace deps on genesis
|
||||
|
||||
| Package | Role |
|
||||
|---|---|
|
||||
| `@suaveplan/error` (`workspace:^`) | Typed `SANDBOX.*` error codes |
|
||||
| `@suaveplan/types` (`workspace:^`) | `LoggerLike` |
|
||||
| `@suaveplan/logger` (`workspace:^`) | Structured logs |
|
||||
| `zod` (`catalog:forms`) | Options validation |
|
||||
| `@opentelemetry/api` (`catalog:observability`) | Spans + counters + histograms |
|
||||
| `tslib` (`catalog:build`) | Runtime helpers |
|
||||
|
||||
---
|
||||
|
||||
## Public API surface
|
||||
|
||||
```ts
|
||||
interface Sandbox {
|
||||
run(code: string, context?: SandboxContext, options?: RunOptions): Promise<SandboxResult>
|
||||
dispose(): Promise<void>
|
||||
}
|
||||
|
||||
function createWorkerSandbox(options?: SandboxOptions): Sandbox
|
||||
function createProcessSandbox(options?: SandboxOptions): Sandbox
|
||||
|
||||
interface SandboxOptions {
|
||||
timeoutMs?: number // default: 5000
|
||||
memoryLimitMb?: number // default: 128
|
||||
allowNetwork?: boolean // default: false
|
||||
allowFs?: boolean // default: false
|
||||
logger?: LoggerLike
|
||||
}
|
||||
|
||||
type SandboxContext = Record<string, unknown> // serializable globals injected into sandbox
|
||||
|
||||
interface RunOptions {
|
||||
timeoutMs?: number // override per-run
|
||||
signal?: AbortSignal
|
||||
}
|
||||
|
||||
interface SandboxResult {
|
||||
ok: boolean
|
||||
value?: unknown // returned value (JSON-serializable)
|
||||
stdout: string
|
||||
stderr: string
|
||||
duration: number
|
||||
exitCode?: number
|
||||
error?: string
|
||||
}
|
||||
|
||||
type SandboxErrorCode =
|
||||
| 'SANDBOX_TIMEOUT'
|
||||
| 'SANDBOX_MEMORY_EXCEEDED'
|
||||
| 'SANDBOX_EXECUTION_ERROR'
|
||||
| 'SANDBOX_INVALID_CODE'
|
||||
| 'SANDBOX_DISPOSED'
|
||||
|
||||
function registerSandboxErrorCodes(): void
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## File layout
|
||||
|
||||
```text
|
||||
server/infra/sandbox/
|
||||
package.json
|
||||
tsconfig.json
|
||||
vitest.config.ts
|
||||
src/
|
||||
index.ts
|
||||
error-codes.ts
|
||||
constants/index.ts
|
||||
types/
|
||||
sandbox.types.ts
|
||||
sandbox.types.md
|
||||
worker/
|
||||
worker-sandbox/
|
||||
worker-sandbox.ts
|
||||
worker-sandbox.md
|
||||
worker-sandbox.types.ts
|
||||
worker-sandbox.test.ts
|
||||
index.ts
|
||||
sandbox-worker-script.ts # Worker inner script
|
||||
process/
|
||||
process-sandbox/
|
||||
process-sandbox.ts
|
||||
process-sandbox.md
|
||||
process-sandbox.types.ts
|
||||
process-sandbox.test.ts
|
||||
index.ts
|
||||
tests/
|
||||
security.test.ts
|
||||
integration.test.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Impact
|
||||
|
||||
Downstream: user-defined rule engines, server-side template execution, AI tool calls. Server-only, Bun runtime required.
|
||||
|
||||
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/logger`.
|
||||
74
openspec/changes/enterprise-server-sandbox/tasks.md
Normal file
74
openspec/changes/enterprise-server-sandbox/tasks.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# Tasks: enterprise-server-sandbox
|
||||
|
||||
**Change ID**: enterprise-server-sandbox
|
||||
**Package**: `@suaveplan/sandbox`
|
||||
**Target path**: `server/infra/sandbox`
|
||||
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/logger`
|
||||
|
||||
---
|
||||
|
||||
## 0. Pre-flight (non-negotiable)
|
||||
|
||||
- [ ] 0.1 Confirm clean working tree (`git status` reports nothing) and no in-flight worktrees with uncommitted work (`git worktree list` then `git status` in each)
|
||||
- [ ] 0.2 If any worktrees exist under `.claude/worktrees/`, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (`git worktree remove`) before opening a new one — code that has been lost twice on main is the failure mode this guards against.
|
||||
- [ ] 0.3 Confirm the package set in `MANIFEST.md` matches `find packages -maxdepth 3 -name package.json` count. Drift here means a prior change archived without regenerating the manifest.
|
||||
|
||||
## 1. Scaffold
|
||||
|
||||
- [ ] Create directory tree per `proposal.md`.
|
||||
- [ ] `package.json` at `0.1.0`, `"sideEffects": false`, exports `"."` + `worker` + `process` subpaths.
|
||||
- [ ] Deps: `@suaveplan/error workspace:^`, `@suaveplan/types workspace:^`, `@suaveplan/logger workspace:^`, `zod catalog:forms`, `@opentelemetry/api catalog:observability`, `tslib catalog:build`.
|
||||
- [ ] DevDeps: `vitest catalog:testing`, `@vitest/coverage-istanbul catalog:testing`, `@suaveplan/testing workspace:^`.
|
||||
- [ ] `tsconfig.json` extending `library-node.json`; `vitest.config.ts` with `pool: 'forks'`, `setupFiles: ['@suaveplan/testing/polyfills']`.
|
||||
- [ ] Scripts: `lint`, `typecheck`, `test`, `test:coverage`, `build`, `docs:check`.
|
||||
|
||||
## 2. Types and errors
|
||||
|
||||
- [ ] Define all interfaces and Zod schemas (`SandboxOptionsSchema`, `RunOptionsSchema`) in `src/types/sandbox.types.ts`.
|
||||
- [ ] Write `sandbox.types.md` (≥200 words, both examples, API reference).
|
||||
- [ ] `src/error-codes.ts`: five codes; `registerSandboxErrorCodes()`; eager call.
|
||||
|
||||
## 3. Worker sandbox
|
||||
|
||||
- [ ] Write `src/worker/sandbox-worker-script.ts`: minimal worker entry that receives `{ code, context }` via `self.onmessage`, evaluates code in a `Function('context', code)(context)` with captured stdout/stderr, posts result back, handles errors.
|
||||
- [ ] Implement `WorkerSandbox` in `src/worker/worker-sandbox/worker-sandbox.ts`:
|
||||
- Creates a new `Worker(sandboxWorkerScript)` per `run()` call (for isolation reset).
|
||||
- Posts `{ code, context }` to worker; sets a timeout via `AbortController`; on timeout terminate worker and throw `SANDBOX_TIMEOUT`.
|
||||
- Captures postMessage response as `SandboxResult`.
|
||||
- `dispose()`: no-op (workers are per-run).
|
||||
- OTel: span `sandbox.run`, counter `sandbox.runs_total`, histogram `sandbox.duration_ms`.
|
||||
- [ ] Write `worker-sandbox.test.ts` (≤500 lines): simple return value; console.log captured; context injection; timeout; syntax error (ok: false, error set); abort signal; `{ timeout: 15000 }`.
|
||||
- [ ] Write `worker-sandbox.md` (≥200 words).
|
||||
|
||||
## 4. Process sandbox
|
||||
|
||||
- [ ] Implement `ProcessSandbox` in `src/process/process-sandbox/process-sandbox.ts`:
|
||||
- `run(code, context, options)`: write a temp JS file; spawn `bun run <tempFile>` with `stdin: JSON.stringify(context)`; capture stdout/stderr; enforce timeout by killing process; return `SandboxResult`.
|
||||
- Propagate exit code; on non-zero, set `ok: false`.
|
||||
- `dispose()`: clean up temp files.
|
||||
- OTel: same instrumentation as worker sandbox.
|
||||
- [ ] Write `process-sandbox.test.ts` (≤500 lines): same scenarios as worker sandbox, plus process exit code handling.
|
||||
- [ ] Write `process-sandbox.md` (≥200 words).
|
||||
|
||||
## 5. Security tests
|
||||
|
||||
- [ ] Write `tests/security.test.ts` (≤500 lines, `{ timeout: 15000 }`):
|
||||
- Code attempting `process.exit()` is caught and sandbox returns error.
|
||||
- Code attempting `require('fs').readFileSync('/etc/passwd')` is blocked (process sandbox with `allowFs: false`).
|
||||
- Infinite loop hits timeout correctly.
|
||||
- Code throwing large strings does not OOM the test runner.
|
||||
- Context values are not accessible outside the sandbox (no prototype pollution).
|
||||
|
||||
## 6. Integration tests
|
||||
|
||||
- [ ] `tests/integration.test.ts`: worker + process sandbox run same set of test snippets; verify identical results; `{ timeout: 30000 }`.
|
||||
|
||||
## 7. Docs + verify + publish
|
||||
|
||||
- [ ] Root `README.md`: security model explanation, worker vs process trade-offs, quick-start examples, API table.
|
||||
- [ ] `bunx turbo lint --filter=@suaveplan/sandbox` — zero errors.
|
||||
- [ ] `bunx turbo typecheck --filter=@suaveplan/sandbox` — zero errors.
|
||||
- [ ] `bunx turbo test --filter=@suaveplan/sandbox` — all pass.
|
||||
- [ ] `bunx turbo test:coverage --filter=@suaveplan/sandbox` — 100%.
|
||||
- [ ] `bun scripts/publish.ts --package @suaveplan/sandbox`.
|
||||
- [ ] Update `CLAUDE.md`; archive to `openspec/archive/2026-04-24-enterprise-server-sandbox/`.
|
||||
Reference in New Issue
Block a user