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
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Judge `bwrap --version` output for overlay support: `--overlay-src` / `--overlay` exist from bubblewrap 0.11.0.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} versionOutput text printed by `bwrap --version` (for example `bubblewrap 0.12.0`)
|
|
5
|
+
* @returns {Error|null} null when the version is at least 0.11.0, otherwise the refusal to throw (unparseable output counts as `unknown`)
|
|
6
|
+
* @fails Never throws; returns the error instead.
|
|
7
|
+
*/
|
|
8
|
+
export function overlaySupportError(versionOutput: string): Error | null;
|
|
9
|
+
/**
|
|
10
|
+
* Refuse the overlay on a bubblewrap older than 0.11.0. Runs `bwrap --version` once per process.
|
|
11
|
+
*
|
|
12
|
+
* @returns {void}
|
|
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
|
+
*/
|
|
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;
|
|
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).
|
|
25
|
+
*
|
|
26
|
+
* @param {string} root directory to walk
|
|
27
|
+
* @param {Set<string>} ignore path segments invisible to the layer
|
|
28
|
+
* @param {Set<string>|null} [hashOnly] when set, only these relative paths get full entries (hashes, link targets); every other path is still `lstat`ed but records type + mode only
|
|
29
|
+
* @returns {Map<string, {type: string, mode: number, hash?: string, target?: string}>}
|
|
30
|
+
*/
|
|
31
|
+
export function walkLocal(root: string, ignore: Set<string>, hashOnly?: Set<string> | null): Map<string, {
|
|
32
|
+
type: string;
|
|
33
|
+
mode: number;
|
|
34
|
+
hash?: string;
|
|
35
|
+
target?: string;
|
|
36
|
+
}>;
|
|
37
|
+
/** Prepare a fresh upper: refuse leftovers, snapshot the project as the conflict baseline. An empty upper re-snapshots. */
|
|
38
|
+
export function setupOverlay({ runtime, agent, proj, ignore }: {
|
|
39
|
+
runtime: any;
|
|
40
|
+
agent: any;
|
|
41
|
+
proj: any;
|
|
42
|
+
ignore?: string[] | undefined;
|
|
43
|
+
}): {
|
|
44
|
+
upper: string;
|
|
45
|
+
work: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Review one run's tracing paper: every file the sandbox added, modified or deleted, each flagging a project change made after the run started.
|
|
49
|
+
*
|
|
50
|
+
* @param {object} opts
|
|
51
|
+
* @param {string} opts.runtime runtime dir holding the run's overlay (`<runtime>/overlay/<agent>`)
|
|
52
|
+
* @param {string} opts.agent agent name whose upper to read
|
|
53
|
+
* @returns {OverlayChange[]} one entry per added/modified/deleted path, each flagging a mid-run project change
|
|
54
|
+
* @when Reach for this to review one run's tracing paper: it lists every file the sandboxed run added, modified or deleted, flagging the ones the project changed meanwhile.
|
|
55
|
+
* @fails Throws when no overlay exists for the agent, when the project is gone, when a leftover commit folder blocks, when bubblewrap is older than 0.11.0, or when the overlay view cannot be mounted; never writes anything.
|
|
56
|
+
* @example
|
|
57
|
+
* import { diffOverlay } from "barebox";
|
|
58
|
+
* const changes = diffOverlay({ runtime: "/tmp/sbx-abc", agent: "agent" });
|
|
59
|
+
* for (const c of changes) console.log(c.status, c.path, c.conflict ? "(CONFLICT)" : "");
|
|
60
|
+
*/
|
|
61
|
+
export function diffOverlay({ runtime, agent }: {
|
|
62
|
+
runtime: string;
|
|
63
|
+
agent: string;
|
|
64
|
+
}): OverlayChange[];
|
|
65
|
+
/** Fingerprint one project path now, or null when absent. ENOTDIR also reads as absent: a non-dir prefix (a file becoming a folder) cannot hold the path. */
|
|
66
|
+
export function fpNow(abs: any): string | null;
|
|
67
|
+
/**
|
|
68
|
+
* First path in `paths` whose live fingerprint differs from the snapshot
|
|
69
|
+
* (absent reads as null both sides), or null when nothing moved. A project
|
|
70
|
+
* edit landing between the diff and the swap would otherwise be silently
|
|
71
|
+
* overwritten, so `commitOverlay` refuses when this returns a path.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} proj project directory
|
|
74
|
+
* @param {string[]} paths project-relative paths about to change
|
|
75
|
+
* @param {Map<string, string|null>} lowerFps snapshot fingerprints from the diff
|
|
76
|
+
* @returns {string|null} the first changed path, or null
|
|
77
|
+
*/
|
|
78
|
+
export function changedSince(proj: string, paths: string[], lowerFps: Map<string, string | null>): string | null;
|
|
79
|
+
/** One journal entry (the single shape): a phase-2 step actually taken; undo runs these in reverse.
|
|
80
|
+
*
|
|
81
|
+
* @typedef {object} JournalEntry
|
|
82
|
+
* @property {string} op `aside` (original to `slot`) | `in` (staged content to dest) | `rmdir` | `mkdir` | `chmod` (restore `mode`)
|
|
83
|
+
* @property {string} path project-relative path the step touched
|
|
84
|
+
* @property {string} [slot] `aside`: backup path under `old/`
|
|
85
|
+
* @property {number} [mode] `rmdir`/`chmod`: the original mode to restore
|
|
86
|
+
*/
|
|
87
|
+
/** Undo a journal in reverse, best effort: a failed step is recorded and the rest still runs. Returns exactly the steps left un-undone.
|
|
88
|
+
*
|
|
89
|
+
* @param {JournalEntry[]} journal steps taken, in order
|
|
90
|
+
* @param {string} proj project directory the steps were applied to
|
|
91
|
+
* @returns {string[]} `"<op> <path>"` for every step left un-undone, in undo order
|
|
92
|
+
*/
|
|
93
|
+
export function undoJournal(journal: JournalEntry[], proj: string): string[];
|
|
94
|
+
/**
|
|
95
|
+
* Press one run's tracing paper down: stage the merged view, then swap it into the project by rename. Applies everything or undoes it: a full undo reports "nothing was applied"; a partial one lists exactly the steps not undone, keeps the commit folder (originals in `old/`), and names its path.
|
|
96
|
+
*
|
|
97
|
+
* @param {object} opts
|
|
98
|
+
* @param {string} opts.runtime runtime dir holding the run's overlay (`<runtime>/overlay/<agent>`)
|
|
99
|
+
* @param {string} opts.agent agent name whose upper to commit
|
|
100
|
+
* @param {string} [opts.onConflict] `refuse` (default) throws before writing anything; `skip` commits the rest and reports the skipped paths
|
|
101
|
+
* @returns {{committed: string[], skipped: {path: string, reason: string}[]}} committed project-relative paths, plus conflicts left uncommitted under `skip`
|
|
102
|
+
* @when Reach for this to press one run's tracing paper down: it stages the sandbox's changes and swaps them into the project by rename, refusing loudly on mid-run conflicts.
|
|
103
|
+
* @fails Throws (writing nothing) when any changed file was also changed in the project mid-run, unless onConflict is `skip`; throws on unsupported file kinds, on a project that moved mid-commit, and when bubblewrap is older than 0.11.0. A phase-2 failure undoes every journaled step: a full undo reports `nothing was applied`, a partial one throws `SBX_COMMIT_FAILED` listing exactly the steps not undone and keeping the commit folder (originals in `old/`) named in the error. Setuid/setgid bits are stripped from committed modes; mtimes are not preserved. A killed commit leaves a folder that blocks the next diff/commit/setup until removed.
|
|
104
|
+
* @example
|
|
105
|
+
* import { commitOverlay } from "barebox";
|
|
106
|
+
* const { committed, skipped } = commitOverlay({ runtime: "/tmp/sbx-abc", agent: "agent" });
|
|
107
|
+
* console.log(`committed ${committed.length} file(s), skipped ${skipped.length}`);
|
|
108
|
+
*/
|
|
109
|
+
export function commitOverlay({ runtime, agent, onConflict }: {
|
|
110
|
+
runtime: string;
|
|
111
|
+
agent: string;
|
|
112
|
+
onConflict?: string | undefined;
|
|
113
|
+
}): {
|
|
114
|
+
committed: string[];
|
|
115
|
+
skipped: {
|
|
116
|
+
path: string;
|
|
117
|
+
reason: string;
|
|
118
|
+
}[];
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Decide what survives a failed commit: the staging folder is deleted only
|
|
122
|
+
* when the undo left nothing behind (`notUndone` empty — nothing was
|
|
123
|
+
* applied); otherwise it is kept, because `old/` holds the only copy of
|
|
124
|
+
* the originals, and the error names the folder. Returns the error to throw.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} commitDir the run's staging folder in the project
|
|
127
|
+
* @param {unknown} failure whatever the commit threw (phase-1 refusals carry no `notUndone`)
|
|
128
|
+
* @returns {unknown} the failure, to throw
|
|
129
|
+
*/
|
|
130
|
+
export function finishFailedCommit(commitDir: string, failure: unknown): unknown;
|
|
131
|
+
/**
|
|
132
|
+
* Throw one run's tracing paper away: drop every sandboxed change and leave the project exactly as it was. Reports exactly what `diffOverlay` returns, taken before removal, so rollback and diff can never disagree; when the project is gone (or the view cannot be mounted) the report is the raw relative paths found in the upper, with no added/modified/deleted inference. Then the overlay dir is removed completely, so the next run starts fresh. Never touches the project.
|
|
133
|
+
*
|
|
134
|
+
* @param {object} opts
|
|
135
|
+
* @param {string} opts.runtime runtime dir holding the run's overlay (`<runtime>/overlay/<agent>`)
|
|
136
|
+
* @param {string} opts.agent agent name whose upper to discard
|
|
137
|
+
* @returns {{discarded: OverlayChange[], exact: boolean, why: string|null}} the changes that were dropped; `exact` is false for the raw fallback, and then `why` names the actual reason (diff's error, or `project <proj> is gone`, plus the raw-listing reason when that failed too). `why` is null when exact.
|
|
138
|
+
* @when Reach for this to throw one run's tracing paper away: it drops every sandboxed change and leaves the project exactly as it was.
|
|
139
|
+
* @fails Throws when no overlay exists for the agent or its record is corrupt, or when the overlay folder cannot be removed even after a permission repair (the error names the folder). Listing the changes never throws and never invents entries: an unlistable upper reports `discarded: []` with the reasons in `why`. Removal depends on neither listing nor the first permission pass: a partial failure retries removal once after a fresh permission pass. Dropping the upper never touches the project.
|
|
140
|
+
* @example
|
|
141
|
+
* import { rollbackOverlay } from "barebox";
|
|
142
|
+
* const { discarded } = rollbackOverlay({ runtime: "/tmp/sbx-abc", agent: "agent" });
|
|
143
|
+
* console.log(`discarded ${discarded.length} change(s)`);
|
|
144
|
+
*/
|
|
145
|
+
export function rollbackOverlay({ runtime, agent }: {
|
|
146
|
+
runtime: string;
|
|
147
|
+
agent: string;
|
|
148
|
+
}): {
|
|
149
|
+
discarded: OverlayChange[];
|
|
150
|
+
exact: boolean;
|
|
151
|
+
why: string | null;
|
|
152
|
+
};
|
|
153
|
+
/** Path segments invisible to the layer (Q3): never snapshotted, diffed, committed or deleted. */
|
|
154
|
+
export const DEFAULT_IGNORE: string[];
|
|
155
|
+
/** Refused commit: paths changed in the project mid-run. A conflict refusal is a phase-1 failure: nothing was applied — resolve and retry. */
|
|
156
|
+
export class OverlayConflictError extends Error {
|
|
157
|
+
/** @param {{path: string, status: string, reason: string|null}[]} conflicts conflicting changes */
|
|
158
|
+
constructor(conflicts: {
|
|
159
|
+
path: string;
|
|
160
|
+
status: string;
|
|
161
|
+
reason: string | null;
|
|
162
|
+
}[]);
|
|
163
|
+
code: string;
|
|
164
|
+
/** @type {{path: string, status: string, reason: string|null}[]} */
|
|
165
|
+
conflicts: {
|
|
166
|
+
path: string;
|
|
167
|
+
status: string;
|
|
168
|
+
reason: string | null;
|
|
169
|
+
}[];
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* One journal entry (the single shape): a phase-2 step actually taken; undo runs these in reverse.
|
|
173
|
+
*/
|
|
174
|
+
export type JournalEntry = {
|
|
175
|
+
/**
|
|
176
|
+
* `aside` (original to `slot`) | `in` (staged content to dest) | `rmdir` | `mkdir` | `chmod` (restore `mode`)
|
|
177
|
+
*/
|
|
178
|
+
op: string;
|
|
179
|
+
/**
|
|
180
|
+
* project-relative path the step touched
|
|
181
|
+
*/
|
|
182
|
+
path: string;
|
|
183
|
+
/**
|
|
184
|
+
* `aside`: backup path under `old/`
|
|
185
|
+
*/
|
|
186
|
+
slot?: string | undefined;
|
|
187
|
+
/**
|
|
188
|
+
* `rmdir`/`chmod`: the original mode to restore
|
|
189
|
+
*/
|
|
190
|
+
mode?: number | undefined;
|
|
191
|
+
};
|
|
192
|
+
/**
|
|
193
|
+
* One reviewed file change: what the sandbox did at `path`, and whether the project changed underneath it mid-run.
|
|
194
|
+
*/
|
|
195
|
+
export type OverlayChange = {
|
|
196
|
+
/**
|
|
197
|
+
* project-relative path with `/` separators
|
|
198
|
+
*/
|
|
199
|
+
path: string;
|
|
200
|
+
/**
|
|
201
|
+
* `added` | `modified` | `deleted` (`unknown`: rollback's raw fallback, which infers nothing)
|
|
202
|
+
*/
|
|
203
|
+
status: string;
|
|
204
|
+
/**
|
|
205
|
+
* true when the project changed this path after the run started
|
|
206
|
+
*/
|
|
207
|
+
conflict: boolean;
|
|
208
|
+
/**
|
|
209
|
+
* why `conflict` is set, or null when clean
|
|
210
|
+
*/
|
|
211
|
+
reason: string | null;
|
|
212
|
+
/**
|
|
213
|
+
* merged-view kind: `f` file, `d` dir, `l` symlink (set unless deleted)
|
|
214
|
+
*/
|
|
215
|
+
type?: string | undefined;
|
|
216
|
+
/**
|
|
217
|
+
* merged-view permission bits (set unless deleted)
|
|
218
|
+
*/
|
|
219
|
+
mode?: number | undefined;
|
|
220
|
+
/**
|
|
221
|
+
* sha256 of a merged-view regular file
|
|
222
|
+
*/
|
|
223
|
+
hash?: string | undefined;
|
|
224
|
+
/**
|
|
225
|
+
* symlink target of a merged-view symlink
|
|
226
|
+
*/
|
|
227
|
+
target?: string | undefined;
|
|
228
|
+
};
|
package/types/proxy.d.ts
CHANGED
|
@@ -1,62 +1,19 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Why `ip` (already resolved) is denied, or null when it may go through.
|
|
3
|
-
*
|
|
4
|
-
* @param {string} rawIp resolved destination IP
|
|
5
|
-
* @param {boolean} allowLoopback test-only opt-in for loopback + unspecified
|
|
6
|
-
* @returns {string|null} `loopback` / `unspecified` / `link-local` / `host-local` / `private`, or null
|
|
7
|
-
*/
|
|
8
|
-
export function denyReasonForIp(rawIp: string, allowLoopback: boolean): string | null;
|
|
9
|
-
/**
|
|
10
|
-
* Check an agent name before it reaches the filesystem.
|
|
11
|
-
*
|
|
12
|
-
* @param {string} name agent name
|
|
13
|
-
* @returns {void} throws on `../` and friends
|
|
14
|
-
*/
|
|
15
|
-
export function assertAgentName(name: string): void;
|
|
16
|
-
/**
|
|
17
|
-
* Resolve symlinks through the nearest existing ancestor: socket paths may
|
|
18
|
-
* not exist yet, so `realpathSync` on the full path would throw. Walks up
|
|
19
|
-
* to the first existing ancestor, resolves that, then re-attaches the rest.
|
|
20
|
-
*
|
|
21
|
-
* @param {string} p any path, existing or not
|
|
22
|
-
* @returns {string} the real (symlink-free) spelling of `p`
|
|
23
|
-
*/
|
|
24
|
-
export function realPathOf(p: string): string;
|
|
25
|
-
/**
|
|
26
|
-
* The one path both sides agree on: the proxy listens here, the launcher
|
|
27
|
-
* binds it into the sandbox. This function is the single writer of the shape.
|
|
28
|
-
*
|
|
29
|
-
* @param {string} runtime runtime dir
|
|
30
|
-
* @param {string} name agent name
|
|
31
|
-
* @returns {string} `<runtime>/socks/<name>.sock`
|
|
32
|
-
*/
|
|
33
|
-
export function socketPathForAgent(runtime: string, name: string): string;
|
|
34
1
|
/**
|
|
35
2
|
* Options for {@link createProxy}.
|
|
36
3
|
*
|
|
37
4
|
* @typedef {object} ProxyOptions
|
|
38
5
|
* @property {string} logFile JSON-lines log path (`req`, `resp`, `deny`, `hold`, `decide`, `hold-abandoned`)
|
|
39
|
-
* @property {string}
|
|
40
|
-
* Must live inside `runtime` (compared via {@link realPathOf}, so symlinks
|
|
41
|
-
* cannot smuggle it out): anything else throws before any side effect.
|
|
42
|
-
* The CA private key lives here, and the sandbox bind-mounts only the
|
|
43
|
-
* runtime dir — an outside certDir would expose `ca.key` through the binds.
|
|
44
|
-
* @property {string} [runtime] runtime dir; required for `addAgent`, default parent of `certDir`
|
|
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.
|
|
45
7
|
* @property {number} [port] opt-in TCP listener for tests, bound to 127.0.0.1 only (default: none)
|
|
46
8
|
* @property {string} [upstreamCa] extra PEM the proxy trusts for upstream TLS, on top of system roots
|
|
47
9
|
* @property {HoldOptions} [hold] hold queue config, or omit to forward everything
|
|
48
|
-
* @property {string|null} [controlSocket] unix socket path for `pending | approve | deny`.
|
|
49
|
-
* Must live inside `runtime` (compared via {@link realPathOf}, so symlinks
|
|
50
|
-
* cannot smuggle it out): anything else throws before any side effect.
|
|
51
|
-
* No default — omit it when no CLI decisions are needed.
|
|
52
10
|
* @property {number} [upstreamTimeoutMs] give up on an upstream response after this (default 120000)
|
|
53
|
-
* @property {
|
|
11
|
+
* @property {object} [unsafeTestOnly] TEST-ONLY escape hatch: everything under
|
|
12
|
+
* it weakens the sandbox wall, so production omits it and tests name it loudly.
|
|
13
|
+
* @property {boolean} [unsafeTestOnly.allowLoopback] let loopback and unspecified
|
|
54
14
|
* destinations through (default false). Fixture servers listen on 127.0.0.1,
|
|
55
15
|
* so tests pass this; production never should.
|
|
56
|
-
* @property {(host: string
|
|
57
|
-
* consulted after DNS with each resolved IP: return true to let through a
|
|
58
|
-
* destination the default wall would deny. Only tests use this.
|
|
59
|
-
* @property {(host: string) => Promise<string[] | string | null>} [resolveUpstream] TEST-ONLY
|
|
16
|
+
* @property {(host: string) => Promise<string[] | string | null>} [unsafeTestOnly.resolveUpstream]
|
|
60
17
|
* DNS stub: replaces the wall's resolver so rebinding is testable. Production omits it.
|
|
61
18
|
* @property {number} [clientIdleMs] kill a client tunnel idle this long; never fires mid-request (default 60000)
|
|
62
19
|
* @property {number} [maxLogBody] log at most this many request-body bytes, then `truncated: true` (default 1048576)
|
|
@@ -70,6 +27,11 @@ export function socketPathForAgent(runtime: string, name: string): string;
|
|
|
70
27
|
* @property {import('./holds.js').ShouldHold} [shouldHold] decision function; overrides `match`
|
|
71
28
|
* @property {number|string} [timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
72
29
|
* @property {number} [maxBody] bodies over this are denied at the door, never queued (default 1048576)
|
|
30
|
+
* @property {import('./holds.js').OnAsk|null} [onAsk] ask seam (bareguard `humanChannel` shape).
|
|
31
|
+
* When present no files are involved; omit it for the default file checkpoint
|
|
32
|
+
* (`<runtime>/asks` + `<runtime>/answers`, `sbx pending/approve/deny`).
|
|
33
|
+
* @property {number} [pollMs] answer poll interval in ms for the file checkpoint (default 250)
|
|
34
|
+
* @property {import('./holds.js').OnEvent} [onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle
|
|
73
35
|
*/
|
|
74
36
|
/**
|
|
75
37
|
* One upstream forwarding: buffered body (approve path) or live stream.
|
|
@@ -96,10 +58,10 @@ export function socketPathForAgent(runtime: string, name: string): string;
|
|
|
96
58
|
* @param {ProxyOptions} opts
|
|
97
59
|
* @returns live proxy: `ready` resolves when listeners + CA are up; the shape is inferred, see `Proxy`
|
|
98
60
|
* @when Reach for this HARNESS-ONLY to run the front door: it terminates agent TLS, sees every request, and parks the ones a human must decide. It decides nothing itself; pair with the launcher for sandboxes and `sbx` for decisions.
|
|
99
|
-
* @fails Throws synchronously on a missing `logFile`
|
|
61
|
+
* @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.
|
|
100
62
|
* @example
|
|
101
63
|
* import { createProxy } from "barebox";
|
|
102
|
-
* const proxy = createProxy({ port: 0,
|
|
64
|
+
* const proxy = createProxy({ port: 0, runtime: "./sbx-runtime", logFile: "./mitm.log" });
|
|
103
65
|
* const { port } = await proxy.ready;
|
|
104
66
|
* await proxy.close();
|
|
105
67
|
*/
|
|
@@ -122,17 +84,9 @@ export type ProxyOptions = {
|
|
|
122
84
|
*/
|
|
123
85
|
logFile: string;
|
|
124
86
|
/**
|
|
125
|
-
* dir
|
|
126
|
-
* Must live inside `runtime` (compared via {@link realPathOf}, so symlinks
|
|
127
|
-
* cannot smuggle it out): anything else throws before any side effect.
|
|
128
|
-
* The CA private key lives here, and the sandbox bind-mounts only the
|
|
129
|
-
* runtime dir — an outside certDir would expose `ca.key` through the binds.
|
|
130
|
-
*/
|
|
131
|
-
certDir?: string | undefined;
|
|
132
|
-
/**
|
|
133
|
-
* runtime dir; required for `addAgent`, default parent of `certDir`
|
|
87
|
+
* runtime dir; required. Holds the CA + minted host certs (`certs/`, see `runtime.js`), the agent sockets, and the hold queue files.
|
|
134
88
|
*/
|
|
135
|
-
runtime
|
|
89
|
+
runtime: string;
|
|
136
90
|
/**
|
|
137
91
|
* opt-in TCP listener for tests, bound to 127.0.0.1 only (default: none)
|
|
138
92
|
*/
|
|
@@ -145,34 +99,26 @@ export type ProxyOptions = {
|
|
|
145
99
|
* hold queue config, or omit to forward everything
|
|
146
100
|
*/
|
|
147
101
|
hold?: HoldOptions | undefined;
|
|
148
|
-
/**
|
|
149
|
-
* unix socket path for `pending | approve | deny`.
|
|
150
|
-
* Must live inside `runtime` (compared via {@link realPathOf}, so symlinks
|
|
151
|
-
* cannot smuggle it out): anything else throws before any side effect.
|
|
152
|
-
* No default — omit it when no CLI decisions are needed.
|
|
153
|
-
*/
|
|
154
|
-
controlSocket?: string | null | undefined;
|
|
155
102
|
/**
|
|
156
103
|
* give up on an upstream response after this (default 120000)
|
|
157
104
|
*/
|
|
158
105
|
upstreamTimeoutMs?: number | undefined;
|
|
159
106
|
/**
|
|
160
|
-
* TEST-ONLY
|
|
161
|
-
*
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
resolveUpstream?: ((host: string) => Promise<string[] | string | null>) | undefined;
|
|
107
|
+
* TEST-ONLY escape hatch: everything under
|
|
108
|
+
* it weakens the sandbox wall, so production omits it and tests name it loudly.
|
|
109
|
+
*/
|
|
110
|
+
unsafeTestOnly?: {
|
|
111
|
+
/**
|
|
112
|
+
* let loopback and unspecified
|
|
113
|
+
* destinations through (default false). Fixture servers listen on 127.0.0.1,
|
|
114
|
+
* so tests pass this; production never should.
|
|
115
|
+
*/
|
|
116
|
+
allowLoopback?: boolean | undefined;
|
|
117
|
+
/**
|
|
118
|
+
* DNS stub: replaces the wall's resolver so rebinding is testable. Production omits it.
|
|
119
|
+
*/
|
|
120
|
+
resolveUpstream?: ((host: string) => Promise<string[] | string | null>) | undefined;
|
|
121
|
+
} | undefined;
|
|
176
122
|
/**
|
|
177
123
|
* kill a client tunnel idle this long; never fires mid-request (default 60000)
|
|
178
124
|
*/
|
|
@@ -206,6 +152,20 @@ export type HoldOptions = {
|
|
|
206
152
|
* bodies over this are denied at the door, never queued (default 1048576)
|
|
207
153
|
*/
|
|
208
154
|
maxBody?: number | undefined;
|
|
155
|
+
/**
|
|
156
|
+
* ask seam (bareguard `humanChannel` shape).
|
|
157
|
+
* When present no files are involved; omit it for the default file checkpoint
|
|
158
|
+
* (`<runtime>/asks` + `<runtime>/answers`, `sbx pending/approve/deny`).
|
|
159
|
+
*/
|
|
160
|
+
onAsk?: import("./holds.js").OnAsk | null | undefined;
|
|
161
|
+
/**
|
|
162
|
+
* answer poll interval in ms for the file checkpoint (default 250)
|
|
163
|
+
*/
|
|
164
|
+
pollMs?: number | undefined;
|
|
165
|
+
/**
|
|
166
|
+
* event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle
|
|
167
|
+
*/
|
|
168
|
+
onEvent?: import("./holds.js").OnEvent | undefined;
|
|
209
169
|
};
|
|
210
170
|
/**
|
|
211
171
|
* One upstream forwarding: buffered body (approve path) or live stream.
|
package/types/run.d.ts
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build the hold decision for a mode. Pure: unit-testable without a sandbox.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} mode `ask` | `auto` | `trust`
|
|
5
|
+
* @returns {import('./holds.js').ShouldHold} decision function for the hold queue
|
|
6
|
+
*/
|
|
7
|
+
export function shouldHoldForMode(mode: string): import("./holds.js").ShouldHold;
|
|
8
|
+
/**
|
|
9
|
+
* A collected ask record: the parked ask plus how it was decided.
|
|
10
|
+
*
|
|
11
|
+
* @typedef {object} AskRecord
|
|
12
|
+
* @property {string} id hold id (`h<n>`)
|
|
13
|
+
* @property {string} agent agent name (its socket)
|
|
14
|
+
* @property {string} method HTTP method
|
|
15
|
+
* @property {string} url full URL
|
|
16
|
+
* @property {string} rule hold-reason string
|
|
17
|
+
* @property {string} letter always `'x'` until M3 owns letters
|
|
18
|
+
* @property {string} [bodyPreview] request body preview
|
|
19
|
+
* @property {boolean} [truncated] body preview truncation flag
|
|
20
|
+
* @property {string|null} [expiresAt] ISO-8601 UTC deadline, or null
|
|
21
|
+
* @property {string} [decision] `'allow'` | `'deny'`, once decided
|
|
22
|
+
* @property {string} [reason] decision reason, once decided
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Options for {@link run}.
|
|
26
|
+
*
|
|
27
|
+
* @typedef {object} RunOptions
|
|
28
|
+
* @property {string} proj project directory, bound read-only inside the sandbox
|
|
29
|
+
* @property {string[]} command argv to run inside, for example `["bash", "-c", "echo hi"]`
|
|
30
|
+
* @property {string} [mode] `ask` | `auto` (default) | `trust`; see the mode comment at the top of this file
|
|
31
|
+
* @property {import('./holds.js').OnAsk} [onAsk] ask seam; when present no files are involved, otherwise the file checkpoint runs
|
|
32
|
+
* @property {import('./holds.js').OnEvent} [onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon (also feeds `asks`)
|
|
33
|
+
* @property {string} [runtime] runtime dir (created when absent: a short `sbx-*` temp dir, keeping unix socket paths under the ~108-char limit)
|
|
34
|
+
* @property {string} [agent] agent name (default `'agent'`)
|
|
35
|
+
* @property {number|string} [timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
36
|
+
* @property {object} [unsafeTestOnly] TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`): everything under it weakens the sandbox wall
|
|
37
|
+
* @property {NodeJS.WritableStream} [stdout] where the agent's stdout goes (default process.stdout)
|
|
38
|
+
* @property {NodeJS.WritableStream} [stderr] where the agent's stderr goes (default process.stderr)
|
|
39
|
+
* @property {boolean} [overlay] when true, mount a per-run overlay upper over the project (M4 tracing paper) instead of binding it read-only; the result's `diff` lists the run's changes for commit/rollback
|
|
40
|
+
* @property {string[]} [overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Run one sandboxed agent start-to-finish in a single call.
|
|
44
|
+
*
|
|
45
|
+
* @param {object} opts
|
|
46
|
+
* @param {string} opts.proj project directory, bound read-only inside the sandbox
|
|
47
|
+
* @param {string[]} opts.command argv to run inside, for example `["bash", "-c", "echo hi"]`
|
|
48
|
+
* @param {string} [opts.mode] `ask` | `auto` (default) | `trust`; see the mode comment at the top of this file
|
|
49
|
+
* @param {import('./holds.js').OnAsk|null} [opts.onAsk] ask seam; when present no files are involved, otherwise the file checkpoint runs
|
|
50
|
+
* @param {import('./holds.js').OnEvent|null} [opts.onEvent] event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon (also feeds `asks`)
|
|
51
|
+
* @param {string|null} [opts.runtime] runtime dir (created when absent: a short `sbx-*` temp dir, keeping unix socket paths under the ~108-char limit)
|
|
52
|
+
* @param {string} [opts.agent] agent name (default `'agent'`)
|
|
53
|
+
* @param {number|string} [opts.timeoutMs] hold deadline in ms, or `'never'` (default 120000)
|
|
54
|
+
* @param {object|null} [opts.unsafeTestOnly] TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`)
|
|
55
|
+
* @param {NodeJS.WritableStream|null} [opts.stdout] where the agent's stdout goes (default process.stdout)
|
|
56
|
+
* @param {NodeJS.WritableStream|null} [opts.stderr] where the agent's stderr goes (default process.stderr)
|
|
57
|
+
* @param {boolean} [opts.overlay] when true, mount a per-run overlay upper over the project instead of binding it read-only; the result's `diff` lists the run's changes for commit/rollback
|
|
58
|
+
* @param {string[]} [opts.overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
59
|
+
* @returns {Promise<{ exit: number, asks: AskRecord[], audit: string, diff: import('./overlay.js').OverlayChange[], runtime: string }>} the agent process exit code, the collected ask records, the JSONL audit log path, the run's overlay diff (M4), and the runtime dir addressing it
|
|
60
|
+
* @when Reach for this to HARNESS-ONLY run one sandboxed agent start-to-finish in a single call: it starts the proxy, sandboxes the command, answers holds, and returns the exit code with the ask and audit trail (plus the overlay diff once M4 exists).
|
|
61
|
+
* @fails Rejects on a missing/unreadable `proj`, an empty `command`, a bad `mode`, a misshapen `onAsk`, a proxy that never becomes ready, a `launch` that throws (uncommitted overlay changes, a runtime inside the project — the proxy is closed first), or an overlay diff that cannot be read. The agent's own nonzero exit is returned as `exit`, never thrown.
|
|
62
|
+
* @example
|
|
63
|
+
* import { run } from "barebox";
|
|
64
|
+
* const { exit, asks, audit } = await run({ proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
|
|
65
|
+
*/
|
|
66
|
+
export function run({ proj, command, mode, onAsk, onEvent, runtime, agent, timeoutMs, unsafeTestOnly, stdout, stderr, overlay, overlayIgnore }: {
|
|
67
|
+
proj: string;
|
|
68
|
+
command: string[];
|
|
69
|
+
mode?: string | undefined;
|
|
70
|
+
onAsk?: import("./holds.js").OnAsk | null | undefined;
|
|
71
|
+
onEvent?: import("./holds.js").OnEvent | null | undefined;
|
|
72
|
+
runtime?: string | null | undefined;
|
|
73
|
+
agent?: string | undefined;
|
|
74
|
+
timeoutMs?: string | number | undefined;
|
|
75
|
+
unsafeTestOnly?: object | null;
|
|
76
|
+
stdout?: NodeJS.WritableStream | null | undefined;
|
|
77
|
+
stderr?: NodeJS.WritableStream | null | undefined;
|
|
78
|
+
overlay?: boolean | undefined;
|
|
79
|
+
overlayIgnore?: string[] | undefined;
|
|
80
|
+
}): Promise<{
|
|
81
|
+
exit: number;
|
|
82
|
+
asks: AskRecord[];
|
|
83
|
+
audit: string;
|
|
84
|
+
diff: import("./overlay.js").OverlayChange[];
|
|
85
|
+
runtime: string;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* A collected ask record: the parked ask plus how it was decided.
|
|
89
|
+
*/
|
|
90
|
+
export type AskRecord = {
|
|
91
|
+
/**
|
|
92
|
+
* hold id (`h<n>`)
|
|
93
|
+
*/
|
|
94
|
+
id: string;
|
|
95
|
+
/**
|
|
96
|
+
* agent name (its socket)
|
|
97
|
+
*/
|
|
98
|
+
agent: string;
|
|
99
|
+
/**
|
|
100
|
+
* HTTP method
|
|
101
|
+
*/
|
|
102
|
+
method: string;
|
|
103
|
+
/**
|
|
104
|
+
* full URL
|
|
105
|
+
*/
|
|
106
|
+
url: string;
|
|
107
|
+
/**
|
|
108
|
+
* hold-reason string
|
|
109
|
+
*/
|
|
110
|
+
rule: string;
|
|
111
|
+
/**
|
|
112
|
+
* always `'x'` until M3 owns letters
|
|
113
|
+
*/
|
|
114
|
+
letter: string;
|
|
115
|
+
/**
|
|
116
|
+
* request body preview
|
|
117
|
+
*/
|
|
118
|
+
bodyPreview?: string | undefined;
|
|
119
|
+
/**
|
|
120
|
+
* body preview truncation flag
|
|
121
|
+
*/
|
|
122
|
+
truncated?: boolean | undefined;
|
|
123
|
+
/**
|
|
124
|
+
* ISO-8601 UTC deadline, or null
|
|
125
|
+
*/
|
|
126
|
+
expiresAt?: string | null | undefined;
|
|
127
|
+
/**
|
|
128
|
+
* `'allow'` | `'deny'`, once decided
|
|
129
|
+
*/
|
|
130
|
+
decision?: string | undefined;
|
|
131
|
+
/**
|
|
132
|
+
* decision reason, once decided
|
|
133
|
+
*/
|
|
134
|
+
reason?: string | undefined;
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
137
|
+
* Options for {@link run}.
|
|
138
|
+
*/
|
|
139
|
+
export type RunOptions = {
|
|
140
|
+
/**
|
|
141
|
+
* project directory, bound read-only inside the sandbox
|
|
142
|
+
*/
|
|
143
|
+
proj: string;
|
|
144
|
+
/**
|
|
145
|
+
* argv to run inside, for example `["bash", "-c", "echo hi"]`
|
|
146
|
+
*/
|
|
147
|
+
command: string[];
|
|
148
|
+
/**
|
|
149
|
+
* `ask` | `auto` (default) | `trust`; see the mode comment at the top of this file
|
|
150
|
+
*/
|
|
151
|
+
mode?: string | undefined;
|
|
152
|
+
/**
|
|
153
|
+
* ask seam; when present no files are involved, otherwise the file checkpoint runs
|
|
154
|
+
*/
|
|
155
|
+
onAsk?: import("./holds.js").OnAsk | undefined;
|
|
156
|
+
/**
|
|
157
|
+
* event callback: `{ type: 'ask' }` on park, `{ type: 'decided' }` on settle or abandon (also feeds `asks`)
|
|
158
|
+
*/
|
|
159
|
+
onEvent?: import("./holds.js").OnEvent | undefined;
|
|
160
|
+
/**
|
|
161
|
+
* runtime dir (created when absent: a short `sbx-*` temp dir, keeping unix socket paths under the ~108-char limit)
|
|
162
|
+
*/
|
|
163
|
+
runtime?: string | undefined;
|
|
164
|
+
/**
|
|
165
|
+
* agent name (default `'agent'`)
|
|
166
|
+
*/
|
|
167
|
+
agent?: string | undefined;
|
|
168
|
+
/**
|
|
169
|
+
* hold deadline in ms, or `'never'` (default 120000)
|
|
170
|
+
*/
|
|
171
|
+
timeoutMs?: string | number | undefined;
|
|
172
|
+
/**
|
|
173
|
+
* TEST-ONLY escape hatch, forwarded to the proxy (see `createProxy`): everything under it weakens the sandbox wall
|
|
174
|
+
*/
|
|
175
|
+
unsafeTestOnly?: object;
|
|
176
|
+
/**
|
|
177
|
+
* where the agent's stdout goes (default process.stdout)
|
|
178
|
+
*/
|
|
179
|
+
stdout?: NodeJS.WritableStream | undefined;
|
|
180
|
+
/**
|
|
181
|
+
* where the agent's stderr goes (default process.stderr)
|
|
182
|
+
*/
|
|
183
|
+
stderr?: NodeJS.WritableStream | undefined;
|
|
184
|
+
/**
|
|
185
|
+
* when true, mount a per-run overlay upper over the project (M4 tracing paper) instead of binding it read-only; the result's `diff` lists the run's changes for commit/rollback
|
|
186
|
+
*/
|
|
187
|
+
overlay?: boolean | undefined;
|
|
188
|
+
/**
|
|
189
|
+
* path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
|
|
190
|
+
*/
|
|
191
|
+
overlayIgnore?: string[] | undefined;
|
|
192
|
+
};
|