@capybara-hq/device 0.1.0-rc.7 → 0.1.0-rc.9
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/README.md +106 -189
- package/dist/api.d.ts +5 -0
- package/dist/args.d.ts +8 -3
- package/dist/args.js +38 -19
- package/dist/cli.d.ts +7 -5
- package/dist/cli.js +20 -31
- package/dist/client.d.ts +0 -2
- package/dist/client.js +23 -12
- package/dist/command-log.d.ts +3 -5
- package/dist/commands/login.js +55 -1
- package/dist/commands/mode.d.ts +52 -0
- package/dist/commands/mode.js +108 -0
- package/dist/commands/policy.d.ts +11 -65
- package/dist/commands/policy.js +62 -249
- package/dist/commands/service.d.ts +4 -3
- package/dist/commands/service.js +30 -2
- package/dist/commands/start.d.ts +2 -2
- package/dist/commands/start.js +14 -6
- package/dist/commands/status.js +11 -3
- package/dist/exec/docker.d.ts +22 -30
- package/dist/exec/docker.js +81 -69
- package/dist/exec/fs-helper.d.ts +13 -8
- package/dist/exec/fs-helper.js +30 -12
- package/dist/exec/host.d.ts +8 -8
- package/dist/exec/host.js +9 -9
- package/dist/exec/os.d.ts +36 -69
- package/dist/exec/os.js +65 -159
- package/dist/exec/sandbox-messages.d.ts +14 -0
- package/dist/exec/sandbox-messages.js +21 -0
- package/dist/exec/shared.d.ts +21 -36
- package/dist/exec/shared.js +30 -164
- package/dist/exec/switch.d.ts +38 -55
- package/dist/exec/switch.js +58 -108
- package/dist/exec/types.d.ts +8 -18
- package/dist/index.d.ts +1 -1
- package/dist/network.d.ts +7 -7
- package/dist/network.js +11 -18
- package/dist/policy-store.d.ts +7 -8
- package/dist/policy-store.js +11 -15
- package/dist/policy.d.ts +25 -100
- package/dist/policy.js +44 -146
- package/dist/server-ops.d.ts +15 -35
- package/dist/server-ops.js +36 -117
- package/dist/service/launchd.d.ts +11 -1
- package/dist/service/launchd.js +14 -5
- package/dist/state.d.ts +15 -16
- package/dist/state.js +38 -36
- package/package.json +2 -2
- package/dist/proxy-guard.d.ts +0 -58
- package/dist/proxy-guard.js +0 -137
package/dist/commands/policy.js
CHANGED
|
@@ -1,74 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { tierFacts } from "../policy.js";
|
|
1
|
+
import { validateAllowHost } from "@capybara-hq/plugin-sdk";
|
|
3
2
|
import { PolicyStore } from "../policy-store.js";
|
|
4
|
-
import { detectLocalProxy } from "../proxy-guard.js";
|
|
5
3
|
import { policyPath, readCredential } from "../state.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
];
|
|
16
|
-
/** Shown before `set tier open` takes effect (adapted from the network ask-to-allow spec §7). */
|
|
17
|
-
export const OPEN_TIER_RISK = "At tier open, agent commands on this device can reach any address the organization allows " +
|
|
18
|
-
"(any public address if the organization has no limit), including uploading files to " +
|
|
19
|
-
"outside sites. Prompt injection can make an agent do that. Turn it on only on your own " +
|
|
20
|
-
"development machine, with agents and material you trust.";
|
|
21
|
-
/** Printed when a local tier change turned the sandbox back on. */
|
|
22
|
-
export const TIER_CHANGE_SANDBOX_ON = "The tier changed, so the sandbox is back on. To run commands without a sandbox again, set " +
|
|
23
|
-
"tier open and run: capybara-device policy set isolation none";
|
|
24
|
-
/** Shown before `set isolation none` takes effect (spec §5.4). */
|
|
25
|
-
export const UNSANDBOXED_RISK_TEXT = "Without a sandbox, agent commands run on this computer as you: they can read and write all " +
|
|
26
|
-
"your files, use your credentials (SSH keys, cloud accounts, keychain), Docker and any " +
|
|
27
|
-
"network. Prompt injection can make an agent delete files or send your credentials away. " +
|
|
28
|
-
"Turn it on only on your own development machine, with agents and material you fully trust. " +
|
|
29
|
-
"Such commands can also read this program's own credential: if you suspect misuse, run " +
|
|
30
|
-
"capybara-device logout and log in again.";
|
|
31
|
-
const OPEN_TIER = {
|
|
32
|
-
risk: OPEN_TIER_RISK,
|
|
33
|
-
prompt: "Let commands on this device reach any address the organization allows? [y/N] ",
|
|
34
|
-
needsYes: "Nothing changed. To set tier open, run: capybara-device policy set tier open --yes",
|
|
35
|
-
};
|
|
36
|
-
const UNSANDBOXED = {
|
|
37
|
-
risk: UNSANDBOXED_RISK_TEXT,
|
|
38
|
-
prompt: "Run agent commands on this computer without a sandbox? [y/N] ",
|
|
39
|
-
needsYes: "Nothing changed. To run commands without a sandbox, run: " +
|
|
40
|
-
"capybara-device policy set isolation none --yes",
|
|
41
|
-
};
|
|
42
|
-
/** The risk of a `policy` command line, when it is one of the changes that ask first. */
|
|
43
|
-
export function policyRisk(cmd) {
|
|
44
|
-
if (cmd.action !== "set")
|
|
45
|
-
return undefined;
|
|
46
|
-
const value = cmd.value.trim().toLowerCase();
|
|
47
|
-
if (cmd.key === "tier" && value === "open")
|
|
48
|
-
return OPEN_TIER;
|
|
49
|
-
if (cmd.key === "isolation" && value === "none")
|
|
50
|
-
return UNSANDBOXED;
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
/** `set tier open` or `set isolation none` without `--yes`: the changes that ask first. */
|
|
54
|
-
export function needsConfirmation(cmd) {
|
|
55
|
-
return cmd.action === "set" && policyRisk(cmd) !== undefined && cmd.yes !== true;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Asks on a terminal before a change that needs it: prints its risk text and the prompt; true
|
|
59
|
-
* only for an answer of `y` or `yes`.
|
|
60
|
-
*/
|
|
61
|
-
export async function confirmRisk(cmd, deps) {
|
|
62
|
-
const r = policyRisk(cmd);
|
|
63
|
-
if (r === undefined)
|
|
64
|
-
return true;
|
|
65
|
-
deps.out(r.risk);
|
|
66
|
-
const answer = (await deps.ask(r.prompt)).trim().toLowerCase();
|
|
67
|
-
return answer === "y" || answer === "yes";
|
|
68
|
-
}
|
|
69
|
-
/** What the OS mode does not enforce yet, shown with every `show` and `set`. */
|
|
70
|
-
export const NOT_ENFORCED_NOTE = "Note: in OS mode cpu and memoryMb are not enforced yet, and allowCredentials is not used: " +
|
|
71
|
-
"commands get no local credentials.";
|
|
4
|
+
/** The fields `show` prints and `set` may replace. */
|
|
5
|
+
const KEYS = ["allowedDomains", "allowCredentials", "cpu", "memoryMb", "mode"];
|
|
6
|
+
/** What OS mode does not enforce yet, shown with `show` and with a limits change. */
|
|
7
|
+
export const NOT_ENFORCED_NOTE = "Note: in OS mode cpu and memoryMb are not enforced yet; allowCredentials is ignored (auto " +
|
|
8
|
+
"mode already has the owner's credentials, sandbox mode gets none).";
|
|
9
|
+
/** Shown when a Docker-mode device's policy is shown or given allowed domains. */
|
|
10
|
+
export const DOCKER_DOMAIN_NOTE = "Note: this device runs commands in Docker mode, where the mode alone decides network " +
|
|
11
|
+
"access (auto: the default bridge; sandbox: no network). Allowed domains apply in OS mode's " +
|
|
12
|
+
"sandbox only.";
|
|
72
13
|
function list(value) {
|
|
73
14
|
return value
|
|
74
15
|
.split(",")
|
|
@@ -84,40 +25,6 @@ function ceiling(key, value) {
|
|
|
84
25
|
}
|
|
85
26
|
return n;
|
|
86
27
|
}
|
|
87
|
-
/** Shown when a docker-mode device is given an allowed domain. */
|
|
88
|
-
export const DOCKER_NETWORK_NOTE = "Note: this device runs commands in Docker mode, which cannot limit network access per " +
|
|
89
|
-
"domain: while any domain is allowed or the tier is open (and the organization allows " +
|
|
90
|
-
"network), commands reach the whole internet. Use OS mode to allow single domains.";
|
|
91
|
-
/** Shown when a Docker-mode device is told to run commands without a sandbox. */
|
|
92
|
-
export const DOCKER_ISOLATION_REFUSED = "This device runs commands in Docker mode, where they always run in a container; " +
|
|
93
|
-
"isolation none works in OS mode only.";
|
|
94
|
-
/** Refusal of `set isolation none` while the local tier is not explicitly `open`. */
|
|
95
|
-
export const ISOLATION_NEEDS_OPEN = "Nothing changed. Running without a sandbox needs tier open set on this device first: " +
|
|
96
|
-
"capybara-device policy set tier open --yes";
|
|
97
|
-
/** A refusal found under the policy lock, reported as a usage refusal (exit 2). */
|
|
98
|
-
class LockedRefusal extends Error {
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Why `set isolation none` cannot apply on this machine now (Docker mode, or the local tier is
|
|
102
|
-
* not explicitly `open`), checked before any risk text or question; undefined for every other
|
|
103
|
-
* command line. The tier is checked again under the policy lock when the change is written.
|
|
104
|
-
*/
|
|
105
|
-
export function isolationRefusal(cmd, stateDir) {
|
|
106
|
-
if (cmd.action !== "set" || cmd.key !== "isolation")
|
|
107
|
-
return undefined;
|
|
108
|
-
if (cmd.value.trim().toLowerCase() !== "none")
|
|
109
|
-
return undefined;
|
|
110
|
-
if (dockerMode(stateDir))
|
|
111
|
-
return DOCKER_ISOLATION_REFUSED;
|
|
112
|
-
try {
|
|
113
|
-
if (new PolicyStore(stateDir).current().tier !== "open")
|
|
114
|
-
return ISOLATION_NEEDS_OPEN;
|
|
115
|
-
}
|
|
116
|
-
catch {
|
|
117
|
-
// An unreadable policy file is reported by the command itself.
|
|
118
|
-
}
|
|
119
|
-
return undefined;
|
|
120
|
-
}
|
|
121
28
|
/** Whether this machine is logged in in Docker mode (false when unknown). */
|
|
122
29
|
function dockerMode(stateDir) {
|
|
123
30
|
try {
|
|
@@ -160,53 +67,40 @@ function edit(current, action, key, values, out) {
|
|
|
160
67
|
out(has(current, v) ? `Removed ${v} from ${key}.` : `${v} is not in ${key}.`);
|
|
161
68
|
return current.filter((x) => !has(values, x));
|
|
162
69
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
"
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
: "blocked";
|
|
190
|
-
lines.push(`Unix sockets: ${sockets}.`);
|
|
191
|
-
if (facts.effectiveTier !== "strict") {
|
|
192
|
-
lines.push("Package caches are kept in the device directory's cache folder.");
|
|
193
|
-
}
|
|
194
|
-
if (policy.isolation === "none") {
|
|
195
|
-
lines.push("Isolation: none. While the tier is open, commands run directly on this computer as you, " +
|
|
196
|
-
"without a sandbox. To turn the sandbox back on, run: " +
|
|
197
|
-
"capybara-device policy set isolation sandbox");
|
|
198
|
-
}
|
|
199
|
-
return lines;
|
|
70
|
+
/** What `policy show` says the mode gives on this machine (English, one line each). */
|
|
71
|
+
export function describeMode(policy, runMode = "os") {
|
|
72
|
+
if (policy.mode === "auto") {
|
|
73
|
+
if (runMode === "docker") {
|
|
74
|
+
return [
|
|
75
|
+
"Mode: auto. Commands run in the device's container on its default bridge, and the " +
|
|
76
|
+
"platform's file tools reach any path in the container; the deployment reviews every " +
|
|
77
|
+
"command and blocks or asks about dangerous ones.",
|
|
78
|
+
];
|
|
79
|
+
}
|
|
80
|
+
return [
|
|
81
|
+
"Mode: auto. Commands run as you on this machine, with their files, credentials and " +
|
|
82
|
+
"network; the deployment reviews every command and blocks or asks about dangerous ones.",
|
|
83
|
+
"Allowed domains apply in sandbox mode only (capybara-device mode sandbox).",
|
|
84
|
+
];
|
|
85
|
+
}
|
|
86
|
+
if (runMode === "docker") {
|
|
87
|
+
return [
|
|
88
|
+
"Mode: sandbox. Commands run in the device's container with no network; only the " +
|
|
89
|
+
"workspace, skills and temporary files are writable.",
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
return [
|
|
93
|
+
"Mode: sandbox. Only the workspace, skills and temporary files are writable; the network " +
|
|
94
|
+
"reaches the allowed hosts.",
|
|
95
|
+
];
|
|
200
96
|
}
|
|
201
97
|
/**
|
|
202
|
-
* `policy show` prints the local policy and what its
|
|
203
|
-
* `policy set <key> <value>` replaces one field
|
|
204
|
-
*
|
|
205
|
-
*
|
|
206
|
-
*
|
|
207
|
-
*
|
|
208
|
-
* (narrowing always; never `isolation none`). Writes take policy.json.lock, and a running program
|
|
209
|
-
* applies the change at its next command.
|
|
98
|
+
* `policy show` prints the local policy and what its mode gives on this machine;
|
|
99
|
+
* `policy set <key> <value>` replaces one field; `policy add|remove <key> <value>` adds or removes
|
|
100
|
+
* entries of a list field. The mode is set with `capybara-device mode`. The machine's user edits
|
|
101
|
+
* this file; the deployment changes it only through the `policy` op (narrowing only: hosts and
|
|
102
|
+
* `mode: "sandbox"`, never auto). Writes take policy.json.lock, and a running program applies the
|
|
103
|
+
* change at its next command.
|
|
210
104
|
*/
|
|
211
105
|
export async function policyCommand(cmd, deps) {
|
|
212
106
|
const { stateDir, out, err } = deps;
|
|
@@ -218,17 +112,14 @@ export async function policyCommand(cmd, deps) {
|
|
|
218
112
|
err(`Cannot read ${policyPath(stateDir)}: ${e.message}`);
|
|
219
113
|
return 1;
|
|
220
114
|
}
|
|
115
|
+
const docker = dockerMode(stateDir);
|
|
221
116
|
if (cmd.action === "show") {
|
|
222
117
|
const policy = store.current();
|
|
223
118
|
out(JSON.stringify(policy, null, 2));
|
|
224
|
-
const
|
|
225
|
-
const facts = tierFacts(policy, undefined, {
|
|
226
|
-
...(deps.platform !== undefined && { platform: deps.platform }),
|
|
227
|
-
proxyDetected: proxy.detected,
|
|
228
|
-
mode: dockerMode(stateDir) ? "docker" : "os",
|
|
229
|
-
});
|
|
230
|
-
for (const l of describeTier(policy, facts, proxy))
|
|
119
|
+
for (const l of describeMode(policy, docker ? "docker" : "os"))
|
|
231
120
|
out(l);
|
|
121
|
+
if (docker)
|
|
122
|
+
out(DOCKER_DOMAIN_NOTE);
|
|
232
123
|
out(NOT_ENFORCED_NOTE);
|
|
233
124
|
return 0;
|
|
234
125
|
}
|
|
@@ -254,70 +145,18 @@ export async function policyCommand(cmd, deps) {
|
|
|
254
145
|
change = (p) => ({ ...p, [key]: next });
|
|
255
146
|
break;
|
|
256
147
|
}
|
|
257
|
-
case "
|
|
258
|
-
|
|
259
|
-
change = (p, say) => {
|
|
260
|
-
const { tier: old, ...rest } = p;
|
|
261
|
-
// Any tier change turns the sandbox back on (spec §5.4: running without one starts
|
|
262
|
-
// only with a confirmed set isolation none, never by a later tier change).
|
|
263
|
-
const isolation = p.isolation === "none" && next !== old ? "sandbox" : p.isolation;
|
|
264
|
-
if (isolation !== p.isolation)
|
|
265
|
-
say(TIER_CHANGE_SANDBOX_ON);
|
|
266
|
-
return next === undefined ? { ...rest, isolation } : { ...rest, tier: next, isolation };
|
|
267
|
-
};
|
|
268
|
-
break;
|
|
269
|
-
}
|
|
270
|
-
case "isolation": {
|
|
271
|
-
const v = value.trim().toLowerCase();
|
|
272
|
-
if (v !== "sandbox" && v !== "none") {
|
|
273
|
-
throw new Error(`isolation must be sandbox or none, got "${value}"`);
|
|
274
|
-
}
|
|
275
|
-
if (v === "none" && dockerMode(stateDir)) {
|
|
276
|
-
err(DOCKER_ISOLATION_REFUSED);
|
|
277
|
-
return 2;
|
|
278
|
-
}
|
|
279
|
-
// Only an explicit local tier open: the org's default can change under the device.
|
|
280
|
-
if (v === "none" && store.current().tier !== "open") {
|
|
281
|
-
err(ISOLATION_NEEDS_OPEN);
|
|
282
|
-
return 2;
|
|
283
|
-
}
|
|
284
|
-
change = (p) => {
|
|
285
|
-
// Judged again on the file as it is under the lock (the tier may have changed).
|
|
286
|
-
if (v === "none" && p.tier !== "open")
|
|
287
|
-
throw new LockedRefusal(ISOLATION_NEEDS_OPEN);
|
|
288
|
-
return { ...p, isolation: v };
|
|
289
|
-
};
|
|
290
|
-
break;
|
|
291
|
-
}
|
|
292
|
-
case "localhost": {
|
|
293
|
-
const v = value.trim().toLowerCase();
|
|
294
|
-
if (v !== "auto" && v !== "allow") {
|
|
295
|
-
throw new Error(`localhost must be auto or allow, got "${value}"`);
|
|
296
|
-
}
|
|
297
|
-
change = (p) => ({ ...p, localhost: v });
|
|
298
|
-
break;
|
|
299
|
-
}
|
|
300
|
-
case "remoteGrants": {
|
|
301
|
-
if (value !== "owner" && value !== "off") {
|
|
302
|
-
throw new Error(`remoteGrants must be owner or off, got "${value}"`);
|
|
303
|
-
}
|
|
304
|
-
change = (p) => ({ ...p, remoteGrants: value });
|
|
305
|
-
break;
|
|
306
|
-
}
|
|
148
|
+
case "mode":
|
|
149
|
+
throw new Error("the mode is set with: capybara-device mode auto|sandbox");
|
|
307
150
|
case "network":
|
|
308
|
-
|
|
151
|
+
case "tier":
|
|
152
|
+
case "isolation":
|
|
153
|
+
case "localhost":
|
|
154
|
+
case "remoteGrants":
|
|
155
|
+
throw new Error(`"${key}" is gone: the mode is set with capybara-device mode auto|sandbox; ` +
|
|
156
|
+
`allowedDomains with capybara-device policy add allowedDomains <host>`);
|
|
309
157
|
default:
|
|
310
158
|
throw new Error(`unknown policy key "${key}"; one of ${KEYS.join(", ")}`);
|
|
311
159
|
}
|
|
312
|
-
// Widening to tier open or isolation none needs a confirmation; narrowing never does.
|
|
313
|
-
const risk = policyRisk(cmd);
|
|
314
|
-
if (risk !== undefined && deps.confirmed !== true) {
|
|
315
|
-
out(risk.risk);
|
|
316
|
-
if (cmd.yes !== true) {
|
|
317
|
-
err(risk.needsYes);
|
|
318
|
-
return 2;
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
160
|
}
|
|
322
161
|
else {
|
|
323
162
|
if (!LIST_KEYS.includes(key)) {
|
|
@@ -345,46 +184,20 @@ export async function policyCommand(cmd, deps) {
|
|
|
345
184
|
next = store.update((p) => change(p, (l) => lines.push(l)));
|
|
346
185
|
}
|
|
347
186
|
catch (e) {
|
|
348
|
-
if (e instanceof LockedRefusal) {
|
|
349
|
-
err(e.message);
|
|
350
|
-
return 2;
|
|
351
|
-
}
|
|
352
187
|
err(`Cannot change ${policyPath(stateDir)}: ${e.message}`);
|
|
353
188
|
return 1;
|
|
354
189
|
}
|
|
355
190
|
for (const l of lines)
|
|
356
191
|
out(l);
|
|
357
192
|
out(`Saved ${policyPath(stateDir)}. A running capybara-device applies it at the next command.`);
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
: `This device now asks for tier ${next.tier}; the organization's ceiling may lower it.`);
|
|
367
|
-
if (next.tier === "open" && docker)
|
|
368
|
-
out(DOCKER_NETWORK_NOTE);
|
|
369
|
-
}
|
|
370
|
-
if (key === "isolation") {
|
|
371
|
-
out(next.isolation === "none"
|
|
372
|
-
? "While the tier is open, commands run on this computer without a sandbox."
|
|
373
|
-
: "Commands run in the sandbox.");
|
|
374
|
-
}
|
|
375
|
-
if (key === "localhost") {
|
|
376
|
-
out(next.localhost === "allow"
|
|
377
|
-
? "At tier dev or open, commands can reach this machine's localhost even when a local " +
|
|
378
|
-
"proxy runs (it may forward past the network allowlist)."
|
|
379
|
-
: "At tier dev or open, commands reach localhost unless a local proxy is detected.");
|
|
380
|
-
}
|
|
381
|
-
if (key === "remoteGrants") {
|
|
382
|
-
out(value === "owner"
|
|
383
|
-
? "The device owner can now widen this device's network from the web or a chat card."
|
|
384
|
-
: "The deployment can no longer widen this device's network; it can still narrow it.");
|
|
385
|
-
}
|
|
386
|
-
if (!["allowedDomains", "tier", "isolation", "localhost", "remoteGrants"].includes(key)) {
|
|
193
|
+
if (key === "allowedDomains") {
|
|
194
|
+
out(docker
|
|
195
|
+
? DOCKER_DOMAIN_NOTE
|
|
196
|
+
: next.mode === "auto"
|
|
197
|
+
? "Allowed domains apply in sandbox mode only; switch with: capybara-device mode sandbox"
|
|
198
|
+
: "Sandboxed commands may reach these domains when the organization allows them too.");
|
|
199
|
+
}
|
|
200
|
+
if (key === "cpu" || key === "memoryMb" || key === "allowCredentials")
|
|
387
201
|
out(NOT_ENFORCED_NOTE);
|
|
388
|
-
}
|
|
389
202
|
return 0;
|
|
390
203
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export type ServiceAction = "install" | "uninstall" | "status";
|
|
1
2
|
export interface ServiceDeps {
|
|
2
3
|
stateDir: string;
|
|
3
4
|
home: string;
|
|
@@ -25,7 +26,7 @@ export declare const SERVICE_GONE_TIMEOUT_MS = 10000;
|
|
|
25
26
|
/** The waits before each retry of a `launchctl bootstrap` that failed with error 5. */
|
|
26
27
|
export declare const BOOTSTRAP_BACKOFF_MS: readonly [500, 1000, 2000];
|
|
27
28
|
/**
|
|
28
|
-
* `service install|uninstall`: runs `capybara-device start` at login (launchd) or boot
|
|
29
|
-
* user).
|
|
29
|
+
* `service install|uninstall|status`: runs `capybara-device start` at login (launchd) or boot
|
|
30
|
+
* (systemd user), removes it, or reports whether it is loaded and running.
|
|
30
31
|
*/
|
|
31
|
-
export declare function serviceCommand(action:
|
|
32
|
+
export declare function serviceCommand(action: ServiceAction, deps: ServiceDeps): Promise<number>;
|
package/dist/commands/service.js
CHANGED
|
@@ -2,6 +2,7 @@ import { execFileSync } from "node:child_process";
|
|
|
2
2
|
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
3
3
|
import { dirname } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { readRunningPid } from "../pid.js";
|
|
5
6
|
import { LAUNCHD_LABEL, plistContent, plistPath } from "../service/launchd.js";
|
|
6
7
|
import { SYSTEMD_UNIT, unitContent, unitPath } from "../service/systemd.js";
|
|
7
8
|
function defaultRun(cmd, args) {
|
|
@@ -28,8 +29,8 @@ export const SERVICE_GONE_TIMEOUT_MS = 10_000;
|
|
|
28
29
|
/** The waits before each retry of a `launchctl bootstrap` that failed with error 5. */
|
|
29
30
|
export const BOOTSTRAP_BACKOFF_MS = [500, 1000, 2000];
|
|
30
31
|
/**
|
|
31
|
-
* `service install|uninstall`: runs `capybara-device start` at login (launchd) or boot
|
|
32
|
-
* user).
|
|
32
|
+
* `service install|uninstall|status`: runs `capybara-device start` at login (launchd) or boot
|
|
33
|
+
* (systemd user), removes it, or reports whether it is loaded and running.
|
|
33
34
|
*/
|
|
34
35
|
export async function serviceCommand(action, deps) {
|
|
35
36
|
const run = deps.run ?? defaultRun;
|
|
@@ -65,6 +66,20 @@ function failureText(e) {
|
|
|
65
66
|
function isBootstrapIoError(e) {
|
|
66
67
|
return e.status === 5 || /Bootstrap failed: 5:/.test(failureText(e));
|
|
67
68
|
}
|
|
69
|
+
/** One status report, once the caller knows whether launchd/systemd has the service loaded. */
|
|
70
|
+
function reportStatus(deps, loaded) {
|
|
71
|
+
if (!loaded) {
|
|
72
|
+
deps.out("capybara-device service is not loaded. Install it: capybara-device service install");
|
|
73
|
+
return 1;
|
|
74
|
+
}
|
|
75
|
+
const pid = readRunningPid(deps.stateDir);
|
|
76
|
+
if (pid === undefined) {
|
|
77
|
+
deps.out("capybara-device service is loaded, but no capybara-device process is running.");
|
|
78
|
+
return 1;
|
|
79
|
+
}
|
|
80
|
+
deps.out(`capybara-device service is loaded; capybara-device is running (pid ${pid}).`);
|
|
81
|
+
return 0;
|
|
82
|
+
}
|
|
68
83
|
async function launchd(action, spec, deps, run) {
|
|
69
84
|
const file = plistPath(deps.home);
|
|
70
85
|
const domain = `gui/${deps.uid ?? process.getuid?.() ?? 501}`;
|
|
@@ -88,6 +103,8 @@ async function launchd(action, spec, deps, run) {
|
|
|
88
103
|
return false;
|
|
89
104
|
}
|
|
90
105
|
};
|
|
106
|
+
if (action === "status")
|
|
107
|
+
return reportStatus(deps, loaded());
|
|
91
108
|
/** Boots the service out and waits until launchd has let it go (bootout returns before that). */
|
|
92
109
|
const bootout = async () => {
|
|
93
110
|
tryRun(["bootout", service]);
|
|
@@ -133,6 +150,17 @@ async function launchd(action, spec, deps, run) {
|
|
|
133
150
|
}
|
|
134
151
|
function systemd(action, spec, deps, run) {
|
|
135
152
|
const file = unitPath(deps.home);
|
|
153
|
+
if (action === "status") {
|
|
154
|
+
let active = false;
|
|
155
|
+
try {
|
|
156
|
+
run("systemctl", ["--user", "is-active", "--quiet", SYSTEMD_UNIT]);
|
|
157
|
+
active = true;
|
|
158
|
+
}
|
|
159
|
+
catch {
|
|
160
|
+
// Inactive or not installed.
|
|
161
|
+
}
|
|
162
|
+
return reportStatus(deps, active);
|
|
163
|
+
}
|
|
136
164
|
if (action === "install") {
|
|
137
165
|
mkdirSync(dirname(file), { recursive: true });
|
|
138
166
|
writeFileSync(file, unitContent(spec));
|
package/dist/commands/start.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface StartDeps {
|
|
|
16
16
|
kill?: typeof process.kill;
|
|
17
17
|
connect?: typeof connect;
|
|
18
18
|
}
|
|
19
|
-
/** Printed
|
|
20
|
-
export declare
|
|
19
|
+
/** Printed once when policy.json was written before auto mode (spec §4.4). */
|
|
20
|
+
export declare function legacyPolicyNotice(mode: "auto" | "sandbox"): string;
|
|
21
21
|
/** `start`: connect and serve in the foreground until stopped, drained, revoked or replaced. */
|
|
22
22
|
export declare function start(deps: StartDeps): Promise<number>;
|
package/dist/commands/start.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import { connect } from "../client.js";
|
|
2
2
|
import { CommandLog } from "../command-log.js";
|
|
3
|
+
import { sandboxUnavailableMessage } from "../exec/sandbox-messages.js";
|
|
3
4
|
import { readRunningPid, removePid, writePid } from "../pid.js";
|
|
4
5
|
import { PolicyStore } from "../policy-store.js";
|
|
5
6
|
import { rotateServiceLog } from "../service/launchd.js";
|
|
6
7
|
import { readCredential } from "../state.js";
|
|
7
|
-
/** Printed
|
|
8
|
-
export
|
|
9
|
-
"
|
|
10
|
-
|
|
8
|
+
/** Printed once when policy.json was written before auto mode (spec §4.4). */
|
|
9
|
+
export function legacyPolicyNotice(mode) {
|
|
10
|
+
return ("policy.json now uses `mode`; this machine runs in " +
|
|
11
|
+
`${mode} mode; switch with \`capybara-device mode\`.`);
|
|
12
|
+
}
|
|
11
13
|
/** `start`: connect and serve in the foreground until stopped, drained, revoked or replaced. */
|
|
12
14
|
export async function start(deps) {
|
|
13
15
|
const { stateDir, out, err } = deps;
|
|
@@ -58,9 +60,15 @@ export async function start(deps) {
|
|
|
58
60
|
try {
|
|
59
61
|
// An invalid policy.json stops the start; later, a broken edit keeps the last good policy.
|
|
60
62
|
policy = new PolicyStore(stateDir, { err });
|
|
61
|
-
if (policy.
|
|
62
|
-
out(
|
|
63
|
+
if (policy.legacy)
|
|
64
|
+
out(legacyPolicyNotice(policy.current().mode));
|
|
63
65
|
executor = await deps.createExecutor(cred, policy);
|
|
66
|
+
// Without the OS sandbox the device keeps serving: auto mode works, and sandbox mode
|
|
67
|
+
// refuses every command with this reason. A policy set to sandbox says so at start.
|
|
68
|
+
const sandbox = executor;
|
|
69
|
+
if (sandbox.available === false && policy.current().mode === "sandbox") {
|
|
70
|
+
err(sandboxUnavailableMessage(sandbox.unavailableReason ?? ""));
|
|
71
|
+
}
|
|
64
72
|
}
|
|
65
73
|
catch (e) {
|
|
66
74
|
err(`Cannot start: ${e.message}`);
|
package/dist/commands/status.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readCredential } from "../state.js";
|
|
1
|
+
import { readCredential, readPolicy } from "../state.js";
|
|
2
2
|
/** `status`: exit 0 when logged in, 1 when not (or the credential file is unusable). */
|
|
3
3
|
export function status(stateDir, json, out, err) {
|
|
4
4
|
let cred;
|
|
@@ -13,10 +13,18 @@ export function status(stateDir, json, out, err) {
|
|
|
13
13
|
out(json ? JSON.stringify({ loggedIn: false }) : "Not logged in.");
|
|
14
14
|
return 1;
|
|
15
15
|
}
|
|
16
|
+
// An unreadable policy file is reported by `start` and `policy show`; status still answers.
|
|
17
|
+
let sandboxMode;
|
|
18
|
+
try {
|
|
19
|
+
sandboxMode = readPolicy(stateDir).mode;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
sandboxMode = undefined;
|
|
23
|
+
}
|
|
16
24
|
// The credential itself is never printed.
|
|
17
25
|
const { credential: _secret, ...shown } = cred;
|
|
18
26
|
if (json) {
|
|
19
|
-
out(JSON.stringify({ loggedIn: true, ...shown }));
|
|
27
|
+
out(JSON.stringify({ loggedIn: true, ...shown, ...(sandboxMode && { sandboxMode }) }));
|
|
20
28
|
}
|
|
21
29
|
else {
|
|
22
30
|
out([
|
|
@@ -25,7 +33,7 @@ export function status(stateDir, json, out, err) {
|
|
|
25
33
|
` organization: ${cred.orgId}`,
|
|
26
34
|
` device: ${cred.deviceId}`,
|
|
27
35
|
` directory: ${cred.dir}`,
|
|
28
|
-
` mode: ${cred.mode}`,
|
|
36
|
+
` mode: ${cred.mode} | ${sandboxMode ?? "unknown"}`,
|
|
29
37
|
].join("\n"));
|
|
30
38
|
}
|
|
31
39
|
return 0;
|
package/dist/exec/docker.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { type DeviceExecArgs, type ExecResult, type SandboxRoots
|
|
2
|
-
import { type EgressLimits, type TierFacts } from "../policy.js";
|
|
1
|
+
import { type DeviceExecArgs, type ExecResult, type SandboxRoots } from "@capybara-hq/plugin-sdk";
|
|
3
2
|
import type { Policy } from "../state.js";
|
|
4
3
|
import { type Executor } from "./types.js";
|
|
5
4
|
/** The version of the published sandbox images this program runs commands in. */
|
|
@@ -11,20 +10,6 @@ export declare const SANDBOX_IMAGE = "ghcr.io/capybara-hq/capybara-sandbox:0.1.0
|
|
|
11
10
|
* Docker Desktop (macOS) maps file ownership itself, so the image's user is used there.
|
|
12
11
|
*/
|
|
13
12
|
export declare function containerUser(platform?: NodeJS.Platform, uid?: number | undefined, gid?: number | undefined): string;
|
|
14
|
-
/**
|
|
15
|
-
* The container's `--shm-size` for a tier (spec §5.2): `strict` keeps Docker's default (64 MiB);
|
|
16
|
-
* `dev` and `open` get a quarter of the memory ceiling up to 1 GiB (at least the default), or
|
|
17
|
-
* 1 GiB without a ceiling, so browsers have room.
|
|
18
|
-
*/
|
|
19
|
-
export declare function shmSize(tier: SandboxTier, memoryMb: number | null): string | undefined;
|
|
20
|
-
/**
|
|
21
|
-
* Whether a command gets the default bridge (spec §5.2 table). Docker mode has no domain filter,
|
|
22
|
-
* and the bridge reaches the host and the intranet, so only an explicit choice opens it: the
|
|
23
|
-
* device's own allowed domains within the ceiling (as `strict` computes them; the dev hosts do
|
|
24
|
-
* not count), or tier `open` with a ceiling that allows any host (an allowlist ceiling at `open`
|
|
25
|
-
* counts only its explicit local domains, like the lower tiers).
|
|
26
|
-
*/
|
|
27
|
-
export declare function bridgeNetwork(policy: Policy, limits: EgressLimits): boolean;
|
|
28
13
|
/** Container name of a device. */
|
|
29
14
|
export declare function containerName(deviceId: string): string;
|
|
30
15
|
/** Whether the docker CLI can reach a daemon, and why not. */
|
|
@@ -39,9 +24,8 @@ export interface DockerExecutorOptions {
|
|
|
39
24
|
dir: string;
|
|
40
25
|
deviceId: string;
|
|
41
26
|
/**
|
|
42
|
-
* The local policy now. The network decision reads it at each command; cpu
|
|
43
|
-
*
|
|
44
|
-
* idle).
|
|
27
|
+
* The local policy now. The network decision reads it at each command; cpu and memory apply
|
|
28
|
+
* when the container is next created (at the next command that finds it idle).
|
|
45
29
|
*/
|
|
46
30
|
policy: () => Policy;
|
|
47
31
|
}
|
|
@@ -51,10 +35,10 @@ export interface DockerExecutorOptions {
|
|
|
51
35
|
* /workspace, /skills and /tmp. Workspace and tmp files are read and written inside the container
|
|
52
36
|
* (as the `sandbox` user, so a link a command plants resolves in the container and never on the
|
|
53
37
|
* host); the skills directory, which commands cannot write, is accessed from the host with the
|
|
54
|
-
* same real-path checks as the OS executor.
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* at the next command that finds it idle.
|
|
38
|
+
* same real-path checks as the OS executor. Auto mode: the container on its default bridge, and
|
|
39
|
+
* file operations reach any absolute path in the container. Sandbox mode: no network attached and
|
|
40
|
+
* file operations stay in the mounted roots. Docker mode has no per-domain filter. A change of the
|
|
41
|
+
* container's flags recreates it at the next command that finds it idle.
|
|
58
42
|
*/
|
|
59
43
|
export declare class DockerExecutor implements Executor {
|
|
60
44
|
private readonly opts;
|
|
@@ -76,8 +60,10 @@ export declare class DockerExecutor implements Executor {
|
|
|
76
60
|
private readonly queue;
|
|
77
61
|
private pumping;
|
|
78
62
|
private dirty;
|
|
79
|
-
/** The limits of the latest command:
|
|
63
|
+
/** The limits of the latest command: the kernel's mode ceiling for file operations. */
|
|
80
64
|
private lastLimits;
|
|
65
|
+
/** When {@link lastLimits} came (file operations follow it only inside the window). */
|
|
66
|
+
private lastLimitsAt;
|
|
81
67
|
/** The spec hash of the container in use (set when it is created or reused). */
|
|
82
68
|
private spec;
|
|
83
69
|
constructor(opts: DockerExecutorOptions);
|
|
@@ -86,10 +72,13 @@ export declare class DockerExecutor implements Executor {
|
|
|
86
72
|
private serial;
|
|
87
73
|
private inspect;
|
|
88
74
|
private wanted;
|
|
89
|
-
/**
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
75
|
+
/**
|
|
76
|
+
* The mode file operations use now. File operations carry no limits and never pass command
|
|
77
|
+
* review, so they use the sandboxed result before any exec and once the latest decision is stale
|
|
78
|
+
* (the safe default for an organization whose ceiling is sandbox), else
|
|
79
|
+
* `effectiveMode(policy, limits)`.
|
|
80
|
+
*/
|
|
81
|
+
private modeNow;
|
|
93
82
|
/** Every `docker run` flag of the container except its name and spec label. */
|
|
94
83
|
private runArgs;
|
|
95
84
|
/** A hash of the run flags: a container made with other flags is recreated. */
|
|
@@ -100,7 +89,7 @@ export declare class DockerExecutor implements Executor {
|
|
|
100
89
|
/**
|
|
101
90
|
* Creates the container, or reuses the existing one when it matches; call inside `serial`.
|
|
102
91
|
* A file operation (`anySpec`) reuses any container that sees the directory: it needs no
|
|
103
|
-
* particular flags, and before the first command the
|
|
92
|
+
* particular flags, and before the first command the policy is not known yet. The next command
|
|
104
93
|
* that finds it idle recreates it if its flags differ.
|
|
105
94
|
*/
|
|
106
95
|
private ensureContainer;
|
|
@@ -125,7 +114,10 @@ export declare class DockerExecutor implements Executor {
|
|
|
125
114
|
private noteFailure;
|
|
126
115
|
exec(args: DeviceExecArgs, signal: AbortSignal): Promise<ExecResult>;
|
|
127
116
|
private run;
|
|
128
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* `path` made absolute and normalized. Auto mode accepts any clean absolute path (the container
|
|
119
|
+
* is the boundary); sandbox mode refuses anything outside the mounted roots.
|
|
120
|
+
*/
|
|
129
121
|
private containerPath;
|
|
130
122
|
/** The host path of a /skills path (the skills directory is mounted read only). */
|
|
131
123
|
private skillsHostPath;
|