Initial Commit
This commit is contained in:
51
openspec/archive/2026-05-26-api-sandbox-vm/proposal.md
Normal file
51
openspec/archive/2026-05-26-api-sandbox-vm/proposal.md
Normal file
@@ -0,0 +1,51 @@
|
||||
# `@suaveplan/api-sandbox-vm` — `vm.Context`-based sandbox host
|
||||
|
||||
**Package**: `@suaveplan/api-sandbox-vm`
|
||||
**Status**: PROPOSED
|
||||
**Type**: new-package
|
||||
**Stage**: 8 — see umbrella `openspec/changes/api-plugin-foundation/`
|
||||
|
||||
## Why
|
||||
|
||||
Sandboxed-tier plugins that need a JavaScript runtime (no WASM build pipeline) require an in-process execution surface that does NOT give them ambient access to the host's globals. Node's `vm.Context` with frozen globals — plus the worker-thread bulkhead — is the path of least resistance and is well-trodden for trusted-but-isolated JS plugins. This package owns that runtime; it does not own the permission model (that's `api-sandbox-permissions`) and does not own worker spawning (that's `api-worker-host`). It composes both.
|
||||
|
||||
Per design doc §2.2: sandboxed tier = worker thread + `vm.Context` guard + permission-gated fs/fetch proxies + CPU/memory/wall-clock budgets. `api-sandbox-vm` is the `vm.Context` half.
|
||||
|
||||
## What changes
|
||||
|
||||
- ADDED: `createVmSandbox({ pluginModulePath, policy, proxies })` — builds a `vm.Context` with frozen globals (no dynamic-code constructors, no `eval`, no `globalThis.process`, no `import.meta` to outside the sandbox), loads the plugin module via `vm.SourceTextModule`, returns a `VmSandboxHandle` exposing `start()`, `dispose()`, and the capability-proxy receiver.
|
||||
- ADDED: `FrozenGlobals` — the curated allowlist of globals the sandbox sees (`URL`, `URLSearchParams`, `TextEncoder`, `TextDecoder`, `structuredClone`, `console` (logger-routed), `crypto` (subset)). Everything else absent.
|
||||
- ADDED: `installCapabilityProxies(sandbox, proxies)` — wires the capability proxy surface into the sandbox using `vm.Context`-safe stub objects. Every call routed through `api-sandbox-permissions` `PermissionChecker` before reaching the proxy.
|
||||
- ADDED: `installFsProxy(sandbox, policy)` and `installFetchProxy(sandbox, policy)` — host-proxied filesystem and outbound HTTP. Denials raise `PERMISSION_DENIED` inside the sandbox.
|
||||
- ADDED: `runVmSandboxConformance(label, makeSandbox)` (subpath `./testing`) — the conformance suite every sandbox host must pass; in this package it pins the contract for vm-tier specifics (frozen globals, dynamic-code-constructor rejection, indirect-eval rejection).
|
||||
- ADDED: Error codes (`SAND_VM_*`): `EVAL_REJECTED`, `INDIRECT_EVAL_REJECTED`, `MODULE_IMPORT_FORBIDDEN`, `SOURCE_TEXT_MODULE_FAILED`.
|
||||
|
||||
## Out of scope
|
||||
|
||||
- Permission policy resolution (that's `api-sandbox-permissions`).
|
||||
- Worker spawning and lifecycle (that's `api-worker-host`).
|
||||
- WASM runtimes (that's `api-sandbox-wasm`).
|
||||
- Capability adapters themselves; this package only routes proxy traffic. The proxies are constructed by the host.
|
||||
|
||||
## Source mapping
|
||||
|
||||
Genesis-original. Source of design intent: `docs/superpowers/specs/2026-05-13-api-plugin-foundation-design.md`.
|
||||
|
||||
| Requirement | Design doc section |
|
||||
|---|---|
|
||||
| `vm.Context` with frozen globals | §2.2 |
|
||||
| No dynamic-code constructors | §2.2 |
|
||||
| fs/fetch via host proxies only | §2.2, §5.2b |
|
||||
| Permission checks before proxy call | §5.2b |
|
||||
|
||||
## Verification approach
|
||||
|
||||
- `src/sandbox/sandbox.test.ts` — builds a sandbox, confirms frozen globals enumerable list, confirms dynamic-code constructors and indirect-eval (`(0, eval)("...")`) all throw `SAND_VM_*` errors.
|
||||
- `src/sandbox/sandbox.spec-conformance.test.ts` — pins observability: every denial emits `plugin.permission_denied_total` (via `api-sandbox-permissions`) AND surfaces as `PERMISSION_DENIED` inside the sandbox.
|
||||
- `src/proxies/fs-proxy.test.ts`, `src/proxies/fetch-proxy.test.ts` — allowed-path / denied-path matrices using a real `PermissionPolicy`.
|
||||
- `src/testing/run-vm-conformance.test.ts` — self-test of the exported harness against the in-package sandbox.
|
||||
- `tests/dep-graph.test.ts` — pins runtime deps (`api-types`, `api-sandbox-permissions`, `error`, `logger`).
|
||||
|
||||
## Migration impact
|
||||
|
||||
None — additive only. `api-host` registers this as the default sandbox implementation when `host.json` `defaultSandbox = "vm"`.
|
||||
@@ -0,0 +1,101 @@
|
||||
<!-- markdownlint-disable MD024 -->
|
||||
|
||||
# Capability Spec Delta: api-sandbox-vm (api-sandbox-vm)
|
||||
|
||||
**Package**: `@suaveplan/api-sandbox-vm`
|
||||
**Status**: PROPOSED
|
||||
|
||||
> Every requirement below carries a `**Verified by:**` line at authoring time.
|
||||
|
||||
## ADDED Requirements
|
||||
|
||||
### Requirement: Sandbox boots inside a frozen `vm.Context`
|
||||
|
||||
The package SHALL expose `createVmSandbox({ pluginModulePath, policy, proxies })` that constructs a `vm.Context` whose `globalThis` is sealed and frozen. The sandbox MUST expose only the curated `FrozenGlobals` allowlist (`URL`, `URLSearchParams`, `TextEncoder`, `TextDecoder`, `structuredClone`, logger-routed `console`, subset of `crypto`). Any other global (e.g. `process`, `Buffer`, raw outbound HTTP client, raw `require`, `import.meta.url` outside the sandbox path) MUST be absent.
|
||||
|
||||
#### Scenario: process global is unreachable
|
||||
|
||||
- WHEN code inside the sandbox references `process`
|
||||
- THEN evaluation throws a `ReferenceError` AND the sandbox surfaces it as a `SAND_VM_*`-prefixed `SuaveplanError`
|
||||
|
||||
#### Scenario: globalThis is frozen
|
||||
|
||||
- WHEN code inside the sandbox attempts `globalThis.foo = 1`
|
||||
- THEN the assignment throws under strict mode AND no property is added to the sandbox globals
|
||||
|
||||
**Verified by:** `src/sandbox/sandbox.test.ts::"process global is unreachable inside sandbox"` AND `src/sandbox/sandbox.test.ts::"globalThis is frozen"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: Dynamic code constructors are rejected
|
||||
|
||||
The package SHALL ensure direct calls to dynamic-code constructors (the `Function` family — sync, async, generator), direct dynamic-code execution, and indirect dynamic-code execution all throw inside the sandbox. The thrown error MUST be a `SuaveplanError` with code `SAND_VM_EVAL_REJECTED` or `SAND_VM_INDIRECT_EVAL_REJECTED`.
|
||||
|
||||
#### Scenario: direct dynamic execution is rejected
|
||||
|
||||
- WHEN sandboxed code attempts to evaluate a string of source via the built-in dynamic-evaluator
|
||||
- THEN the call throws `SAND_VM_EVAL_REJECTED`
|
||||
|
||||
#### Scenario: indirect dynamic execution is rejected
|
||||
|
||||
- WHEN sandboxed code attempts indirect dynamic evaluation through the comma-operator trick
|
||||
- THEN the call throws `SAND_VM_INDIRECT_EVAL_REJECTED`
|
||||
|
||||
**Verified by:** `src/sandbox/sandbox.test.ts::"direct dynamic execution is rejected"` AND `src/sandbox/sandbox.test.ts::"indirect dynamic execution is rejected"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: fs and fetch are host-proxied only
|
||||
|
||||
The package SHALL install `fs` and outbound-HTTP proxy stubs that route every call to the host via `installCapabilityProxies`. Every proxy call MUST consult `PermissionChecker` from `api-sandbox-permissions` BEFORE forwarding to the host. Denied calls MUST raise `PERMISSION_DENIED` inside the sandbox without contacting the host.
|
||||
|
||||
#### Scenario: allowed fs read proxies to host
|
||||
|
||||
- WHEN sandboxed code calls the fs-proxy with a path matching `permissions.fs[]`
|
||||
- THEN the request reaches the host adapter once AND the response is delivered to the sandbox
|
||||
|
||||
#### Scenario: denied outbound HTTP does not reach the host
|
||||
|
||||
- WHEN sandboxed code calls the outbound-HTTP proxy with a host outside `permissions.fetch[]`
|
||||
- THEN the call raises `PERMISSION_DENIED` inside the sandbox AND no host-side outbound request is initiated AND `plugin.permission_denied_total{capability="fetch"}` increments
|
||||
|
||||
**Verified by:** `src/proxies/fs-proxy/fs-proxy.test.ts::"allowed read proxies once to host"` AND `src/proxies/fetch-proxy/fetch-proxy.test.ts::"denied fetch never contacts host AND increments denial metric"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: Module loading is restricted to the plugin's own module
|
||||
|
||||
The package SHALL refuse `vm.SourceTextModule` import specifiers that resolve outside the plugin module graph. Only relative imports rooted at `pluginModulePath` and a fixed allowlist of platform globals (no node built-ins) are loadable. Forbidden imports raise `SAND_VM_MODULE_IMPORT_FORBIDDEN`.
|
||||
|
||||
#### Scenario: node built-in import is rejected
|
||||
|
||||
- WHEN sandboxed code imports `"node:fs"`
|
||||
- THEN module instantiation throws `SAND_VM_MODULE_IMPORT_FORBIDDEN`
|
||||
|
||||
**Verified by:** `src/sandbox/sandbox.test.ts::"node built-in imports are forbidden"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: Conformance harness exported
|
||||
|
||||
The package SHALL export `runVmSandboxConformance(label, makeSandbox)` on `./testing` and the in-package fixture sandbox MUST pass it. The harness re-runs every scenario above so a future vm-based host (e.g. an alternative isolate library) is gate-checked against the same contract.
|
||||
|
||||
#### Scenario: in-package fixture passes harness
|
||||
|
||||
- WHEN `runVmSandboxConformance("self", makeFixtureSandbox)` runs
|
||||
- THEN every harness `it(...)` passes
|
||||
|
||||
**Verified by:** `src/testing/run-vm-conformance.test.ts::"in-package fixture passes runVmSandboxConformance"`
|
||||
|
||||
---
|
||||
|
||||
### Requirement: Disposal is idempotent
|
||||
|
||||
The package SHALL ensure `VmSandboxHandle.dispose()` releases the `vm.Context`, cancels every in-flight proxy call, and is safe to invoke multiple times. A second call MUST be a no-op (no error, no double-free).
|
||||
|
||||
#### Scenario: dispose twice is a no-op
|
||||
|
||||
- WHEN `dispose()` is called twice on the same handle
|
||||
- THEN the second call returns without error AND no second teardown side-effect is observed
|
||||
|
||||
**Verified by:** `src/sandbox/sandbox.spec-conformance.test.ts::"dispose is idempotent"`
|
||||
70
openspec/archive/2026-05-26-api-sandbox-vm/tasks.md
Normal file
70
openspec/archive/2026-05-26-api-sandbox-vm/tasks.md
Normal file
@@ -0,0 +1,70 @@
|
||||
# Tasks — `@suaveplan/api-sandbox-vm`
|
||||
|
||||
> Every checkbox MUST reflect work actually done in this change.
|
||||
|
||||
## 0. Pre-flight (non-negotiable)
|
||||
|
||||
- [x] 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)
|
||||
- [x] 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.
|
||||
- [x] 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. Spec authoring
|
||||
|
||||
- [x] 1.1 Read design doc (§2.2, §5.2b)
|
||||
- [x] 1.2 Read `openspec/AGENTS.md` Sections 13 and 14
|
||||
- [x] 1.3 Draft `proposal.md`
|
||||
- [x] 1.4 Draft `specs/api-sandbox-vm/spec.md`
|
||||
- [x] 1.5 Every requirement carries `**Verified by:**` line
|
||||
|
||||
## 2. Implementation
|
||||
|
||||
- [x] 2.1 Scaffold `packages/api-stack/api/api-sandbox-vm/`
|
||||
- [x] 2.2 `src/sandbox/` — `createVmSandbox`, `VmSandboxHandle`, frozen-globals construction
|
||||
- [x] 2.3 `src/globals/frozen-globals/` — curated allowlist
|
||||
- [x] 2.4 `src/proxies/fs-proxy/` — host-proxied fs
|
||||
- [x] 2.5 `src/proxies/fetch-proxy/` — host-proxied fetch
|
||||
- [x] 2.6 `src/proxies/install-capability-proxies/` — wire proxy receiver into sandbox
|
||||
- [x] 2.7 `src/error-codes/error-codes.ts` via `defineCodes` (subfolder layout per §"Package Structure")
|
||||
- [x] 2.8 `src/testing/run-vm-conformance.ts`
|
||||
- [x] 2.9 `src/index.ts` — explicit named re-exports
|
||||
- [x] 2.10 `package.json` exports — `./testing` subpath
|
||||
|
||||
## 3. Tests
|
||||
|
||||
- [x] 3.1 `src/sandbox/sandbox.test.ts` — 19 cases including all SAND_VM.* codes
|
||||
- [x] 3.2 `src/sandbox/sandbox.spec-conformance.test.ts` — denial metric + dispose idempotency
|
||||
- [x] 3.3 `src/proxies/fs-proxy/fs-proxy.test.ts`, `src/proxies/fetch-proxy/fetch-proxy.test.ts`
|
||||
- [x] 3.4 `src/testing/run-vm-conformance.test.ts` — in-package fixture passes the harness
|
||||
- [x] 3.5 `tests/dep-graph.test.ts`
|
||||
- [x] 3.6 Every `it(...)` carries `{ timeout: 5000 }`
|
||||
|
||||
## 4. Documentation
|
||||
|
||||
- [x] 4.1 Sibling `.md` per implementation `.ts`
|
||||
- [x] 4.2 `README.md` `## Modules`
|
||||
- [x] 4.3 Update `CLAUDE.md` package listing
|
||||
|
||||
## 5. Pre-archive verification (the 5-gate triple)
|
||||
|
||||
- [x] 5.1 `bun run check` — package-local biome clean
|
||||
- [x] 5.2 `bunx turbo lint typecheck test:coverage --filter=@suaveplan/api-sandbox-vm` — green
|
||||
- [x] 5.3 Coverage 100/100/100/100 (statements/branches/functions/lines)
|
||||
- [x] 5.4 `bun scripts/openspec-conformance.ts packages/api-stack/api/api-sandbox-vm` — exit 0 (0 error, 0 warn)
|
||||
- [x] 5.5 `bunx turbo build --filter=@suaveplan/api-sandbox-vm` — green
|
||||
|
||||
## 6. Pre-archive 7-row checklist
|
||||
|
||||
- [x] 6.1 Generics threaded: `VmSandboxHandle<Caps>` parameter flows through `createVmSandbox<Caps>`
|
||||
- [x] 6.2 Zod-equivalent schema gating: `PermissionsManifest` typed boundary (api-types) drives `createPermissionChecker`; capability decisions checked synchronously before host call
|
||||
- [x] 6.3 Every `SAND_VM.*` code thrown by at least one production path (eval/Function trap, linker refuse, SourceTextModule resolver, dispose, permission denials)
|
||||
- [x] 6.4 Strict-mode behaviours: dynamic-code constructors rejected via Proxy-guarded globalThis traps + per-property `eval`/`Function` overrides + `codeGeneration:{strings:false,wasm:false}` defence-in-depth
|
||||
- [x] 6.5 OTel instruments asserted with real in-memory `MeterProvider` (CUMULATIVE), `metrics.disable()` between cases for clean re-binding
|
||||
- [x] 6.6 Lifecycle dedup: `dispose()` second/third call is a no-op (test covers triple-dispose)
|
||||
- [x] 6.7 No retroactive ticks
|
||||
|
||||
## 7. Archive
|
||||
|
||||
- [x] 7.1 Fold spec delta into `openspec/specs/api-sandbox-vm/spec.md`
|
||||
- [x] 7.2 Move to `openspec/archive/2026-05-26-api-sandbox-vm/`
|
||||
- [x] 7.3 Tick `8.2` in umbrella tasks
|
||||
- [x] 7.4 Refresh `MANIFEST.md`
|
||||
Reference in New Issue
Block a user