barebox 0.6.0 → 0.8.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.
@@ -10,7 +10,8 @@
10
10
  */
11
11
  export function buildCaBundle(runtime: string, caFile: string): string;
12
12
  /**
13
- * Start one sandboxed agent. The agent sees a read-only `/usr` and `/etc`,
13
+ * Start one sandboxed agent. The agent sees a read-only `/usr` and `/etc`
14
+ * (and the host's `/bin`, `/sbin`, `/lib`, `/lib64`, mirrored),
14
15
  * its project and home read-only, the locked drawers masked, no network of
15
16
  * its own, and a single front door: anything sent to `localhost:<proxyPort>`
16
17
  * is carried out through this agent's socket to the proxy. The agent starts
@@ -30,18 +31,19 @@ export function buildCaBundle(runtime: string, caFile: string): string;
30
31
  * @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`.
31
32
  * @param {string|null} [opts.socket] agent socket path, as returned by `proxy.addAgent` (default `<runtime>/socks/<agent>.sock`; the two must agree)
32
33
  * @param {string|null} [opts.caFile] barebox CA cert path (from `proxy.caFile`); when set, the sandbox trusts it for HTTPS alongside the system roots
34
+ * @param {string} [opts.nodeExec] the node executable to run inside (default: this process's `process.execPath`). The install is a `bin/` layout's parent dir (npm and global CLIs live there) or, for any other layout, the exec's own dir. It is bound read-only with the exec's dir first on the inside PATH, unless already visible there (under `/usr`, `$HOME` or the project). Never bound when it is `/`, `$HOME` or above it: then it is not on PATH either (see `hostNode` in `src/runtime.js`). A runtime or project under the install is masked or mounted over it, so the bind never exposes either
33
35
  * @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)
34
36
  * @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)`
35
37
  * @param {string[]} [opts.overlayIgnore] path segments invisible to the overlay (default `node_modules`; see overlay.js Q3)
36
38
  * @returns {import('node:child_process').ChildProcess} the bwrap child (stdio piped); `close` gives its exit code
37
39
  * @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.
38
- * @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.
40
+ * @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`, the project dir, `/etc`, `/usr` or the bound Node install 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.
39
41
  * @example
40
42
  * import { launch } from "barebox";
41
43
  * const child = launch({ runtime: "/tmp/sbx", agent: "fx", proj: "/tmp/proj", command: ["bash", "-c", "echo hi"] });
42
44
  * await new Promise((r) => child.on("close", r)); // child.exitCode === 0
43
45
  */
44
- export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, env, socket, caFile, overlay, overlayIgnore, maskDirs }: {
46
+ export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, env, socket, caFile, overlay, overlayIgnore, maskDirs, nodeExec }: {
45
47
  runtime: string;
46
48
  agent: string;
47
49
  proj: string;
@@ -55,6 +57,7 @@ export function launch({ runtime, agent, proj, command, proxyPort, extraEnv, env
55
57
  } | undefined;
56
58
  socket?: string | null | undefined;
57
59
  caFile?: string | null | undefined;
60
+ nodeExec?: string | undefined;
58
61
  maskDirs?: string[] | undefined;
59
62
  overlay?: boolean | undefined;
60
63
  overlayIgnore?: string[] | undefined;
package/types/policy.d.ts CHANGED
@@ -8,6 +8,15 @@
8
8
  * @returns {Record<string, any>} tool name to letter
9
9
  */
10
10
  export function buildTools(labelTools?: Record<string, any>, localLlm?: string[]): Record<string, any>;
11
+ /**
12
+ * The tool keys that are model calls (they skip the card floor). A label
13
+ * tool that shares a key wins in `buildTools`, so it is not a model here.
14
+ *
15
+ * @param {Record<string, any>} [labelTools] the label files' gate-file tools
16
+ * @param {string[]} [localLlm] `host:port` pairs of local model servers
17
+ * @returns {Set<string>} model tool keys
18
+ */
19
+ export function modelTools(labelTools?: Record<string, any>, localLlm?: string[]): Set<string>;
11
20
  /**
12
21
  * The tool name for one request. A spec key (a label file documents it) is
13
22
  * used as is, even if the gate file lacks it (then the gate denies it:
@@ -0,0 +1,45 @@
1
+ /** @typedef {{ id: string, agent?: string, method: string, url: string, rule: string, letter: string, bodyPreview?: string, expiresAt?: string|null }} AskRecord */
2
+ /**
3
+ * Plain-language lines for one ask record. Every agent-controlled byte goes
4
+ * through `safe()`.
5
+ *
6
+ * @param {AskRecord} ask the parked request
7
+ * @param {number} [now] clock for the countdown, injectable
8
+ * @returns {string[]} the question, one string per line
9
+ */
10
+ export function questionLines(ask: AskRecord, now?: number): string[];
11
+ /**
12
+ * The terminal prompt. One question open at a time. `ask()` opens a question
13
+ * and returns true, or returns false when one is already open (or the prompt
14
+ * is closed) so the caller prints the plain `ask` line instead; `decided()`
15
+ * closes the question when the hold ended some other way, `close()` releases
16
+ * `input`.
17
+ *
18
+ * @param {{ input: import('node:stream').Readable, output: { write: (s: string) => unknown }, answer: (id: string, decision: 'allow'|'deny', reason?: string) => boolean|void }} opts streams and the answer writer
19
+ * @returns {{ ask: (ask: AskRecord) => boolean, decided: (e: { id: string, decision: string, reason?: string }) => void, close: () => void }} the prompt
20
+ */
21
+ export function createTerminalPrompt({ input, output, answer }: {
22
+ input: import("node:stream").Readable;
23
+ output: {
24
+ write: (s: string) => unknown;
25
+ };
26
+ answer: (id: string, decision: "allow" | "deny", reason?: string) => boolean | void;
27
+ }): {
28
+ ask: (ask: AskRecord) => boolean;
29
+ decided: (e: {
30
+ id: string;
31
+ decision: string;
32
+ reason?: string;
33
+ }) => void;
34
+ close: () => void;
35
+ };
36
+ export type AskRecord = {
37
+ id: string;
38
+ agent?: string;
39
+ method: string;
40
+ url: string;
41
+ rule: string;
42
+ letter: string;
43
+ bodyPreview?: string;
44
+ expiresAt?: string | null;
45
+ };
@@ -23,6 +23,37 @@ export function realPathOf(p: string): string;
23
23
  * @returns {boolean} containment on real paths
24
24
  */
25
25
  export function isInside(base: string, p: string): boolean;
26
+ /**
27
+ * Where the host's node sits and what of it to bind so it runs inside the sandbox. An exec in a
28
+ * `bin/` dir (nvm, fnm, toolcache, distro) has its install one dir up, where npm and the global
29
+ * CLIs live; any other layout is its own dir. Never `/`, `$HOME` or an ancestor of it: binding
30
+ * that would hand the sandbox the whole host or every user's home, so it is `refused` and
31
+ * neither bound nor put on PATH.
32
+ *
33
+ * @param {string} execPath absolute path of the node executable
34
+ * @param {string} home the sandbox $HOME
35
+ * @param {string} [proj] the project dir, already visible inside
36
+ * @returns {{dir: string, bin: string|null, bind: string|null, refused: string|null}} `dir` the install folder; `bin` the PATH entry (null when refused); `bind` the dir to bind read-only (null when refused or already under `/usr`, `$HOME` or the project); `refused` a short reason or null
37
+ */
38
+ export function hostNode(execPath: string, home: string, proj?: string): {
39
+ dir: string;
40
+ bin: string | null;
41
+ bind: string | null;
42
+ refused: string | null;
43
+ };
44
+ /**
45
+ * The host's top-level `/bin`, `/sbin`, `/lib` and `/lib64`, as bwrap args that mirror them
46
+ * inside the sandbox. On a merged-/usr host they are symlinks into `/usr` (`/bin -> usr/bin`),
47
+ * recreated as the same links; a real directory (a non-merged distro) is bound read-only;
48
+ * a missing path is skipped. Without them `#!/bin/sh` scripts and Node's `exec` fail.
49
+ *
50
+ * @param {string} [prefix] prepended to each path when looking at the host (tests point it at a fake root); the inside paths are always `/bin` etc.
51
+ * @returns {{args: string[], bound: string[]}} `args` the bwrap args, to go right after the `/usr` bind; `bound` the real directories bound (they are sandbox roots, so the masks apply under them)
52
+ */
53
+ export function usrLinks(prefix?: string): {
54
+ args: string[];
55
+ bound: string[];
56
+ };
26
57
  /**
27
58
  * Create a short-lived runtime dir: a short `sbx-` temp dir, keeping unix
28
59
  * socket paths under the ~108-char limit.
@@ -30,6 +61,21 @@ export function isInside(base: string, p: string): boolean;
30
61
  * @returns {string} the new runtime dir
31
62
  */
32
63
  export function createRuntime(): string;
64
+ /**
65
+ * The runtime dir of a named sandbox (`sbx run --name <n>`): the name replaces
66
+ * a `SBX_RUNTIME` path for every command. `$XDG_RUNTIME_DIR/barebox/<n>` when
67
+ * that variable names an existing dir you own, else
68
+ * `<tmpdir>/sbx-<uid>/<n>`. The one function that maps a name to a dir.
69
+ *
70
+ * @param {string} name sandbox name (the agent-name rules)
71
+ * @param {NodeJS.ProcessEnv} [env] environment to read `XDG_RUNTIME_DIR` from
72
+ * @param {{ create?: boolean }} [opts] `create`: make the parent and runtime dirs owner-only (0700); off, nothing is touched
73
+ * @returns {string} the runtime dir
74
+ * @throws {Error} on a bad name, a socket path over the 107-byte cap, or a foreign-owned parent
75
+ */
76
+ export function namedRuntime(name: string, env?: NodeJS.ProcessEnv, { create }?: {
77
+ create?: boolean;
78
+ }): string;
33
79
  /**
34
80
  * Create a temp dir inside the runtime tree: `mkdtemp` is a runtime-layout
35
81
  * writer, so it lives here, not in `overlay.js`. Used for the merged-view
@@ -11,6 +11,11 @@ export function loadSecrets(config?: Record<string, {
11
11
  value?: string;
12
12
  hosts: string[];
13
13
  }>, env?: NodeJS.ProcessEnv): Secret[];
14
+ /**
15
+ * Which secrets' placeholders sit in a request URL (path and query). Keys are swapped in headers only.
16
+ * @param {string} pathAndQuery @param {Secret[]} secrets @returns {string[]} the secrets' names
17
+ */
18
+ export function urlPlaceholders(pathAndQuery: string, secrets: Secret[]): string[];
14
19
  /**
15
20
  * Which placeholders a request carries, and whether each is headed for a host its secret is bound to.
16
21
  * Each header value is searched as a substring and, for `Basic <base64>`, decoded.