barebox 0.2.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +85 -2
- package/README.md +11 -11
- package/barebox.context.md +91 -18
- package/package.json +2 -2
- package/primitives.json +40 -4
- package/src/certs.js +24 -3
- package/src/cli.js +348 -35
- package/src/holds.js +529 -58
- package/src/index.js +2 -0
- package/src/launcher.js +149 -50
- package/src/overlay-walk.js +49 -0
- package/src/overlay.js +1064 -0
- package/src/proxy.js +171 -310
- package/src/run.js +232 -0
- package/src/runtime.js +295 -0
- package/src/wall.js +217 -0
- package/types/certs.d.ts +2 -0
- package/types/holds.d.ts +168 -14
- package/types/index.d.ts +2 -0
- package/types/launcher.d.ts +13 -4
- package/types/overlay-walk.d.ts +1 -0
- package/types/overlay.d.ts +228 -0
- package/types/proxy.d.ts +43 -83
- package/types/run.d.ts +192 -0
- package/types/runtime.d.ts +131 -0
- package/types/wall.d.ts +30 -0
package/src/wall.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// wall.js — the upstream wall: the one place that decides where the proxy
|
|
2
|
+
// may dial.
|
|
3
|
+
//
|
|
4
|
+
// Default-deny for loopback, link-local (incl. cloud metadata 169.254/16),
|
|
5
|
+
// the host's own addresses, and private LAN ranges (RFC 1918, CGNAT
|
|
6
|
+
// 100.64/10, IPv6 ULA fc00::/7). An IPv4 embedded in IPv6 is checked too, in
|
|
7
|
+
// every spelling: mapped, compat, uncompressed, NAT64 (RFC 6052, RFC 8215)
|
|
8
|
+
// and 6to4 (RFC 3056). The check runs AFTER DNS resolution, so a
|
|
9
|
+
// hostname that resolves to 127.0.0.1 is denied exactly like the literal.
|
|
10
|
+
// Unresolvable names are denied too (fail closed, never 502-then-dial).
|
|
11
|
+
// Only the TEST-ONLY `unsafeTestOnly.allowLoopback` (loopback + unspecified)
|
|
12
|
+
// lets a denied destination through; only tests pass it. There is no
|
|
13
|
+
// allowPrivate flag and no per-host escape hatch: the wall is default-deny.
|
|
14
|
+
import net from 'node:net';
|
|
15
|
+
import os from 'node:os';
|
|
16
|
+
import { lookup as dnsLookup } from 'node:dns/promises';
|
|
17
|
+
|
|
18
|
+
/** Strip brackets/scope, lowercase: one spelling per address. */
|
|
19
|
+
function normIp(ip) {
|
|
20
|
+
let s = String(ip || '').toLowerCase();
|
|
21
|
+
if (s.startsWith('[') && s.endsWith(']')) s = s.slice(1, -1);
|
|
22
|
+
const pct = s.indexOf('%');
|
|
23
|
+
if (pct >= 0) s = s.slice(0, pct);
|
|
24
|
+
return s;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function v4Parts(ip) {
|
|
28
|
+
return ip.split('.').map((p) => parseInt(p, 10));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Parse IPv6 into its 8 groups, handling `::` anywhere and a trailing
|
|
33
|
+
* dotted quad (`::ffff:1.2.3.4`, `64:ff9b::1.2.3.4`).
|
|
34
|
+
*
|
|
35
|
+
* @param {string} ip normalized IP spelling
|
|
36
|
+
* @returns {number[]|null} 8 groups (0–0xffff), or null when not IPv6
|
|
37
|
+
*/
|
|
38
|
+
function v6Groups(ip) {
|
|
39
|
+
if (net.isIP(ip) !== 6) return null;
|
|
40
|
+
const dbl = ip.indexOf('::');
|
|
41
|
+
if (dbl >= 0 && ip.indexOf('::', dbl + 2) >= 0) return null;
|
|
42
|
+
const parts = (s) => s.split(':').filter((p) => p !== '');
|
|
43
|
+
const head = dbl >= 0 ? parts(ip.slice(0, dbl)) : ip.split(':');
|
|
44
|
+
const tail = dbl >= 0 ? parts(ip.slice(dbl + 2)) : [];
|
|
45
|
+
const val = (s) => {
|
|
46
|
+
if (s.includes('.')) {
|
|
47
|
+
if (net.isIP(s) !== 4) return null;
|
|
48
|
+
const p = s.split('.').map((x) => parseInt(x, 10));
|
|
49
|
+
return [(p[0] << 8) | p[1], (p[2] << 8) | p[3]];
|
|
50
|
+
}
|
|
51
|
+
if (!/^[0-9a-f]{1,4}$/.test(s)) return null;
|
|
52
|
+
return [parseInt(s, 16)];
|
|
53
|
+
};
|
|
54
|
+
/** @type {number[]} */
|
|
55
|
+
const hGroups = [];
|
|
56
|
+
/** @type {number[]} */
|
|
57
|
+
const tGroups = [];
|
|
58
|
+
for (const [list, out] of [[head, hGroups], [tail, tGroups]]) {
|
|
59
|
+
for (const s of list) {
|
|
60
|
+
const v = val(s);
|
|
61
|
+
if (!v) return null;
|
|
62
|
+
out.push(...v);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (dbl < 0) return hGroups.length === 8 ? hGroups : null;
|
|
66
|
+
if (hGroups.length + tGroups.length > 8) return null;
|
|
67
|
+
return [...hGroups, ...new Array(8 - hGroups.length - tGroups.length).fill(0), ...tGroups];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Dotted IPv4 from two groups. @param {number[]} g groups @param {number} i first group @returns {string} */
|
|
71
|
+
function v4Of(g, i) {
|
|
72
|
+
return `${(g[i] >> 8) & 255}.${g[i] & 255}.${(g[i + 1] >> 8) & 255}.${g[i + 1] & 255}`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Why a dotted IPv4 is denied, or null when it may go through. The one IPv4
|
|
77
|
+
* rule set: plain IPv4 and every embedded IPv4 use this function.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} v4 dotted IPv4
|
|
80
|
+
* @param {boolean} allowLoopback test-only opt-in for loopback + unspecified
|
|
81
|
+
* @returns {string|null} `loopback` / `unspecified` / `link-local` / `host-local` / `private`, or null
|
|
82
|
+
*/
|
|
83
|
+
function v4Reason(v4, allowLoopback) {
|
|
84
|
+
const [a, b] = v4Parts(v4);
|
|
85
|
+
if (a === 127) return allowLoopback ? null : 'loopback';
|
|
86
|
+
if (a === 0) return allowLoopback ? null : 'unspecified';
|
|
87
|
+
if (a === 169 && b === 254) return 'link-local';
|
|
88
|
+
if (a === 10) return 'private';
|
|
89
|
+
if (a === 172 && b >= 16 && b <= 31) return 'private';
|
|
90
|
+
if (a === 192 && b === 168) return 'private';
|
|
91
|
+
if (a === 100 && b >= 64 && b <= 127) return 'private';
|
|
92
|
+
if (localIps().has(v4)) return 'host-local';
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** This machine's own interface addresses: IPv6 keyed by parsed groups, IPv4 dotted (see `normIp`). */
|
|
97
|
+
function localIps() {
|
|
98
|
+
const out = new Set();
|
|
99
|
+
let ifs = {};
|
|
100
|
+
// networkInterfaces() can throw where interfaces are hidden (a sandbox is
|
|
101
|
+
// exactly where the wall matters most): lose host-local, keep the rest.
|
|
102
|
+
try {
|
|
103
|
+
ifs = os.networkInterfaces();
|
|
104
|
+
} catch {
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
for (const addrs of Object.values(ifs)) {
|
|
108
|
+
for (const a of addrs ?? []) {
|
|
109
|
+
if (!a || !a.address) continue;
|
|
110
|
+
const norm = normIp(a.address);
|
|
111
|
+
const g = v6Groups(norm);
|
|
112
|
+
out.add(g ? g.join(':') : norm);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Why `ip` (already resolved) is denied, or null when it may go through.
|
|
120
|
+
*
|
|
121
|
+
* @param {string} rawIp resolved destination IP
|
|
122
|
+
* @param {boolean} allowLoopback test-only opt-in for loopback + unspecified
|
|
123
|
+
* @returns {string|null} `loopback` / `unspecified` / `link-local` / `host-local` / `private`, or null
|
|
124
|
+
*/
|
|
125
|
+
export function denyReasonForIp(rawIp, allowLoopback) {
|
|
126
|
+
const ip = normIp(rawIp);
|
|
127
|
+
if (net.isIP(ip) === 4) return v4Reason(ip, allowLoopback);
|
|
128
|
+
const g = v6Groups(ip);
|
|
129
|
+
if (!g) return null;
|
|
130
|
+
const zero = (a, b) => g.slice(a, b).every((x) => x === 0);
|
|
131
|
+
if (zero(0, 8)) return allowLoopback ? null : 'unspecified';
|
|
132
|
+
if (zero(0, 7) && g[7] === 1) return allowLoopback ? null : 'loopback';
|
|
133
|
+
// IPv4-mapped ::ffff:0:0/96, then compat ::/96: the embedded IPv4 decides.
|
|
134
|
+
if (zero(0, 5) && g[5] === 0xffff) return v4Reason(v4Of(g, 6), allowLoopback);
|
|
135
|
+
if (zero(0, 6)) return v4Reason(v4Of(g, 6), allowLoopback);
|
|
136
|
+
// NAT64 64:ff9b::/96: the embedded IPv4 decides. Local-use 64:ff9b:1::/48
|
|
137
|
+
// is private outright: the IPv4 position depends on the prefix length,
|
|
138
|
+
// which the wall can't know.
|
|
139
|
+
if (g[0] === 0x64 && g[1] === 0xff9b && zero(2, 6)) return v4Reason(v4Of(g, 6), allowLoopback);
|
|
140
|
+
if (g[0] === 0x64 && g[1] === 0xff9b && g[2] === 1) return 'private';
|
|
141
|
+
// 6to4 2002::/16: the next 32 bits are the IPv4.
|
|
142
|
+
if (g[0] === 0x2002) return v4Reason(v4Of(g, 1), allowLoopback);
|
|
143
|
+
if ((g[0] & 0xffc0) === 0xfe80) return 'link-local';
|
|
144
|
+
if ((g[0] & 0xfe00) === 0xfc00) return 'private';
|
|
145
|
+
if (localIps().has(g.join(':'))) return 'host-local';
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Resolve `host` to every IP for the wall. Literals need no DNS.
|
|
151
|
+
* Fail closed: anything that does not resolve is denied, never dialed.
|
|
152
|
+
*
|
|
153
|
+
* @param {string} host CONNECT/request hostname
|
|
154
|
+
* @param {(host: string) => Promise<string[] | string | null>} [stub] test-only DNS stub
|
|
155
|
+
* @returns {Promise<string[] | null>} all resolved IPs, or null when unresolvable
|
|
156
|
+
*/
|
|
157
|
+
async function resolveAllUpstream(host, stub) {
|
|
158
|
+
if (net.isIP(host)) return [host];
|
|
159
|
+
try {
|
|
160
|
+
if (typeof stub === 'function') {
|
|
161
|
+
const r = await stub(host);
|
|
162
|
+
if (!r) return null;
|
|
163
|
+
if (Array.isArray(r)) return r.length ? [...r] : null;
|
|
164
|
+
if (typeof r === 'string') return [r];
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
const all = await dnsLookup(host, { all: true });
|
|
168
|
+
if (!all || !all.length) return null;
|
|
169
|
+
return all.map((a) => a.address);
|
|
170
|
+
} catch {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Order resolved addresses with IPv4 and IPv6 interleaved, keeping DNS order within each family.
|
|
177
|
+
*
|
|
178
|
+
* @param {string[]} ips resolved addresses, already checked against the wall
|
|
179
|
+
* @returns {string[]} dial order, starting with the family DNS listed first
|
|
180
|
+
*/
|
|
181
|
+
function interleaveIps(ips) {
|
|
182
|
+
const v4 = ips.filter((ip) => net.isIP(ip) !== 6);
|
|
183
|
+
const v6 = ips.filter((ip) => net.isIP(ip) === 6);
|
|
184
|
+
const [first, second] = net.isIP(ips[0]) === 6 ? [v6, v4] : [v4, v6];
|
|
185
|
+
const out = [];
|
|
186
|
+
while (first.length || second.length) {
|
|
187
|
+
if (first.length) out.push(/** @type {string} */ (first.shift()));
|
|
188
|
+
if (second.length) out.push(/** @type {string} */ (second.shift()));
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The wall: resolve every answer, then default-deny loopback/link-local/
|
|
195
|
+
* host-local/private unless the test-only opt-ins allow it. Denies when ANY
|
|
196
|
+
* answer is walled, denies unresolvable names, and pins every checked IP so
|
|
197
|
+
* the dial below tries them in order without resolving a second time (no
|
|
198
|
+
* rebinding, with fallback when the first address refuses).
|
|
199
|
+
*
|
|
200
|
+
* @param {string} host CONNECT/request hostname
|
|
201
|
+
* @param {{ allowLoopback: boolean, resolveUpstream?: (host: string) => Promise<string[] | string | null> }} gate
|
|
202
|
+
* @returns {Promise<{ ok: true, ips: string[] } | { ok: false, ip: string, reason: string }>}
|
|
203
|
+
*/
|
|
204
|
+
export async function checkUpstream(host, gate) {
|
|
205
|
+
const ips = await resolveAllUpstream(host, gate.resolveUpstream);
|
|
206
|
+
if (!ips || !ips.length) {
|
|
207
|
+
return { ok: false, ip: host, reason: `unresolvable upstream denied: ${host}` };
|
|
208
|
+
}
|
|
209
|
+
for (const ip of ips) {
|
|
210
|
+
const kind = denyReasonForIp(ip, gate.allowLoopback);
|
|
211
|
+
if (kind) {
|
|
212
|
+
const where = ip === host ? host : `${host} (${ip})`;
|
|
213
|
+
return { ok: false, ip, reason: `${kind} upstream denied: ${where}` };
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return { ok: true, ips: interleaveIps(ips) };
|
|
217
|
+
}
|
package/types/certs.d.ts
CHANGED
package/types/holds.d.ts
CHANGED
|
@@ -1,15 +1,49 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A parked request, as returned by {@link pending}. Plain objects only —
|
|
3
|
-
* the queue itself never leaves this module.
|
|
3
|
+
* the queue itself never leaves this module. No body: returning whole 1 MB
|
|
4
|
+
* bodies from a status call is never needed, and the ask payload in
|
|
5
|
+
* `asks/<id>.json` carries the preview.
|
|
4
6
|
*
|
|
5
7
|
* @typedef {object} HeldRequest
|
|
6
8
|
* @property {string} id hold id (`h<n>`)
|
|
7
9
|
* @property {string} agent agent name (its socket), never `c<n>`
|
|
8
10
|
* @property {string} method HTTP method
|
|
9
11
|
* @property {string} url full URL
|
|
10
|
-
* @property {string}
|
|
12
|
+
* @property {string} rule hold-reason string from the decision function
|
|
11
13
|
* @property {number} waitingSec seconds since the hold parked
|
|
12
14
|
*/
|
|
15
|
+
/**
|
|
16
|
+
* A queue event, as delivered to the `onEvent` callback: `{ type: 'ask' }`
|
|
17
|
+
* when a hold parks, `{ type: 'decided' }` when it settles or its client
|
|
18
|
+
* goes away (`deny` / `client gone`): every ask ends in exactly one.
|
|
19
|
+
*
|
|
20
|
+
* @typedef {{ type: 'ask', ask: AskPayload } | { type: 'decided', id: string, decision: 'allow' | 'deny', reason?: string }} HoldEvent
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* Queue event callback.
|
|
24
|
+
*
|
|
25
|
+
* @typedef {(e: HoldEvent) => void} OnEvent
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* The decision-ready ask written to `asks/<id>.json`.
|
|
29
|
+
*
|
|
30
|
+
* @typedef {object} AskPayload
|
|
31
|
+
* @property {string} id hold id (`h<n>`)
|
|
32
|
+
* @property {string} agent agent name (its socket)
|
|
33
|
+
* @property {string} method HTTP method
|
|
34
|
+
* @property {string} url full URL
|
|
35
|
+
* @property {string} rule hold-reason string from the decision function
|
|
36
|
+
* @property {string} letter always `'x'` until M3 owns letters
|
|
37
|
+
* @property {string} bodyPreview request body preview, utf8, capped at ASK_BODY_LIMIT bytes
|
|
38
|
+
* @property {boolean} truncated true when the body exceeded ASK_BODY_LIMIT
|
|
39
|
+
* @property {number} waitingSec 0 at park; readers use the file mtime for a live value
|
|
40
|
+
* @property {string|null} expiresAt ISO-8601 UTC deadline, or null when `timeoutMs: 'never'`
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Decide a parked ask. Resolves `{ decision: 'allow' | 'deny', reason? }`.
|
|
44
|
+
*
|
|
45
|
+
* @typedef {(ask: AskPayload) => Promise<{ decision: string, reason?: string }>} OnAsk
|
|
46
|
+
*/
|
|
13
47
|
/**
|
|
14
48
|
* Decide whether a request parks. Return a human-readable reason, or null
|
|
15
49
|
* to forward. M3 plugs the rwxmap + bareguard classifier into this one seam.
|
|
@@ -17,7 +51,40 @@
|
|
|
17
51
|
* @typedef {(req: { agent: string, method: string, url: string, headers: Record<string, string | string[] | undefined> }) => string | null} ShouldHold
|
|
18
52
|
*/
|
|
19
53
|
/**
|
|
20
|
-
*
|
|
54
|
+
* Atomic write: tmp file + rename, so readers never see a partial payload.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} file destination path
|
|
57
|
+
* @param {string} text content
|
|
58
|
+
* @returns {void}
|
|
59
|
+
*/
|
|
60
|
+
export function writeFileAtomic(file: string, text: string): void;
|
|
61
|
+
/**
|
|
62
|
+
* The one normalization of decisions, applied once wherever a decision
|
|
63
|
+
* enters (an answer file, or an `onAsk` result). The audit log speaks
|
|
64
|
+
* `allow`/`deny`, which is bareguard's vocabulary.
|
|
65
|
+
*
|
|
66
|
+
* @param {{ decision?: unknown, reason?: unknown } | null} input raw decision pair
|
|
67
|
+
* @param {object} [opts]
|
|
68
|
+
* @param {boolean} [opts.approveAlias] accept `'approve'` as an alias of `'allow'` (answer files only)
|
|
69
|
+
* @returns {{ decision: 'allow' | 'deny', reason?: string } | null} normalized, or null when not a decision at all
|
|
70
|
+
*/
|
|
71
|
+
export function normalizeDecision(input: {
|
|
72
|
+
decision?: unknown;
|
|
73
|
+
reason?: unknown;
|
|
74
|
+
} | null, { approveAlias }?: {
|
|
75
|
+
approveAlias?: boolean | undefined;
|
|
76
|
+
}): {
|
|
77
|
+
decision: "allow" | "deny";
|
|
78
|
+
reason?: string;
|
|
79
|
+
} | null;
|
|
80
|
+
/**
|
|
81
|
+
* Create the hold queue. One queue per runtime, enforced by `claimRuntime`
|
|
82
|
+
* in `createProxy` (a second live proxy on the same runtime never starts):
|
|
83
|
+
* on the file channel the queue owns `asks/` and `answers/` for its
|
|
84
|
+
* runtime, clearing both at start and removing its asks on close, so no
|
|
85
|
+
* answer outlives its queue. One parked request per agent: `reserve` hands
|
|
86
|
+
* out the agent's hold slot before a held body is read, and the slot passes
|
|
87
|
+
* to the next waiter when the parked entry leaves by any path.
|
|
21
88
|
*
|
|
22
89
|
* @param {object} opts
|
|
23
90
|
* @param {(o: Record<string, unknown>) => void} opts.log audit-log writer
|
|
@@ -25,34 +92,45 @@
|
|
|
25
92
|
* @param {string} [opts.match] path substring that parks a request (default impl when `shouldHold` is absent)
|
|
26
93
|
* @param {number|string} [opts.timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
27
94
|
* @param {number} [opts.maxBody] bodies over this are denied at the door, never queued (default 1048576)
|
|
28
|
-
* @param {string|null} [opts.
|
|
95
|
+
* @param {string|null} [opts.runtime] runtime dir; required for the default file checkpoint (writes `asks/`, polls `answers/`)
|
|
96
|
+
* @param {OnAsk|null} [opts.onAsk] ask seam (bareguard `humanChannel` shape); when present no files are involved
|
|
97
|
+
* @param {number} [opts.pollMs] answer poll interval in ms for the default file checkpoint (default 250, PRD Q9)
|
|
98
|
+
* @param {OnEvent|null} [opts.onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon
|
|
29
99
|
* @param {(entry: object) => void} opts.onApprove forward the parked request upstream
|
|
30
100
|
* @param {(entry: object, reason: string) => void} opts.onDeny refuse the parked request
|
|
31
|
-
* @returns {{ add: (entry: object) => string,
|
|
101
|
+
* @returns {{ add: (entry: object) => string|null, reserve: (agent: string, cbs: { onReady: () => void, onExpire: (reason: string) => void }) => { state: 'now'|'queued'|'full', arrivedAt: number, cancel: () => void }, decide: (req: { agent: string, method: string, url: string, headers: Record<string, string | string[] | undefined> }) => string | null, abandon: (id: string) => void, abandonSocket: (sock: object) => void, pending: () => HeldRequest[], close: () => void }} queue handle
|
|
32
102
|
*/
|
|
33
|
-
export function createHolds({ log, shouldHold, match, timeoutMs, maxBody,
|
|
103
|
+
export function createHolds({ log, shouldHold, match, timeoutMs, maxBody, runtime, onAsk, pollMs, onEvent, onApprove, onDeny }: {
|
|
34
104
|
log: (o: Record<string, unknown>) => void;
|
|
35
105
|
shouldHold?: ShouldHold | undefined;
|
|
36
106
|
match?: string | undefined;
|
|
37
107
|
timeoutMs?: string | number | undefined;
|
|
38
108
|
maxBody?: number | undefined;
|
|
39
|
-
|
|
109
|
+
runtime?: string | null | undefined;
|
|
110
|
+
onAsk?: OnAsk | null | undefined;
|
|
111
|
+
pollMs?: number | undefined;
|
|
112
|
+
onEvent?: OnEvent | null | undefined;
|
|
40
113
|
onApprove: (entry: object) => void;
|
|
41
114
|
onDeny: (entry: object, reason: string) => void;
|
|
42
115
|
}): {
|
|
43
|
-
add: (entry: object) => string;
|
|
116
|
+
add: (entry: object) => string | null;
|
|
117
|
+
reserve: (agent: string, cbs: {
|
|
118
|
+
onReady: () => void;
|
|
119
|
+
onExpire: (reason: string) => void;
|
|
120
|
+
}) => {
|
|
121
|
+
state: "now" | "queued" | "full";
|
|
122
|
+
arrivedAt: number;
|
|
123
|
+
cancel: () => void;
|
|
124
|
+
};
|
|
44
125
|
decide: (req: {
|
|
45
126
|
agent: string;
|
|
46
127
|
method: string;
|
|
47
128
|
url: string;
|
|
48
129
|
headers: Record<string, string | string[] | undefined>;
|
|
49
130
|
}) => string | null;
|
|
50
|
-
approve: (id: string) => string;
|
|
51
|
-
deny: (id: string, reason: string) => string;
|
|
52
131
|
abandon: (id: string) => void;
|
|
53
132
|
abandonSocket: (sock: object) => void;
|
|
54
133
|
pending: () => HeldRequest[];
|
|
55
|
-
size: () => number;
|
|
56
134
|
close: () => void;
|
|
57
135
|
};
|
|
58
136
|
/**
|
|
@@ -62,9 +140,15 @@ export function createHolds({ log, shouldHold, match, timeoutMs, maxBody, contro
|
|
|
62
140
|
* @returns {ShouldHold} decision function
|
|
63
141
|
*/
|
|
64
142
|
export function matchPath(match: string): ShouldHold;
|
|
143
|
+
/** Body preview cap for `asks/<id>.json` (bytes). */
|
|
144
|
+
export const ASK_BODY_LIMIT: 4096;
|
|
145
|
+
/** Default answer poll interval in ms (PRD Q9). */
|
|
146
|
+
export const DEFAULT_POLL_MS: 250;
|
|
65
147
|
/**
|
|
66
148
|
* A parked request, as returned by {@link pending}. Plain objects only —
|
|
67
|
-
* the queue itself never leaves this module.
|
|
149
|
+
* the queue itself never leaves this module. No body: returning whole 1 MB
|
|
150
|
+
* bodies from a status call is never needed, and the ask payload in
|
|
151
|
+
* `asks/<id>.json` carries the preview.
|
|
68
152
|
*/
|
|
69
153
|
export type HeldRequest = {
|
|
70
154
|
/**
|
|
@@ -84,14 +168,84 @@ export type HeldRequest = {
|
|
|
84
168
|
*/
|
|
85
169
|
url: string;
|
|
86
170
|
/**
|
|
87
|
-
*
|
|
171
|
+
* hold-reason string from the decision function
|
|
88
172
|
*/
|
|
89
|
-
|
|
173
|
+
rule: string;
|
|
90
174
|
/**
|
|
91
175
|
* seconds since the hold parked
|
|
92
176
|
*/
|
|
93
177
|
waitingSec: number;
|
|
94
178
|
};
|
|
179
|
+
/**
|
|
180
|
+
* A queue event, as delivered to the `onEvent` callback: `{ type: 'ask' }`
|
|
181
|
+
* when a hold parks, `{ type: 'decided' }` when it settles or its client
|
|
182
|
+
* goes away (`deny` / `client gone`): every ask ends in exactly one.
|
|
183
|
+
*/
|
|
184
|
+
export type HoldEvent = {
|
|
185
|
+
type: "ask";
|
|
186
|
+
ask: AskPayload;
|
|
187
|
+
} | {
|
|
188
|
+
type: "decided";
|
|
189
|
+
id: string;
|
|
190
|
+
decision: "allow" | "deny";
|
|
191
|
+
reason?: string;
|
|
192
|
+
};
|
|
193
|
+
/**
|
|
194
|
+
* Queue event callback.
|
|
195
|
+
*/
|
|
196
|
+
export type OnEvent = (e: HoldEvent) => void;
|
|
197
|
+
/**
|
|
198
|
+
* The decision-ready ask written to `asks/<id>.json`.
|
|
199
|
+
*/
|
|
200
|
+
export type AskPayload = {
|
|
201
|
+
/**
|
|
202
|
+
* hold id (`h<n>`)
|
|
203
|
+
*/
|
|
204
|
+
id: string;
|
|
205
|
+
/**
|
|
206
|
+
* agent name (its socket)
|
|
207
|
+
*/
|
|
208
|
+
agent: string;
|
|
209
|
+
/**
|
|
210
|
+
* HTTP method
|
|
211
|
+
*/
|
|
212
|
+
method: string;
|
|
213
|
+
/**
|
|
214
|
+
* full URL
|
|
215
|
+
*/
|
|
216
|
+
url: string;
|
|
217
|
+
/**
|
|
218
|
+
* hold-reason string from the decision function
|
|
219
|
+
*/
|
|
220
|
+
rule: string;
|
|
221
|
+
/**
|
|
222
|
+
* always `'x'` until M3 owns letters
|
|
223
|
+
*/
|
|
224
|
+
letter: string;
|
|
225
|
+
/**
|
|
226
|
+
* request body preview, utf8, capped at ASK_BODY_LIMIT bytes
|
|
227
|
+
*/
|
|
228
|
+
bodyPreview: string;
|
|
229
|
+
/**
|
|
230
|
+
* true when the body exceeded ASK_BODY_LIMIT
|
|
231
|
+
*/
|
|
232
|
+
truncated: boolean;
|
|
233
|
+
/**
|
|
234
|
+
* 0 at park; readers use the file mtime for a live value
|
|
235
|
+
*/
|
|
236
|
+
waitingSec: number;
|
|
237
|
+
/**
|
|
238
|
+
* ISO-8601 UTC deadline, or null when `timeoutMs: 'never'`
|
|
239
|
+
*/
|
|
240
|
+
expiresAt: string | null;
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* Decide a parked ask. Resolves `{ decision: 'allow' | 'deny', reason? }`.
|
|
244
|
+
*/
|
|
245
|
+
export type OnAsk = (ask: AskPayload) => Promise<{
|
|
246
|
+
decision: string;
|
|
247
|
+
reason?: string;
|
|
248
|
+
}>;
|
|
95
249
|
/**
|
|
96
250
|
* Decide whether a request parks. Return a human-readable reason, or null
|
|
97
251
|
* to forward. M3 plugs the rwxmap + bareguard classifier into this one seam.
|
package/types/index.d.ts
CHANGED
package/types/launcher.d.ts
CHANGED
|
@@ -13,7 +13,12 @@ export function buildCaBundle(runtime: string, caFile: string): string;
|
|
|
13
13
|
* Start one sandboxed agent. The agent sees a read-only `/usr` and `/etc`,
|
|
14
14
|
* its project and home read-only, the locked drawers masked, no network of
|
|
15
15
|
* its own, and a single front door: anything sent to `localhost:<proxyPort>`
|
|
16
|
-
* is carried out through this agent's socket to the proxy.
|
|
16
|
+
* is carried out through this agent's socket to the proxy. The agent starts
|
|
17
|
+
* in the project dir (`--chdir proj`), so relative paths resolve there no
|
|
18
|
+
* matter where the caller runs from. The agent always runs in a new session
|
|
19
|
+
* (`--new-session`), detached from the caller's terminal, so it cannot inject
|
|
20
|
+
* keystrokes via TIOCSTI on kernels with `dev.tty.legacy_tiocsti=1`. Its stdin
|
|
21
|
+
* is never the terminal (`stdio: ignore`), so detaching costs nothing.
|
|
17
22
|
*
|
|
18
23
|
* @param {object} opts
|
|
19
24
|
* @param {string} opts.runtime where the runtime tree lives (`socks/` is created inside)
|
|
@@ -24,15 +29,17 @@ export function buildCaBundle(runtime: string, caFile: string): string;
|
|
|
24
29
|
* @param {object} [opts.extraEnv] extra environment entries for the inside
|
|
25
30
|
* @param {string|null} [opts.socket] agent socket path, as returned by `proxy.addAgent` (default `<runtime>/socks/<agent>.sock`; the two must agree)
|
|
26
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 {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
|
+
* @param {string[]} [opts.overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
27
34
|
* @returns {import('node:child_process').ChildProcess} the bwrap child (stdio piped); `close` gives its exit code
|
|
28
|
-
* @when Reach for this to HARNESS-ONLY start one sandboxed agent run: it creates the sandbox the agent lives in. It does not decide anything; combine with the proxy for the front door and the gate (M3) for policy.
|
|
29
|
-
* @fails Throws synchronously on a missing/empty `runtime`, `agent`, `proj`, or `command`, on a bad agent name, on an over-long socket path, when `socket` disagrees with `<runtime>/socks/<agent>.sock`,
|
|
35
|
+
* @when Reach for this to HARNESS-ONLY start one sandboxed agent run: it creates the sandbox the agent lives in. It does not decide anything; combine with the proxy for the front door and the gate (M3) for policy. Pass overlay:true for a writable tracing-paper run (M4). The agent starts in the project dir.
|
|
36
|
+
* @fails Throws synchronously on a missing/empty `runtime`, `agent`, `proj`, or `command`, on a bad agent name, on a non-integer `proxyPort`, on an over-long socket path, when `socket` disagrees with `<runtime>/socks/<agent>.sock`, when `runtime` IS `HOME` or the project dir (its sockets could not be hidden from the agent), or with overlay on when the runtime sits inside the project or uncommitted changes linger. The child fails at runtime (nonzero exit) when `bwrap` or `socat` is missing inside, or when nothing listens on the agent socket.
|
|
30
37
|
* @example
|
|
31
38
|
* import { launch } from "barebox";
|
|
32
39
|
* const child = launch({ runtime: "/tmp/sbx", agent: "fx", proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
|
|
33
40
|
* await new Promise((r) => child.on("close", r)); // child.exitCode === 0
|
|
34
41
|
*/
|
|
35
|
-
export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, socket, caFile }: {
|
|
42
|
+
export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, socket, caFile, overlay, overlayIgnore }: {
|
|
36
43
|
runtime: string;
|
|
37
44
|
agent: string;
|
|
38
45
|
proj: string;
|
|
@@ -41,4 +48,6 @@ export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, soc
|
|
|
41
48
|
extraEnv?: object;
|
|
42
49
|
socket?: string | null | undefined;
|
|
43
50
|
caFile?: string | null | undefined;
|
|
51
|
+
overlay?: boolean | undefined;
|
|
52
|
+
overlayIgnore?: string[] | undefined;
|
|
44
53
|
}): import("node:child_process").ChildProcess;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|