@capybara-hq/device 0.1.0-rc.18 → 0.1.0-rc.19

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.
Files changed (72) hide show
  1. package/README.md +65 -33
  2. package/dist/api.d.ts +3 -3
  3. package/dist/args.d.ts +8 -3
  4. package/dist/args.js +27 -14
  5. package/dist/cli.js +1 -1
  6. package/dist/client.js +1 -1
  7. package/dist/commands/ceiling.d.ts +3 -2
  8. package/dist/commands/ceiling.js +39 -21
  9. package/dist/commands/login.d.ts +15 -11
  10. package/dist/commands/login.js +46 -33
  11. package/dist/commands/policy.js +16 -10
  12. package/dist/commands/start.d.ts +11 -3
  13. package/dist/commands/start.js +25 -9
  14. package/dist/commands/status.js +10 -6
  15. package/dist/exec/docker-operations.d.ts +14 -2
  16. package/dist/exec/docker-operations.js +32 -5
  17. package/dist/exec/linux-agent.d.ts +13 -3
  18. package/dist/exec/linux-agent.js +450 -43
  19. package/dist/exec/linux-operation-backend.d.ts +27 -6
  20. package/dist/exec/linux-operation-backend.js +30 -9
  21. package/dist/exec/linux-operation-worker.d.ts +51 -3
  22. package/dist/exec/linux-operation-worker.js +55 -3
  23. package/dist/exec/linux-runtime.d.ts +3 -2
  24. package/dist/exec/linux-runtime.js +4 -2
  25. package/dist/exec/mac-operation-backend.d.ts +7 -2
  26. package/dist/exec/mac-operation-backend.js +18 -10
  27. package/dist/exec/mac-write-policy.d.ts +2 -0
  28. package/dist/exec/mac-write-policy.js +1 -1
  29. package/dist/exec/operation-types.d.ts +35 -5
  30. package/dist/exec/operation-types.js +73 -23
  31. package/dist/exec/operation-worker.d.ts +80 -1
  32. package/dist/exec/operation-worker.js +197 -14
  33. package/dist/exec/protected-targets.d.ts +16 -0
  34. package/dist/exec/protected-targets.js +53 -0
  35. package/dist/exec/shared.d.ts +7 -3
  36. package/dist/exec/shared.js +7 -5
  37. package/dist/node_modules/@anthropic-ai/sandbox-runtime/CAPYBARA-MODIFICATIONS.patch +343 -7
  38. package/dist/node_modules/@anthropic-ai/sandbox-runtime/dist/sandbox/resolved-address-guard.js +220 -6
  39. package/dist/node_modules/@anthropic-ai/sandbox-runtime/dist/sandbox/sandbox-config.js +17 -0
  40. package/dist/node_modules/@anthropic-ai/sandbox-runtime/dist/sandbox/sandbox-manager.js +11 -0
  41. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/device-protocol.d.ts +77 -46
  42. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/device-protocol.js +206 -83
  43. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/index.d.ts +3 -2
  44. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/index.js +3 -2
  45. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/network-classes.d.ts +98 -0
  46. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/network-classes.js +291 -0
  47. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-linux-runtime-source.js +460 -28
  48. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-native-runtime-source.js +143 -3
  49. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-operation.d.ts +36 -4
  50. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-operation.js +21 -4
  51. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-runtime-source.js +2 -2
  52. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-write-denies.d.ts +87 -8
  53. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox-write-denies.js +249 -10
  54. package/dist/node_modules/@capybara-hq/plugin-sdk/dist/sandbox.d.ts +11 -22
  55. package/dist/node_modules/@capybara-hq/plugin-sdk/package.json +1 -1
  56. package/dist/owner-report.d.ts +1 -1
  57. package/dist/owner-report.js +5 -4
  58. package/dist/policy-store.d.ts +2 -1
  59. package/dist/policy-store.js +2 -1
  60. package/dist/policy.d.ts +23 -14
  61. package/dist/policy.js +28 -21
  62. package/dist/runtime-manifest.json +1 -1
  63. package/dist/server-ops.d.ts +30 -5
  64. package/dist/server-ops.js +105 -85
  65. package/dist/staged-connect.d.ts +1 -1
  66. package/dist/staged-connect.js +1 -1
  67. package/dist/staged-device.d.ts +1 -1
  68. package/dist/staged-device.js +7 -5
  69. package/dist/staged-session.d.ts +1 -1
  70. package/dist/state.d.ts +41 -21
  71. package/dist/state.js +102 -45
  72. package/package.json +2 -2
@@ -9,13 +9,15 @@ import { resetOwnerReport } from "../owner-report.js";
9
9
  import { hasLoginService } from "../service/installed.js";
10
10
  import { DEFAULT_MAX_FRAME_BYTES, DEFAULT_POLICY, defaultWorkDir, policyPath, readCredential, readPolicy, withPolicyLock, writeCredential, writePolicy, } from "../state.js";
11
11
  /**
12
- * Shown once at login when the owner ceiling allows Full access in an organization that allows
13
- * it: the trust model of Full access is command review, not the OS sandbox.
12
+ * Shown once at login when the owner ceiling allows running outside the sandbox in an organization
13
+ * that allows it: an escalated call's trust model is its approval and the command review, not the
14
+ * OS sandbox.
14
15
  */
15
- export const FULL_ACCESS_RISK_TEXT = "With Full access, agent commands run on this computer as you: they can read and write all " +
16
- "your files, use your credentials (SSH keys, cloud accounts, keychain), Docker and any network, " +
17
- "and the deployment reviews every command before it runs. Prompt injection can make an agent " +
18
- "try to delete files or send your credentials away; the review blocks or asks about the " +
16
+ export const OUTSIDE_SANDBOX_RISK_TEXT = "A call approved to run outside the sandbox runs on this computer as you: it can read and " +
17
+ "write all your files, use your credentials (SSH keys, cloud accounts, keychain), Docker and " +
18
+ "any network. The person, the review model or a Full access auto-grant approves each such " +
19
+ "call, and the deployment reviews every command before it runs. Prompt injection can make an " +
20
+ "agent try to delete files or send your credentials away; the review blocks or asks about the " +
19
21
  "dangerous ones. Such commands can also read this program's own credential: if you suspect " +
20
22
  "misuse, run capybara-device logout and log in again.";
21
23
  async function checkSandbox(check) {
@@ -25,23 +27,32 @@ async function checkSandbox(check) {
25
27
  return sandboxAvailability();
26
28
  }
27
29
  /** How the owner widens the ceiling, on this machine only (the deployment may only narrow it). */
28
- export const WIDEN_CEILING_HINT = "Only you can widen it, on this machine: `capybara-device ceiling set --full-access allow " +
29
- "--project-reach os-user` (see `capybara-device ceiling show`).";
30
+ export const WIDEN_CEILING_HINT = "Only you can widen it, on this machine: `capybara-device ceiling set --outside-sandbox on` " +
31
+ "(see `capybara-device ceiling show`).";
32
+ /** The sandbox network line of the login output. */
33
+ export function networkLoginLine(network) {
34
+ return network === "open"
35
+ ? "Sandboxed commands reach the public internet (private, local and metadata addresses stay " +
36
+ "closed); `capybara-device ceiling set --network allowlist` limits them to allowedDomains."
37
+ : "Sandboxed commands reach only allowedDomains; open the network with " +
38
+ "`capybara-device ceiling set --network open`.";
39
+ }
30
40
  /**
31
- * The line `login` prints for a restricted owner ceiling (the default of a new setup, B-14): no
32
- * Full access, restricted work in the device's workspace only.
41
+ * The line `login` prints when the owner ceiling keeps every call in the sandbox (the default of a
42
+ * new setup): commands run in the sandbox under every profile, Full access included.
33
43
  */
34
- export const RESTRICTED_CEILING_LOGIN_LINE = "This machine's owner ceiling allows restricted work only: Full access is off, and restricted " +
35
- `work reaches the device's workspace. ${WIDEN_CEILING_HINT}`;
36
- /** The line `login` prints when the owner ceiling allows Full access. */
37
- export const FULL_CEILING_LOGIN_LINE = "This machine's owner ceiling allows Full access: a conversation set to Full access runs agent " +
38
- "commands as you. Narrow it with `capybara-device ceiling set --full-access deny " +
39
- "--project-reach workspace`.";
40
- /** Added when the organization does not allow Full access while the owner ceiling does. */
41
- export const FULL_ACCESS_NOT_ALLOWED_LOGIN_LINE = "This organization does not allow Full access, so agents use the restricted profiles here.";
42
- /** The line `login` prints when restricted work cannot run here (no OS sandbox). */
44
+ export const SANDBOX_ONLY_LOGIN_LINE = "This machine's owner ceiling keeps every command in the sandbox, Full access included: no " +
45
+ `call runs outside it. ${WIDEN_CEILING_HINT}`;
46
+ /** The line `login` prints when the owner ceiling allows running outside the sandbox. */
47
+ export const OUTSIDE_SANDBOX_LOGIN_LINE = "This machine's owner ceiling allows running outside the sandbox: commands still run in the " +
48
+ "sandbox, and one approved call may run as you. Turn it off with " +
49
+ "`capybara-device ceiling set --outside-sandbox off`.";
50
+ /** Added when the organization does not allow running outside the sandbox while the owner does. */
51
+ export const OUTSIDE_SANDBOX_NOT_ALLOWED_LOGIN_LINE = "This organization does not allow running outside the sandbox, so every call stays in the " +
52
+ "sandbox here.";
53
+ /** The line `login` prints when sandboxed work cannot run here (no OS sandbox). */
43
54
  export function restrictedUnavailableLoginLine(reason) {
44
- return (`The OS sandbox is unavailable on this machine (${reason}), so restricted work cannot run ` +
55
+ return (`The OS sandbox is unavailable on this machine (${reason}), so sandboxed work cannot run ` +
45
56
  `here yet: ${SANDBOX_INSTALL_HINT}; or log in again with --mode docker. ${WIDEN_CEILING_HINT}`);
46
57
  }
47
58
  const SLOW_DOWN_STEP_SECONDS = 5;
@@ -59,8 +70,8 @@ function defaultOpen(platform) {
59
70
  };
60
71
  }
61
72
  /**
62
- * A new setup writes the default policy (B-14): a restricted owner ceiling, no Full access and the
63
- * workspace as the restricted reach. Written under the policy lock, and only while no policy file
73
+ * A new setup writes the default policy: no running outside the sandbox, the open network and the
74
+ * workspace as the project reach. Written under the policy lock, and only while no policy file
64
75
  * exists: an existing policy is the owner's choice and is never changed here (an old format still
65
76
  * converts exactly at start). Returns whether this call created the file.
66
77
  */
@@ -159,8 +170,8 @@ export async function login(cmd, deps) {
159
170
  : resolve(cmd.dir);
160
171
  // Recreates a lost directory, marker or root: the device starts afresh.
161
172
  prepareDeviceDir(dir);
162
- // A new setup gets the restricted default written before the credential makes the login
163
- // final; the printed lines follow the policy this machine actually has.
173
+ // A new setup gets the default written before the credential makes the login final; the
174
+ // printed lines follow the policy this machine actually has.
164
175
  let ceiling = { ...DEFAULT_POLICY.ownerCeiling };
165
176
  try {
166
177
  persistDefaultPolicy(deps.stateDir);
@@ -174,7 +185,7 @@ export async function login(cmd, deps) {
174
185
  catch (e) {
175
186
  err(`Cannot read the local policy in ${deps.stateDir}: ${e.message}`);
176
187
  }
177
- /** Why restricted work cannot run here: the OS sandbox is missing (OS mode only). */
188
+ /** Why sandboxed work cannot run here: the OS sandbox is missing (OS mode only). */
178
189
  let noSandbox;
179
190
  if (cmd.mode !== "docker") {
180
191
  const available = await checkSandbox(deps.sandboxAvailable);
@@ -207,18 +218,20 @@ export async function login(cmd, deps) {
207
218
  });
208
219
  emit({ event: "done", deviceId: granted.deviceId, orgId: granted.orgId, dir });
209
220
  out(`Logged in. Device ${granted.deviceId}, working directory ${dir}`);
210
- if (ceiling.fullAccess) {
211
- out(FULL_CEILING_LOGIN_LINE);
212
- if (granted.fullAccess === "off")
213
- out(FULL_ACCESS_NOT_ALLOWED_LOGIN_LINE);
214
- // the risk text is shown once at login, and only when Full access runs as the owner on
215
- // this machine (outside Docker)
221
+ if (ceiling.outsideSandbox) {
222
+ out(OUTSIDE_SANDBOX_LOGIN_LINE);
223
+ // the token's `outsideSandbox` is the platform and organization outside-sandbox setting
224
+ if (granted.outsideSandbox === "off")
225
+ out(OUTSIDE_SANDBOX_NOT_ALLOWED_LOGIN_LINE);
226
+ // the risk text is shown once at login, and only when an escalated call runs as the
227
+ // owner on this machine (outside Docker)
216
228
  else if (cmd.mode !== "docker")
217
- out(FULL_ACCESS_RISK_TEXT);
229
+ out(OUTSIDE_SANDBOX_RISK_TEXT);
218
230
  }
219
231
  else {
220
- out(RESTRICTED_CEILING_LOGIN_LINE);
232
+ out(SANDBOX_ONLY_LOGIN_LINE);
221
233
  }
234
+ out(networkLoginLine(ceiling.network));
222
235
  if (noSandbox !== undefined)
223
236
  out(restrictedUnavailableLoginLine(noSandbox));
224
237
  // An installed service stays stopped after a deliberate exit (not logged in, logged out):
@@ -5,10 +5,11 @@ import { policyPath, readCredential } from "../state.js";
5
5
  /** The fields `show` prints and `set` may replace. */
6
6
  const KEYS = ["allowedDomains", "allowCredentials", "cpu", "memoryMb"];
7
7
  /** What OS mode does not enforce yet, shown with `show` and with a limits change. */
8
- export const NOT_ENFORCED_NOTE = "Note: in OS mode cpu and memoryMb are not enforced yet; allowCredentials is ignored (Full " +
9
- "access already has the owner's credentials, restricted work gets none).";
8
+ export const NOT_ENFORCED_NOTE = "Note: in OS mode cpu and memoryMb are not enforced yet; allowCredentials is ignored (a call " +
9
+ "outside the sandbox already has the owner's credentials, sandboxed work gets none).";
10
10
  /** Where the owner ceiling is changed (the policy command never changes it). */
11
- const CEILING_COMMAND = "capybara-device ceiling set --full-access allow|deny --project-reach workspace|os-user";
11
+ const CEILING_COMMAND = "capybara-device ceiling set --outside-sandbox on|off --network open|allowlist " +
12
+ "--project-reach workspace|os-user";
12
13
  function list(value) {
13
14
  return value
14
15
  .split(",")
@@ -68,15 +69,19 @@ function edit(current, action, key, values, out) {
68
69
  }
69
70
  /** What `policy show` says the owner ceiling gives on this machine (English, one line each). */
70
71
  export function describeCeiling(policy, runMode = "os") {
71
- const { fullAccess, restrictedProjectReach } = policy.ownerCeiling;
72
+ const { outsideSandbox, network, restrictedProjectReach } = policy.ownerCeiling;
72
73
  const where = runMode === "docker" ? "in the device's container" : "as you on this machine";
73
74
  return [
74
- fullAccess
75
- ? `Owner ceiling: Full access allowed: a conversation set to Full access runs commands ${where}.`
76
- : "Owner ceiling: Full access denied: conversations here use the restricted profiles only.",
75
+ "Commands run in the sandbox under every profile, Full access included.",
76
+ outsideSandbox
77
+ ? `Owner ceiling: outside the sandbox on: one approved call may run ${where}.`
78
+ : "Owner ceiling: outside the sandbox off: no call runs outside the sandbox here.",
79
+ network === "open"
80
+ ? "Sandbox network: open (the public internet; private, local and metadata addresses stay closed)."
81
+ : "Sandbox network: allowlist (allowedDomains only).",
77
82
  restrictedProjectReach === "os-user"
78
- ? "Restricted work and project browsing may read projects outside the device's workspace."
79
- : "Restricted work and project browsing stay in the device's workspace.",
83
+ ? "Sandboxed work and project browsing may read projects outside the device's workspace."
84
+ : "Sandboxed work and project browsing stay in the device's workspace.",
80
85
  `Change the owner ceiling with: ${CEILING_COMMAND}`,
81
86
  ];
82
87
  }
@@ -176,7 +181,8 @@ export async function policyCommand(cmd, deps) {
176
181
  out(l);
177
182
  out(`Saved ${policyPath(stateDir)}. A running capybara-device applies it at the next operation.`);
178
183
  if (key === "allowedDomains") {
179
- out("Restricted work may reach these domains when the organization allows them too.");
184
+ out("Sandboxed work may reach these domains while the network is an allowlist, when the " +
185
+ "organization allows them too.");
180
186
  }
181
187
  if (key === "cpu" || key === "memoryMb" || key === "allowCredentials")
182
188
  out(NOT_ENFORCED_NOTE);
@@ -3,7 +3,7 @@ import { type ProcessLookup } from "../pid.js";
3
3
  import { PolicyStore } from "../policy-store.js";
4
4
  import { connectStaged } from "../staged-connect.js";
5
5
  import type { StagedDevice } from "../staged-device.js";
6
- import { type Credential } from "../state.js";
6
+ import { type Credential, type OwnerCeilingPolicy } from "../state.js";
7
7
  import { WakeDetector } from "../wake.js";
8
8
  export interface StartDeps {
9
9
  stateDir: string;
@@ -32,7 +32,15 @@ export interface StartDeps {
32
32
  /** How often the policy is re-read and a wake is looked for (default 2 s). */
33
33
  tickMs?: number;
34
34
  }
35
- /** Printed once when start converted an old-format policy.json to version 2. */
36
- export declare function legacyPolicyNotice(mode: "auto" | "sandbox"): string;
35
+ /**
36
+ * Printed once when start converted an old-format policy.json to version 3 (never wider). A device
37
+ * that kept the allowlist learns how to open the network.
38
+ */
39
+ export declare function legacyPolicyNotice(ceiling: OwnerCeilingPolicy): string;
40
+ /**
41
+ * Printed once when start found no policy.json (a device set up before rc.58, whose `login` wrote
42
+ * none) and wrote the closed ceiling it already ran with.
43
+ */
44
+ export declare const MISSING_POLICY_NOTICE: string;
37
45
  /** `start`: connect and serve in the foreground until stopped, drained, revoked or replaced. */
38
46
  export declare function start(deps: StartDeps): Promise<number>;
@@ -8,14 +8,28 @@ import { hasLoginService } from "../service/installed.js";
8
8
  import { rotateServiceLog } from "../service/launchd.js";
9
9
  import { startedByServiceManager } from "../service/manager.js";
10
10
  import { connectStaged } from "../staged-connect.js";
11
- import { convertPolicyFile, DEFAULT_POLICY, policyMode, readCredential, readPolicy, } from "../state.js";
11
+ import { convertPolicyFile, DEFAULT_POLICY, readCredential, readPolicy, } from "../state.js";
12
12
  import { PROGRAM_VERSION } from "../version.js";
13
13
  import { WakeDetector } from "../wake.js";
14
- /** Printed once when start converted an old-format policy.json to version 2. */
15
- export function legacyPolicyNotice(mode) {
16
- return ("policy.json now uses the version 2 owner ceiling; this machine runs in " +
17
- `${mode} mode; see \`capybara-device ceiling show\`.`);
14
+ /**
15
+ * Printed once when start converted an old-format policy.json to version 3 (never wider). A device
16
+ * that kept the allowlist learns how to open the network.
17
+ */
18
+ export function legacyPolicyNotice(ceiling) {
19
+ return ("policy.json now uses the version 3 owner ceiling: commands run in the sandbox under every " +
20
+ `profile; outside the sandbox ${ceiling.outsideSandbox ? "on" : "off"}, network ` +
21
+ `${ceiling.network}; see \`capybara-device ceiling show\`.` +
22
+ (ceiling.network === "allowlist"
23
+ ? " To open the sandbox network: capybara-device ceiling set --network open"
24
+ : ""));
18
25
  }
26
+ /**
27
+ * Printed once when start found no policy.json (a device set up before rc.58, whose `login` wrote
28
+ * none) and wrote the closed ceiling it already ran with.
29
+ */
30
+ export const MISSING_POLICY_NOTICE = "policy.json was missing, so this machine keeps the sandbox it had: no network host allowed and " +
31
+ "no running outside the sandbox (written to policy.json). To open the sandbox network: " +
32
+ "capybara-device ceiling set --network open";
19
33
  /** `start`: connect and serve in the foreground until stopped, drained, revoked or replaced. */
20
34
  export async function start(deps) {
21
35
  const { stateDir, out, err } = deps;
@@ -202,7 +216,7 @@ export async function start(deps) {
202
216
  }
203
217
  }
204
218
  /**
205
- * The start proper (device protocol 3): the policy store, the program's parts (binder,
219
+ * The start proper (device protocol 4): the policy store, the program's parts (binder,
206
220
  * per-generation backends, per-credential session state), recovery of a persisted binding, then
207
221
  * {@link connectStaged}.
208
222
  */
@@ -210,10 +224,12 @@ async function serveStaged(deps, cred, signals) {
210
224
  const { stateDir, out, err } = deps;
211
225
  let device;
212
226
  try {
213
- const converted = convertPolicyFile(stateDir) === "converted";
227
+ const conversion = convertPolicyFile(stateDir);
214
228
  const policy = new PolicyStore(stateDir);
215
- if (converted)
216
- out(legacyPolicyNotice(policyMode(policy.current())));
229
+ if (conversion === "converted")
230
+ out(legacyPolicyNotice(policy.current().ownerCeiling));
231
+ if (conversion === "created")
232
+ out(MISSING_POLICY_NOTICE);
217
233
  device = await deps.createStaged(cred, policy);
218
234
  }
219
235
  catch (e) {
@@ -1,7 +1,7 @@
1
1
  import { queryControl } from "../control/client.js";
2
2
  import { controlUnavailableReason } from "../control/server.js";
3
3
  import { readRunningPid } from "../pid.js";
4
- import { policyMode, readCredential, readPolicy } from "../state.js";
4
+ import { readCredential, readPolicy } from "../state.js";
5
5
  /** How long `status` waits for the running program to answer on its control socket. */
6
6
  export const STATUS_CONTROL_TIMEOUT_MS = 500;
7
7
  /** A light shape check of the runner's answer before it is printed. */
@@ -51,18 +51,19 @@ export async function status(stateDir, json, out, err, opts = {}) {
51
51
  return 1;
52
52
  }
53
53
  // An unreadable policy file is reported by `start` and `policy show`; status still answers.
54
- let sandboxMode;
54
+ let ceiling;
55
55
  try {
56
- sandboxMode = policyMode(readPolicy(stateDir));
56
+ ceiling = readPolicy(stateDir).ownerCeiling;
57
57
  }
58
58
  catch {
59
- sandboxMode = undefined;
59
+ ceiling = undefined;
60
60
  }
61
+ const sandbox = ceiling && { network: ceiling.network, outsideSandbox: ceiling.outsideSandbox };
61
62
  // The credential itself is never printed.
62
63
  const { credential: _secret, ...shown } = cred;
63
64
  const control = await controlState(stateDir, opts);
64
65
  if (json) {
65
- out(JSON.stringify({ loggedIn: true, ...shown, ...(sandboxMode && { sandboxMode }), ...control }));
66
+ out(JSON.stringify({ loggedIn: true, ...shown, ...(sandbox && { sandbox }), ...control }));
66
67
  }
67
68
  else {
68
69
  out([
@@ -71,7 +72,10 @@ export async function status(stateDir, json, out, err, opts = {}) {
71
72
  ` organization: ${cred.orgId}`,
72
73
  ` device: ${cred.deviceId}`,
73
74
  ` directory: ${cred.dir}`,
74
- ` mode: ${cred.mode} | ${sandboxMode ?? "unknown"}`,
75
+ ` mode: ${cred.mode}`,
76
+ ` sandbox: ${sandbox
77
+ ? `network ${sandbox.network}, outside the sandbox ${sandbox.outsideSandbox ? "allowed" : "off"}`
78
+ : "unknown"}`,
75
79
  ` running: ${runningLine(control)}`,
76
80
  ].join("\n"));
77
81
  }
@@ -1,3 +1,4 @@
1
+ import { networkInterfaces } from "node:os";
1
2
  import { type SandboxRoots } from "@capybara-hq/plugin-sdk";
2
3
  import { type LinuxBackendDependencies, type LocalOperationBackend } from "./linux-operation-backend.js";
3
4
  import { type LinuxAgentTransport } from "./linux-runtime.js";
@@ -11,8 +12,19 @@ export interface OperationContainerHost {
11
12
  release: () => void;
12
13
  }>;
13
14
  }
14
- /** A container command's starting environment: the image's ordinary one, never this host's. */
15
- export declare function containerBaseEnv(full: boolean): Record<string, string>;
15
+ /** The device's files lock inside its container (see the worker's `filesLock`). */
16
+ export declare const CONTAINER_FILES_LOCK = "/dev/shm/capybara-device-files.lock";
17
+ /**
18
+ * A container command's starting environment: the image's ordinary one, never this host's (so an
19
+ * escalated call carries none of the device's variables either).
20
+ */
21
+ export declare function containerBaseEnv(escalated: boolean): Record<string, string>;
22
+ /**
23
+ * This host's own interface addresses (loopback aside, which the container's forwarder closes as a
24
+ * class). A container reaches its host through NAT, so the forwarder in the container closes these
25
+ * explicitly: a service on the host is not reachable at the host's public address either.
26
+ */
27
+ export declare function hostAddresses(interfaces?: ReturnType<typeof networkInterfaces>): string[];
16
28
  /**
17
29
  * Agents in the device's local container: one `docker exec` per operation as the container user,
18
30
  * with the agent's source inline (nothing staged in the container). The container stays held
@@ -1,6 +1,7 @@
1
1
  import { spawn } from "node:child_process";
2
2
  import { constants } from "node:fs";
3
3
  import { open, realpath } from "node:fs/promises";
4
+ import { networkInterfaces } from "node:os";
4
5
  import { basename, dirname, join } from "node:path";
5
6
  import { DEFAULT_SANDBOX_ROOTS, SandboxScopeDeniedError, } from "@capybara-hq/plugin-sdk";
6
7
  import { checkLinuxRestrictedGrant, } from "./linux-operation-backend.js";
@@ -13,10 +14,15 @@ import { createCancellationAttempt, } from "./operation-worker.js";
13
14
  import { acquireSealedRuntime, stageRuntimeSource } from "./runtime-source.js";
14
15
  /** The image's interpreter, by absolute path (never looked up through a PATH). */
15
16
  const CONTAINER_PYTHON = "/usr/bin/python3";
17
+ /** The device's files lock inside its container (see the worker's `filesLock`). */
18
+ export const CONTAINER_FILES_LOCK = "/dev/shm/capybara-device-files.lock";
16
19
  const CONTAINER_PATH = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
17
- /** A container command's starting environment: the image's ordinary one, never this host's. */
18
- export function containerBaseEnv(full) {
19
- return full
20
+ /**
21
+ * A container command's starting environment: the image's ordinary one, never this host's (so an
22
+ * escalated call carries none of the device's variables either).
23
+ */
24
+ export function containerBaseEnv(escalated) {
25
+ return escalated
20
26
  ? {
21
27
  PATH: CONTAINER_PATH,
22
28
  LANG: "C.UTF-8",
@@ -26,6 +32,19 @@ export function containerBaseEnv(full) {
26
32
  }
27
33
  : { PATH: CONTAINER_PATH, LANG: "C.UTF-8" };
28
34
  }
35
+ /**
36
+ * This host's own interface addresses (loopback aside, which the container's forwarder closes as a
37
+ * class). A container reaches its host through NAT, so the forwarder in the container closes these
38
+ * explicitly: a service on the host is not reachable at the host's public address either.
39
+ */
40
+ export function hostAddresses(interfaces = networkInterfaces()) {
41
+ const out = [];
42
+ for (const entries of Object.values(interfaces))
43
+ for (const entry of entries ?? [])
44
+ if (!entry.internal)
45
+ out.push(entry.address);
46
+ return out;
47
+ }
29
48
  /**
30
49
  * Ends an agent left inside the container after its `docker exec` client died: the exact process
31
50
  * (PID plus start time, checked again before each signal) gets SIGTERM, then SIGKILL, and this
@@ -229,7 +248,7 @@ dependencies = {}) {
229
248
  let probe = await (dependencies.probe ?? (() => probeLinuxRuntime(transport)))();
230
249
  if (probe.restricted !== null && !dependencies.probe) {
231
250
  // the kernel supports it; the bind mounts must keep Landlock's rules as well
232
- const failure = await checkRestrictedRoot(transport, DEFAULT_SANDBOX_ROOTS.tmp, containerBaseEnv(false));
251
+ const failure = await checkRestrictedRoot(transport, DEFAULT_SANDBOX_ROOTS.tmp, containerBaseEnv(false), CONTAINER_FILES_LOCK);
233
252
  if (failure)
234
253
  probe = { restricted: null, reason: failure };
235
254
  }
@@ -244,9 +263,15 @@ dependencies = {}) {
244
263
  const worker = dependencies.startWorker ??
245
264
  linuxOperationWorker({
246
265
  transport,
266
+ // one container per device holds every operation's agent: its private /dev/shm is the
267
+ // device's, and outside every root a restricted grant can name (workspace, tmp, skills)
268
+ filesLock: CONTAINER_FILES_LOCK,
247
269
  baseEnv: containerBaseEnv,
248
270
  acquireRuntime: acquire,
271
+ closedAddresses: () => hostAddresses(),
249
272
  ...(dependencies.allowPrivateProxyTargets ? { allowPrivateProxyTargets: true } : {}),
273
+ ...(dependencies.fakeIpDnsTest ? { fakeIpDnsTest: dependencies.fakeIpDnsTest } : {}),
274
+ ...(dependencies.faultInjection ? { faultInjection: dependencies.faultInjection } : {}),
250
275
  });
251
276
  const startWorker = async (request, signal) => request.kind === "native" &&
252
277
  request.purpose === "preparation" &&
@@ -264,7 +289,9 @@ dependencies = {}) {
264
289
  }, {
265
290
  restricted: unavailableReason === undefined,
266
291
  ...(unavailableReason ? { unavailableReason } : {}),
267
- fullAccess: probe.agentAvailable !== false,
292
+ // the operation's forwarder enforces the open public network on the resolved address
293
+ openPublic: true,
294
+ outsideSandbox: probe.agentAvailable !== false,
268
295
  background: probe.agentAvailable !== false,
269
296
  pending: unavailableReason ?? "Restricted execution in the device container is unavailable",
270
297
  startWorker,
@@ -12,9 +12,19 @@
12
12
  * base64, at most 64 KiB), `native`, `probe`, `reaped`, `refused`, `exit`.
13
13
  *
14
14
  * - restricted exec: pins every granted root once (no-follow, canonical), runs the SDK launcher on
15
- * those descriptors (Landlock rules, the seccomp broker), and for an allowlist gives it exactly
16
- * one endpoint, a loopback proxy owned by this operation that tunnels only the allowlisted names.
17
- * Whatever ends the launcher, the agent then reaps its closed session (exact SID plus start time).
15
+ * those descriptors (Landlock rules, the seccomp broker), and for an allowlist or the open
16
+ * network gives it exactly one endpoint, a loopback proxy owned by this operation: it tunnels
17
+ * only the allowlisted names, or (open) any name and address literal on any port, and dials only
18
+ * resolved addresses the SDK's address-class table calls public (a copy kept equal by a test),
19
+ * never this host's own nor one of the request's extra closed addresses (a local-Docker
20
+ * device's host), re-resolving a fake-IP resolver's answers through encrypted DNS. A command
21
+ * connects to nothing else, not even its own loopback listener. Whatever ends the launcher, the agent then reaps its closed session (exact SID plus
22
+ * start time). Write denies the device resolved for this operation (protected home locations
23
+ * reached through links) go to the launcher and to native work. The agent opens the device's
24
+ * files lock (a private file at the path the device chose, outside every sandbox) and hands its
25
+ * descriptor to the launcher only, so every operation of the device shares one write lock.
26
+ * A restricted native write takes the same lock in this process (shared, from its check to its
27
+ * effect), and the native helper checks each entry it creates again where it landed.
18
28
  * - Full exec: no restricted filters; the command runs as the OS user in a session of its own, and
19
29
  * the agent ends that session (exact SID plus start time) when it exits, is cancelled or times out.
20
30
  * - native: the SDK native helper on pinned descriptors (descriptor-relative, no-follow), in this