barebox 0.1.0 → 0.3.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 +97 -0
- package/README.md +152 -7
- package/barebox.context.md +144 -0
- package/package.json +40 -3
- package/primitives.json +59 -0
- package/src/certs.js +109 -0
- package/src/cli.js +361 -0
- package/src/holds.js +476 -0
- package/src/index.js +5 -0
- package/src/launcher.js +272 -0
- package/src/overlay-walk.js +49 -0
- package/src/overlay.js +1012 -0
- package/src/proxy.js +572 -0
- package/src/run.js +226 -0
- package/src/runtime.js +263 -0
- package/src/wall.js +156 -0
- package/types/certs.d.ts +26 -0
- package/types/cli.d.ts +2 -0
- package/types/holds.d.ts +248 -0
- package/types/index.d.ts +4 -0
- package/types/launcher.d.ts +53 -0
- package/types/overlay-walk.d.ts +1 -0
- package/types/overlay.d.ts +220 -0
- package/types/proxy.d.ts +204 -0
- package/types/run.d.ts +192 -0
- package/types/runtime.d.ts +112 -0
- package/types/wall.d.ts +30 -0
- package/.claude/remember/AGENT_RULES.md +0 -291
- package/docs/product/agent-sandbox-prd.md +0 -418
- package/docs/product/agent-sandbox.md +0 -218
- package/poc/m0/README.md +0 -20
- package/poc/m0/func-fx.txt +0 -15
- package/poc/m0/func-fy.txt +0 -15
- package/poc/m0/launch.sh +0 -43
- package/poc/m0/measure.js +0 -53
- package/poc/m0/measure.txt +0 -23
- package/poc/m0/proxy.js +0 -74
- package/poc/m0/proxy.log +0 -28
- package/poc/m0/red-mem.txt +0 -4
- package/poc/m0/red-proxy.log +0 -13
- package/poc/m0/redteam.sh +0 -118
- package/poc/m0/run-m0.sh +0 -136
package/types/proxy.d.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for {@link createProxy}.
|
|
3
|
+
*
|
|
4
|
+
* @typedef {object} ProxyOptions
|
|
5
|
+
* @property {string} logFile JSON-lines log path (`req`, `resp`, `deny`, `hold`, `decide`, `hold-abandoned`)
|
|
6
|
+
* @property {string} runtime runtime dir; required. Holds the CA + minted host certs (`certs/`, see `runtime.js`), the agent sockets, and the hold queue files.
|
|
7
|
+
* @property {number} [port] opt-in TCP listener for tests, bound to 127.0.0.1 only (default: none)
|
|
8
|
+
* @property {string} [upstreamCa] extra PEM the proxy trusts for upstream TLS, on top of system roots
|
|
9
|
+
* @property {HoldOptions} [hold] hold queue config, or omit to forward everything
|
|
10
|
+
* @property {number} [upstreamTimeoutMs] give up on an upstream response after this (default 120000)
|
|
11
|
+
* @property {object} [unsafeTestOnly] TEST-ONLY escape hatch: everything under
|
|
12
|
+
* it weakens the sandbox wall, so production omits it and tests name it loudly.
|
|
13
|
+
* @property {boolean} [unsafeTestOnly.allowLoopback] let loopback and unspecified
|
|
14
|
+
* destinations through (default false). Fixture servers listen on 127.0.0.1,
|
|
15
|
+
* so tests pass this; production never should.
|
|
16
|
+
* @property {(host: string) => Promise<string[] | string | null>} [unsafeTestOnly.resolveUpstream]
|
|
17
|
+
* DNS stub: replaces the wall's resolver so rebinding is testable. Production omits it.
|
|
18
|
+
* @property {number} [clientIdleMs] kill a client tunnel idle this long; never fires mid-request (default 60000)
|
|
19
|
+
* @property {number} [maxLogBody] log at most this many request-body bytes, then `truncated: true` (default 1048576)
|
|
20
|
+
* @property {(entry: Record<string, unknown>) => Record<string, unknown>} [redact] log redaction hook, applied after built-in secret scrubbing (default: identity)
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Hold queue options (the decision seam lives in holds.js).
|
|
24
|
+
*
|
|
25
|
+
* @typedef {object} HoldOptions
|
|
26
|
+
* @property {string} [match] path substring that parks a request (default decision)
|
|
27
|
+
* @property {import('./holds.js').ShouldHold} [shouldHold] decision function; overrides `match`
|
|
28
|
+
* @property {number|string} [timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
29
|
+
* @property {number} [maxBody] bodies over this are denied at the door, never queued (default 1048576)
|
|
30
|
+
* @property {import('./holds.js').OnAsk|null} [onAsk] ask seam (bareguard `humanChannel` shape).
|
|
31
|
+
* When present no files are involved; omit it for the default file checkpoint
|
|
32
|
+
* (`<runtime>/asks` + `<runtime>/answers`, `sbx pending/approve/deny`).
|
|
33
|
+
* @property {number} [pollMs] answer poll interval in ms for the file checkpoint (default 250)
|
|
34
|
+
* @property {import('./holds.js').OnEvent} [onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* One upstream forwarding: buffered body (approve path) or live stream.
|
|
38
|
+
*
|
|
39
|
+
* @typedef {object} ForwardArgs
|
|
40
|
+
* @property {string} agent agent name (its socket)
|
|
41
|
+
* @property {string} method HTTP method
|
|
42
|
+
* @property {string} url full URL
|
|
43
|
+
* @property {Record<string, string | string[] | undefined>} headers
|
|
44
|
+
* @property {Buffer} [body] buffered body (approve path)
|
|
45
|
+
* @property {import('node:http').IncomingMessage} [stream] live body (direct path)
|
|
46
|
+
* @property {import('node:http').ServerResponse} cres parked or live client response
|
|
47
|
+
*/
|
|
48
|
+
/**
|
|
49
|
+
* Live proxy. Let `tsc` infer the shape; adopters import the name.
|
|
50
|
+
*
|
|
51
|
+
* @typedef {ReturnType<typeof createProxy>} Proxy
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* Start the front-door proxy. Every request is logged with its method, URL,
|
|
55
|
+
* headers and body; bytes go upstream untouched. Requests the hold rule
|
|
56
|
+
* flags park for a human decision instead of forwarding.
|
|
57
|
+
*
|
|
58
|
+
* @param {ProxyOptions} opts
|
|
59
|
+
* @returns live proxy: `ready` resolves when listeners + CA are up; the shape is inferred, see `Proxy`
|
|
60
|
+
* @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.
|
|
61
|
+
* @fails Throws synchronously on a missing `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.
|
|
62
|
+
* @example
|
|
63
|
+
* import { createProxy } from "barebox";
|
|
64
|
+
* const proxy = createProxy({ port: 0, runtime: "./sbx-runtime", logFile: "./mitm.log" });
|
|
65
|
+
* const { port } = await proxy.ready;
|
|
66
|
+
* await proxy.close();
|
|
67
|
+
*/
|
|
68
|
+
export function createProxy(opts: ProxyOptions): {
|
|
69
|
+
ready: Promise<{
|
|
70
|
+
port: number | null;
|
|
71
|
+
}>;
|
|
72
|
+
close: () => Promise<void>;
|
|
73
|
+
addAgent: (name: any) => string;
|
|
74
|
+
removeAgent: (name: any) => void;
|
|
75
|
+
pending: () => import("./holds.js").HeldRequest[];
|
|
76
|
+
readonly caFile: string;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Options for {@link createProxy}.
|
|
80
|
+
*/
|
|
81
|
+
export type ProxyOptions = {
|
|
82
|
+
/**
|
|
83
|
+
* JSON-lines log path (`req`, `resp`, `deny`, `hold`, `decide`, `hold-abandoned`)
|
|
84
|
+
*/
|
|
85
|
+
logFile: string;
|
|
86
|
+
/**
|
|
87
|
+
* runtime dir; required. Holds the CA + minted host certs (`certs/`, see `runtime.js`), the agent sockets, and the hold queue files.
|
|
88
|
+
*/
|
|
89
|
+
runtime: string;
|
|
90
|
+
/**
|
|
91
|
+
* opt-in TCP listener for tests, bound to 127.0.0.1 only (default: none)
|
|
92
|
+
*/
|
|
93
|
+
port?: number | undefined;
|
|
94
|
+
/**
|
|
95
|
+
* extra PEM the proxy trusts for upstream TLS, on top of system roots
|
|
96
|
+
*/
|
|
97
|
+
upstreamCa?: string | undefined;
|
|
98
|
+
/**
|
|
99
|
+
* hold queue config, or omit to forward everything
|
|
100
|
+
*/
|
|
101
|
+
hold?: HoldOptions | undefined;
|
|
102
|
+
/**
|
|
103
|
+
* give up on an upstream response after this (default 120000)
|
|
104
|
+
*/
|
|
105
|
+
upstreamTimeoutMs?: number | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* TEST-ONLY escape hatch: everything under
|
|
108
|
+
* it weakens the sandbox wall, so production omits it and tests name it loudly.
|
|
109
|
+
*/
|
|
110
|
+
unsafeTestOnly?: {
|
|
111
|
+
/**
|
|
112
|
+
* let loopback and unspecified
|
|
113
|
+
* destinations through (default false). Fixture servers listen on 127.0.0.1,
|
|
114
|
+
* so tests pass this; production never should.
|
|
115
|
+
*/
|
|
116
|
+
allowLoopback?: boolean | undefined;
|
|
117
|
+
/**
|
|
118
|
+
* DNS stub: replaces the wall's resolver so rebinding is testable. Production omits it.
|
|
119
|
+
*/
|
|
120
|
+
resolveUpstream?: ((host: string) => Promise<string[] | string | null>) | undefined;
|
|
121
|
+
} | undefined;
|
|
122
|
+
/**
|
|
123
|
+
* kill a client tunnel idle this long; never fires mid-request (default 60000)
|
|
124
|
+
*/
|
|
125
|
+
clientIdleMs?: number | undefined;
|
|
126
|
+
/**
|
|
127
|
+
* log at most this many request-body bytes, then `truncated: true` (default 1048576)
|
|
128
|
+
*/
|
|
129
|
+
maxLogBody?: number | undefined;
|
|
130
|
+
/**
|
|
131
|
+
* log redaction hook, applied after built-in secret scrubbing (default: identity)
|
|
132
|
+
*/
|
|
133
|
+
redact?: ((entry: Record<string, unknown>) => Record<string, unknown>) | undefined;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Hold queue options (the decision seam lives in holds.js).
|
|
137
|
+
*/
|
|
138
|
+
export type HoldOptions = {
|
|
139
|
+
/**
|
|
140
|
+
* path substring that parks a request (default decision)
|
|
141
|
+
*/
|
|
142
|
+
match?: string | undefined;
|
|
143
|
+
/**
|
|
144
|
+
* decision function; overrides `match`
|
|
145
|
+
*/
|
|
146
|
+
shouldHold?: import("./holds.js").ShouldHold | undefined;
|
|
147
|
+
/**
|
|
148
|
+
* hold deadline in ms, or `'never'` (default 120000)
|
|
149
|
+
*/
|
|
150
|
+
timeoutMs?: string | number | undefined;
|
|
151
|
+
/**
|
|
152
|
+
* bodies over this are denied at the door, never queued (default 1048576)
|
|
153
|
+
*/
|
|
154
|
+
maxBody?: number | undefined;
|
|
155
|
+
/**
|
|
156
|
+
* ask seam (bareguard `humanChannel` shape).
|
|
157
|
+
* When present no files are involved; omit it for the default file checkpoint
|
|
158
|
+
* (`<runtime>/asks` + `<runtime>/answers`, `sbx pending/approve/deny`).
|
|
159
|
+
*/
|
|
160
|
+
onAsk?: import("./holds.js").OnAsk | null | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* answer poll interval in ms for the file checkpoint (default 250)
|
|
163
|
+
*/
|
|
164
|
+
pollMs?: number | undefined;
|
|
165
|
+
/**
|
|
166
|
+
* event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle
|
|
167
|
+
*/
|
|
168
|
+
onEvent?: import("./holds.js").OnEvent | undefined;
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* One upstream forwarding: buffered body (approve path) or live stream.
|
|
172
|
+
*/
|
|
173
|
+
export type ForwardArgs = {
|
|
174
|
+
/**
|
|
175
|
+
* agent name (its socket)
|
|
176
|
+
*/
|
|
177
|
+
agent: string;
|
|
178
|
+
/**
|
|
179
|
+
* HTTP method
|
|
180
|
+
*/
|
|
181
|
+
method: string;
|
|
182
|
+
/**
|
|
183
|
+
* full URL
|
|
184
|
+
*/
|
|
185
|
+
url: string;
|
|
186
|
+
headers: Record<string, string | string[] | undefined>;
|
|
187
|
+
/**
|
|
188
|
+
* buffered body (approve path)
|
|
189
|
+
*/
|
|
190
|
+
body?: Buffer<ArrayBufferLike> | undefined;
|
|
191
|
+
/**
|
|
192
|
+
* live body (direct path)
|
|
193
|
+
*/
|
|
194
|
+
stream?: http.IncomingMessage | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* parked or live client response
|
|
197
|
+
*/
|
|
198
|
+
cres: import("node:http").ServerResponse;
|
|
199
|
+
};
|
|
200
|
+
/**
|
|
201
|
+
* Live proxy. Let `tsc` infer the shape; adopters import the name.
|
|
202
|
+
*/
|
|
203
|
+
export type Proxy = ReturnType<typeof createProxy>;
|
|
204
|
+
import http from 'node:http';
|
package/types/run.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the hold decision for a mode. Pure: unit-testable without a sandbox.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} mode `ask` | `auto` | `trust`
|
|
5
|
+
* @returns {import('./holds.js').ShouldHold} decision function for the hold queue
|
|
6
|
+
*/
|
|
7
|
+
export function shouldHoldForMode(mode: string): import("./holds.js").ShouldHold;
|
|
8
|
+
/**
|
|
9
|
+
* A collected ask record: the parked ask plus how it was decided.
|
|
10
|
+
*
|
|
11
|
+
* @typedef {object} AskRecord
|
|
12
|
+
* @property {string} id hold id (`h<n>`)
|
|
13
|
+
* @property {string} agent agent name (its socket)
|
|
14
|
+
* @property {string} method HTTP method
|
|
15
|
+
* @property {string} url full URL
|
|
16
|
+
* @property {string} rule hold-reason string
|
|
17
|
+
* @property {string} letter always `'x'` until M3 owns letters
|
|
18
|
+
* @property {string} [bodyPreview] request body preview
|
|
19
|
+
* @property {boolean} [truncated] body preview truncation flag
|
|
20
|
+
* @property {string|null} [expiresAt] ISO-8601 UTC deadline, or null
|
|
21
|
+
* @property {string} [decision] `'allow'` | `'deny'`, once decided
|
|
22
|
+
* @property {string} [reason] decision reason, once decided
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Options for {@link run}.
|
|
26
|
+
*
|
|
27
|
+
* @typedef {object} RunOptions
|
|
28
|
+
* @property {string} proj project directory, bound read-only inside the sandbox
|
|
29
|
+
* @property {string[]} command argv to run inside, for example `["bash", "-c", "echo hi"]`
|
|
30
|
+
* @property {string} [mode] `ask` | `auto` (default) | `trust`; see the mode comment at the top of this file
|
|
31
|
+
* @property {import('./holds.js').OnAsk} [onAsk] ask seam; when present no files are involved, otherwise the file checkpoint runs
|
|
32
|
+
* @property {import('./holds.js').OnEvent} [onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon (also feeds `asks`)
|
|
33
|
+
* @property {string} [runtime] runtime dir (created when absent: a short `sbx-*` temp dir, keeping unix socket paths under the ~108-char limit)
|
|
34
|
+
* @property {string} [agent] agent name (default `'agent'`)
|
|
35
|
+
* @property {number|string} [timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
36
|
+
* @property {object} [unsafeTestOnly] TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`): everything under it weakens the sandbox wall
|
|
37
|
+
* @property {NodeJS.WritableStream} [stdout] where the agent's stdout goes (default process.stdout)
|
|
38
|
+
* @property {NodeJS.WritableStream} [stderr] where the agent's stderr goes (default process.stderr)
|
|
39
|
+
* @property {boolean} [overlay] when true, mount a per-run overlay upper over the project (M4 tracing paper) instead of binding it read-only; the result's `diff` lists the run's changes for commit/rollback
|
|
40
|
+
* @property {string[]} [overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Run one sandboxed agent start-to-finish in a single call.
|
|
44
|
+
*
|
|
45
|
+
* @param {object} opts
|
|
46
|
+
* @param {string} opts.proj project directory, bound read-only inside the sandbox
|
|
47
|
+
* @param {string[]} opts.command argv to run inside, for example `["bash", "-c", "echo hi"]`
|
|
48
|
+
* @param {string} [opts.mode] `ask` | `auto` (default) | `trust`; see the mode comment at the top of this file
|
|
49
|
+
* @param {import('./holds.js').OnAsk|null} [opts.onAsk] ask seam; when present no files are involved, otherwise the file checkpoint runs
|
|
50
|
+
* @param {import('./holds.js').OnEvent|null} [opts.onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon (also feeds `asks`)
|
|
51
|
+
* @param {string|null} [opts.runtime] runtime dir (created when absent: a short `sbx-*` temp dir, keeping unix socket paths under the ~108-char limit)
|
|
52
|
+
* @param {string} [opts.agent] agent name (default `'agent'`)
|
|
53
|
+
* @param {number|string} [opts.timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
54
|
+
* @param {object|null} [opts.unsafeTestOnly] TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`)
|
|
55
|
+
* @param {NodeJS.WritableStream|null} [opts.stdout] where the agent's stdout goes (default process.stdout)
|
|
56
|
+
* @param {NodeJS.WritableStream|null} [opts.stderr] where the agent's stderr goes (default process.stderr)
|
|
57
|
+
* @param {boolean} [opts.overlay] when true, mount a per-run overlay upper over the project instead of binding it read-only; the result's `diff` lists the run's changes for commit/rollback
|
|
58
|
+
* @param {string[]} [opts.overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
59
|
+
* @returns {Promise<{ exit: number, asks: AskRecord[], audit: string, diff: import('./overlay.js').OverlayChange[], runtime: string }>} the agent process exit code, the collected ask records, the JSONL audit log path, the run's overlay diff (M4), and the runtime dir addressing it
|
|
60
|
+
* @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).
|
|
61
|
+
* @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.
|
|
62
|
+
* @example
|
|
63
|
+
* import { run } from "barebox";
|
|
64
|
+
* const { exit, asks, audit } = await run({ proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
|
|
65
|
+
*/
|
|
66
|
+
export function run({ proj, command, mode, onAsk, onEvent, runtime, agent, timeoutMs, unsafeTestOnly, stdout, stderr, overlay, overlayIgnore }: {
|
|
67
|
+
proj: string;
|
|
68
|
+
command: string[];
|
|
69
|
+
mode?: string | undefined;
|
|
70
|
+
onAsk?: import("./holds.js").OnAsk | null | undefined;
|
|
71
|
+
onEvent?: import("./holds.js").OnEvent | null | undefined;
|
|
72
|
+
runtime?: string | null | undefined;
|
|
73
|
+
agent?: string | undefined;
|
|
74
|
+
timeoutMs?: string | number | undefined;
|
|
75
|
+
unsafeTestOnly?: object | null;
|
|
76
|
+
stdout?: NodeJS.WritableStream | null | undefined;
|
|
77
|
+
stderr?: NodeJS.WritableStream | null | undefined;
|
|
78
|
+
overlay?: boolean | undefined;
|
|
79
|
+
overlayIgnore?: string[] | undefined;
|
|
80
|
+
}): Promise<{
|
|
81
|
+
exit: number;
|
|
82
|
+
asks: AskRecord[];
|
|
83
|
+
audit: string;
|
|
84
|
+
diff: import("./overlay.js").OverlayChange[];
|
|
85
|
+
runtime: string;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* A collected ask record: the parked ask plus how it was decided.
|
|
89
|
+
*/
|
|
90
|
+
export type AskRecord = {
|
|
91
|
+
/**
|
|
92
|
+
* hold id (`h<n>`)
|
|
93
|
+
*/
|
|
94
|
+
id: string;
|
|
95
|
+
/**
|
|
96
|
+
* agent name (its socket)
|
|
97
|
+
*/
|
|
98
|
+
agent: string;
|
|
99
|
+
/**
|
|
100
|
+
* HTTP method
|
|
101
|
+
*/
|
|
102
|
+
method: string;
|
|
103
|
+
/**
|
|
104
|
+
* full URL
|
|
105
|
+
*/
|
|
106
|
+
url: string;
|
|
107
|
+
/**
|
|
108
|
+
* hold-reason string
|
|
109
|
+
*/
|
|
110
|
+
rule: string;
|
|
111
|
+
/**
|
|
112
|
+
* always `'x'` until M3 owns letters
|
|
113
|
+
*/
|
|
114
|
+
letter: string;
|
|
115
|
+
/**
|
|
116
|
+
* request body preview
|
|
117
|
+
*/
|
|
118
|
+
bodyPreview?: string | undefined;
|
|
119
|
+
/**
|
|
120
|
+
* body preview truncation flag
|
|
121
|
+
*/
|
|
122
|
+
truncated?: boolean | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* ISO-8601 UTC deadline, or null
|
|
125
|
+
*/
|
|
126
|
+
expiresAt?: string | null | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* `'allow'` | `'deny'`, once decided
|
|
129
|
+
*/
|
|
130
|
+
decision?: string | undefined;
|
|
131
|
+
/**
|
|
132
|
+
* decision reason, once decided
|
|
133
|
+
*/
|
|
134
|
+
reason?: string | undefined;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Options for {@link run}.
|
|
138
|
+
*/
|
|
139
|
+
export type RunOptions = {
|
|
140
|
+
/**
|
|
141
|
+
* project directory, bound read-only inside the sandbox
|
|
142
|
+
*/
|
|
143
|
+
proj: string;
|
|
144
|
+
/**
|
|
145
|
+
* argv to run inside, for example `["bash", "-c", "echo hi"]`
|
|
146
|
+
*/
|
|
147
|
+
command: string[];
|
|
148
|
+
/**
|
|
149
|
+
* `ask` | `auto` (default) | `trust`; see the mode comment at the top of this file
|
|
150
|
+
*/
|
|
151
|
+
mode?: string | undefined;
|
|
152
|
+
/**
|
|
153
|
+
* ask seam; when present no files are involved, otherwise the file checkpoint runs
|
|
154
|
+
*/
|
|
155
|
+
onAsk?: import("./holds.js").OnAsk | undefined;
|
|
156
|
+
/**
|
|
157
|
+
* event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon (also feeds `asks`)
|
|
158
|
+
*/
|
|
159
|
+
onEvent?: import("./holds.js").OnEvent | undefined;
|
|
160
|
+
/**
|
|
161
|
+
* runtime dir (created when absent: a short `sbx-*` temp dir, keeping unix socket paths under the ~108-char limit)
|
|
162
|
+
*/
|
|
163
|
+
runtime?: string | undefined;
|
|
164
|
+
/**
|
|
165
|
+
* agent name (default `'agent'`)
|
|
166
|
+
*/
|
|
167
|
+
agent?: string | undefined;
|
|
168
|
+
/**
|
|
169
|
+
* hold deadline in ms, or `'never'` (default 120000)
|
|
170
|
+
*/
|
|
171
|
+
timeoutMs?: string | number | undefined;
|
|
172
|
+
/**
|
|
173
|
+
* TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`): everything under it weakens the sandbox wall
|
|
174
|
+
*/
|
|
175
|
+
unsafeTestOnly?: object;
|
|
176
|
+
/**
|
|
177
|
+
* where the agent's stdout goes (default process.stdout)
|
|
178
|
+
*/
|
|
179
|
+
stdout?: NodeJS.WritableStream | undefined;
|
|
180
|
+
/**
|
|
181
|
+
* where the agent's stderr goes (default process.stderr)
|
|
182
|
+
*/
|
|
183
|
+
stderr?: NodeJS.WritableStream | undefined;
|
|
184
|
+
/**
|
|
185
|
+
* when true, mount a per-run overlay upper over the project (M4 tracing paper) instead of binding it read-only; the result's `diff` lists the run's changes for commit/rollback
|
|
186
|
+
*/
|
|
187
|
+
overlay?: boolean | undefined;
|
|
188
|
+
/**
|
|
189
|
+
* path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
190
|
+
*/
|
|
191
|
+
overlayIgnore?: string[] | undefined;
|
|
192
|
+
};
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check an agent name before it reaches the filesystem.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} name agent name
|
|
5
|
+
* @returns {void} throws on `../` and friends
|
|
6
|
+
*/
|
|
7
|
+
export function assertAgentName(name: string): void;
|
|
8
|
+
/**
|
|
9
|
+
* Resolve symlinks through the nearest existing ancestor: socket paths may
|
|
10
|
+
* not exist yet, so `realpathSync` on the full path would throw. Walks up
|
|
11
|
+
* to the first existing ancestor, resolves that, then re-attaches the rest.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} p any path, existing or not
|
|
14
|
+
* @returns {string} the real (symlink-free) spelling of `p`
|
|
15
|
+
*/
|
|
16
|
+
export function realPathOf(p: string): string;
|
|
17
|
+
/**
|
|
18
|
+
* True when `p` is `base` or lives under it, compared as real (symlink-free)
|
|
19
|
+
* paths, so a symlinked spelling cannot smuggle a path out.
|
|
20
|
+
*
|
|
21
|
+
* @param {string} base container path
|
|
22
|
+
* @param {string} p path to test
|
|
23
|
+
* @returns {boolean} containment on real paths
|
|
24
|
+
*/
|
|
25
|
+
export function isInside(base: string, p: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Create a short-lived runtime dir: a short `sbx-` temp dir, keeping unix
|
|
28
|
+
* socket paths under the ~108-char limit.
|
|
29
|
+
*
|
|
30
|
+
* @returns {string} the new runtime dir
|
|
31
|
+
*/
|
|
32
|
+
export function createRuntime(): string;
|
|
33
|
+
/**
|
|
34
|
+
* Create a temp dir inside the runtime tree: `mkdtemp` is a runtime-layout
|
|
35
|
+
* writer, so it lives here, not in `overlay.js`. Used for the merged-view
|
|
36
|
+
* mount (`merged`) and the walker manifest dir (`walk`).
|
|
37
|
+
*
|
|
38
|
+
* @param {string} scratch overlay record dir (`overlay/<agent>`)
|
|
39
|
+
* @param {string} prefix dir prefix (`merged`, `walk`)
|
|
40
|
+
* @returns {string} the new dir
|
|
41
|
+
*/
|
|
42
|
+
export function runtimeTempDir(scratch: string, prefix: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* The abstract-socket name of a runtime's claim lock. Hashes the real path,
|
|
45
|
+
* device and inode, so a symlinked path finds the same lock and a recreated
|
|
46
|
+
* dir gets a new one.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} runtime runtime dir
|
|
49
|
+
* @returns {string} the abstract socket name
|
|
50
|
+
*/
|
|
51
|
+
export function lockName(runtime: string): string;
|
|
52
|
+
/**
|
|
53
|
+
* True when a live proxy owns the runtime: something answers on its lock.
|
|
54
|
+
* Never binds, so it can never steal the claim from a proxy that's starting.
|
|
55
|
+
* A runtime that doesn't exist is not live.
|
|
56
|
+
*
|
|
57
|
+
* @param {string} runtime runtime dir
|
|
58
|
+
* @returns {Promise<boolean>} whether the lock connects
|
|
59
|
+
*/
|
|
60
|
+
export function runtimeLive(runtime: string): Promise<boolean>;
|
|
61
|
+
/**
|
|
62
|
+
* Claim one runtime for one live proxy: the single enforcer of "one proxy
|
|
63
|
+
* owns a runtime". The claim is an abstract unix socket the proxy binds for
|
|
64
|
+
* as long as it lives (Linux). The kernel frees it when the process dies,
|
|
65
|
+
* SIGKILL included, so a claim is never stale: nothing is judged, taken
|
|
66
|
+
* over or deleted by hand. The name hashes the real path, device and inode,
|
|
67
|
+
* so a symlinked path finds the same lock and a recreated dir gets a new one.
|
|
68
|
+
* The bind result is synchronous (`server.listening` right after `listen`),
|
|
69
|
+
* so a second claim, in this process or another, throws before the caller
|
|
70
|
+
* touches the runtime. The lock is `unref`'d: it never keeps a process alive.
|
|
71
|
+
* The owner file `{"pid"}` is written by the lock holder only, to name it in
|
|
72
|
+
* the in-use message.
|
|
73
|
+
*
|
|
74
|
+
* @param {string} runtime runtime dir to claim
|
|
75
|
+
* @returns {() => void} `release()`: unlinks the owner file only when it holds our pid, then closes the lock; idempotent per claim
|
|
76
|
+
*/
|
|
77
|
+
export function claimRuntime(runtime: string): () => void;
|
|
78
|
+
/**
|
|
79
|
+
* Strip terminal control before printing agent-controlled bytes (ask URLs,
|
|
80
|
+
* rules, body previews, overlay paths): an escape sequence smuggled into a
|
|
81
|
+
* preview could otherwise rewrite the approver's terminal line before
|
|
82
|
+
* approval, and bidi controls in an agent-chosen filename could flip how a
|
|
83
|
+
* reviewed path reads.
|
|
84
|
+
*
|
|
85
|
+
* @param {unknown} s value to print
|
|
86
|
+
* @returns {string} printable text with no VT sequences, C0/C1 controls, or bidi controls
|
|
87
|
+
*/
|
|
88
|
+
export function safe(s: unknown): string;
|
|
89
|
+
/**
|
|
90
|
+
* Every path in a runtime dir. The one place that names them.
|
|
91
|
+
*
|
|
92
|
+
* @param {string} runtime runtime dir
|
|
93
|
+
* @returns {{ socket: (agent: string) => string, asksDir: string, answersDir: string, askFile: (id: string) => string, answerFile: (id: string) => string, certsDir: string, caBundle: string, audit: string, owner: string, overlay: (agent: string) => { base: string, upper: string, work: string, manifest: string } }} layout
|
|
94
|
+
*/
|
|
95
|
+
export function runtimePaths(runtime: string): {
|
|
96
|
+
socket: (agent: string) => string;
|
|
97
|
+
asksDir: string;
|
|
98
|
+
answersDir: string;
|
|
99
|
+
askFile: (id: string) => string;
|
|
100
|
+
answerFile: (id: string) => string;
|
|
101
|
+
certsDir: string;
|
|
102
|
+
caBundle: string;
|
|
103
|
+
audit: string;
|
|
104
|
+
owner: string;
|
|
105
|
+
overlay: (agent: string) => {
|
|
106
|
+
base: string;
|
|
107
|
+
upper: string;
|
|
108
|
+
work: string;
|
|
109
|
+
manifest: string;
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
export const AGENT_RE: RegExp;
|
package/types/wall.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Why `ip` (already resolved) is denied, or null when it may go through.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} rawIp resolved destination IP
|
|
5
|
+
* @param {boolean} allowLoopback test-only opt-in for loopback + unspecified
|
|
6
|
+
* @returns {string|null} `loopback` / `unspecified` / `link-local` / `host-local` / `private`, or null
|
|
7
|
+
*/
|
|
8
|
+
export function denyReasonForIp(rawIp: string, allowLoopback: boolean): string | null;
|
|
9
|
+
/**
|
|
10
|
+
* The wall: resolve every answer, then default-deny loopback/link-local/
|
|
11
|
+
* host-local/private unless the test-only opt-ins allow it. Denies when ANY
|
|
12
|
+
* answer is walled, denies unresolvable names, and pins every checked IP so
|
|
13
|
+
* the dial below tries them in order without resolving a second time (no
|
|
14
|
+
* rebinding, with fallback when the first address refuses).
|
|
15
|
+
*
|
|
16
|
+
* @param {string} host CONNECT/request hostname
|
|
17
|
+
* @param {{ allowLoopback: boolean, resolveUpstream?: (host: string) => Promise<string[] | string | null> }} gate
|
|
18
|
+
* @returns {Promise<{ ok: true, ips: string[] } | { ok: false, ip: string, reason: string }>}
|
|
19
|
+
*/
|
|
20
|
+
export function checkUpstream(host: string, gate: {
|
|
21
|
+
allowLoopback: boolean;
|
|
22
|
+
resolveUpstream?: (host: string) => Promise<string[] | string | null>;
|
|
23
|
+
}): Promise<{
|
|
24
|
+
ok: true;
|
|
25
|
+
ips: string[];
|
|
26
|
+
} | {
|
|
27
|
+
ok: false;
|
|
28
|
+
ip: string;
|
|
29
|
+
reason: string;
|
|
30
|
+
}>;
|