|
| 1 | +# vm2 |
| 2 | + |
| 3 | +vm2 safely sandboxes untrusted JavaScript code running in Node.js. This is a security-critical project -- every change must be evaluated for sandbox escape potential. |
| 4 | + |
| 5 | +## Public API |
| 6 | + |
| 7 | +Exported from `lib/main.js` via `index.js`: |
| 8 | + |
| 9 | +- **`VM`** -- isolated context for synchronous execution without `require`. |
| 10 | +- **`NodeVM`** -- sandbox emulating Node's module loader with fine-grained `require` controls. |
| 11 | +- **`VMScript`** -- compilation, caching, and compiler selection wrapper. |
| 12 | +- **`VMFileSystem`** -- controls sandbox filesystem access. |
| 13 | +- **`VMError`** -- raised when sandbox policy is violated. |
| 14 | + |
| 15 | +## Architecture |
| 16 | + |
| 17 | +### The Boundary (Security-Critical) |
| 18 | + |
| 19 | +| File | Role | |
| 20 | +|------|------| |
| 21 | +| `lib/bridge.js` | Core proxy layer. Paired proxies for every value crossing host/sandbox. WeakMap caches for identity. Proxy trap handlers (`BaseHandler`, `ProtectedHandler`, `ReadOnlyHandler`). | |
| 22 | +| `lib/setup-sandbox.js` | Sandbox bootstrap for `VM`. `handleException`, Promise wrapping, `Symbol.for` override, symbol filtering, `Error.prepareStackTrace` safe default, `WebAssembly.JSTag` deletion. | |
| 23 | +| `lib/setup-node-sandbox.js` | Sandbox bootstrap for `NodeVM`. `require`, module resolution, console redirection. | |
| 24 | +| `lib/transformer.js` | Acorn parser instrumenting `catch` blocks and `with` statements. **Limitation**: `ecmaVersion: 2022` -- post-ES2022 syntax (e.g., `using`) is invisible. | |
| 25 | + |
| 26 | +### Other Files |
| 27 | + |
| 28 | +| File | Role | |
| 29 | +|------|------| |
| 30 | +| `lib/vm.js` | `VM` class. Compiles via `vm.Script`, applies transformer, enforces timeout/async. | |
| 31 | +| `lib/nodevm.js` | `NodeVM` class. Module loader, external module access, console redirection. | |
| 32 | +| `lib/script.js` | `VMScript`. Options, compilers, caching, async detection. | |
| 33 | +| `lib/compiler.js` | Compiler resolution (JS passthrough, optional CoffeeScript/TypeScript). | |
| 34 | +| `lib/filesystem.js` | `VMFileSystem` implementation. | |
| 35 | +| `lib/resolver.js` | Module resolution for `NodeVM`. | |
| 36 | +| `lib/events.js` | Sandbox-safe copy of Node's `events`. | |
| 37 | + |
| 38 | +### CLI |
| 39 | + |
| 40 | +```sh |
| 41 | +npx vm2 path/to/script.js # NodeVM with external modules, verbose logging |
| 42 | +``` |
| 43 | + |
| 44 | +## Security |
| 45 | + |
| 46 | +See [`docs/ATTACKS.md`](docs/ATTACKS.md) for the full catalog of attack patterns, fundamentals of realm separation and V8 internals, and the defense table. |
| 47 | + |
| 48 | +**Key insight**: V8 internal algorithms (ArraySpeciesCreate, FormatStackTrace, PromiseResolveThenableJob) operate on raw objects, bypassing proxy traps. Defenses must neutralize raw objects directly, not just intercept via proxies. |
| 49 | + |
| 50 | +### Principles |
| 51 | + |
| 52 | +1. Never expose host constructors or prototypes. |
| 53 | +2. Rebound every function crossing the bridge to the destination realm. |
| 54 | +3. Freeze or proxy shared mutable state. |
| 55 | +4. Filter property access via `Object.getOwnPropertyDescriptor`. Never `for...in` or spread on host objects. |
| 56 | +5. Treat new symbols and language features as suspect until vetted. |
| 57 | +6. Cache `Reflect.*` and other critical references at init time. |
| 58 | + |
| 59 | +### Checklist for Boundary Changes |
| 60 | + |
| 61 | +1. Does this expose any new return path for host objects? |
| 62 | +2. Can sandbox code call this method directly (not through a proxy)? |
| 63 | +3. Does this accept attacker-controlled parameters? |
| 64 | +4. Are all `Reflect.*` calls using cached references? |
| 65 | +5. Could V8 internal algorithms trigger this path (bypassing traps)? |
| 66 | +6. Does this handle host-realm errors that could be thrown? |
| 67 | +7. Are there new well-known symbols that need filtering? |
| 68 | + |
| 69 | +## Tests |
| 70 | + |
| 71 | +```sh |
| 72 | +npm test # Main suite (Mocha) |
| 73 | +npm run test:compilers # Optional (needs typescript, coffee-script) |
| 74 | +npm run lint # ESLint |
| 75 | +``` |
| 76 | + |
| 77 | +- `test/vm.js` -- Main sandbox tests. Uses `makeHelpers()` for proxy boundary checks, `it.cond(name, condition, fn)` for version-gated tests. |
| 78 | +- `test/nodevm.js` -- NodeVM module loading tests. |
| 79 | +- `test/escape-scanner.js` -- Automated escape scanner. Serializable, runs inside `vm.run()`. |
| 80 | + |
| 81 | +Every security fix must include tests that reproduce the attack, verify the defense, and test bypass variants. Use `it.cond()` for Node version requirements. |
| 82 | + |
| 83 | +Use the `/hacker` skill after making security-related changes to systematically red-team the sandbox and verify it still holds. |
| 84 | + |
| 85 | +## Updating ATTACKS.md |
| 86 | + |
| 87 | +**Every time the library is patched**, update [`docs/ATTACKS.md`](docs/ATTACKS.md): |
| 88 | + |
| 89 | +- Add the attack to the appropriate tier/category (or create a new one). |
| 90 | +- Document: attack flow, canonical example, why it works, mitigation, detection rules. |
| 91 | +- Update the Compound Attack Patterns and "How The Bridge Defends" table. |
| 92 | +- Add new APIs/features to "Considered Attack Surfaces" or "Future Risks". |
| 93 | + |
| 94 | +## Workflow |
| 95 | + |
| 96 | +- Keep changes small and focused. Security-sensitive code discourages large refactors. |
| 97 | +- Include threat model reasoning and escape-prevention tests for boundary changes. |
| 98 | +- Follow existing ESLint code style. |
| 99 | +- Vulnerabilities: follow `SECURITY.md`, not public issues. |
0 commit comments