barebox 0.4.0 → 0.5.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/types/proxy.d.ts CHANGED
@@ -2,11 +2,14 @@
2
2
  * Options for {@link createProxy}.
3
3
  *
4
4
  * @typedef {object} ProxyOptions
5
- * @property {string} logFile JSON-lines log path (`req`, `resp`, `deny`, `hold`, `decide`, `hold-abandoned`)
5
+ * @property {string} logFile the runtime's one audit file (`runtimePaths(runtime).audit`), in bareguard's JSON-lines shape: every agent's gate and the runtime's own `sbx` gate append to it. barebox's own facts are `annotate` lines (`verdict` = `resp`, `deny`, `hold`, `decide`, `hold-abandoned`, ...); a response status is a `record` line joined to its request's `check` by `aid`.
6
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
7
  * @property {number} [port] opt-in TCP listener for tests, bound to 127.0.0.1 only (default: none)
8
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
9
+ * @property {(agent: string, ctx: { humanChannel: (event: any) => Promise<{ decision: 'allow' | 'deny', reason?: string }>, tools: Record<string, any> }) => GateLike | Promise<GateLike>} [gateFor] builds the gate that decides one agent's requests (called once per agent, and once for the TCP door's `tcp` agent); `ctx.humanChannel` is the hold queue and `ctx.tools` the runtime's one tools table, both to hand to a bareguard `Gate`. Omit it to forward everything.
10
+ * @property {{ matcher: { match: (method: string, url: string) => { key: string, source: string } }, tools: Record<string, any> }} [labels] the loaded label files (see `loadLabels`); omitted means no labels, only the built-in model endpoints
11
+ * @property {string[]} [allowLocal] exact `host:port` pairs (the `localLlm` setting) that may pass the wall's private/loopback check; any other local destination is still denied
12
+ * @property {HoldOptions} [hold] hold queue config (deadline, caps, ask seam)
10
13
  * @property {number} [upstreamTimeoutMs] give up on an upstream response after this (default 120000)
11
14
  * @property {object} [unsafeTestOnly] TEST-ONLY escape hatch: everything under
12
15
  * it weakens the sandbox wall, so production omits it and tests name it loudly.
@@ -16,17 +19,20 @@
16
19
  * @property {(host: string) => Promise<string[] | string | null>} [unsafeTestOnly.resolveUpstream]
17
20
  * DNS stub: replaces the wall's resolver so rebinding is testable. Production omits it.
18
21
  * @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)
22
+ * @property {(entry: Record<string, unknown>) => Record<string, unknown>} [redact] log redaction hook, applied after built-in secret scrubbing; its result becomes the `meta` of an `annotate` line (default: identity)
21
23
  */
22
24
  /**
23
- * Hold queue options (the decision seam lives in holds.js).
25
+ * Anything with bareguard's `check`: a `Gate`, or a plain object for tests. An
26
+ * `askHuman` outcome from a plain object goes to the same human channel.
27
+ *
28
+ * @typedef {{ check: (action: Record<string, unknown>) => Promise<{ outcome: string, rule?: string, reason?: string | null, aid?: string }>, record?: (action: Record<string, unknown>, result: Record<string, unknown>, opts?: { aid?: string }) => Promise<void>, annotate?: (fact: Record<string, unknown>) => Promise<void>, drainAnnotations?: () => unknown[] }} GateLike
29
+ */
30
+ /**
31
+ * Hold queue options (the queue lives in holds.js).
24
32
  *
25
33
  * @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
34
  * @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)
35
+ * @property {number} [maxBody] bodies over this are denied at the door, never queued; held bodies spool to `<runtime>/held` (default 52428800)
30
36
  * @property {import('./holds.js').OnAsk|null} [onAsk] ask seam (bareguard `humanChannel` shape).
31
37
  * When present no files are involved; omit it for the default file checkpoint
32
38
  * (`<runtime>/asks` + `<runtime>/answers`, `sbx pending/approve/deny`).
@@ -41,9 +47,11 @@
41
47
  * @property {string} method HTTP method
42
48
  * @property {string} url full URL
43
49
  * @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)
50
+ * @property {Buffer} [body] buffered body
51
+ * @property {Buffer} [prefix] bytes already read from `stream`, written upstream before it
52
+ * @property {import('node:stream').Readable} [stream] live body: the request itself, or a held body's file
46
53
  * @property {import('node:http').ServerResponse} cres parked or live client response
54
+ * @property {{ gate: GateLike, action: Record<string, unknown>, aid: string }} [rec] the deciding gate, the request's action and the decision's aid: the response status is recorded there (absent: it is logged as a `resp` fact)
47
55
  */
48
56
  /**
49
57
  * Live proxy. Let `tsc` infer the shape; adopters import the name.
@@ -51,8 +59,7 @@
51
59
  * @typedef {ReturnType<typeof createProxy>} Proxy
52
60
  */
53
61
  /**
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
62
+ * Start the front-door proxy. Every request is audited with its method and URL, and a request with a body also with its byte count and SHA-256 (never headers or body text); bytes go upstream untouched. Requests the hold rule
56
63
  * flags park for a human decision instead of forwarding.
57
64
  *
58
65
  * @param {ProxyOptions} opts
@@ -80,7 +87,7 @@ export function createProxy(opts: ProxyOptions): {
80
87
  */
81
88
  export type ProxyOptions = {
82
89
  /**
83
- * JSON-lines log path (`req`, `resp`, `deny`, `hold`, `decide`, `hold-abandoned`)
90
+ * the runtime's one audit file (`runtimePaths(runtime).audit`), in bareguard's JSON-lines shape: every agent's gate and the runtime's own `sbx` gate append to it. barebox's own facts are `annotate` lines (`verdict` = `resp`, `deny`, `hold`, `decide`, `hold-abandoned`, ...); a response status is a `record` line joined to its request's `check` by `aid`.
84
91
  */
85
92
  logFile: string;
86
93
  /**
@@ -96,7 +103,33 @@ export type ProxyOptions = {
96
103
  */
97
104
  upstreamCa?: string | undefined;
98
105
  /**
99
- * hold queue config, or omit to forward everything
106
+ * builds the gate that decides one agent's requests (called once per agent, and once for the TCP door's `tcp` agent); `ctx.humanChannel` is the hold queue and `ctx.tools` the runtime's one tools table, both to hand to a bareguard `Gate`. Omit it to forward everything.
107
+ */
108
+ gateFor?: ((agent: string, ctx: {
109
+ humanChannel: (event: any) => Promise<{
110
+ decision: "allow" | "deny";
111
+ reason?: string;
112
+ }>;
113
+ tools: Record<string, any>;
114
+ }) => GateLike | Promise<GateLike>) | undefined;
115
+ /**
116
+ * the loaded label files (see `loadLabels`); omitted means no labels, only the built-in model endpoints
117
+ */
118
+ labels?: {
119
+ matcher: {
120
+ match: (method: string, url: string) => {
121
+ key: string;
122
+ source: string;
123
+ };
124
+ };
125
+ tools: Record<string, any>;
126
+ } | undefined;
127
+ /**
128
+ * exact `host:port` pairs (the `localLlm` setting) that may pass the wall's private/loopback check; any other local destination is still denied
129
+ */
130
+ allowLocal?: string[] | undefined;
131
+ /**
132
+ * hold queue config (deadline, caps, ask seam)
100
133
  */
101
134
  hold?: HoldOptions | undefined;
102
135
  /**
@@ -124,32 +157,37 @@ export type ProxyOptions = {
124
157
  */
125
158
  clientIdleMs?: number | undefined;
126
159
  /**
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)
160
+ * log redaction hook, applied after built-in secret scrubbing; its result becomes the `meta` of an `annotate` line (default: identity)
132
161
  */
133
162
  redact?: ((entry: Record<string, unknown>) => Record<string, unknown>) | undefined;
134
163
  };
135
164
  /**
136
- * Hold queue options (the decision seam lives in holds.js).
165
+ * Anything with bareguard's `check`: a `Gate`, or a plain object for tests. An
166
+ * `askHuman` outcome from a plain object goes to the same human channel.
167
+ */
168
+ export type GateLike = {
169
+ check: (action: Record<string, unknown>) => Promise<{
170
+ outcome: string;
171
+ rule?: string;
172
+ reason?: string | null;
173
+ aid?: string;
174
+ }>;
175
+ record?: (action: Record<string, unknown>, result: Record<string, unknown>, opts?: {
176
+ aid?: string;
177
+ }) => Promise<void>;
178
+ annotate?: (fact: Record<string, unknown>) => Promise<void>;
179
+ drainAnnotations?: () => unknown[];
180
+ };
181
+ /**
182
+ * Hold queue options (the queue lives in holds.js).
137
183
  */
138
184
  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
185
  /**
148
186
  * hold deadline in ms, or `'never'` (default 120000)
149
187
  */
150
188
  timeoutMs?: string | number | undefined;
151
189
  /**
152
- * bodies over this are denied at the door, never queued (default 1048576)
190
+ * bodies over this are denied at the door, never queued; held bodies spool to `<runtime>/held` (default 52428800)
153
191
  */
154
192
  maxBody?: number | undefined;
155
193
  /**
@@ -185,20 +223,31 @@ export type ForwardArgs = {
185
223
  url: string;
186
224
  headers: Record<string, string | string[] | undefined>;
187
225
  /**
188
- * buffered body (approve path)
226
+ * buffered body
189
227
  */
190
228
  body?: Buffer<ArrayBufferLike> | undefined;
191
229
  /**
192
- * live body (direct path)
230
+ * bytes already read from `stream`, written upstream before it
193
231
  */
194
- stream?: http.IncomingMessage | undefined;
232
+ prefix?: Buffer<ArrayBufferLike> | undefined;
233
+ /**
234
+ * live body: the request itself, or a held body's file
235
+ */
236
+ stream?: import("stream").Readable | undefined;
195
237
  /**
196
238
  * parked or live client response
197
239
  */
198
240
  cres: import("node:http").ServerResponse;
241
+ /**
242
+ * the deciding gate, the request's action and the decision's aid: the response status is recorded there (absent: it is logged as a `resp` fact)
243
+ */
244
+ rec?: {
245
+ gate: GateLike;
246
+ action: Record<string, unknown>;
247
+ aid: string;
248
+ } | undefined;
199
249
  };
200
250
  /**
201
251
  * Live proxy. Let `tsc` infer the shape; adopters import the name.
202
252
  */
203
253
  export type Proxy = ReturnType<typeof createProxy>;
204
- import http from 'node:http';
package/types/run.d.ts CHANGED
@@ -1,10 +1,3 @@
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
1
  /**
9
2
  * A collected ask record: the parked ask plus how it was decided.
10
3
  *
@@ -14,7 +7,7 @@ export function shouldHoldForMode(mode: string): import("./holds.js").ShouldHold
14
7
  * @property {string} method HTTP method
15
8
  * @property {string} url full URL
16
9
  * @property {string} rule hold-reason string
17
- * @property {string} letter always `'x'` until M3 owns letters
10
+ * @property {string} letter the matched rwx letter, `'x'` when the ask names none
18
11
  * @property {string} [bodyPreview] request body preview
19
12
  * @property {boolean} [truncated] body preview truncation flag
20
13
  * @property {string|null} [expiresAt] ISO-8601 UTC deadline, or null
@@ -33,6 +26,9 @@ export function shouldHoldForMode(mode: string): import("./holds.js").ShouldHold
33
26
  * @property {string} [runtime] runtime dir (created when absent: a short `sbx-*` temp dir, keeping unix socket paths under the ~108-char limit)
34
27
  * @property {string} [agent] agent name (default `'agent'`)
35
28
  * @property {number|string} [timeoutMs] hold deadline in ms, or `'never'` (default 120000)
29
+ * @property {number} [heldBodyMaxBytes] cap on a held request's body, in bytes (default 52428800); over it the request is denied
30
+ * @property {{ matcher: any, tools: Record<string, any>, dirs?: string[] }} [labels] the loaded label files (`loadLabels`); their folders are masked from the sandbox
31
+ * @property {string[]} [allowLocal] exact `host:port` pairs (for example a local model server) that may pass the wall's private/loopback check (the `localLlm` setting)
36
32
  * @property {object} [unsafeTestOnly] TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`): everything under it weakens the sandbox wall
37
33
  * @property {NodeJS.WritableStream} [stdout] where the agent's stdout goes (default process.stdout)
38
34
  * @property {NodeJS.WritableStream} [stderr] where the agent's stderr goes (default process.stderr)
@@ -51,6 +47,9 @@ export function shouldHoldForMode(mode: string): import("./holds.js").ShouldHold
51
47
  * @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
48
  * @param {string} [opts.agent] agent name (default `'agent'`)
53
49
  * @param {number|string} [opts.timeoutMs] hold deadline in ms, or `'never'` (default 120000)
50
+ * @param {number} [opts.heldBodyMaxBytes] cap on a held request's body, in bytes (default 52428800); over it the request is denied
51
+ * @param {{ matcher: any, tools: Record<string, any>, dirs?: string[] }|null} [opts.labels] the loaded label files (`loadLabels`); their folders are masked from the sandbox
52
+ * @param {string[]} [opts.allowLocal] exact `host:port` pairs (for example a local model server) that may pass the wall's private/loopback check (the `localLlm` setting)
54
53
  * @param {object|null} [opts.unsafeTestOnly] TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`)
55
54
  * @param {NodeJS.WritableStream|null} [opts.stdout] where the agent's stdout goes (default process.stdout)
56
55
  * @param {NodeJS.WritableStream|null} [opts.stderr] where the agent's stderr goes (default process.stderr)
@@ -63,7 +62,7 @@ export function shouldHoldForMode(mode: string): import("./holds.js").ShouldHold
63
62
  * import { run } from "barebox";
64
63
  * const { exit, asks, audit } = await run({ proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
65
64
  */
66
- export function run({ proj, command, mode, onAsk, onEvent, runtime, agent, timeoutMs, unsafeTestOnly, stdout, stderr, overlay, overlayIgnore }: {
65
+ export function run({ proj, command, mode, onAsk, onEvent, runtime, agent, timeoutMs, heldBodyMaxBytes, allowLocal, labels, unsafeTestOnly, stdout, stderr, overlay, overlayIgnore }: {
67
66
  proj: string;
68
67
  command: string[];
69
68
  mode?: string | undefined;
@@ -72,6 +71,13 @@ export function run({ proj, command, mode, onAsk, onEvent, runtime, agent, timeo
72
71
  runtime?: string | null | undefined;
73
72
  agent?: string | undefined;
74
73
  timeoutMs?: string | number | undefined;
74
+ heldBodyMaxBytes?: number | undefined;
75
+ labels?: {
76
+ matcher: any;
77
+ tools: Record<string, any>;
78
+ dirs?: string[];
79
+ } | null | undefined;
80
+ allowLocal?: string[] | undefined;
75
81
  unsafeTestOnly?: object | null;
76
82
  stdout?: NodeJS.WritableStream | null | undefined;
77
83
  stderr?: NodeJS.WritableStream | null | undefined;
@@ -109,7 +115,7 @@ export type AskRecord = {
109
115
  */
110
116
  rule: string;
111
117
  /**
112
- * always `'x'` until M3 owns letters
118
+ * the matched rwx letter, `'x'` when the ask names none
113
119
  */
114
120
  letter: string;
115
121
  /**
@@ -169,6 +175,22 @@ export type RunOptions = {
169
175
  * hold deadline in ms, or `'never'` (default 120000)
170
176
  */
171
177
  timeoutMs?: string | number | undefined;
178
+ /**
179
+ * cap on a held request's body, in bytes (default 52428800); over it the request is denied
180
+ */
181
+ heldBodyMaxBytes?: number | undefined;
182
+ /**
183
+ * the loaded label files (`loadLabels`); their folders are masked from the sandbox
184
+ */
185
+ labels?: {
186
+ matcher: any;
187
+ tools: Record<string, any>;
188
+ dirs?: string[];
189
+ } | undefined;
190
+ /**
191
+ * exact `host:port` pairs (for example a local model server) that may pass the wall's private/loopback check (the `localLlm` setting)
192
+ */
193
+ allowLocal?: string[] | undefined;
172
194
  /**
173
195
  * TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`): everything under it weakens the sandbox wall
174
196
  */
@@ -109,7 +109,7 @@ export function shellQuote(s: string): string;
109
109
  * Every path in a runtime dir. The one place that names them.
110
110
  *
111
111
  * @param {string} runtime runtime dir
112
- * @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
112
+ * @returns {{ socket: (agent: string) => string, asksDir: string, answersDir: string, askFile: (id: string) => string, answerFile: (id: string) => string, heldDir: string, heldFile: (id: string) => string, certsDir: string, caBundle: string, audit: string, owner: string, overlay: (agent: string) => { base: string, upper: string, work: string, manifest: string } }} layout
113
113
  */
114
114
  export function runtimePaths(runtime: string): {
115
115
  socket: (agent: string) => string;
@@ -117,6 +117,8 @@ export function runtimePaths(runtime: string): {
117
117
  answersDir: string;
118
118
  askFile: (id: string) => string;
119
119
  answerFile: (id: string) => string;
120
+ heldDir: string;
121
+ heldFile: (id: string) => string;
120
122
  certsDir: string;
121
123
  caBundle: string;
122
124
  audit: string;
package/types/wall.d.ts CHANGED
@@ -14,13 +14,15 @@ export function denyReasonForIp(rawIp: string, allowLoopback: boolean): string |
14
14
  * rebinding, with fallback when the first address refuses).
15
15
  *
16
16
  * @param {string} host CONNECT/request hostname
17
- * @param {{ allowLoopback: boolean, resolveUpstream?: (host: string) => Promise<string[] | string | null> }} gate
17
+ * @param {{ allowLoopback: boolean, allowLocal?: ReadonlySet<string>, resolveUpstream?: (host: string) => Promise<string[] | string | null> }} gate
18
+ * @param {number} port destination port (matched against `gate.allowLocal`)
18
19
  * @returns {Promise<{ ok: true, ips: string[] } | { ok: false, ip: string, reason: string }>}
19
20
  */
20
21
  export function checkUpstream(host: string, gate: {
21
22
  allowLoopback: boolean;
23
+ allowLocal?: ReadonlySet<string>;
22
24
  resolveUpstream?: (host: string) => Promise<string[] | string | null>;
23
- }): Promise<{
25
+ }, port: number): Promise<{
24
26
  ok: true;
25
27
  ips: string[];
26
28
  } | {