barebox 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +85 -2
- package/README.md +11 -11
- package/barebox.context.md +91 -18
- package/package.json +2 -2
- package/primitives.json +40 -4
- package/src/certs.js +24 -3
- package/src/cli.js +348 -35
- package/src/holds.js +529 -58
- package/src/index.js +2 -0
- package/src/launcher.js +149 -50
- package/src/overlay-walk.js +49 -0
- package/src/overlay.js +1064 -0
- package/src/proxy.js +171 -310
- package/src/run.js +232 -0
- package/src/runtime.js +295 -0
- package/src/wall.js +217 -0
- package/types/certs.d.ts +2 -0
- package/types/holds.d.ts +168 -14
- package/types/index.d.ts +2 -0
- package/types/launcher.d.ts +13 -4
- package/types/overlay-walk.d.ts +1 -0
- package/types/overlay.d.ts +228 -0
- package/types/proxy.d.ts +43 -83
- package/types/run.d.ts +192 -0
- package/types/runtime.d.ts +131 -0
- package/types/wall.d.ts +30 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,9 +4,77 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
6
6
|
|
|
7
|
+
## [0.4.0] - 2026-10-29
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- Held requests are one per agent (pre-1.0 behaviour change): each agent has at most one held request being read or parked; later ones wait in line, unread, in order. At most 16 wait; the next gets `429 hold queue full`.
|
|
12
|
+
- The hold deadline counts from arrival (default 120 s, `never` supported), time in line and reading the body included; a request past its deadline is denied `hold timeout` whether waiting, sending, or parked.
|
|
13
|
+
- `proxy.removeAgent(name)` ends that agent's open connections, and its held requests end `deny` / `client gone` at once. `run()` calls it when the sandbox exits, so shutting down no longer races a held request into a `proxy closing` log line.
|
|
14
|
+
- An existing runtime dir you don't own (e.g. `SBX_RUNTIME=/tmp`, which 0.3.0 accepted) is now refused with a message naming the dir, its owner and a subdir to use, such as `/tmp/sbx`.
|
|
15
|
+
- The TCP door is one agent: every connection through `createProxy`'s TCP `port` counts as the agent `tcp` and shares one hold line. Use `addAgent` sockets (`sbx run` does) for separate lines.
|
|
16
|
+
|
|
17
|
+
### Security
|
|
18
|
+
|
|
19
|
+
- The upstream wall checks an IPv4 embedded in IPv6 in every spelling: IPv4-mapped (`::ffff:7f00:1` as well as `::ffff:127.0.0.1`) and compat forms, uncompressed loopback (`0:0:0:0:0:0:0:1`), NAT64 `64:ff9b::/96` (local-use `64:ff9b:1::/48` denied outright) and 6to4 `2002::/16`. NAT64 and 6to4 of public sites stay reachable.
|
|
20
|
+
- Runtime folders are owner-only (0700) whatever the umask: the runtime root, `asks/`, `answers/` and `socks/`. A runtime dir you own is tightened to 0700.
|
|
21
|
+
- Each minted certificate gets a random 128-bit serial instead of a shared serial file (`ca.srl`) that concurrent mints could corrupt (a failed mint reached the agent as a 502); at most 4 mints run at once.
|
|
22
|
+
|
|
23
|
+
### Fixed
|
|
24
|
+
|
|
25
|
+
- `createProxy` throws synchronously on an unopenable `logFile` and releases the runtime. A later audit-log write error prints one `sbx: audit log … write failed` line to stderr and the proxy keeps running.
|
|
26
|
+
- The `sbx run` "answer with" hint quotes the runtime path, so it is safe to copy-paste.
|
|
27
|
+
- Overlay hashing reads files in 1 MiB chunks, so very large files no longer exhaust memory.
|
|
28
|
+
- `run()` releases the runtime when the agent socket can't be created; the claim no longer stays held refusing a second `run()` on the same runtime as "in use".
|
|
29
|
+
|
|
30
|
+
## [0.3.0] - 2026-10-28
|
|
31
|
+
|
|
32
|
+
Breaking changes (pre-1.0): the control socket, `createProxy`'s `certDir` and `allowUpstream`, `run()`'s low-level options, the hold queue's `approve`/`deny`/`size` and the top-level `allowLoopback` option are removed or moved; `runtime` is now required. Each is marked **Breaking** below.
|
|
33
|
+
|
|
34
|
+
### Added
|
|
35
|
+
|
|
36
|
+
- M2.5 integration seam: `ask`, `auto` (the default) and `trust` modes with the card/payment floor in every mode; the `onAsk` seam plus the file-checkpoint ask channel (`asks/<id>.json` → `answers/<id>.json`, control socket deleted); `run()` wrapping proxy + launch; `sbx run [--mode ask|auto|trust] [--json] [--overlay]`; a 0600 JSONL audit log (bareguard's line shape deferred to M3); `sbx pending` / `approve` / `deny` refuse when no live proxy owns the runtime.
|
|
37
|
+
- M4 undo layer: per-run overlay tracing paper with `sbx diff` / `commit` / `rollback`, conflict flagging (refuse by default, `--skip-conflicts`), and `node_modules` invisibility. Diff walks the merged view with node inside the mount (no shell); `commit` stages into the project and swaps by journaled rename, undoing every step on failure (a full undo reports nothing was applied; a partial one names the steps not undone and keeps the commit folder with the originals); it strips setuid/setgid bits, and committed files get fresh mtimes. `rollback` reports diff's answer, or raw upper paths when the project is gone. Needs bubblewrap ≥ 0.11.0; older bwrap gets a clear refusal.
|
|
38
|
+
- The agent always starts in the project dir.
|
|
39
|
+
- The commit applies under read-only project folders.
|
|
40
|
+
- Rollback always removes the overlay upper, whatever the agent left there.
|
|
41
|
+
- The walker has no output cap, and node is resolved wherever it is installed.
|
|
42
|
+
- One proxy per runtime, enforced by a kernel-held lock: `createProxy` binds an abstract unix socket named from the runtime's real path, device and inode and holds it while the proxy lives. A second live proxy throws "in use" naming the owner's pid, before touching sockets or `asks/`. The kernel frees the lock when the process dies (SIGKILL included), so there is no stale claim and no takeover. `<runtime>/owner` (0600) only names the pid.
|
|
43
|
+
- `sbx pending`, `approve` and `deny` need a live proxy on the runtime and refuse otherwise.
|
|
44
|
+
- `SBX_OVERLAY_IGNORE=a,b` extends the list of folders the overlay layer ignores (`node_modules` is always ignored).
|
|
45
|
+
- Locked credential paths: `launch` hides 21 credential stores from the agent (keys, password stores, keyrings, browser profiles, cloud/cluster/container credentials, `gh`/`op` CLIs, token files). Folders are masked with an empty tmpfs and files read empty; a symlinked drawer is masked at its real target inside `$HOME` or the project. `~/.claude` is deliberately left visible.
|
|
46
|
+
- The upstream wall lives in `src/wall.js`: every resolved IP is checked after DNS, unresolvable names fail closed, and the dial is pinned to the checked IPs. One writer (`denyRes`) produces every HTTP error response.
|
|
47
|
+
- The agent sandbox always runs with bwrap `--new-session`, so its stdin is never the terminal.
|
|
48
|
+
|
|
49
|
+
### Security
|
|
50
|
+
|
|
51
|
+
- The audit log is owner-only (0600) and scrubs sensitive header values to `[redacted]` before any user `redact` hook runs.
|
|
52
|
+
- Ask and answer files are owner-only (0600); `sbx approve`/`deny` accept only `h<n>` ids; `sbx pending` and `sbx run` strip terminal control sequences from agent-controlled fields before printing.
|
|
53
|
+
|
|
54
|
+
### Removed
|
|
55
|
+
|
|
56
|
+
- **Breaking:** Control socket (`createProxy`'s `controlSocket` option, `SBX_SOCK`): decisions flow through the file checkpoint (`<runtime>/asks` + `<runtime>/answers`) or `hold.onAsk`; the migration guards are deleted.
|
|
57
|
+
- **Breaking:** `createProxy`'s `certDir`. Certificates always live in `<runtime>/certs`, and `runtime` is now required.
|
|
58
|
+
- **Breaking:** `createProxy`'s `allowUpstream`. There is no escape hatch around the upstream wall.
|
|
59
|
+
- **Breaking:** `run()`'s `gate`, `match`, `shouldHold`, `pollMs`, `proxyPort` and `extraEnv`. Use `createProxy` + `launch` for low-level control.
|
|
60
|
+
- **Breaking:** The hold queue's `approve(id)`, `deny(id)` and `size()`, and `body` on `pending()` entries (the ask payload carries `bodyPreview`).
|
|
61
|
+
- **Breaking:** The library's `ASK OPEN <id>` write to stderr. The library never prints.
|
|
62
|
+
|
|
63
|
+
### Changed
|
|
64
|
+
|
|
65
|
+
- **Breaking:** Test-only options moved under one key: `createProxy({ unsafeTestOnly: { allowLoopback, resolveUpstream } })`, `run({ unsafeTestOnly: { allowLoopback } })`. The CLI variable is renamed from `SBX_ALLOW_LOOPBACK` to `SBX_UNSAFE_TEST_ALLOW_LOOPBACK`.
|
|
66
|
+
- `onEvent(e)` on `createHolds` / `createProxy`'s `hold` / `run()`: `{ type: 'ask', ask }` when a hold parks, `{ type: 'decided', id, decision, reason }` when it settles. `run()`'s `asks` result is built from these events.
|
|
67
|
+
- `sbx run` prints how each ask ended: `decided <id> <allow|deny> [(reason)]`, or `{"ev":"decided","id","decision","reason"}` with `--json`, between the `ask` and `done` lines. A hold whose client went away reports `deny` / `client gone` (also in `run()`'s `asks`).
|
|
68
|
+
- Decisions are written as `allow` / `deny` everywhere, the audit log included. `approve` is still accepted, as an alias in answer files only.
|
|
69
|
+
- `sbx` results go to stdout; every error, refusal and usage-on-error goes to stderr through `err`. `--help` stays on stdout, exit 0. Exit codes don't change.
|
|
70
|
+
|
|
7
71
|
## [0.2.0] - 2026-10-27
|
|
8
72
|
|
|
9
|
-
Graduation rewrite: the M0
|
|
73
|
+
Graduation rewrite: the M0–M2 POCs become a library. The POCs (Oct 26, `poc/`, not shipped) passed all three go/no-go gates:
|
|
74
|
+
|
|
75
|
+
- walls + front door: 14/14 checks, red-team 8/8
|
|
76
|
+
- inspection inside HTTPS: curl, node, python requests and git over the real internet
|
|
77
|
+
- hold-and-ask: a 9-scenario matrix
|
|
10
78
|
|
|
11
79
|
### Added
|
|
12
80
|
|
|
@@ -15,19 +83,30 @@ Graduation rewrite: the M0-M2 POCs become a library.
|
|
|
15
83
|
- Hold queue: requests park for approve/deny or time out; holds abandoned by a disconnecting client are dropped; `maxBody` caps queued bodies.
|
|
16
84
|
- `launch`: bwrap sandbox with locked paths, a CA trust bundle exported through the usual env vars, and the runtime sockets masked from the sandbox (including a runtime reached through a symlink).
|
|
17
85
|
- `sbx` CLI: `pending`, `approve`, `deny`, `--help`, defined exit codes; socket from `SBX_SOCK` or `SBX_RUNTIME`, with no cwd default.
|
|
18
|
-
- `npm run bench`: per-sandbox and proxy RSS benchmark.
|
|
86
|
+
- `npm run bench`: per-sandbox and proxy RSS benchmark. Measured with 20 agents: 7.8 MB mean per sandbox (budget 10), and proxy 60.1 MB idle, 71.9 MB with 20 parked holds (budget 80).
|
|
19
87
|
- TypeScript types, `primitives.json` and `barebox.context.md`.
|
|
88
|
+
- `package.json` `repository` field, which npm provenance needs. Published with SLSA provenance.
|
|
20
89
|
|
|
21
90
|
### Changed
|
|
22
91
|
|
|
23
92
|
- `createProxy` throws unless `certDir` and the control socket live inside `runtime` (real paths compared).
|
|
93
|
+
- HTTP handling uses the stdlib `http` server instead of hand-rolled framing.
|
|
94
|
+
- The `bin` path is written as `src/cli.js`, without `./`.
|
|
24
95
|
|
|
25
96
|
### Fixed
|
|
26
97
|
|
|
27
98
|
- POST/PUT bodies to real hostnames no longer hang; the client stream is paused until the upstream pipe is attached.
|
|
99
|
+
- HEAD requests no longer return 502 after 6 s, and slow upstreams are no longer killed at 10 s.
|
|
100
|
+
- Plain-HTTP requests now go through holds the same way HTTPS ones do.
|
|
101
|
+
- The publish workflow installs the same sandbox runner deps as CI (bubblewrap, socat, curl, openssl, userns sysctl).
|
|
102
|
+
- The adopter-gate quickstart uses the 0.2.0 API (`pending()`, a JSON import with `with { type: "json" }`).
|
|
28
103
|
|
|
29
104
|
### Security
|
|
30
105
|
|
|
106
|
+
- The proxy listens on 127.0.0.1 only, not on all interfaces.
|
|
107
|
+
- An agent can no longer approve its own holds. A read-only bind does not block `connect` on a unix socket, so the runtime is masked inside the sandbox and the control socket must live in the runtime.
|
|
108
|
+
- Loopback is no longer reachable through the proxy. A failed DNS lookup is denied, and the private LAN is blocked.
|
|
109
|
+
- `ca.key` can no longer leak through a `certDir` outside the runtime.
|
|
31
110
|
- Audit log is created 0600 and `Authorization` headers are scrubbed from logged lines.
|
|
32
111
|
|
|
33
112
|
## [0.1.0] - 2026-10-26
|
|
@@ -35,3 +114,7 @@ Graduation rewrite: the M0-M2 POCs become a library.
|
|
|
35
114
|
### Added
|
|
36
115
|
|
|
37
116
|
- Reserved `barebox` on npm; repo scaffold (docs, M0 throwaway POC, publish workflow).
|
|
117
|
+
|
|
118
|
+
[0.3.0]: https://github.com/hamr0/barebox/compare/v0.2.0...v0.3.0
|
|
119
|
+
[0.2.0]: https://github.com/hamr0/barebox/compare/1f144f4...v0.2.0
|
|
120
|
+
[0.1.0]: https://github.com/hamr0/barebox/commit/1f144f4
|
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<img src="https://img.shields.io/badge/status-POC-c0841a" alt="status: POC">
|
|
19
19
|
</p>
|
|
20
20
|
|
|
21
|
-
> **Status:**
|
|
21
|
+
> **Status:** early. The walls (M0), HTTPS inspection (M1), hold-and-ask (M2), the one-call `run()` and file ask channel (M2.5) and the undo layer (M4) are built and measured; the decision rules (M3), key custody (M5) and the rest are not. Today's hold rule is interim: payment destinations, plus every non-GET/HEAD request in `ask` and `auto` modes. Linux only.
|
|
22
22
|
|
|
23
23
|
## Why
|
|
24
24
|
|
|
@@ -41,8 +41,8 @@ Three letters, each handled differently — the same `r`/`w`/`x` as [bareguard](
|
|
|
41
41
|
|
|
42
42
|
The agent works in your house:
|
|
43
43
|
|
|
44
|
-
- **Tracing paper (w).** The agent sees your real files, but every change lands on an overlay laid over them. At the end you commit it or roll it back.
|
|
45
|
-
- **Locked drawers (r).** It reads most things. `~/.ssh`, `~/secrets`, `~/.gnupg`, browser profiles stay shut.
|
|
44
|
+
- **Tracing paper (w).** The agent sees your real files, but every change lands on an overlay laid over them. At the end you commit it or roll it back (`sbx run --overlay -- <cmd>`, then `sbx diff`, `sbx commit`, `sbx rollback`).
|
|
45
|
+
- **Locked drawers (r).** It reads most things. Keys and password stores (`~/.ssh`, `~/secrets`, `~/.gnupg`, `~/.password-store`, keyrings), browser profiles, cloud and cluster credentials (`~/.aws`, `~/.azure`, `~/.config/gcloud`, `~/.kube`, `~/.docker`, `~/.config/gh`, `~/.config/op`) and token files (`~/.netrc`, `~/.npmrc`, `~/.pypirc`, `~/.git-credentials`, `~/.cargo/credentials.toml`) stay shut. The agent's own model credentials (`~/.claude`) do not; moving those out is M5.
|
|
46
46
|
- **One front door with a doorman (x).** No network of its own. Lookups pass; anything that sends, pays, or publishes waits for your yes.
|
|
47
47
|
- **Keys it never holds.** The agent types a placeholder; the doorman puts the real API key in the request header only after you approve that request.
|
|
48
48
|
|
|
@@ -60,7 +60,7 @@ The agent works in your house:
|
|
|
60
60
|
+-----+--------------- bwrap sandbox (unprivileged) ---------------+
|
|
61
61
|
| files: overlay lower = real project (read only) |
|
|
62
62
|
| upper = per-run tracing paper |
|
|
63
|
-
| locked paths (~/.ssh etc.) -> empty tmpfs
|
|
63
|
+
| locked paths (~/.ssh etc.) -> empty tmpfs or /dev/null |
|
|
64
64
|
| network: none of its own |
|
|
65
65
|
| socat inside: 127.0.0.1:3128 -> per-agent unix socket |
|
|
66
66
|
+-----+------------------------------------------------------------+
|
|
@@ -76,7 +76,7 @@ The agent works in your house:
|
|
|
76
76
|
- **Identity** is the socket, not a header the agent could fake. One socket per agent, all into one proxy.
|
|
77
77
|
- **HTTPS inspection** uses a local CA that exists only inside the sandbox; the proxy reads method, URL, headers and body, then forwards.
|
|
78
78
|
- **The doorman's x-rules**, strongest first: card numbers in the body (Luhn) → payment domains and paths (`/checkout`, `/payment`, …) → rwxmap labels for known APIs → unknown `POST` by mode (strict: ask once per domain; normal: allow and log).
|
|
79
|
-
- **Approvals** come from a second terminal (`sbx pending`, `sbx approve <id>`, `sbx deny <id>`). Timeout means deny, with a clear `403` reason.
|
|
79
|
+
- **Approvals** come from a second terminal (`sbx pending`, `sbx approve <id>`, `sbx deny <id>`). Timeout means deny, with a clear `403` reason. One held request per agent at a time: later ones wait in line (16 at most, then `429 hold queue full`), and the deadline counts from arrival.
|
|
80
80
|
|
|
81
81
|
## Numbers
|
|
82
82
|
|
|
@@ -85,11 +85,11 @@ Measured, not estimated. Fedora, kernel 7.2, bubblewrap 0.12.0, Node 22.
|
|
|
85
85
|
| | barebox (measured) | [srt](https://github.com/anthropic-experimental/sandbox-runtime) 0.0.81 (measured) |
|
|
86
86
|
|---|---|---|
|
|
87
87
|
| Memory per sandbox | **7.8 MB** mean, 7.9 MB max (20 concurrent) | ~100 MB |
|
|
88
|
-
| Proxy | one shared, **~60 MB idle, ~
|
|
88
|
+
| Proxy | one shared, **~60 MB idle, ~78 MB loaded** (20 agents, each with 1 parked + 16 waiting; within the ≤ 80 MB budget) | one per sandbox |
|
|
89
89
|
| Start + exit | **10 ms** (M0 POC; unmeasured on the rewrite — TODO re-measure) | 1.87–2.05 s |
|
|
90
|
-
| 20 agents | ~
|
|
90
|
+
| 20 agents | ~234 MB (rough sum: ~78 MB proxy + 20 × 7.8 MB) | ~2 GB |
|
|
91
91
|
|
|
92
|
-
Targets: shared proxy ≤ 80 MB, each extra sandbox ≤ 10 MB, 20 agents on a 2 GB VPS. Bypass attempts (`--noproxy`, unset proxy env, direct IP, host localhost) all fail closed, and a red-team script breaks each wall on purpose to prove the checks can fail. Setup, dead ends and why: [findings](docs/
|
|
92
|
+
Targets: shared proxy ≤ 80 MB, each extra sandbox ≤ 10 MB, 20 agents on a 2 GB VPS. Bypass attempts (`--noproxy`, unset proxy env, direct IP, host localhost) all fail closed, and a red-team script breaks each wall on purpose to prove the checks can fail. Setup, dead ends and why: [findings](docs/logs/findings.md).
|
|
93
93
|
|
|
94
94
|
## Roadmap
|
|
95
95
|
|
|
@@ -99,7 +99,7 @@ Targets: shared proxy ≤ 80 MB, each extra sandbox ≤ 10 MB, 20 agents on a 2
|
|
|
99
99
|
| M1 | Proxy sees inside HTTPS (curl, node, python requests, git) | **PASS** — real-internet matrix + negatives, peak 63 MB |
|
|
100
100
|
| M2 | Hold one `x` request, ask, keep everything else running | **PASS** — 9-scenario hold matrix, 120 s + never timeouts |
|
|
101
101
|
| M3 | Decide r/w/x deterministically (rwxmap + bareguard), audit log | |
|
|
102
|
-
| M4 | Undo layer: `diff`, `commit`, `rollback`, conflict flagging | |
|
|
102
|
+
| M4 | Undo layer: `diff`, `commit`, `rollback`, conflict flagging | **PASS** — paper + review flow implemented, full bwrap+unix proof in a real shell |
|
|
103
103
|
| M5 | Keys the agent never holds | |
|
|
104
104
|
| M6 | Scale run: 20 agents + shared proxy | |
|
|
105
105
|
| M7 | Demos: payment held for approval; prompt-injected `~/.ssh` exfil blocked | |
|
|
@@ -112,11 +112,11 @@ M0–M2 are the go/no-go. Later: macOS (`sandbox-exec` + APFS clones), Windows v
|
|
|
112
112
|
npm install barebox
|
|
113
113
|
```
|
|
114
114
|
|
|
115
|
-
Requires Node.js >= 22. Zero production dependencies.
|
|
115
|
+
Requires Node.js >= 22. Zero production dependencies. Needs bubblewrap >= 0.11.0 for `--overlay` / `sbx diff|commit|rollback` (Ubuntu 24.04 ships 0.9.0: build from source or use a newer distro); plain runs work with older bwrap. CI builds bubblewrap 0.12.0 from a checksum-pinned source tarball and fails if any test is skipped. The API is in [`barebox.context.md`](barebox.context.md) and `primitives.json` (both ship in the package); the `sbx` CLI comes with it.
|
|
116
116
|
|
|
117
117
|
## Try the POCs
|
|
118
118
|
|
|
119
|
-
Linux with `bwrap
|
|
119
|
+
Linux with `bwrap` (>= 0.11.0 only for the overlay), `socat`, `openssl`, Node >= 22 and Python 3. Needs a normal shell (namespaces + listening sockets), not a tool sandbox. The POCs are throwaway proofs, not the product.
|
|
120
120
|
|
|
121
121
|
```
|
|
122
122
|
git clone https://github.com/hamr0/barebox && cd barebox
|
package/barebox.context.md
CHANGED
|
@@ -23,36 +23,104 @@ npx sbx --help
|
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
```js
|
|
26
|
-
import { launch } from "barebox";
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
26
|
+
import { createProxy, launch } from "barebox";
|
|
27
|
+
import { mkdtempSync } from "node:fs";
|
|
28
|
+
import { tmpdir } from "node:os";
|
|
29
|
+
import { join } from "node:path";
|
|
30
|
+
// one sandboxed agent: walls up, front door on, locked drawers masked.
|
|
31
|
+
// The agent's socket must already be listening: create the front door,
|
|
32
|
+
// then add the agent to it.
|
|
33
|
+
const runtime = mkdtempSync(join(tmpdir(), "sbx-"));
|
|
34
|
+
const proj = mkdtempSync(join(tmpdir(), "proj-"));
|
|
35
|
+
const proxy = createProxy({ runtime, port: 0, logFile: join(runtime, "mitm.log") });
|
|
36
|
+
await proxy.ready;
|
|
37
|
+
const child = launch({ runtime, agent: "fx", proj, command: ["bash", "-c", "echo hi"], socket: proxy.addAgent("fx"), caFile: proxy.caFile });
|
|
30
38
|
await new Promise((r) => child.on("close", r));
|
|
39
|
+
await proxy.close();
|
|
31
40
|
```
|
|
32
41
|
|
|
33
42
|
```js
|
|
34
43
|
import { createProxy } from "barebox";
|
|
44
|
+
import { mkdtempSync } from "node:fs";
|
|
45
|
+
import { tmpdir } from "node:os";
|
|
46
|
+
import { join } from "node:path";
|
|
35
47
|
// the front door: terminates agent TLS, logs every request, parks holds.
|
|
36
48
|
// The TCP port is opt-in for tests (bound to 127.0.0.1); agents connect
|
|
37
49
|
// over their own unix socket: proxy.addAgent("fx") -> <runtime>/socks/fx.sock
|
|
38
|
-
const
|
|
50
|
+
const runtime = mkdtempSync(join(tmpdir(), "sbx-"));
|
|
51
|
+
const proxy = createProxy({ runtime, port: 0, logFile: join(runtime, "mitm.log") });
|
|
39
52
|
await proxy.ready;
|
|
40
53
|
await proxy.close();
|
|
41
54
|
```
|
|
42
55
|
|
|
43
56
|
## Holds (human approval)
|
|
44
57
|
|
|
45
|
-
Start the proxy with a hold rule
|
|
46
|
-
|
|
58
|
+
Start the proxy with a hold rule, then decide from a second terminal
|
|
59
|
+
through the file checkpoint (`<runtime>/asks` + `<runtime>/answers`):
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
SBX_RUNTIME=/tmp/sbx npx sbx pending
|
|
63
|
+
SBX_RUNTIME=/tmp/sbx npx sbx approve h1
|
|
64
|
+
SBX_RUNTIME=/tmp/sbx npx sbx deny h2 "reason here"
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Held requests wait (default 120 s, `never` supported), counting from arrival:
|
|
68
|
+
time spent in line and reading the body counts too, and a request past its
|
|
69
|
+
deadline is denied `hold timeout` whether waiting, sending, or parked. Each
|
|
70
|
+
agent holds at most one request being read or parked; later ones wait in
|
|
71
|
+
line, unread, in order — at most 16, the next gets `429 hold queue full`.
|
|
72
|
+
Approve forwards, deny and timeout answer 403 with the reason. These commands
|
|
73
|
+
need the proxy to be alive: with no live proxy on the runtime `sbx pending`,
|
|
74
|
+
`approve` and `deny` exit 1 and say so, because nothing would read the answer.
|
|
75
|
+
|
|
76
|
+
## One call: `run()` and `sbx run`
|
|
77
|
+
|
|
78
|
+
`run()` starts the proxy, sandboxes one command, answers holds and returns
|
|
79
|
+
the exit code with the ask and audit trail. Modes: `ask`, `auto` (default),
|
|
80
|
+
`trust`. Payment destinations (`/checkout`, `/payment`, `/orders`,
|
|
81
|
+
`/purchase`) hold in every mode; `ask` and `auto` also hold every
|
|
82
|
+
non-GET/HEAD request (an interim rule until the policy module lands).
|
|
83
|
+
|
|
84
|
+
```js no-run
|
|
85
|
+
import { run } from "barebox";
|
|
86
|
+
const { exit, asks, audit, diff, runtime } = await run({
|
|
87
|
+
proj: "/path/to/project",
|
|
88
|
+
command: ["bash", "-c", "echo hi"],
|
|
89
|
+
mode: "auto",
|
|
90
|
+
overlay: true, // writes land on the undo layer; `diff` lists them
|
|
91
|
+
onAsk: async (ask) => ({ decision: "deny", reason: "not today" }), // omit for the file checkpoint
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
sbx run [--mode ask|auto|trust] [--json] [--overlay] -- <cmd> [...]
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
`sbx run` uses the current directory as the project and `SBX_RUNTIME` as the
|
|
100
|
+
runtime (a short temp dir otherwise, printed on stderr). It prints an `ask`
|
|
101
|
+
line when a request parks and a `decided` line when it ends, then `done`;
|
|
102
|
+
with `--json` stdout carries only JSON lines. Its exit code mirrors the
|
|
103
|
+
agent's.
|
|
104
|
+
|
|
105
|
+
## Undo layer
|
|
106
|
+
|
|
107
|
+
`overlay: true` (`sbx run --overlay`) mounts a per-run upper over the
|
|
108
|
+
project: the agent sees the real files, every write lands on the upper.
|
|
109
|
+
Review and press it down from the same runtime:
|
|
47
110
|
|
|
48
111
|
```bash
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
112
|
+
SBX_RUNTIME=/tmp/sbx npx sbx diff [--agent <name>] # A/M/D, conflicts flagged
|
|
113
|
+
SBX_RUNTIME=/tmp/sbx npx sbx commit [--skip-conflicts] # refuses on conflicts by default
|
|
114
|
+
SBX_RUNTIME=/tmp/sbx npx sbx rollback # throw the paper away
|
|
52
115
|
```
|
|
53
116
|
|
|
54
|
-
|
|
55
|
-
|
|
117
|
+
A path the project changed after the run started is a conflict: commit
|
|
118
|
+
refuses all of it unless `--skip-conflicts`. `node_modules` is invisible to
|
|
119
|
+
the layer (extend with `SBX_OVERLAY_IGNORE=a,b`). Commit strips setuid/setgid
|
|
120
|
+
and does not keep mtimes; a killed commit leaves a `.sbx-commit-*` folder in
|
|
121
|
+
the project that blocks the next diff/commit/run until you remove it. The
|
|
122
|
+
library equivalents are `diffOverlay`, `commitOverlay` and `rollbackOverlay`.
|
|
123
|
+
The overlay needs bubblewrap >= 0.11.0 for `--overlay` / `sbx diff|commit|rollback` (Ubuntu 24.04 ships 0.9.0: build from source or use a newer distro); plain runs work with older bwrap.
|
|
56
124
|
|
|
57
125
|
## Gotchas
|
|
58
126
|
|
|
@@ -63,14 +131,19 @@ deny and timeout answer 403 with the reason.
|
|
|
63
131
|
`NODE_EXTRA_CA_CERTS`, `REQUESTS_CA_BUNDLE`, `CURL_CA_BUNDLE`,
|
|
64
132
|
`GIT_SSL_CAINFO`. Without it every HTTPS call from inside fails
|
|
65
133
|
verification. Only `ca.pem` enters the sandbox, never the key.
|
|
66
|
-
-
|
|
67
|
-
- The
|
|
68
|
-
- The
|
|
69
|
-
- `
|
|
134
|
+
- Certificates live in `<runtime>/certs` (`runtime` is required). There is no `certDir` option and no control socket: decisions flow through `<runtime>/asks` + `<runtime>/answers` or `hold.onAsk`. Each minted certificate gets a random 128-bit serial (no shared `ca.srl`), and at most 4 mints run at once.
|
|
135
|
+
- The TCP door is one agent: every connection through `createProxy`'s TCP `port` counts as the agent `tcp` and shares one hold line. Use `addAgent` sockets (`sbx run` does) for separate lines per agent. `removeAgent(name)` ends that agent's open connections; its held requests end as deny / client gone. `run()` calls it when the sandbox exits, so a request still held then ends as client gone, not `proxy closing`.
|
|
136
|
+
- The upstream wall answers `403` for loopback, link-local (incl. `169.254.169.254`), the host's own addresses, private LAN ranges and unresolvable names. An IPv4 embedded in IPv6 is checked in every spelling: mapped, compat, uncompressed, NAT64 `64:ff9b::/96` (local-use `/48` denied outright) and 6to4 `2002::/16`; NAT64 and 6to4 of public sites stay reachable. Every DNS answer is checked and the dial is pinned to the checked IP; test-only options live under `unsafeTestOnly` (`allowLoopback`, `resolveUpstream`) and production never sets them.
|
|
137
|
+
- `createProxy` throws synchronously on an unopenable `logFile` (a directory, a missing parent, no permission) and releases the runtime. The runtime root, `asks/`, `answers/` and `socks/` are owner-only (`0700`) whatever the umask. An existing runtime dir you own is tightened to `0700`; one you don't own (such as `/tmp`) is refused with an error naming the dir and its owner and suggesting a subdir like `/tmp/sbx`. The audit log (`logFile`) is set to `0600` every time a proxy starts. The values of `Authorization`, `Proxy-Authorization`, `Cookie`, `Set-Cookie`, `X-Api-Key`, `X-Auth-Token` and `Api-Key` are logged as `[redacted]`; other headers and bodies are logged as-is unless your `redact` hook shapes them (it runs after the built-in scrub).
|
|
138
|
+
- One proxy per runtime: `createProxy` throws "in use by pid N" (`run()` rejects, `sbx run` exits 1) while a live proxy owns the runtime. The claim is held by the proxy process and the kernel frees it when the process dies, so a crashed proxy never blocks the runtime. Use another `SBX_RUNTIME`, or leave it unset for a fresh one.
|
|
139
|
+
- `launch()` starts the agent in the project dir, in a new session (no controlling terminal). Locked drawers are 21 credential stores (see `LOCKED_PATHS` in `src/launcher.js`); the agent's own model credentials in `~/.claude` stay readable until keys move out of its reach.
|
|
140
|
+
- `sbx` exits 0 ok, 1 unknown id / refused / no live proxy / run failure, 2 usage; `sbx run` mirrors the agent's exit code.
|
|
70
141
|
- Any listening unix socket under `$HOME` (IDE IPC, rootless-container sockets) is reachable from inside the sandbox: a read-only bind does not block `connect(2)`; the locked-paths list is the lever.
|
|
71
142
|
|
|
72
143
|
## What this package does not do
|
|
73
144
|
|
|
74
|
-
Enforcement policy (which requests hold) arrives in a later module;
|
|
75
|
-
|
|
145
|
+
Enforcement policy (which requests hold) arrives in a later module; until
|
|
146
|
+
then holds follow the interim rules above (a path rule for `createProxy`;
|
|
147
|
+
payment destinations plus non-GET/HEAD for `run()`). The hold decision never
|
|
148
|
+
sees the request body, so card numbers in a body are not caught yet. No Docker, no MCP server, no macOS/Windows —
|
|
76
149
|
Linux only. POC harnesses live in `poc/` in the repo and never ship.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barebox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Lightweight local sandbox for AI agents: every write undoable, nothing leaves the machine without passing the doorman.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"redteam:m0": "bash poc/m0/redteam.sh",
|
|
38
38
|
"bench": "node scripts/bench.mjs",
|
|
39
39
|
"test": "env -u NODE_USE_ENV_PROXY -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u FTP_PROXY -u http_proxy -u https_proxy -u all_proxy -u ftp_proxy node --test --import ./test/setup-env.mjs \"test/*.test.mjs\"",
|
|
40
|
-
"typecheck": "tsc --noEmit",
|
|
40
|
+
"typecheck": "tsc --noEmit && tsc -p tsconfig.scripts.json",
|
|
41
41
|
"build:types": "tsc",
|
|
42
42
|
"build:primitives": "node scripts/gen-primitives.mjs",
|
|
43
43
|
"check:primitives": "node scripts/gen-primitives.mjs --check",
|
package/primitives.json
CHANGED
|
@@ -1,23 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"package": "barebox",
|
|
3
3
|
"primitives": [
|
|
4
|
+
{
|
|
5
|
+
"name": "commitOverlay",
|
|
6
|
+
"category": "overlay",
|
|
7
|
+
"when": "Reach for this to press one run's tracing paper down: it stages the sandbox's changes and swaps them into the project by rename, refusing loudly on mid-run conflicts.",
|
|
8
|
+
"import": "import { commitOverlay } from 'barebox'",
|
|
9
|
+
"signature": "commitOverlay(opts: object) => {committed: string[], skipped: {path: string, reason: string}[]}",
|
|
10
|
+
"fails": "Throws (writing nothing) when any changed file was also changed in the project mid-run, unless onConflict is `skip`; throws on unsupported file kinds, on a project that moved mid-commit, and when bubblewrap is older than 0.11.0. A phase-2 failure undoes every journaled step: a full undo reports `nothing was applied`, a partial one throws `SBX_COMMIT_FAILED` listing exactly the steps not undone and keeping the commit folder (originals in `old/`) named in the error. Setuid/setgid bits are stripped from committed modes; mtimes are not preserved. A killed commit leaves a folder that blocks the next diff/commit/setup until removed.",
|
|
11
|
+
"example": "import { commitOverlay } from \"barebox\";\nconst { committed, skipped } = commitOverlay({ runtime: \"/tmp/sbx-abc\", agent: \"agent\" });\nconsole.log(`committed ${committed.length} file(s), skipped ${skipped.length}`);"
|
|
12
|
+
},
|
|
4
13
|
{
|
|
5
14
|
"name": "createProxy",
|
|
6
15
|
"category": "proxy",
|
|
7
16
|
"when": "Reach for this HARNESS-ONLY to run the front door: it terminates agent TLS, sees every request, and parks the ones a human must decide. It decides nothing itself; pair with the launcher for sandboxes and `sbx` for decisions.",
|
|
8
17
|
"import": "import { createProxy } from 'barebox'",
|
|
9
18
|
"signature": "createProxy(opts: ProxyOptions)",
|
|
10
|
-
"fails": "Throws synchronously on a missing `logFile`
|
|
11
|
-
"example": "import { createProxy } from \"barebox\";\nconst proxy = createProxy({ port: 0,
|
|
19
|
+
"fails": "Throws synchronously on a missing or unopenable `logFile` or `runtime`. Throws when another live proxy owns the runtime; the claim dies with its process; nothing is ever taken over. At runtime, dead upstreams, malformed input, over-cap and timed-out holds all become loud logged HTTP errors, never silence and never a crash. A second proxy on the same port rejects `ready`, never crashes the process.",
|
|
20
|
+
"example": "import { createProxy } from \"barebox\";\nconst proxy = createProxy({ port: 0, runtime: \"./sbx-runtime\", logFile: \"./mitm.log\" });\nconst { port } = await proxy.ready;\nawait proxy.close();"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"name": "diffOverlay",
|
|
24
|
+
"category": "overlay",
|
|
25
|
+
"when": "Reach for this to review one run's tracing paper: it lists every file the sandboxed run added, modified or deleted, flagging the ones the project changed meanwhile.",
|
|
26
|
+
"import": "import { diffOverlay } from 'barebox'",
|
|
27
|
+
"signature": "diffOverlay(opts: object) => OverlayChange[]",
|
|
28
|
+
"fails": "Throws when no overlay exists for the agent, when the project is gone, when a leftover commit folder blocks, when bubblewrap is older than 0.11.0, or when the overlay view cannot be mounted; never writes anything.",
|
|
29
|
+
"example": "import { diffOverlay } from \"barebox\";\nconst changes = diffOverlay({ runtime: \"/tmp/sbx-abc\", agent: \"agent\" });\nfor (const c of changes) console.log(c.status, c.path, c.conflict ? \"(CONFLICT)\" : \"\");"
|
|
12
30
|
},
|
|
13
31
|
{
|
|
14
32
|
"name": "launch",
|
|
15
33
|
"category": "launcher",
|
|
16
|
-
"when": "Reach for this to HARNESS-ONLY start one sandboxed agent run: it creates the sandbox the agent lives in. It does not decide anything; combine with the proxy for the front door and the gate (M3) for policy.",
|
|
34
|
+
"when": "Reach for this to HARNESS-ONLY start one sandboxed agent run: it creates the sandbox the agent lives in. It does not decide anything; combine with the proxy for the front door and the gate (M3) for policy. Pass overlay:true for a writable tracing-paper run (M4). The agent starts in the project dir.",
|
|
17
35
|
"import": "import { launch } from 'barebox'",
|
|
18
36
|
"signature": "launch(opts: object) => ChildProcess",
|
|
19
|
-
"fails": "Throws synchronously on a missing/empty `runtime`, `agent`, `proj`, or `command`, on a bad agent name, on an over-long socket path, when `socket` disagrees with `<runtime>/socks/<agent>.sock`,
|
|
37
|
+
"fails": "Throws synchronously on a missing/empty `runtime`, `agent`, `proj`, or `command`, on a bad agent name, on a non-integer `proxyPort`, on an over-long socket path, when `socket` disagrees with `<runtime>/socks/<agent>.sock`, when `runtime` IS `HOME` or the project dir (its sockets could not be hidden from the agent), or with overlay on when the runtime sits inside the project or uncommitted changes linger. The child fails at runtime (nonzero exit) when `bwrap` or `socat` is missing inside, or when nothing listens on the agent socket.",
|
|
20
38
|
"example": "import { launch } from \"barebox\";\nconst child = launch({ runtime: \"/tmp/sbx\", agent: \"fx\", proj: \"/tmp/proj\", command: [\"bash\", \"-c\", \"echo hi\"] });\nawait new Promise((r) => child.on(\"close\", r)); // child.exitCode === 0"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"name": "rollbackOverlay",
|
|
42
|
+
"category": "overlay",
|
|
43
|
+
"when": "Reach for this to throw one run's tracing paper away: it drops every sandboxed change and leaves the project exactly as it was.",
|
|
44
|
+
"import": "import { rollbackOverlay } from 'barebox'",
|
|
45
|
+
"signature": "rollbackOverlay(opts: object) => {discarded: OverlayChange[], exact: boolean, why: string|null}",
|
|
46
|
+
"fails": "Throws when no overlay exists for the agent or its record is corrupt, or when the overlay folder cannot be removed even after a permission repair (the error names the folder). Listing the changes never throws and never invents entries: an unlistable upper reports `discarded: []` with the reasons in `why`. Removal depends on neither listing nor the first permission pass: a partial failure retries removal once after a fresh permission pass. Dropping the upper never touches the project.",
|
|
47
|
+
"example": "import { rollbackOverlay } from \"barebox\";\nconst { discarded } = rollbackOverlay({ runtime: \"/tmp/sbx-abc\", agent: \"agent\" });\nconsole.log(`discarded ${discarded.length} change(s)`);"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"name": "run",
|
|
51
|
+
"category": "run",
|
|
52
|
+
"when": "Reach for this to HARNESS-ONLY run one sandboxed agent start-to-finish in a single call: it starts the proxy, sandboxes the command, answers holds, and returns the exit code with the ask and audit trail (plus the overlay diff once M4 exists).",
|
|
53
|
+
"import": "import { run } from 'barebox'",
|
|
54
|
+
"signature": "run(opts: object) => Promise<{ exit: number, asks: AskRecord[], audit: string, diff: OverlayChange[], runtime: string }>",
|
|
55
|
+
"fails": "Rejects on a missing/unreadable `proj`, an empty `command`, a bad `mode`, a misshapen `onAsk`, a proxy that never becomes ready, a `launch` that throws (uncommitted overlay changes, a runtime inside the project — the proxy is closed first), or an overlay diff that cannot be read. The agent's own nonzero exit is returned as `exit`, never thrown.",
|
|
56
|
+
"example": "import { run } from \"barebox\";\nconst { exit, asks, audit } = await run({ proj: \"/tmp/proj\", command: [\"bash\", \"-c\", \"echo hi\"] });"
|
|
21
57
|
}
|
|
22
58
|
]
|
|
23
59
|
}
|
package/src/certs.js
CHANGED
|
@@ -6,8 +6,10 @@
|
|
|
6
6
|
import { execFile } from 'node:child_process';
|
|
7
7
|
import { mkdirSync, chmodSync, existsSync, writeFileSync, rmSync } from 'node:fs';
|
|
8
8
|
import { join } from 'node:path';
|
|
9
|
+
import { randomBytes } from 'node:crypto';
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
/** Hostname/IP spelling both the mint cache and the proxy wall accept. Single definition (see proxy.js). */
|
|
12
|
+
export const HOST_RE = /^[A-Za-z0-9.-]+$/;
|
|
11
13
|
|
|
12
14
|
function sh(file, args) {
|
|
13
15
|
return new Promise((resolve, reject) => {
|
|
@@ -21,6 +23,25 @@ function sh(file, args) {
|
|
|
21
23
|
// In-flight mints, so concurrent CONNECTs to a new host mint once.
|
|
22
24
|
const inflight = new Map();
|
|
23
25
|
|
|
26
|
+
// At most this many mints run at once: each is several `openssl` processes.
|
|
27
|
+
const MAX_MINTS = 4;
|
|
28
|
+
let minting = 0;
|
|
29
|
+
/** @type {Array<() => void>} */
|
|
30
|
+
const mintQueue = [];
|
|
31
|
+
|
|
32
|
+
/** @template T @param {() => Promise<T>} fn @returns {Promise<T>} fn's result, run once a mint slot is free */
|
|
33
|
+
async function withMintSlot(fn) {
|
|
34
|
+
if (minting >= MAX_MINTS) await new Promise((resolve) => mintQueue.push(() => resolve(undefined)));
|
|
35
|
+
else minting++;
|
|
36
|
+
try {
|
|
37
|
+
return await fn();
|
|
38
|
+
} finally {
|
|
39
|
+
// Hand the slot straight to the next waiter, else free it.
|
|
40
|
+
const next = mintQueue.shift();
|
|
41
|
+
if (next) next(); else minting--;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
24
45
|
/**
|
|
25
46
|
* Create the CA in `dir` unless it already exists. Safe to call twice:
|
|
26
47
|
* an existing `ca.pem` is reused, never regenerated.
|
|
@@ -73,7 +94,7 @@ export async function certFor(dir, host) {
|
|
|
73
94
|
}
|
|
74
95
|
let p = inflight.get(host);
|
|
75
96
|
if (!p) {
|
|
76
|
-
p = mint(dir, host, keyPath, certPath).finally(() => { inflight.delete(host); });
|
|
97
|
+
p = withMintSlot(() => mint(dir, host, keyPath, certPath)).finally(() => { inflight.delete(host); });
|
|
77
98
|
inflight.set(host, p);
|
|
78
99
|
}
|
|
79
100
|
return p;
|
|
@@ -99,7 +120,7 @@ async function mint(dir, host, keyPath, certPath) {
|
|
|
99
120
|
try {
|
|
100
121
|
await sh('openssl', ['req', '-new', '-key', keyPath, '-out', csr, '-subj', `/CN=${host}`]);
|
|
101
122
|
await sh('openssl', ['x509', '-req', '-in', csr, '-CA', caCert, '-CAkey', caKey,
|
|
102
|
-
'-
|
|
123
|
+
'-set_serial', '0x' + randomBytes(16).toString('hex'), '-days', '825', '-sha256', '-out', certPath, '-extfile', ext]);
|
|
103
124
|
} finally {
|
|
104
125
|
rmSync(csr, { force: true });
|
|
105
126
|
rmSync(ext, { force: true });
|