barebox 0.3.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/CHANGELOG.md +80 -0
- package/README.md +21 -22
- package/barebox.context.md +136 -16
- package/package.json +6 -1
- package/primitives.json +1 -1
- package/src/cardscan.js +96 -0
- package/src/certs.js +22 -2
- package/src/cli.js +31 -6
- package/src/config.js +96 -0
- package/src/holds.js +237 -50
- package/src/labels.js +88 -0
- package/src/launcher.js +11 -2
- package/src/overlay.js +54 -2
- package/src/policy.js +138 -0
- package/src/proxy.js +596 -132
- package/src/run.js +40 -73
- package/src/runtime.js +40 -3
- package/src/wall.js +104 -37
- package/types/cardscan.d.ts +17 -0
- package/types/cli.d.ts +1 -1
- package/types/config.d.ts +41 -0
- package/types/holds.d.ts +37 -48
- package/types/labels.d.ts +21 -0
- package/types/launcher.d.ts +3 -1
- package/types/overlay.d.ts +8 -0
- package/types/policy.d.ts +64 -0
- package/types/proxy.d.ts +83 -34
- package/types/run.d.ts +32 -10
- package/types/runtime.d.ts +22 -1
- package/types/wall.d.ts +4 -2
package/types/holds.d.ts
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one body preview, for the ask file: the first
|
|
3
|
+
* ASK_BODY_LIMIT bytes as utf8, with card numbers masked.
|
|
4
|
+
*
|
|
5
|
+
* @param {Buffer} head the body's first bytes (anything past ASK_BODY_LIMIT is ignored)
|
|
6
|
+
* @returns {string} masked preview
|
|
7
|
+
*/
|
|
8
|
+
export function bodyPreview(head: Buffer): string;
|
|
1
9
|
/**
|
|
2
10
|
* A parked request, as returned by {@link pending}. Plain objects only —
|
|
3
|
-
* the queue itself never leaves this module. No body: returning whole
|
|
11
|
+
* the queue itself never leaves this module. No body: returning whole
|
|
4
12
|
* bodies from a status call is never needed, and the ask payload in
|
|
5
13
|
* `asks/<id>.json` carries the preview.
|
|
6
14
|
*
|
|
@@ -9,7 +17,7 @@
|
|
|
9
17
|
* @property {string} agent agent name (its socket), never `c<n>`
|
|
10
18
|
* @property {string} method HTTP method
|
|
11
19
|
* @property {string} url full URL
|
|
12
|
-
* @property {string} rule
|
|
20
|
+
* @property {string} rule the gate's ask rule (hold reason)
|
|
13
21
|
* @property {number} waitingSec seconds since the hold parked
|
|
14
22
|
*/
|
|
15
23
|
/**
|
|
@@ -32,9 +40,9 @@
|
|
|
32
40
|
* @property {string} agent agent name (its socket)
|
|
33
41
|
* @property {string} method HTTP method
|
|
34
42
|
* @property {string} url full URL
|
|
35
|
-
* @property {string} rule
|
|
36
|
-
* @property {string} letter
|
|
37
|
-
* @property {string} bodyPreview request body preview, utf8, capped at ASK_BODY_LIMIT bytes
|
|
43
|
+
* @property {string} rule the gate's ask rule
|
|
44
|
+
* @property {string} letter the matched rwx letter, `'x'` when the ask names none
|
|
45
|
+
* @property {string} bodyPreview request body preview, utf8, capped at ASK_BODY_LIMIT bytes, card numbers masked
|
|
38
46
|
* @property {boolean} truncated true when the body exceeded ASK_BODY_LIMIT
|
|
39
47
|
* @property {number} waitingSec 0 at park; readers use the file mtime for a live value
|
|
40
48
|
* @property {string|null} expiresAt ISO-8601 UTC deadline, or null when `timeoutMs: 'never'`
|
|
@@ -44,12 +52,6 @@
|
|
|
44
52
|
*
|
|
45
53
|
* @typedef {(ask: AskPayload) => Promise<{ decision: string, reason?: string }>} OnAsk
|
|
46
54
|
*/
|
|
47
|
-
/**
|
|
48
|
-
* Decide whether a request parks. Return a human-readable reason, or null
|
|
49
|
-
* to forward. M3 plugs the rwxmap + bareguard classifier into this one seam.
|
|
50
|
-
*
|
|
51
|
-
* @typedef {(req: { agent: string, method: string, url: string, headers: Record<string, string | string[] | undefined> }) => string | null} ShouldHold
|
|
52
|
-
*/
|
|
53
55
|
/**
|
|
54
56
|
* Atomic write: tmp file + rename, so readers never see a partial payload.
|
|
55
57
|
*
|
|
@@ -82,26 +84,24 @@ export function normalizeDecision(input: {
|
|
|
82
84
|
* in `createProxy` (a second live proxy on the same runtime never starts):
|
|
83
85
|
* on the file channel the queue owns `asks/` and `answers/` for its
|
|
84
86
|
* runtime, clearing both at start and removing its asks on close, so no
|
|
85
|
-
* answer outlives its queue.
|
|
87
|
+
* answer outlives its queue. One parked request per agent: `reserve` hands
|
|
88
|
+
* out the agent's hold slot before a held body is read, and the slot passes
|
|
89
|
+
* to the next waiter when the parked entry leaves by any path.
|
|
86
90
|
*
|
|
87
91
|
* @param {object} opts
|
|
88
92
|
* @param {(o: Record<string, unknown>) => void} opts.log audit-log writer
|
|
89
|
-
* @param {ShouldHold} [opts.shouldHold] decision function (default: path-substring `match`)
|
|
90
|
-
* @param {string} [opts.match] path substring that parks a request (default impl when `shouldHold` is absent)
|
|
91
93
|
* @param {number|string} [opts.timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
92
|
-
* @param {number} [opts.maxBody] bodies over this are denied at the door, never queued (default
|
|
94
|
+
* @param {number} [opts.maxBody] bodies over this are denied at the door, never queued (default 52428800)
|
|
93
95
|
* @param {string|null} [opts.runtime] runtime dir; required for the default file checkpoint (writes `asks/`, polls `answers/`)
|
|
94
96
|
* @param {OnAsk|null} [opts.onAsk] ask seam (bareguard `humanChannel` shape); when present no files are involved
|
|
95
97
|
* @param {number} [opts.pollMs] answer poll interval in ms for the default file checkpoint (default 250, PRD Q9)
|
|
96
98
|
* @param {OnEvent|null} [opts.onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon
|
|
97
99
|
* @param {(entry: object) => void} opts.onApprove forward the parked request upstream
|
|
98
100
|
* @param {(entry: object, reason: string) => void} opts.onDeny refuse the parked request
|
|
99
|
-
* @returns {{ add: (entry: object) => string,
|
|
101
|
+
* @returns {{ add: (entry: object) => string|null, dropBody: (id: string) => void, reserve: (agent: string, cbs: { onReady: () => void, onExpire: (reason: string) => void }) => { state: 'now'|'queued'|'full', id: string, arrivedAt: number, cancel: () => void }, abandon: (id: string) => void, abandonSocket: (sock: object) => void, pending: () => HeldRequest[], close: () => void }} queue handle
|
|
100
102
|
*/
|
|
101
|
-
export function createHolds({ log,
|
|
103
|
+
export function createHolds({ log, timeoutMs, maxBody, runtime, onAsk, pollMs, onEvent, onApprove, onDeny }: {
|
|
102
104
|
log: (o: Record<string, unknown>) => void;
|
|
103
|
-
shouldHold?: ShouldHold | undefined;
|
|
104
|
-
match?: string | undefined;
|
|
105
105
|
timeoutMs?: string | number | undefined;
|
|
106
106
|
maxBody?: number | undefined;
|
|
107
107
|
runtime?: string | null | undefined;
|
|
@@ -111,32 +111,31 @@ export function createHolds({ log, shouldHold, match, timeoutMs, maxBody, runtim
|
|
|
111
111
|
onApprove: (entry: object) => void;
|
|
112
112
|
onDeny: (entry: object, reason: string) => void;
|
|
113
113
|
}): {
|
|
114
|
-
add: (entry: object) => string;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
114
|
+
add: (entry: object) => string | null;
|
|
115
|
+
dropBody: (id: string) => void;
|
|
116
|
+
reserve: (agent: string, cbs: {
|
|
117
|
+
onReady: () => void;
|
|
118
|
+
onExpire: (reason: string) => void;
|
|
119
|
+
}) => {
|
|
120
|
+
state: "now" | "queued" | "full";
|
|
121
|
+
id: string;
|
|
122
|
+
arrivedAt: number;
|
|
123
|
+
cancel: () => void;
|
|
124
|
+
};
|
|
121
125
|
abandon: (id: string) => void;
|
|
122
126
|
abandonSocket: (sock: object) => void;
|
|
123
127
|
pending: () => HeldRequest[];
|
|
124
128
|
close: () => void;
|
|
125
129
|
};
|
|
126
|
-
/**
|
|
127
|
-
* Default decision: park requests whose path contains `match`.
|
|
128
|
-
*
|
|
129
|
-
* @param {string} match path substring
|
|
130
|
-
* @returns {ShouldHold} decision function
|
|
131
|
-
*/
|
|
132
|
-
export function matchPath(match: string): ShouldHold;
|
|
133
130
|
/** Body preview cap for `asks/<id>.json` (bytes). */
|
|
134
131
|
export const ASK_BODY_LIMIT: 4096;
|
|
132
|
+
/** Default cap on a held body (bytes): over it the request is denied at the door. */
|
|
133
|
+
export const DEFAULT_HOLD_MAX_BODY: 52428800;
|
|
135
134
|
/** Default answer poll interval in ms (PRD Q9). */
|
|
136
135
|
export const DEFAULT_POLL_MS: 250;
|
|
137
136
|
/**
|
|
138
137
|
* A parked request, as returned by {@link pending}. Plain objects only —
|
|
139
|
-
* the queue itself never leaves this module. No body: returning whole
|
|
138
|
+
* the queue itself never leaves this module. No body: returning whole
|
|
140
139
|
* bodies from a status call is never needed, and the ask payload in
|
|
141
140
|
* `asks/<id>.json` carries the preview.
|
|
142
141
|
*/
|
|
@@ -158,7 +157,7 @@ export type HeldRequest = {
|
|
|
158
157
|
*/
|
|
159
158
|
url: string;
|
|
160
159
|
/**
|
|
161
|
-
*
|
|
160
|
+
* the gate's ask rule (hold reason)
|
|
162
161
|
*/
|
|
163
162
|
rule: string;
|
|
164
163
|
/**
|
|
@@ -205,15 +204,15 @@ export type AskPayload = {
|
|
|
205
204
|
*/
|
|
206
205
|
url: string;
|
|
207
206
|
/**
|
|
208
|
-
*
|
|
207
|
+
* the gate's ask rule
|
|
209
208
|
*/
|
|
210
209
|
rule: string;
|
|
211
210
|
/**
|
|
212
|
-
*
|
|
211
|
+
* the matched rwx letter, `'x'` when the ask names none
|
|
213
212
|
*/
|
|
214
213
|
letter: string;
|
|
215
214
|
/**
|
|
216
|
-
* request body preview, utf8, capped at ASK_BODY_LIMIT bytes
|
|
215
|
+
* request body preview, utf8, capped at ASK_BODY_LIMIT bytes, card numbers masked
|
|
217
216
|
*/
|
|
218
217
|
bodyPreview: string;
|
|
219
218
|
/**
|
|
@@ -236,13 +235,3 @@ export type OnAsk = (ask: AskPayload) => Promise<{
|
|
|
236
235
|
decision: string;
|
|
237
236
|
reason?: string;
|
|
238
237
|
}>;
|
|
239
|
-
/**
|
|
240
|
-
* Decide whether a request parks. Return a human-readable reason, or null
|
|
241
|
-
* to forward. M3 plugs the rwxmap + bareguard classifier into this one seam.
|
|
242
|
-
*/
|
|
243
|
-
export type ShouldHold = (req: {
|
|
244
|
-
agent: string;
|
|
245
|
-
method: string;
|
|
246
|
-
url: string;
|
|
247
|
-
headers: Record<string, string | string[] | undefined>;
|
|
248
|
-
}) => string | null;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load every label folder: the matcher for request keys and the tools table
|
|
3
|
+
* (key to `{ letter, marker }`) from the gate files. Call once at startup so a
|
|
4
|
+
* bad file fails the run before any agent starts.
|
|
5
|
+
*
|
|
6
|
+
* @param {object} cfg
|
|
7
|
+
* @param {string[]} [cfg.labels] folders the rwxmap CLI wrote (`~/` means `$HOME`)
|
|
8
|
+
* @param {Record<string, string[]>} [cfg.tenants] vendor to tenant base URLs, replacing the sidecar's `servers`
|
|
9
|
+
* @param {NodeJS.ProcessEnv} [env] environment for `$HOME`
|
|
10
|
+
* @returns {{ matcher: ReturnType<typeof matcherFrom>, tools: Record<string, any>, dirs: string[] }} `dirs` are the absolute folders (the launcher masks them)
|
|
11
|
+
* @throws {Error} naming the file or folder on a missing gate or review file, a duplicate key, an unknown tenant vendor or a bad sidecar
|
|
12
|
+
*/
|
|
13
|
+
export function loadLabels({ labels, tenants }: {
|
|
14
|
+
labels?: string[] | undefined;
|
|
15
|
+
tenants?: Record<string, string[]> | undefined;
|
|
16
|
+
}, env?: NodeJS.ProcessEnv): {
|
|
17
|
+
matcher: ReturnType<typeof matcherFrom>;
|
|
18
|
+
tools: Record<string, any>;
|
|
19
|
+
dirs: string[];
|
|
20
|
+
};
|
|
21
|
+
import { matcherFrom } from 'rwxmap/match';
|
package/types/launcher.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export function buildCaBundle(runtime: string, caFile: string): string;
|
|
|
29
29
|
* @param {object} [opts.extraEnv] extra environment entries for the inside
|
|
30
30
|
* @param {string|null} [opts.socket] agent socket path, as returned by `proxy.addAgent` (default `<runtime>/socks/<agent>.sock`; the two must agree)
|
|
31
31
|
* @param {string|null} [opts.caFile] barebox CA cert path (from `proxy.caFile`); when set, the sandbox trusts it for HTTPS alongside the system roots
|
|
32
|
+
* @param {string[]} [opts.maskDirs] extra folders hidden from the agent exactly like barebox's config dir (the `labels` folders: the agent must not read or rewrite a letter)
|
|
32
33
|
* @param {boolean} [opts.overlay] when true, mount a per-run overlay upper over the project (M4 tracing paper) instead of binding it read-only; diff/commit/rollback address it as `runtimePaths(runtime).overlay(agent)`
|
|
33
34
|
* @param {string[]} [opts.overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
34
35
|
* @returns {import('node:child_process').ChildProcess} the bwrap child (stdio piped); `close` gives its exit code
|
|
@@ -39,7 +40,7 @@ export function buildCaBundle(runtime: string, caFile: string): string;
|
|
|
39
40
|
* const child = launch({ runtime: "/tmp/sbx", agent: "fx", proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
|
|
40
41
|
* await new Promise((r) => child.on("close", r)); // child.exitCode === 0
|
|
41
42
|
*/
|
|
42
|
-
export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, socket, caFile, overlay, overlayIgnore }: {
|
|
43
|
+
export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, socket, caFile, overlay, overlayIgnore, maskDirs }: {
|
|
43
44
|
runtime: string;
|
|
44
45
|
agent: string;
|
|
45
46
|
proj: string;
|
|
@@ -48,6 +49,7 @@ export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, soc
|
|
|
48
49
|
extraEnv?: object;
|
|
49
50
|
socket?: string | null | undefined;
|
|
50
51
|
caFile?: string | null | undefined;
|
|
52
|
+
maskDirs?: string[] | undefined;
|
|
51
53
|
overlay?: boolean | undefined;
|
|
52
54
|
overlayIgnore?: string[] | undefined;
|
|
53
55
|
}): import("node:child_process").ChildProcess;
|
package/types/overlay.d.ts
CHANGED
|
@@ -13,6 +13,14 @@ export function overlaySupportError(versionOutput: string): Error | null;
|
|
|
13
13
|
* @fails Throws `the overlay needs bubblewrap >= 0.11.0 (found X)` when bwrap is older or its version is unreadable. A missing bwrap does not throw here: the real bwrap call then fails as it always did.
|
|
14
14
|
*/
|
|
15
15
|
export function assertOverlaySupport(): void;
|
|
16
|
+
/**
|
|
17
|
+
* Hash a file in 1 MiB chunks, never whole: a multi-GiB file must not
|
|
18
|
+
* exhaust memory at setup. Errors name the path.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} abs absolute path to hash
|
|
21
|
+
* @returns {string} sha256 hex
|
|
22
|
+
*/
|
|
23
|
+
export function hashFile(abs: string): string;
|
|
16
24
|
/** The one walker: project, upper, merged view. Skips ignored/commit dirs (never descended); strict UTF-8 names (non-UTF-8 refused loudly); unreadable entries are refused loudly (never chmodded readable).
|
|
17
25
|
*
|
|
18
26
|
* @param {string} root directory to walk
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one tools table of a runtime: the `http.*` tools, the model endpoints
|
|
3
|
+
* (`r`) and the label tools. Model keys come from `matcher.match()` itself so
|
|
4
|
+
* there is no second key builder.
|
|
5
|
+
*
|
|
6
|
+
* @param {Record<string, any>} [labelTools] the label files' gate-file tools (`{ key: { letter, marker } }`)
|
|
7
|
+
* @param {string[]} [localLlm] `host:port` pairs of local model servers
|
|
8
|
+
* @returns {Record<string, any>} tool name to letter
|
|
9
|
+
*/
|
|
10
|
+
export function buildTools(labelTools?: Record<string, any>, localLlm?: string[]): Record<string, any>;
|
|
11
|
+
/**
|
|
12
|
+
* The tool name for one request. A spec key (a label file documents it) is
|
|
13
|
+
* used as is, even if the gate file lacks it (then the gate denies it:
|
|
14
|
+
* fail closed). A request-only key is used only when it is a known model
|
|
15
|
+
* endpoint; everything else is `http.<METHOD>`. Never the matcher's letter.
|
|
16
|
+
*
|
|
17
|
+
* @param {{ match: (method: string, url: string) => { key: string, source: string } }} matcher
|
|
18
|
+
* @param {Record<string, any>} tools the runtime's tools table
|
|
19
|
+
* @param {string} method HTTP method
|
|
20
|
+
* @param {string} url full request URL
|
|
21
|
+
* @returns {string} the tool name
|
|
22
|
+
*/
|
|
23
|
+
export function toolFor(matcher: {
|
|
24
|
+
match: (method: string, url: string) => {
|
|
25
|
+
key: string;
|
|
26
|
+
source: string;
|
|
27
|
+
};
|
|
28
|
+
}, tools: Record<string, any>, method: string, url: string): string;
|
|
29
|
+
/**
|
|
30
|
+
* True when the URL's path names a payment destination (the floor's
|
|
31
|
+
* URL-visible half). An unparsable URL is not one.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} url full request URL
|
|
34
|
+
* @returns {boolean} whether the path contains a payment fragment
|
|
35
|
+
*/
|
|
36
|
+
export function paymentPath(url: string): boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Build and initialise one agent's gate.
|
|
39
|
+
*
|
|
40
|
+
* @param {object} opts
|
|
41
|
+
* @param {string} opts.agent agent name (the gate's rwx agent)
|
|
42
|
+
* @param {string} opts.mode `ask` | `auto` | `trust`
|
|
43
|
+
* @param {string} opts.auditPath bareguard audit file (shared by every gate of a runtime)
|
|
44
|
+
* @param {Record<string, any>} [opts.tools] the runtime's tools table (default: the `http.*` tools)
|
|
45
|
+
* @param {(event: any) => Promise<{ decision: 'allow' | 'deny', reason?: string }>} opts.humanChannel resolves every ask
|
|
46
|
+
* @returns {Promise<Gate>} an initialised gate
|
|
47
|
+
*/
|
|
48
|
+
export function gateFor({ agent, mode, auditPath, humanChannel, tools }: {
|
|
49
|
+
agent: string;
|
|
50
|
+
mode: string;
|
|
51
|
+
auditPath: string;
|
|
52
|
+
tools?: Record<string, any> | undefined;
|
|
53
|
+
humanChannel: (event: any) => Promise<{
|
|
54
|
+
decision: "allow" | "deny";
|
|
55
|
+
reason?: string;
|
|
56
|
+
}>;
|
|
57
|
+
}): Promise<Gate>;
|
|
58
|
+
/** The mode names, in the order the CLI and config list them. */
|
|
59
|
+
export const MODES: string[];
|
|
60
|
+
/** The matcher used when no label files are loaded: every request is request-only. */
|
|
61
|
+
export const emptyMatcher: {
|
|
62
|
+
match: (method: string, url: string) => import("rwxmap/match").MatchResult;
|
|
63
|
+
};
|
|
64
|
+
import { Gate } from 'bareguard';
|
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
|
|
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 {
|
|
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 {
|
|
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
|
-
*
|
|
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
|
|
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
|
|
45
|
-
* @property {
|
|
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,14 +59,13 @@
|
|
|
51
59
|
* @typedef {ReturnType<typeof createProxy>} Proxy
|
|
52
60
|
*/
|
|
53
61
|
/**
|
|
54
|
-
* Start the front-door proxy. Every request is
|
|
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
|
|
59
66
|
* @returns live proxy: `ready` resolves when listeners + CA are up; the shape is inferred, see `Proxy`
|
|
60
67
|
* @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.
|
|
68
|
+
* @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.
|
|
62
69
|
* @example
|
|
63
70
|
* import { createProxy } from "barebox";
|
|
64
71
|
* const proxy = createProxy({ port: 0, runtime: "./sbx-runtime", logFile: "./mitm.log" });
|
|
@@ -80,7 +87,7 @@ export function createProxy(opts: ProxyOptions): {
|
|
|
80
87
|
*/
|
|
81
88
|
export type ProxyOptions = {
|
|
82
89
|
/**
|
|
83
|
-
* JSON-lines
|
|
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
|
|
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
|
|
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
|
-
*
|
|
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
|
|
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
|
|
226
|
+
* buffered body
|
|
189
227
|
*/
|
|
190
228
|
body?: Buffer<ArrayBufferLike> | undefined;
|
|
191
229
|
/**
|
|
192
|
-
*
|
|
230
|
+
* bytes already read from `stream`, written upstream before it
|
|
193
231
|
*/
|
|
194
|
-
|
|
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
|
|
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
|
-
*
|
|
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
|
*/
|
package/types/runtime.d.ts
CHANGED
|
@@ -40,6 +40,17 @@ export function createRuntime(): string;
|
|
|
40
40
|
* @returns {string} the new dir
|
|
41
41
|
*/
|
|
42
42
|
export function runtimeTempDir(scratch: string, prefix: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Make a runtime dir owner-only whatever the umask: `mode` is masked by
|
|
45
|
+
* the umask and doesn't apply to an existing dir, hence the `chmod`. An
|
|
46
|
+
* existing dir you own is tightened to 0700; one you don't own (`/tmp`,
|
|
47
|
+
* root-owned, shared) is refused, since it can't be made private.
|
|
48
|
+
*
|
|
49
|
+
* @param {string} dir dir to create or fix up
|
|
50
|
+
* @returns {void}
|
|
51
|
+
* @throws {Error} when the dir exists and is owned by another uid
|
|
52
|
+
*/
|
|
53
|
+
export function ensurePrivateDir(dir: string): void;
|
|
43
54
|
/**
|
|
44
55
|
* The abstract-socket name of a runtime's claim lock. Hashes the real path,
|
|
45
56
|
* device and inode, so a symlinked path finds the same lock and a recreated
|
|
@@ -86,11 +97,19 @@ export function claimRuntime(runtime: string): () => void;
|
|
|
86
97
|
* @returns {string} printable text with no VT sequences, C0/C1 controls, or bidi controls
|
|
87
98
|
*/
|
|
88
99
|
export function safe(s: unknown): string;
|
|
100
|
+
/**
|
|
101
|
+
* Quote a path for pasting into a shell: safe spellings come back
|
|
102
|
+
* unchanged, anything else is single-quoted with `'` escaped.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} s path to quote
|
|
105
|
+
* @returns {string} shell-safe spelling
|
|
106
|
+
*/
|
|
107
|
+
export function shellQuote(s: string): string;
|
|
89
108
|
/**
|
|
90
109
|
* Every path in a runtime dir. The one place that names them.
|
|
91
110
|
*
|
|
92
111
|
* @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
|
|
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
|
|
94
113
|
*/
|
|
95
114
|
export function runtimePaths(runtime: string): {
|
|
96
115
|
socket: (agent: string) => string;
|
|
@@ -98,6 +117,8 @@ export function runtimePaths(runtime: string): {
|
|
|
98
117
|
answersDir: string;
|
|
99
118
|
askFile: (id: string) => string;
|
|
100
119
|
answerFile: (id: string) => string;
|
|
120
|
+
heldDir: string;
|
|
121
|
+
heldFile: (id: string) => string;
|
|
101
122
|
certsDir: string;
|
|
102
123
|
caBundle: string;
|
|
103
124
|
audit: string;
|