bun-sandbox
v0.1.0
Published
macOS sandbox for Bun using libsandbox
Downloads
94
Readme
bun-sandbox
macOS sandbox library for Bun using libsandbox (SBPL).
Install
bun add bun-sandboxmacOS only. Requires Bun runtime.
Usage
spawn — Run a command in a sandbox
import { spawn } from "bun-sandbox";
const proc = spawn(["node", "untrusted.js"], {
allow: { read: true, write: ["/tmp"], network: false },
});
await proc.exited;apply — Sandbox the current process (irreversible)
import { apply } from "bun-sandbox";
apply({ allow: { read: true, write: false, network: false } });
// All code after this is sandboxedSandboxWorker — Worker-like sandboxed child process
import { SandboxWorker } from "bun-sandbox";
const worker = new SandboxWorker(new URL("./worker.ts", import.meta.url), {
allow: { read: true, write: false, network: false },
});
worker.onmessage = (event) => console.log(event.data);
worker.postMessage({ type: "compute", data: [1, 2, 3] });
worker.terminate();
await worker.exited;Worker side (worker.ts):
process.on("message", (data) => {
const result = compute(data);
process.send(result);
});Presets
import { presets } from "bun-sandbox";
// presets.readOnly — read: true, write: false, network: false
// presets.noNetwork — read: true, write: true, network: false
// presets.writeTmpOnly — read: true, write: ["/tmp"], network: false
// presets.noWrite — read: true, write: false, network: trueAllowOptions
| Option | Type | Description |
|---|---|---|
| read | boolean \| string[] | File read access |
| write | boolean \| string[] | File write access |
| network | boolean \| NetworkOptions | Network access |
| process | boolean \| ProcessOptions | Process execution |
License
MIT
