barebox 0.4.0 → 0.6.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 +79 -0
- package/README.md +37 -23
- package/barebox.context.md +196 -13
- package/package.json +7 -2
- package/primitives.json +10 -1
- package/src/cardscan.js +96 -0
- package/src/cli.js +29 -4
- package/src/config.js +147 -0
- package/src/holds.js +76 -48
- package/src/index.js +1 -0
- package/src/labels.js +94 -0
- package/src/launcher.js +52 -7
- package/src/policy.js +141 -0
- package/src/proxy.js +599 -161
- package/src/run.js +40 -71
- package/src/runtime.js +7 -2
- package/src/secrets.js +113 -0
- package/src/wall.js +12 -6
- package/types/cardscan.d.ts +17 -0
- package/types/cli.d.ts +1 -1
- package/types/config.d.ts +59 -0
- package/types/holds.d.ts +25 -46
- package/types/index.d.ts +1 -0
- package/types/labels.d.ts +27 -0
- package/types/launcher.d.ts +10 -2
- package/types/policy.d.ts +64 -0
- package/types/proxy.d.ts +87 -33
- package/types/run.d.ts +61 -11
- package/types/runtime.d.ts +3 -1
- package/types/secrets.d.ts +63 -0
- package/types/wall.d.ts +4 -2
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
* @when Reach for this to build the `labels` value `run()` and `createProxy()` take: it reads rwxmap label folders once at startup and returns the request matcher, the tools table (letters from each gate file) and the absolute folders.
|
|
13
|
+
* @fails Throws, naming the file or folder, on a folder with no review file, a review file without its gate file, a duplicate key across vendors, a tenants vendor no folder defines, an unreadable folder, or a label file written before rwxmap 0.8.0.
|
|
14
|
+
* @example
|
|
15
|
+
* import { loadLabels, run } from "barebox";
|
|
16
|
+
* const labels = loadLabels({ labels: ["/home/me/labels/jira"], tenants: { jira: ["https://acme.atlassian.net"] } });
|
|
17
|
+
* await run({ proj: "/tmp/proj", command: ["bash", "-c", "true"], labels });
|
|
18
|
+
*/
|
|
19
|
+
export function loadLabels({ labels, tenants }: {
|
|
20
|
+
labels?: string[] | undefined;
|
|
21
|
+
tenants?: Record<string, string[]> | undefined;
|
|
22
|
+
}, env?: NodeJS.ProcessEnv): {
|
|
23
|
+
matcher: ReturnType<typeof matcherFrom>;
|
|
24
|
+
tools: Record<string, any>;
|
|
25
|
+
dirs: string[];
|
|
26
|
+
};
|
|
27
|
+
import { matcherFrom } from 'rwxmap/match';
|
package/types/launcher.d.ts
CHANGED
|
@@ -26,9 +26,11 @@ export function buildCaBundle(runtime: string, caFile: string): string;
|
|
|
26
26
|
* @param {string} opts.proj project directory, bound read-only
|
|
27
27
|
* @param {string[]} opts.command argv to run inside, for example `["python3", "agent.py"]`
|
|
28
28
|
* @param {number} [opts.proxyPort] front-door port inside the sandbox (default 3128)
|
|
29
|
-
* @param {object} [opts.extraEnv] extra environment entries for the inside
|
|
29
|
+
* @param {object} [opts.extraEnv] extra environment entries for the inside, set last (they win)
|
|
30
|
+
* @param {{ pass?: string[], set?: Record<string, string>, placeholders?: Record<string, string> }} [opts.env] the inside's environment (M5). The host environment never reaches it except: a short default list (`TERM`, `COLORTERM`, `LANG`, `LANGUAGE`, `LC_*`, `TZ`, `USER`, `LOGNAME`, `SHELL`, `EDITOR`, `PAGER`), the host variables named in `pass`, the values in `set`, and one placeholder per secret under its own name (`placeholders`, name to value). Set in that order, then `extraEnv`.
|
|
30
31
|
* @param {string|null} [opts.socket] agent socket path, as returned by `proxy.addAgent` (default `<runtime>/socks/<agent>.sock`; the two must agree)
|
|
31
32
|
* @param {string|null} [opts.caFile] barebox CA cert path (from `proxy.caFile`); when set, the sandbox trusts it for HTTPS alongside the system roots
|
|
33
|
+
* @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
34
|
* @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
35
|
* @param {string[]} [opts.overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
34
36
|
* @returns {import('node:child_process').ChildProcess} the bwrap child (stdio piped); `close` gives its exit code
|
|
@@ -39,15 +41,21 @@ export function buildCaBundle(runtime: string, caFile: string): string;
|
|
|
39
41
|
* const child = launch({ runtime: "/tmp/sbx", agent: "fx", proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
|
|
40
42
|
* await new Promise((r) => child.on("close", r)); // child.exitCode === 0
|
|
41
43
|
*/
|
|
42
|
-
export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, socket, caFile, overlay, overlayIgnore }: {
|
|
44
|
+
export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, env, socket, caFile, overlay, overlayIgnore, maskDirs }: {
|
|
43
45
|
runtime: string;
|
|
44
46
|
agent: string;
|
|
45
47
|
proj: string;
|
|
46
48
|
command: string[];
|
|
47
49
|
proxyPort?: number | undefined;
|
|
48
50
|
extraEnv?: object;
|
|
51
|
+
env?: {
|
|
52
|
+
pass?: string[];
|
|
53
|
+
set?: Record<string, string>;
|
|
54
|
+
placeholders?: Record<string, string>;
|
|
55
|
+
} | undefined;
|
|
49
56
|
socket?: string | null | undefined;
|
|
50
57
|
caFile?: string | null | undefined;
|
|
58
|
+
maskDirs?: string[] | undefined;
|
|
51
59
|
overlay?: boolean | undefined;
|
|
52
60
|
overlayIgnore?: string[] | undefined;
|
|
53
61
|
}): import("node:child_process").ChildProcess;
|
|
@@ -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.
|
|
@@ -15,18 +18,22 @@
|
|
|
15
18
|
* so tests pass this; production never should.
|
|
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.
|
|
21
|
+
* @property {import('./secrets.js').Secret[]} [secrets] the loaded secrets (`loadSecrets`): a request carrying a secret's placeholder in a header gets the real value there only after the gate allowed it and only for a bound host; for any other host it is refused `403 denied: secret.host`. The value never reaches the audit file, an ask or an error body.
|
|
18
22
|
* @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)
|
|
23
|
+
* @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
24
|
*/
|
|
22
25
|
/**
|
|
23
|
-
*
|
|
26
|
+
* Anything with bareguard's `check`: a `Gate`, or a plain object for tests. An
|
|
27
|
+
* `askHuman` outcome from a plain object goes to the same human channel.
|
|
28
|
+
*
|
|
29
|
+
* @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
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Hold queue options (the queue lives in holds.js).
|
|
24
33
|
*
|
|
25
34
|
* @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
35
|
* @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
|
|
36
|
+
* @property {number} [maxBody] bodies over this are denied at the door, never queued; held bodies spool to `<runtime>/held` (default 52428800)
|
|
30
37
|
* @property {import('./holds.js').OnAsk|null} [onAsk] ask seam (bareguard `humanChannel` shape).
|
|
31
38
|
* When present no files are involved; omit it for the default file checkpoint
|
|
32
39
|
* (`<runtime>/asks` + `<runtime>/answers`, `sbx pending/approve/deny`).
|
|
@@ -41,9 +48,11 @@
|
|
|
41
48
|
* @property {string} method HTTP method
|
|
42
49
|
* @property {string} url full URL
|
|
43
50
|
* @property {Record<string, string | string[] | undefined>} headers
|
|
44
|
-
* @property {Buffer} [body] buffered body
|
|
45
|
-
* @property {
|
|
51
|
+
* @property {Buffer} [body] buffered body
|
|
52
|
+
* @property {Buffer} [prefix] bytes already read from `stream`, written upstream before it
|
|
53
|
+
* @property {import('node:stream').Readable} [stream] live body: the request itself, or a held body's file
|
|
46
54
|
* @property {import('node:http').ServerResponse} cres parked or live client response
|
|
55
|
+
* @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
56
|
*/
|
|
48
57
|
/**
|
|
49
58
|
* Live proxy. Let `tsc` infer the shape; adopters import the name.
|
|
@@ -51,8 +60,7 @@
|
|
|
51
60
|
* @typedef {ReturnType<typeof createProxy>} Proxy
|
|
52
61
|
*/
|
|
53
62
|
/**
|
|
54
|
-
* Start the front-door proxy. Every request is
|
|
55
|
-
* headers and body; bytes go upstream untouched. Requests the hold rule
|
|
63
|
+
* 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
64
|
* flags park for a human decision instead of forwarding.
|
|
57
65
|
*
|
|
58
66
|
* @param {ProxyOptions} opts
|
|
@@ -80,7 +88,7 @@ export function createProxy(opts: ProxyOptions): {
|
|
|
80
88
|
*/
|
|
81
89
|
export type ProxyOptions = {
|
|
82
90
|
/**
|
|
83
|
-
* JSON-lines
|
|
91
|
+
* 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
92
|
*/
|
|
85
93
|
logFile: string;
|
|
86
94
|
/**
|
|
@@ -96,7 +104,33 @@ export type ProxyOptions = {
|
|
|
96
104
|
*/
|
|
97
105
|
upstreamCa?: string | undefined;
|
|
98
106
|
/**
|
|
99
|
-
* hold queue
|
|
107
|
+
* 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.
|
|
108
|
+
*/
|
|
109
|
+
gateFor?: ((agent: string, ctx: {
|
|
110
|
+
humanChannel: (event: any) => Promise<{
|
|
111
|
+
decision: "allow" | "deny";
|
|
112
|
+
reason?: string;
|
|
113
|
+
}>;
|
|
114
|
+
tools: Record<string, any>;
|
|
115
|
+
}) => GateLike | Promise<GateLike>) | undefined;
|
|
116
|
+
/**
|
|
117
|
+
* the loaded label files (see `loadLabels`); omitted means no labels, only the built-in model endpoints
|
|
118
|
+
*/
|
|
119
|
+
labels?: {
|
|
120
|
+
matcher: {
|
|
121
|
+
match: (method: string, url: string) => {
|
|
122
|
+
key: string;
|
|
123
|
+
source: string;
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
tools: Record<string, any>;
|
|
127
|
+
} | undefined;
|
|
128
|
+
/**
|
|
129
|
+
* exact `host:port` pairs (the `localLlm` setting) that may pass the wall's private/loopback check; any other local destination is still denied
|
|
130
|
+
*/
|
|
131
|
+
allowLocal?: string[] | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* hold queue config (deadline, caps, ask seam)
|
|
100
134
|
*/
|
|
101
135
|
hold?: HoldOptions | undefined;
|
|
102
136
|
/**
|
|
@@ -120,36 +154,45 @@ export type ProxyOptions = {
|
|
|
120
154
|
resolveUpstream?: ((host: string) => Promise<string[] | string | null>) | undefined;
|
|
121
155
|
} | undefined;
|
|
122
156
|
/**
|
|
123
|
-
*
|
|
157
|
+
* the loaded secrets (`loadSecrets`): a request carrying a secret's placeholder in a header gets the real value there only after the gate allowed it and only for a bound host; for any other host it is refused `403 denied: secret.host`. The value never reaches the audit file, an ask or an error body.
|
|
124
158
|
*/
|
|
125
|
-
|
|
159
|
+
secrets?: import("./secrets.js").Secret[] | undefined;
|
|
126
160
|
/**
|
|
127
|
-
*
|
|
161
|
+
* kill a client tunnel idle this long; never fires mid-request (default 60000)
|
|
128
162
|
*/
|
|
129
|
-
|
|
163
|
+
clientIdleMs?: number | undefined;
|
|
130
164
|
/**
|
|
131
|
-
* log redaction hook, applied after built-in secret scrubbing (default: identity)
|
|
165
|
+
* log redaction hook, applied after built-in secret scrubbing; its result becomes the `meta` of an `annotate` line (default: identity)
|
|
132
166
|
*/
|
|
133
167
|
redact?: ((entry: Record<string, unknown>) => Record<string, unknown>) | undefined;
|
|
134
168
|
};
|
|
135
169
|
/**
|
|
136
|
-
*
|
|
170
|
+
* Anything with bareguard's `check`: a `Gate`, or a plain object for tests. An
|
|
171
|
+
* `askHuman` outcome from a plain object goes to the same human channel.
|
|
172
|
+
*/
|
|
173
|
+
export type GateLike = {
|
|
174
|
+
check: (action: Record<string, unknown>) => Promise<{
|
|
175
|
+
outcome: string;
|
|
176
|
+
rule?: string;
|
|
177
|
+
reason?: string | null;
|
|
178
|
+
aid?: string;
|
|
179
|
+
}>;
|
|
180
|
+
record?: (action: Record<string, unknown>, result: Record<string, unknown>, opts?: {
|
|
181
|
+
aid?: string;
|
|
182
|
+
}) => Promise<void>;
|
|
183
|
+
annotate?: (fact: Record<string, unknown>) => Promise<void>;
|
|
184
|
+
drainAnnotations?: () => unknown[];
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Hold queue options (the queue lives in holds.js).
|
|
137
188
|
*/
|
|
138
189
|
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
190
|
/**
|
|
148
191
|
* hold deadline in ms, or `'never'` (default 120000)
|
|
149
192
|
*/
|
|
150
193
|
timeoutMs?: string | number | undefined;
|
|
151
194
|
/**
|
|
152
|
-
* bodies over this are denied at the door, never queued (default
|
|
195
|
+
* bodies over this are denied at the door, never queued; held bodies spool to `<runtime>/held` (default 52428800)
|
|
153
196
|
*/
|
|
154
197
|
maxBody?: number | undefined;
|
|
155
198
|
/**
|
|
@@ -185,20 +228,31 @@ export type ForwardArgs = {
|
|
|
185
228
|
url: string;
|
|
186
229
|
headers: Record<string, string | string[] | undefined>;
|
|
187
230
|
/**
|
|
188
|
-
* buffered body
|
|
231
|
+
* buffered body
|
|
189
232
|
*/
|
|
190
233
|
body?: Buffer<ArrayBufferLike> | undefined;
|
|
191
234
|
/**
|
|
192
|
-
*
|
|
235
|
+
* bytes already read from `stream`, written upstream before it
|
|
236
|
+
*/
|
|
237
|
+
prefix?: Buffer<ArrayBufferLike> | undefined;
|
|
238
|
+
/**
|
|
239
|
+
* live body: the request itself, or a held body's file
|
|
193
240
|
*/
|
|
194
|
-
stream?:
|
|
241
|
+
stream?: import("stream").Readable | undefined;
|
|
195
242
|
/**
|
|
196
243
|
* parked or live client response
|
|
197
244
|
*/
|
|
198
245
|
cres: import("node:http").ServerResponse;
|
|
246
|
+
/**
|
|
247
|
+
* 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)
|
|
248
|
+
*/
|
|
249
|
+
rec?: {
|
|
250
|
+
gate: GateLike;
|
|
251
|
+
action: Record<string, unknown>;
|
|
252
|
+
aid: string;
|
|
253
|
+
} | undefined;
|
|
199
254
|
};
|
|
200
255
|
/**
|
|
201
256
|
* Live proxy. Let `tsc` infer the shape; adopters import the name.
|
|
202
257
|
*/
|
|
203
258
|
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,9 +26,14 @@ 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)
|
|
35
|
+
* @property {Record<string, { fromEnv?: string, value?: string, hosts: string[] }>} [secrets] keys the agent never holds (M5), the config `secrets` shape: the sandbox gets a random placeholder under each NAME; the proxy swaps in the real value on an allowed request to one of its `hosts` (see `createProxy`). A `fromEnv` variable missing from this process's environment rejects the run, naming the secret
|
|
36
|
+
* @property {{ pass?: string[], set?: Record<string, string> }} [env] the sandbox's environment (M5), the config `env` shape: the host environment never reaches it except a short default list (`TERM`, `LANG`, `LC_*`, `TZ`, ...), the host variables named in `pass` and the values in `set` (see `launch`)
|
|
39
37
|
* @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
38
|
* @property {string[]} [overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
41
39
|
*/
|
|
@@ -51,19 +49,24 @@ export function shouldHoldForMode(mode: string): import("./holds.js").ShouldHold
|
|
|
51
49
|
* @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
50
|
* @param {string} [opts.agent] agent name (default `'agent'`)
|
|
53
51
|
* @param {number|string} [opts.timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
52
|
+
* @param {number} [opts.heldBodyMaxBytes] cap on a held request's body, in bytes (default 52428800); over it the request is denied
|
|
53
|
+
* @param {{ matcher: any, tools: Record<string, any>, dirs?: string[] }|null} [opts.labels] the loaded label files (`loadLabels`); their folders are masked from the sandbox
|
|
54
|
+
* @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
55
|
* @param {object|null} [opts.unsafeTestOnly] TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`)
|
|
55
56
|
* @param {NodeJS.WritableStream|null} [opts.stdout] where the agent's stdout goes (default process.stdout)
|
|
56
57
|
* @param {NodeJS.WritableStream|null} [opts.stderr] where the agent's stderr goes (default process.stderr)
|
|
58
|
+
* @param {Record<string, { fromEnv?: string, value?: string, hosts: string[] }>|null} [opts.secrets] keys the agent never holds (M5), the config `secrets` shape; see {@link RunOptions}
|
|
59
|
+
* @param {{ pass?: string[], set?: Record<string, string> }|null} [opts.env] the sandbox's environment (M5), the config `env` shape; see {@link RunOptions}
|
|
57
60
|
* @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
61
|
* @param {string[]} [opts.overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
59
62
|
* @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
63
|
* @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.
|
|
64
|
+
* @fails Rejects on a missing/unreadable `proj`, an empty `command`, a bad `mode`, a misshapen `onAsk`, a `secrets` entry with no value (a `fromEnv` variable missing from this process's environment; the message names the secret, never a value), 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
65
|
* @example
|
|
63
66
|
* import { run } from "barebox";
|
|
64
67
|
* const { exit, asks, audit } = await run({ proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
|
|
65
68
|
*/
|
|
66
|
-
export function run({ proj, command, mode, onAsk, onEvent, runtime, agent, timeoutMs, unsafeTestOnly, stdout, stderr, overlay, overlayIgnore }: {
|
|
69
|
+
export function run({ proj, command, mode, onAsk, onEvent, runtime, agent, timeoutMs, heldBodyMaxBytes, allowLocal, labels, unsafeTestOnly, stdout, stderr, overlay, overlayIgnore, secrets, env }: {
|
|
67
70
|
proj: string;
|
|
68
71
|
command: string[];
|
|
69
72
|
mode?: string | undefined;
|
|
@@ -72,9 +75,25 @@ export function run({ proj, command, mode, onAsk, onEvent, runtime, agent, timeo
|
|
|
72
75
|
runtime?: string | null | undefined;
|
|
73
76
|
agent?: string | undefined;
|
|
74
77
|
timeoutMs?: string | number | undefined;
|
|
78
|
+
heldBodyMaxBytes?: number | undefined;
|
|
79
|
+
labels?: {
|
|
80
|
+
matcher: any;
|
|
81
|
+
tools: Record<string, any>;
|
|
82
|
+
dirs?: string[];
|
|
83
|
+
} | null | undefined;
|
|
84
|
+
allowLocal?: string[] | undefined;
|
|
75
85
|
unsafeTestOnly?: object | null;
|
|
76
86
|
stdout?: NodeJS.WritableStream | null | undefined;
|
|
77
87
|
stderr?: NodeJS.WritableStream | null | undefined;
|
|
88
|
+
secrets?: Record<string, {
|
|
89
|
+
fromEnv?: string;
|
|
90
|
+
value?: string;
|
|
91
|
+
hosts: string[];
|
|
92
|
+
}> | null | undefined;
|
|
93
|
+
env?: {
|
|
94
|
+
pass?: string[];
|
|
95
|
+
set?: Record<string, string>;
|
|
96
|
+
} | null | undefined;
|
|
78
97
|
overlay?: boolean | undefined;
|
|
79
98
|
overlayIgnore?: string[] | undefined;
|
|
80
99
|
}): Promise<{
|
|
@@ -109,7 +128,7 @@ export type AskRecord = {
|
|
|
109
128
|
*/
|
|
110
129
|
rule: string;
|
|
111
130
|
/**
|
|
112
|
-
*
|
|
131
|
+
* the matched rwx letter, `'x'` when the ask names none
|
|
113
132
|
*/
|
|
114
133
|
letter: string;
|
|
115
134
|
/**
|
|
@@ -169,6 +188,22 @@ export type RunOptions = {
|
|
|
169
188
|
* hold deadline in ms, or `'never'` (default 120000)
|
|
170
189
|
*/
|
|
171
190
|
timeoutMs?: string | number | undefined;
|
|
191
|
+
/**
|
|
192
|
+
* cap on a held request's body, in bytes (default 52428800); over it the request is denied
|
|
193
|
+
*/
|
|
194
|
+
heldBodyMaxBytes?: number | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* the loaded label files (`loadLabels`); their folders are masked from the sandbox
|
|
197
|
+
*/
|
|
198
|
+
labels?: {
|
|
199
|
+
matcher: any;
|
|
200
|
+
tools: Record<string, any>;
|
|
201
|
+
dirs?: string[];
|
|
202
|
+
} | undefined;
|
|
203
|
+
/**
|
|
204
|
+
* exact `host:port` pairs (for example a local model server) that may pass the wall's private/loopback check (the `localLlm` setting)
|
|
205
|
+
*/
|
|
206
|
+
allowLocal?: string[] | undefined;
|
|
172
207
|
/**
|
|
173
208
|
* TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`): everything under it weakens the sandbox wall
|
|
174
209
|
*/
|
|
@@ -181,6 +216,21 @@ export type RunOptions = {
|
|
|
181
216
|
* where the agent's stderr goes (default process.stderr)
|
|
182
217
|
*/
|
|
183
218
|
stderr?: NodeJS.WritableStream | undefined;
|
|
219
|
+
/**
|
|
220
|
+
* keys the agent never holds (M5), the config `secrets` shape: the sandbox gets a random placeholder under each NAME; the proxy swaps in the real value on an allowed request to one of its `hosts` (see `createProxy`). A `fromEnv` variable missing from this process's environment rejects the run, naming the secret
|
|
221
|
+
*/
|
|
222
|
+
secrets?: Record<string, {
|
|
223
|
+
fromEnv?: string;
|
|
224
|
+
value?: string;
|
|
225
|
+
hosts: string[];
|
|
226
|
+
}> | undefined;
|
|
227
|
+
/**
|
|
228
|
+
* the sandbox's environment (M5), the config `env` shape: the host environment never reaches it except a short default list (`TERM`, `LANG`, `LC_*`, `TZ`, ...), the host variables named in `pass` and the values in `set` (see `launch`)
|
|
229
|
+
*/
|
|
230
|
+
env?: {
|
|
231
|
+
pass?: string[];
|
|
232
|
+
set?: Record<string, string>;
|
|
233
|
+
} | undefined;
|
|
184
234
|
/**
|
|
185
235
|
* 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
236
|
*/
|
package/types/runtime.d.ts
CHANGED
|
@@ -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;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read the secrets from the config shape and give each a fresh placeholder.
|
|
3
|
+
*
|
|
4
|
+
* @param {Record<string, { fromEnv?: string, value?: string, hosts: string[] }>} [config] the config `secrets` object
|
|
5
|
+
* @param {NodeJS.ProcessEnv} [env] where `fromEnv` is read (default `process.env`)
|
|
6
|
+
* @returns {Secret[]} one entry per secret, new placeholders on every call (so per run)
|
|
7
|
+
* @throws {Error} naming the secret (never a value) when it has no value or no hosts
|
|
8
|
+
*/
|
|
9
|
+
export function loadSecrets(config?: Record<string, {
|
|
10
|
+
fromEnv?: string;
|
|
11
|
+
value?: string;
|
|
12
|
+
hosts: string[];
|
|
13
|
+
}>, env?: NodeJS.ProcessEnv): Secret[];
|
|
14
|
+
/**
|
|
15
|
+
* Which placeholders a request carries, and whether each is headed for a host its secret is bound to.
|
|
16
|
+
* Each header value is searched as a substring and, for `Basic <base64>`, decoded.
|
|
17
|
+
*
|
|
18
|
+
* @param {Headers} headers the request headers
|
|
19
|
+
* @param {string} host the request's hostname (no port)
|
|
20
|
+
* @param {Secret[]} secrets from {@link loadSecrets}
|
|
21
|
+
* @returns {{ name: string, header: string, bound: boolean }[]} one entry per (secret, header)
|
|
22
|
+
*/
|
|
23
|
+
export function findPlaceholders(headers: Headers, host: string, secrets: Secret[]): {
|
|
24
|
+
name: string;
|
|
25
|
+
header: string;
|
|
26
|
+
bound: boolean;
|
|
27
|
+
}[];
|
|
28
|
+
/**
|
|
29
|
+
* The headers with every bound placeholder replaced by its real value. A new
|
|
30
|
+
* object; the input (which the agent's request owns) is never changed.
|
|
31
|
+
* Placeholders of a secret not bound to `host` stay as they are.
|
|
32
|
+
*
|
|
33
|
+
* @param {Headers} headers the request headers
|
|
34
|
+
* @param {string} host the request's hostname (no port)
|
|
35
|
+
* @param {Secret[]} secrets from {@link loadSecrets}
|
|
36
|
+
* @returns {Headers} new headers
|
|
37
|
+
*/
|
|
38
|
+
export function applySecrets(headers: Headers, host: string, secrets: Secret[]): Headers;
|
|
39
|
+
/**
|
|
40
|
+
* One loaded secret.
|
|
41
|
+
*/
|
|
42
|
+
export type Secret = {
|
|
43
|
+
/**
|
|
44
|
+
* the variable name the sandbox sees the placeholder under
|
|
45
|
+
*/
|
|
46
|
+
name: string;
|
|
47
|
+
/**
|
|
48
|
+
* the real value; never leaves the proxy except in an allowed request's header
|
|
49
|
+
*/
|
|
50
|
+
value: string;
|
|
51
|
+
/**
|
|
52
|
+
* hostnames (no port) the real value may be sent to
|
|
53
|
+
*/
|
|
54
|
+
hosts: string[];
|
|
55
|
+
/**
|
|
56
|
+
* `sbx-ph-<16 hex>`, fresh per call
|
|
57
|
+
*/
|
|
58
|
+
placeholder: string;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Request headers as node reads them: lowercase names, string or string[] values.
|
|
62
|
+
*/
|
|
63
|
+
export type Headers = Record<string, string | string[] | undefined>;
|
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
|
} | {
|