barebox 0.15.0 → 0.16.1
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 +17 -0
- package/README.md +1 -1
- package/barebox.context.md +6 -3
- package/package.json +2 -2
- package/src/cli.js +11 -8
- package/src/config.js +66 -9
- package/src/policy.js +2 -0
- package/src/prompt.js +4 -3
- package/src/proxy.js +9 -8
- package/types/config.d.ts +17 -28
- package/types/prompt.d.ts +2 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,23 @@ All notable changes to this project will be documented in this file.
|
|
|
4
4
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
|
|
6
6
|
|
|
7
|
+
## [0.16.1] - 2026-12-01
|
|
8
|
+
|
|
9
|
+
### Changed
|
|
10
|
+
|
|
11
|
+
- bareguard pin 0.38.0 to 0.40.0. bareguard now closes its defaults: an unset `net.allowDomains` asks or denies, and `denyPrivateIps` is on by default (which also denies `localhost.`, `*.localhost` and a trailing dot). barebox already decides hosts itself (the `hosts` lists, then the wall's check of every resolved IP after DNS, which is stricter), so each agent's gate sets `net { allowDomains: ['*'], denyPrivateIps: false }` and nothing changes for you.
|
|
12
|
+
- `denyPrivateIps` stays off in the gate because bareguard's check would otherwise refuse a `localLlm` host such as `localhost:11434` before the wall's local-model exception applies.
|
|
13
|
+
|
|
14
|
+
## [0.16.0] - 2026-12-01
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- `profiles` config key (`{ "<name>": { mode?, hosts?, secrets? } }`) and `sbx run --profile <name>`: one config file gives each sandbox or group its own policy, so two runs can sit side by side with different rules. A profile's `mode`, `hosts.allow`, `hosts.unknown` and whole `secrets` map replace the top level's; `hosts.deny` is the union, so a profile can add a denied host but never remove one; `--mode` beats the profile's mode; `floor` stays top-level and applies to every profile. Errors are loud: an unknown `--profile` exits 2 and lists the defined ones, and a bad profile stops every run, even one that picks another. `sbx run` prints `profile <name> (mode <mode>)` at the start, and an ask names the profile (a `profile=<name>` tail, a `profile` field with `--json`, `(profile <name>)` in the terminal question). A `fromEnv` secret is now checked only when the run's effective secrets use it.
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
|
|
22
|
+
- A request refused at the CONNECT step, mostly an HTTPS host refused before any path is sent, now has `method: "CONNECT"` and its `host:port` target on its audit `deny` line. 0.15.0 said every refused request's deny line names its method and URL, but these lines named neither; only the `host` line next to them named the host. Upgrade and inner `CONNECT` refusals now name their method too.
|
|
23
|
+
|
|
7
24
|
## [0.15.0] - 2026-12-01
|
|
8
25
|
|
|
9
26
|
### Added
|
package/README.md
CHANGED
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
|
|
35
35
|
```
|
|
36
36
|
npm install -g barebox
|
|
37
|
-
sbx run -- claude
|
|
37
|
+
sbx run -- claude -p "your task" # auto mode: reads pass, sends ask (the agent gets no terminal, so -p)
|
|
38
38
|
sbx run --mode ask --overlay -- node agent.js
|
|
39
39
|
sbx diff && sbx commit # keep the file changes, or: sbx rollback
|
|
40
40
|
```
|
package/barebox.context.md
CHANGED
|
@@ -124,7 +124,7 @@ const { exit, asks, audit, diff, runtime, hosts } = await run({
|
|
|
124
124
|
```
|
|
125
125
|
|
|
126
126
|
```bash
|
|
127
|
-
sbx run [--name <n>] [--mode ask|auto|trust] [--hold <seconds|never>] [--json] [--overlay] -- <cmd> [...]
|
|
127
|
+
sbx run [--name <n>] [--mode ask|auto|trust] [--profile <name>] [--hold <seconds|never>] [--json] [--overlay] -- <cmd> [...]
|
|
128
128
|
```
|
|
129
129
|
|
|
130
130
|
`sbx run` uses the current directory as the project and `--name`'s runtime or
|
|
@@ -140,7 +140,7 @@ Its exit code mirrors the agent's.
|
|
|
140
140
|
(positive integer) and `localLlm` (array of `"host:port"`). A flag beats the file, the file beats the default; an
|
|
141
141
|
unknown key, bad value or bad JSON exits 2 naming the file and key. The file
|
|
142
142
|
is hidden inside the sandbox wherever it resolves. The library does not read it.
|
|
143
|
-
`hosts` (`{ allow?: string[], deny?: string[], unknown?: "ask"|"deny" }`; entries are a lower-case host or `*.domain`) decides which hosts the agent may reach: a `deny` host is refused in every mode before any DNS lookup, an `allow` host never gets the first-contact question, and any other host asks once on first contact in `ask` and `auto` (allow for this run, or deny; `trust` lets it through). `unknown: "deny"` refuses such hosts at once, which `sbx run` does by default with no terminal. Model endpoints, secret hosts and `localLlm` hosts never ask. In the library, `run({ hosts })` takes the same shape and its result carries `hosts: { allowed, refused }`; `createProxy({ hosts })` also takes `unknown: "allow"`, its default. `sbx run` prints a `hosts:` summary line at the start, and at the end the hosts you allowed as a line to paste into `hosts.allow`. Two more keys, `labels` and `tenants`, are described under "Labels and model endpoints". `floor` (`{ pay?: string[], send?: string[] }`) lists your own payment and send domains: an entry is a host, `*.domain` (subdomains only) or `"METHOD host/path"`; any write to a listed host, or the one listed endpoint, asks in every mode, `trust` included, and the question shows the recipient, amount and currency fields found in the body (labelled by path), or says why none was read. Each of the three lists in the effect holds at most 6 fields, and 6 means "more than 5": a list of more than 5 is shown as "more than 5 fields", none listed. Endpoint entries match the request path case-insensitively, with `%XX` of unreserved characters decoded and `;params` dropped; `sbx pending` shows the floor entry and the effect lines. There are no built-in entries; an absent `floor` lists nothing. Two more, `env` and `secrets`, are described under "Keys the agent never holds".
|
|
143
|
+
`hosts` (`{ allow?: string[], deny?: string[], unknown?: "ask"|"deny" }`; entries are a lower-case host or `*.domain`) decides which hosts the agent may reach: a `deny` host is refused in every mode before any DNS lookup, an `allow` host never gets the first-contact question, and any other host asks once on first contact in `ask` and `auto` (allow for this run, or deny; `trust` lets it through). `unknown: "deny"` refuses such hosts at once, which `sbx run` does by default with no terminal. Model endpoints, secret hosts and `localLlm` hosts never ask. In the library, `run({ hosts })` takes the same shape and its result carries `hosts: { allowed, refused }`; `createProxy({ hosts })` also takes `unknown: "allow"`, its default. `sbx run` prints a `hosts:` summary line at the start, and at the end the hosts you allowed as a line to paste into `hosts.allow`. Two more keys, `labels` and `tenants`, are described under "Labels and model endpoints". `floor` (`{ pay?: string[], send?: string[] }`) lists your own payment and send domains: an entry is a host, `*.domain` (subdomains only) or `"METHOD host/path"`; any write to a listed host, or the one listed endpoint, asks in every mode, `trust` included, and the question shows the recipient, amount and currency fields found in the body (labelled by path), or says why none was read. Each of the three lists in the effect holds at most 6 fields, and 6 means "more than 5": a list of more than 5 is shown as "more than 5 fields", none listed. Endpoint entries match the request path case-insensitively, with `%XX` of unreserved characters decoded and `;params` dropped; `sbx pending` shows the floor entry and the effect lines. There are no built-in entries; an absent `floor` lists nothing. Two more, `env` and `secrets`, are described under "Keys the agent never holds". `profiles` (`{ "<name>": { mode?, hosts?, secrets? } }`, names as for `--name`) lets `sbx run --profile <name>` lay one set over the top level for that run: its `mode`, `hosts.allow`, `hosts.unknown` and whole `secrets` map replace the top level's, `hosts.deny` is the union (a profile can add a deny, never remove one), and `--mode` beats both; `floor` and every other key stay top-level. A bad profile stops every `sbx run`; an unknown `--profile` exits 2 and lists the defined ones. `sbx run` prints `profile <name> (mode <mode>)` before the `hosts:` line, and its ask lines carry the profile (` profile=<name>`, a `profile` field with `--json`). The library does not read profiles.
|
|
144
144
|
|
|
145
145
|
## Modes and the gate
|
|
146
146
|
|
|
@@ -282,7 +282,10 @@ it between runs (no proxy running) to start a new one.
|
|
|
282
282
|
`action.rid`.
|
|
283
283
|
- Every refused request has a `deny` line with `meta: { agent, code, note, method, url }`,
|
|
284
284
|
refusals before the gate included (a bad target, a host deny, the wall, a hold
|
|
285
|
-
timeout, a full hold queue). Without a `Host` header, `url` is the raw target.
|
|
285
|
+
timeout, a full hold queue). Without a `Host` header, `url` is the raw target. A
|
|
286
|
+
refusal at the CONNECT step (an HTTPS host refused before any path is sent) has
|
|
287
|
+
`method: "CONNECT"` and `url` set to the `host:port` target. A request head over 64 KB is
|
|
288
|
+
refused before its first line is read, so its line has no method or url.
|
|
286
289
|
- In barebox's own lines, `url` (in `meta` and in `where`) has userinfo masked whole (`http://***@host/...`) and card
|
|
287
290
|
numbers masked to their last 4 digits. bareguard's gate lines keep the URL as sent.
|
|
288
291
|
- Lines for an agent whose gate is not built, and every line when `gateFor` is
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "barebox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.1",
|
|
4
4
|
"description": "Lightweight local sandbox for AI agents: every write undoable, nothing leaves the machine without passing the doorman.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"typescript": "5"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"bareguard": "0.
|
|
53
|
+
"bareguard": "0.40.0",
|
|
54
54
|
"rwxmap": "0.11.1"
|
|
55
55
|
}
|
|
56
56
|
}
|
package/src/cli.js
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
// `--name <n>` names the sandbox (agent name + runtime dir, runtime.js namedRuntime) and every
|
|
14
14
|
// command takes `--name` instead of SBX_RUNTIME (both together: exit 2).
|
|
15
15
|
//
|
|
16
|
-
// sbx run [--mode ask|auto|trust] [--hold <seconds|never>] [--name <n>] [--json] [--overlay] -- <cmd> [...]
|
|
16
|
+
// sbx run [--mode ask|auto|trust] [--profile <name>] [--hold <seconds|never>] [--name <n>] [--json] [--overlay] -- <cmd> [...]
|
|
17
17
|
// Runs one sandboxed agent start-to-finish in this process (project = cwd).
|
|
18
18
|
// The runtime is SBX_RUNTIME when set, else a short temp dir (printed to
|
|
19
19
|
// stderr so a second terminal can approve from it). With --json, stdout
|
|
@@ -45,7 +45,7 @@ import { assertAgentName, createRuntime, namedRuntime, hostNode, runtimePaths, r
|
|
|
45
45
|
|
|
46
46
|
// Seconds a held request waits when no one is at a terminal and neither --hold nor holdTimeoutSec is set.
|
|
47
47
|
const HOLD_NO_TERMINAL_SEC = 180;
|
|
48
|
-
const USAGE = 'usage: sbx [--help] | run [--mode ask|auto|trust] [--hold <seconds|never>] [--name <n>] [--json] [--overlay] -- <cmd> [...] | pending|diff|rollback [--name <n>] | approve <id> [--name <n>] | deny <id> [reason] [--name <n>] | commit [--name <n>] [--skip-conflicts] (--name replaces SBX_RUNTIME; --agent stays for diff/commit/rollback)';
|
|
48
|
+
const USAGE = 'usage: sbx [--help] | run [--mode ask|auto|trust] [--profile <name>] [--hold <seconds|never>] [--name <n>] [--json] [--overlay] -- <cmd> [...] | pending|diff|rollback [--name <n>] | approve <id> [--name <n>] | deny <id> [reason] [--name <n>] | commit [--name <n>] [--skip-conflicts] (--name replaces SBX_RUNTIME; --agent stays for diff/commit/rollback)';
|
|
49
49
|
|
|
50
50
|
/** Hold ids the CLI accepts: `h<n>` only. Anything else is a usage error, never a path. */
|
|
51
51
|
const HOLD_ID_RE = /^h\d+$/;
|
|
@@ -199,7 +199,7 @@ async function run(args) {
|
|
|
199
199
|
if (sep < 0) usage();
|
|
200
200
|
const flags = args.slice(0, sep);
|
|
201
201
|
const command = args.slice(sep + 1);
|
|
202
|
-
/** @type {{ name?: string, mode?: string, hold?: string, json?: boolean, overlay?: boolean }} */
|
|
202
|
+
/** @type {{ name?: string, mode?: string, profile?: string, hold?: string, json?: boolean, overlay?: boolean }} */
|
|
203
203
|
let values;
|
|
204
204
|
try {
|
|
205
205
|
({ values } = parseArgs({
|
|
@@ -207,6 +207,7 @@ async function run(args) {
|
|
|
207
207
|
options: {
|
|
208
208
|
name: { type: 'string' },
|
|
209
209
|
mode: { type: 'string' },
|
|
210
|
+
profile: { type: 'string' },
|
|
210
211
|
hold: { type: 'string' },
|
|
211
212
|
json: { type: 'boolean' },
|
|
212
213
|
overlay: { type: 'boolean' },
|
|
@@ -218,10 +219,10 @@ async function run(args) {
|
|
|
218
219
|
usage();
|
|
219
220
|
}
|
|
220
221
|
if (values.mode !== undefined && !MODES.includes(values.mode)) usage();
|
|
221
|
-
/** @type {
|
|
222
|
+
/** @type {import('./config.js').Settings} */
|
|
222
223
|
let cfg;
|
|
223
224
|
try {
|
|
224
|
-
cfg = resolveConfig({ mode: values.mode, hold: values.hold });
|
|
225
|
+
cfg = resolveConfig({ mode: values.mode, profile: values.profile, hold: values.hold });
|
|
225
226
|
} catch (e) {
|
|
226
227
|
err(e instanceof Error ? e.message : String(e));
|
|
227
228
|
process.exit(2);
|
|
@@ -267,11 +268,12 @@ async function run(args) {
|
|
|
267
268
|
})
|
|
268
269
|
: null;
|
|
269
270
|
const prompted = new Set(); // holds the prompt showed: their answer is shown there too
|
|
270
|
-
const emitAsk = (
|
|
271
|
+
const emitAsk = (rec) => {
|
|
272
|
+
const h = values.profile !== undefined ? { ...rec, profile: values.profile } : rec;
|
|
271
273
|
if (prompt?.ask(h)) { prompted.add(h.id); return; }
|
|
272
|
-
const c = clean(h, ['id', 'agent', 'method', 'url', 'rule', 'bodyPreview']);
|
|
274
|
+
const c = clean(h, ['id', 'agent', 'method', 'url', 'rule', 'bodyPreview', 'profile']);
|
|
273
275
|
if (json) out(JSON.stringify({ ev: 'ask', ...c }));
|
|
274
|
-
else out(`ask ${c.id} ${c.agent} ${c.method} ${askUrl(h)} rule=${c.rule}${floorTail(h)}${keyTail(h)}`);
|
|
276
|
+
else out(`ask ${c.id} ${c.agent} ${c.method} ${askUrl(h)} rule=${c.rule}${floorTail(h)}${keyTail(h)}${c.profile !== undefined ? ` profile=${c.profile}` : ''}`);
|
|
275
277
|
};
|
|
276
278
|
const emitDecided = (e) => {
|
|
277
279
|
if (!json && prompted.delete(e.id)) return;
|
|
@@ -286,6 +288,7 @@ async function run(args) {
|
|
|
286
288
|
} else err(name !== undefined
|
|
287
289
|
? `sandbox ${name} (answer from another terminal: sbx approve --name ${name} <id>)`
|
|
288
290
|
: `runtime ${runtime} (answer with: SBX_RUNTIME=${shellQuote(runtime)} sbx approve <id>)`);
|
|
291
|
+
if (values.profile !== undefined) err(`profile ${safe(values.profile)} (mode ${mode})`);
|
|
289
292
|
// M14: hosts on neither list ask when a person is here, else are refused; `trust` lets them through.
|
|
290
293
|
const hostsUnknown = cfg.hosts?.unknown ?? (interactive ? 'ask' : 'deny');
|
|
291
294
|
err(`hosts: ${cfg.hosts?.allow?.length ?? 0} allowed, ${cfg.hosts?.deny?.length ?? 0} denied in config; other hosts ${
|
package/src/config.js
CHANGED
|
@@ -6,6 +6,7 @@ import fs from 'node:fs';
|
|
|
6
6
|
import os from 'node:os';
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import { MODES } from './policy.js';
|
|
9
|
+
import { AGENT_RE } from './runtime.js';
|
|
9
10
|
import { floorList } from './lists.js';
|
|
10
11
|
import { hostMatcher } from 'bareguard';
|
|
11
12
|
|
|
@@ -98,6 +99,24 @@ const checkHosts = (/** @type {any} */ v) => {
|
|
|
98
99
|
return null;
|
|
99
100
|
};
|
|
100
101
|
|
|
102
|
+
/** Keys a profile may set; each reuses the validator of the same top-level key. */
|
|
103
|
+
const PROFILE_KEYS = ['mode', 'hosts', 'secrets'];
|
|
104
|
+
|
|
105
|
+
/** `profiles`: name -> `{ mode?, hosts?, secrets? }`. @param {unknown} v @returns {string|null} */
|
|
106
|
+
const checkProfiles = (v) => {
|
|
107
|
+
if (!isObj(v)) return 'must be an object of name -> { mode?, hosts?, secrets? }';
|
|
108
|
+
for (const [name, p] of Object.entries(/** @type {Record<string, any>} */ (v))) {
|
|
109
|
+
if (!AGENT_RE.test(name)) return `profile name ${JSON.stringify(name)} must match ${AGENT_RE}`;
|
|
110
|
+
if (!isObj(p)) return `profile "${name}" must be an object`;
|
|
111
|
+
for (const [k, val] of Object.entries(p)) {
|
|
112
|
+
if (!PROFILE_KEYS.includes(k)) return `profile "${name}": key "${k}" is not allowed (only ${PROFILE_KEYS.join(', ')})`;
|
|
113
|
+
const bad = KEYS[/** @type {'mode'|'hosts'|'secrets'} */ (k)](val);
|
|
114
|
+
if (bad) return `profile "${name}": key "${k}" ${bad}`;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
};
|
|
119
|
+
|
|
101
120
|
/** Validators by key; each returns an error string or null. */
|
|
102
121
|
const KEYS = {
|
|
103
122
|
mode: (v) => (MODES.includes(v) ? null : `must be one of ${MODES.join(', ')}`),
|
|
@@ -115,6 +134,7 @@ const KEYS = {
|
|
|
115
134
|
hosts: checkHosts,
|
|
116
135
|
env: checkEnv,
|
|
117
136
|
secrets: checkSecrets,
|
|
137
|
+
profiles: checkProfiles,
|
|
118
138
|
};
|
|
119
139
|
|
|
120
140
|
/**
|
|
@@ -128,11 +148,15 @@ export function configPath(env = process.env) {
|
|
|
128
148
|
return path.join(base, 'barebox', 'config.json');
|
|
129
149
|
}
|
|
130
150
|
|
|
151
|
+
/**
|
|
152
|
+
* @typedef {{ mode: string, holdTimeoutSec?: number|string, localLlm: string[], labels: string[], tenants: Record<string, string[]>, heldBodyMaxMB?: number, floor?: { pay?: string[], send?: string[] }, hosts?: { allow?: string[], deny?: string[], unknown?: 'ask'|'deny' }, env?: { pass?: string[], set?: Record<string, string> }, secrets?: Record<string, { fromEnv?: string, value?: string, hosts: string[] }> }} Settings the effective settings of one run (`heldBodyMaxMB` absent means the library default)
|
|
153
|
+
*/
|
|
154
|
+
|
|
131
155
|
/**
|
|
132
156
|
* Read the config file over the defaults. No file means defaults.
|
|
133
157
|
*
|
|
134
158
|
* @param {NodeJS.ProcessEnv} [env] environment to read (default `process.env`)
|
|
135
|
-
* @returns {
|
|
159
|
+
* @returns {Settings & { profiles?: Record<string, Partial<Pick<Settings, 'mode'|'hosts'|'secrets'>>> }} the file's settings over the defaults
|
|
136
160
|
* @throws {Error} naming the file (and the key) on invalid JSON, an unknown key or a bad value
|
|
137
161
|
*/
|
|
138
162
|
export function loadConfig(env = process.env) {
|
|
@@ -158,27 +182,60 @@ export function loadConfig(env = process.env) {
|
|
|
158
182
|
const bad = KEYS[/** @type {keyof typeof KEYS} */ (k)](v);
|
|
159
183
|
if (bad) throw new Error(`${file}: key "${k}" ${bad}`);
|
|
160
184
|
}
|
|
161
|
-
// A secret read from the host environment must be there at startup (the name, never a value).
|
|
162
|
-
for (const [name, s] of Object.entries(data.secrets ?? {})) {
|
|
163
|
-
if (s.fromEnv !== undefined && !env[s.fromEnv]) {
|
|
164
|
-
throw new Error(`${file}: secret ${name}: host variable ${s.fromEnv} is not set`);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
185
|
return { ...DEFAULTS, ...data };
|
|
168
186
|
}
|
|
169
187
|
|
|
188
|
+
/**
|
|
189
|
+
* Lay one profile over the top-level settings: the profile's `mode`, `hosts.allow`,
|
|
190
|
+
* `hosts.unknown` and `secrets` replace the top level's when set; `hosts.deny` is
|
|
191
|
+
* the union, so a profile can add a deny but never remove one. Inputs are not changed.
|
|
192
|
+
*
|
|
193
|
+
* @param {ReturnType<typeof loadConfig>} top the file's settings without `profiles`
|
|
194
|
+
* @param {NonNullable<ReturnType<typeof loadConfig>['profiles']>[string]} p one validated profile
|
|
195
|
+
* @returns {Settings} the settings the run uses
|
|
196
|
+
*/
|
|
197
|
+
function withProfile(top, p) {
|
|
198
|
+
const out = { ...top };
|
|
199
|
+
if (p.mode !== undefined) out.mode = p.mode;
|
|
200
|
+
if (p.secrets !== undefined) out.secrets = p.secrets;
|
|
201
|
+
if (p.hosts !== undefined) {
|
|
202
|
+
out.hosts = { ...top.hosts, ...p.hosts };
|
|
203
|
+
const deny = [...new Set([...(top.hosts?.deny ?? []), ...(p.hosts.deny ?? [])])];
|
|
204
|
+
if (deny.length) out.hosts.deny = deny;
|
|
205
|
+
}
|
|
206
|
+
return out;
|
|
207
|
+
}
|
|
208
|
+
|
|
170
209
|
/**
|
|
171
210
|
* Settings for one `sbx run`: a CLI flag beats the file, the file beats the default.
|
|
211
|
+
* With `flags.profile`, that profile is laid over the top level first: `mode`,
|
|
212
|
+
* `hosts.allow`, `hosts.unknown` and `secrets` are replaced when the profile
|
|
213
|
+
* sets them, `hosts.deny` is the union. The result has no `profiles` key.
|
|
172
214
|
*
|
|
173
215
|
* @param {object} [flags] values given on the command line
|
|
174
216
|
* @param {string} [flags.mode] `--mode`
|
|
217
|
+
* @param {string} [flags.profile] `--profile`: a name under `profiles`
|
|
175
218
|
* @param {string} [flags.hold] `--hold` (`"never"` or a positive integer of seconds, as typed)
|
|
176
219
|
* @param {NodeJS.ProcessEnv} [env] environment to read (default `process.env`)
|
|
177
|
-
* @returns {
|
|
220
|
+
* @returns {Settings} effective settings, no `profiles` key
|
|
178
221
|
* @throws {Error} as {@link loadConfig}
|
|
179
222
|
*/
|
|
180
223
|
export function resolveConfig(flags = {}, env = process.env) {
|
|
181
|
-
const
|
|
224
|
+
const { profiles = {}, ...top } = loadConfig(env);
|
|
225
|
+
let cfg = top;
|
|
226
|
+
if (flags.profile !== undefined) {
|
|
227
|
+
if (!Object.hasOwn(profiles, flags.profile)) {
|
|
228
|
+
const names = Object.keys(profiles);
|
|
229
|
+
throw new Error(`unknown profile ${JSON.stringify(flags.profile)}; ${names.length ? `defined: ${names.join(', ')}` : 'no profiles are defined'}`);
|
|
230
|
+
}
|
|
231
|
+
cfg = withProfile(top, profiles[flags.profile]);
|
|
232
|
+
}
|
|
233
|
+
// A secret read from the host environment must be there at startup (the name, never a value).
|
|
234
|
+
for (const [name, s] of Object.entries(cfg.secrets ?? {})) {
|
|
235
|
+
if (s.fromEnv !== undefined && !env[s.fromEnv]) {
|
|
236
|
+
throw new Error(`${configPath(env)}: secret ${name}: host variable ${s.fromEnv} is not set`);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
182
239
|
let hold;
|
|
183
240
|
if (flags.hold !== undefined) {
|
|
184
241
|
hold = flags.hold === 'never' ? 'never' : /^\d+$/.test(flags.hold) ? Number(flags.hold) : flags.hold;
|
package/src/policy.js
CHANGED
|
@@ -192,6 +192,8 @@ export async function gateFor({ agent, mode, auditPath, humanChannel, tools = TO
|
|
|
192
192
|
// default word patterns scan the URL and method and would fire before rwx.
|
|
193
193
|
content: { askPatterns: [], denyPatterns: [] },
|
|
194
194
|
flags: FLAGS,
|
|
195
|
+
// barebox decides hosts itself (hosts lists, the wall); bareguard's net check stays open.
|
|
196
|
+
net: { allowDomains: ['*'], denyPrivateIps: false },
|
|
195
197
|
humanChannel,
|
|
196
198
|
});
|
|
197
199
|
await gate.init();
|
package/src/prompt.js
CHANGED
|
@@ -30,7 +30,7 @@ export const ANSI = { bold: '\x1b[1m', dim: '\x1b[2m', red: '\x1b[31m', green: '
|
|
|
30
30
|
*/
|
|
31
31
|
export const paint = (on, codes, text) => (on ? `${codes}${text}${ANSI.reset}` : text);
|
|
32
32
|
|
|
33
|
-
/** @typedef {{ id: string, agent?: string, method: string, url: string, rule: string, letter: string, bodyPreview?: string, expiresAt?: string|null, floor?: { kind: string, entry: string }, effect?: import('./effect.js').Effect, keyLast4?: string, modelKey?: 'none'|'other' }} AskRecord */
|
|
33
|
+
/** @typedef {{ id: string, agent?: string, method: string, url: string, rule: string, letter: string, bodyPreview?: string, expiresAt?: string|null, floor?: { kind: string, entry: string }, effect?: import('./effect.js').Effect, keyLast4?: string, modelKey?: 'none'|'other', profile?: string }} AskRecord */
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
36
|
* The `To:` / `Amount:` / `Currency:` lines of a floor request, or one `Effect:` line saying why none was read.
|
|
@@ -128,9 +128,10 @@ export function questionLines(ask, now = Date.now(), color = false) {
|
|
|
128
128
|
: rule;
|
|
129
129
|
const left = ask.expiresAt ? Math.max(0, Math.round((Date.parse(ask.expiresAt) - now) / 1000)) : null;
|
|
130
130
|
const name = safe(ask.agent) || 'agent';
|
|
131
|
+
const via = ask.profile !== undefined ? ` (profile ${safe(ask.profile)})` : '';
|
|
131
132
|
const head = color
|
|
132
|
-
? paint(true, yellow, `⏸ ${bold}${name}${unbold} wants to ${verb} ${host}`)
|
|
133
|
-
: `⏸ ${name} wants to ${verb} ${host}`;
|
|
133
|
+
? paint(true, yellow, `⏸ ${bold}${name}${unbold}${via} wants to ${verb} ${host}`)
|
|
134
|
+
: `⏸ ${name}${via} wants to ${verb} ${host}`;
|
|
134
135
|
const lines = [
|
|
135
136
|
head,
|
|
136
137
|
` ${paint(color, bold, 'Why:')} ${why}`,
|
package/src/proxy.js
CHANGED
|
@@ -314,9 +314,10 @@ export function createProxy(opts) {
|
|
|
314
314
|
}
|
|
315
315
|
|
|
316
316
|
// Raw sockets: pre-HTTP (CONNECT line, oversized head, walled CONNECT, cert failure) and the
|
|
317
|
-
// upgrade / inner CONNECT refusals below. `url`
|
|
318
|
-
|
|
319
|
-
|
|
317
|
+
// upgrade / inner CONNECT refusals below. `url` and `method` are the target and method the proxy
|
|
318
|
+
// saw, for the proxy log only.
|
|
319
|
+
function denyRaw(sock, code, note, url, method) {
|
|
320
|
+
log({ ev: 'deny', agent: agentOf(sock), code, note, ...(url ? { url } : {}), ...(method ? { method } : {}) });
|
|
320
321
|
if (sock.destroyed) return;
|
|
321
322
|
try { sock.end(`HTTP/1.1 ${code} ${note}\r\nContent-Type: text/plain\r\nConnection: close\r\n\r\n${note}\n`); }
|
|
322
323
|
catch {}
|
|
@@ -1204,9 +1205,9 @@ export function createProxy(opts) {
|
|
|
1204
1205
|
// listeners node would forward the upgrade as a GET, or close the socket with no reason.
|
|
1205
1206
|
server.on('upgrade', (creq, sock) => {
|
|
1206
1207
|
const t = creq.url || '';
|
|
1207
|
-
denyRaw(sock, 403, 'denied: upgrade', targetUrl(creq)?.toString() ?? t);
|
|
1208
|
+
denyRaw(sock, 403, 'denied: upgrade', targetUrl(creq)?.toString() ?? t, creq.method);
|
|
1208
1209
|
});
|
|
1209
|
-
server.on('connect', (creq, sock) => { denyRaw(sock, 403, 'denied: connect', creq.url || ''); });
|
|
1210
|
+
server.on('connect', (creq, sock) => { denyRaw(sock, 403, 'denied: connect', creq.url || '', 'CONNECT'); });
|
|
1210
1211
|
|
|
1211
1212
|
// Dispatch: one CONNECT line, or plain HTTP straight into the same server.
|
|
1212
1213
|
function admit(sock, agent) {
|
|
@@ -1253,7 +1254,7 @@ export function createProxy(opts) {
|
|
|
1253
1254
|
const [host, portStr] = String(target || '').split(':');
|
|
1254
1255
|
const portNum = portStr ? parseInt(portStr, 10) : 443;
|
|
1255
1256
|
if (!HOST_RE.test(host || '') || !Number.isFinite(portNum) || portNum < 1 || portNum > 65535) {
|
|
1256
|
-
denyRaw(sock, 400, 'bad connect line');
|
|
1257
|
+
denyRaw(sock, 400, 'bad connect line', target, 'CONNECT');
|
|
1257
1258
|
return;
|
|
1258
1259
|
}
|
|
1259
1260
|
// Deny the tunnel itself: no cert is minted for a walled destination,
|
|
@@ -1263,13 +1264,13 @@ export function createProxy(opts) {
|
|
|
1263
1264
|
const hd = hostDecision(host);
|
|
1264
1265
|
if (hd.d === 'deny') {
|
|
1265
1266
|
hostRefusal(agentOf(sock), hd);
|
|
1266
|
-
denyRaw(sock, 403, /** @type {string} */ (hd.reason));
|
|
1267
|
+
denyRaw(sock, 403, /** @type {string} */ (hd.reason), target, 'CONNECT');
|
|
1267
1268
|
return;
|
|
1268
1269
|
}
|
|
1269
1270
|
if (hd.d === 'allow') {
|
|
1270
1271
|
const verdict = await checkUpstream(host, gate, portNum);
|
|
1271
1272
|
if (!verdict.ok) {
|
|
1272
|
-
denyRaw(sock, 403, verdict.reason);
|
|
1273
|
+
denyRaw(sock, 403, verdict.reason, target, 'CONNECT');
|
|
1273
1274
|
return;
|
|
1274
1275
|
}
|
|
1275
1276
|
}
|
package/types/config.d.ts
CHANGED
|
@@ -5,53 +5,42 @@
|
|
|
5
5
|
* @returns {string} absolute path of `config.json`
|
|
6
6
|
*/
|
|
7
7
|
export function configPath(env?: NodeJS.ProcessEnv): string;
|
|
8
|
+
/**
|
|
9
|
+
* @typedef {{ mode: string, holdTimeoutSec?: number|string, localLlm: string[], labels: string[], tenants: Record<string, string[]>, heldBodyMaxMB?: number, floor?: { pay?: string[], send?: string[] }, hosts?: { allow?: string[], deny?: string[], unknown?: 'ask'|'deny' }, env?: { pass?: string[], set?: Record<string, string> }, secrets?: Record<string, { fromEnv?: string, value?: string, hosts: string[] }> }} Settings the effective settings of one run (`heldBodyMaxMB` absent means the library default)
|
|
10
|
+
*/
|
|
8
11
|
/**
|
|
9
12
|
* Read the config file over the defaults. No file means defaults.
|
|
10
13
|
*
|
|
11
14
|
* @param {NodeJS.ProcessEnv} [env] environment to read (default `process.env`)
|
|
12
|
-
* @returns {
|
|
15
|
+
* @returns {Settings & { profiles?: Record<string, Partial<Pick<Settings, 'mode'|'hosts'|'secrets'>>> }} the file's settings over the defaults
|
|
13
16
|
* @throws {Error} naming the file (and the key) on invalid JSON, an unknown key or a bad value
|
|
14
17
|
*/
|
|
15
|
-
export function loadConfig(env?: NodeJS.ProcessEnv): {
|
|
16
|
-
mode
|
|
17
|
-
holdTimeoutSec?: number | string;
|
|
18
|
-
localLlm: string[];
|
|
19
|
-
labels: string[];
|
|
20
|
-
tenants: Record<string, string[]>;
|
|
21
|
-
heldBodyMaxMB?: number;
|
|
22
|
-
floor?: {
|
|
23
|
-
pay?: string[];
|
|
24
|
-
send?: string[];
|
|
25
|
-
};
|
|
26
|
-
hosts?: {
|
|
27
|
-
allow?: string[];
|
|
28
|
-
deny?: string[];
|
|
29
|
-
unknown?: "ask" | "deny";
|
|
30
|
-
};
|
|
31
|
-
env?: {
|
|
32
|
-
pass?: string[];
|
|
33
|
-
set?: Record<string, string>;
|
|
34
|
-
};
|
|
35
|
-
secrets?: Record<string, {
|
|
36
|
-
fromEnv?: string;
|
|
37
|
-
value?: string;
|
|
38
|
-
hosts: string[];
|
|
39
|
-
}>;
|
|
18
|
+
export function loadConfig(env?: NodeJS.ProcessEnv): Settings & {
|
|
19
|
+
profiles?: Record<string, Partial<Pick<Settings, "mode" | "hosts" | "secrets">>>;
|
|
40
20
|
};
|
|
41
21
|
/**
|
|
42
22
|
* Settings for one `sbx run`: a CLI flag beats the file, the file beats the default.
|
|
23
|
+
* With `flags.profile`, that profile is laid over the top level first: `mode`,
|
|
24
|
+
* `hosts.allow`, `hosts.unknown` and `secrets` are replaced when the profile
|
|
25
|
+
* sets them, `hosts.deny` is the union. The result has no `profiles` key.
|
|
43
26
|
*
|
|
44
27
|
* @param {object} [flags] values given on the command line
|
|
45
28
|
* @param {string} [flags.mode] `--mode`
|
|
29
|
+
* @param {string} [flags.profile] `--profile`: a name under `profiles`
|
|
46
30
|
* @param {string} [flags.hold] `--hold` (`"never"` or a positive integer of seconds, as typed)
|
|
47
31
|
* @param {NodeJS.ProcessEnv} [env] environment to read (default `process.env`)
|
|
48
|
-
* @returns {
|
|
32
|
+
* @returns {Settings} effective settings, no `profiles` key
|
|
49
33
|
* @throws {Error} as {@link loadConfig}
|
|
50
34
|
*/
|
|
51
35
|
export function resolveConfig(flags?: {
|
|
52
36
|
mode?: string | undefined;
|
|
37
|
+
profile?: string | undefined;
|
|
53
38
|
hold?: string | undefined;
|
|
54
|
-
}, env?: NodeJS.ProcessEnv):
|
|
39
|
+
}, env?: NodeJS.ProcessEnv): Settings;
|
|
40
|
+
/**
|
|
41
|
+
* the effective settings of one run (`heldBodyMaxMB` absent means the library default)
|
|
42
|
+
*/
|
|
43
|
+
export type Settings = {
|
|
55
44
|
mode: string;
|
|
56
45
|
holdTimeoutSec?: number | string;
|
|
57
46
|
localLlm: string[];
|
package/types/prompt.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** @typedef {{ id: string, agent?: string, method: string, url: string, rule: string, letter: string, bodyPreview?: string, expiresAt?: string|null, floor?: { kind: string, entry: string }, effect?: import('./effect.js').Effect, keyLast4?: string, modelKey?: 'none'|'other' }} AskRecord */
|
|
1
|
+
/** @typedef {{ id: string, agent?: string, method: string, url: string, rule: string, letter: string, bodyPreview?: string, expiresAt?: string|null, floor?: { kind: string, entry: string }, effect?: import('./effect.js').Effect, keyLast4?: string, modelKey?: 'none'|'other', profile?: string }} AskRecord */
|
|
2
2
|
/**
|
|
3
3
|
* The `To:` / `Amount:` / `Currency:` lines of a floor request, or one `Effect:` line saying why none was read.
|
|
4
4
|
* Up to LIST_MAX fields are all listed; more than that lists none (too many to tell the real one).
|
|
@@ -95,4 +95,5 @@ export type AskRecord = {
|
|
|
95
95
|
effect?: import("./effect.js").Effect;
|
|
96
96
|
keyLast4?: string;
|
|
97
97
|
modelKey?: "none" | "other";
|
|
98
|
+
profile?: string;
|
|
98
99
|
};
|