@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/exec/switch.js
CHANGED
|
@@ -1,76 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { deviceTier } from "../policy.js";
|
|
1
|
+
import { effectiveMode } from "../policy.js";
|
|
3
2
|
import { DeviceOpError } from "./types.js";
|
|
4
3
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Decides how one command runs (spec §5.4). Without a sandbox only when the local policy says
|
|
14
|
-
* `isolation: "none"` with its own tier `open` ({@link policyUnsandboxed}), the device runs in OS
|
|
15
|
-
* mode, the kernel allows it for this command (`limits.allowUnsandboxed`: the platform and the
|
|
16
|
-
* org allow it and an unattended run has its grant; absent counts as not allowed), and the
|
|
17
|
-
* command's tier (that choice cut to the org's ceiling) is `open`. When the policy says `none`
|
|
18
|
-
* but any of those does not hold, the command is refused, never run in the sandbox silently: the
|
|
19
|
-
* owner asked for no sandbox. The device enforces the kernel's decision itself, because the
|
|
20
|
-
* kernel knows the local policy only from the last hello or policy op (spec §4.3); its refusal is
|
|
21
|
-
* {@link UNSANDBOXED_NOT_ALLOWED_MESSAGE}, which the kernel maps to its own error. Docker mode
|
|
22
|
-
* ignores `isolation` (its container is the isolation).
|
|
23
|
-
*/
|
|
24
|
-
export function execIsolation(policy, limits, mode) {
|
|
25
|
-
const tier = deviceTier(policy, limits);
|
|
26
|
-
if (mode !== "os" || policy.isolation !== "none")
|
|
27
|
-
return { tier, isolation: "sandbox" };
|
|
28
|
-
if (policy.tier !== "open") {
|
|
29
|
-
return { tier, isolation: "none", refusal: unsandboxedRefusal(policy, tier) };
|
|
30
|
-
}
|
|
31
|
-
if (limits.allowUnsandboxed !== true) {
|
|
32
|
-
return { tier, isolation: "none", refusal: UNSANDBOXED_NOT_ALLOWED_MESSAGE };
|
|
33
|
-
}
|
|
34
|
-
if (tier === "open")
|
|
35
|
-
return { tier, isolation: "none" };
|
|
36
|
-
return { tier, isolation: "none", refusal: unsandboxedRefusal(policy, tier) };
|
|
37
|
-
}
|
|
38
|
-
/** The refusal of a command the policy asks to run without a sandbox below tier `open`. */
|
|
39
|
-
export function unsandboxedRefusal(policy, tier) {
|
|
40
|
-
if (policy.tier !== "open") {
|
|
41
|
-
const own = policy.tier === undefined
|
|
42
|
-
? "its tier is not set on this machine (it follows the organization's default)"
|
|
43
|
-
: `its tier is ${policy.tier}`;
|
|
44
|
-
return (`This device is set to run commands without a sandbox, but ${own}; running without a ` +
|
|
45
|
-
"sandbox needs tier open set on this machine. Run capybara-device policy set tier open, " +
|
|
46
|
-
"or turn the sandbox back on with capybara-device policy set isolation sandbox.");
|
|
47
|
-
}
|
|
48
|
-
return ("This device is set to run commands without a sandbox, but the organization allows at most " +
|
|
49
|
-
`the ${tier} tier, and running without a sandbox needs tier open. The device's owner can ` +
|
|
50
|
-
"turn the sandbox back on with capybara-device policy set isolation sandbox.");
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* How long the latest command's limits decide file operations. The kernel judges the org's
|
|
54
|
-
* settings at each exec, and file operations carry no limits, so an unsandboxed decision older
|
|
55
|
-
* than this no longer counts: file operations fall back to the sandboxed helper, which reads and
|
|
56
|
-
* writes the same roots (only slower), until the next command decides again. A minute covers a
|
|
57
|
-
* turn's file operations around its commands while bounding how long an org's change (or a lower
|
|
58
|
-
* ceiling) takes to reach a device that runs no more commands.
|
|
4
|
+
* How long the latest command's limits decide file operations. The kernel judges the mode at each
|
|
5
|
+
* exec, and file operations carry no limits, so a decision older than this no longer counts: file
|
|
6
|
+
* operations fall back to the sandboxed helper (the safe default for an organization whose ceiling
|
|
7
|
+
* is sandbox), until the next command decides again. A minute covers a turn's file operations
|
|
8
|
+
* around its commands while bounding how long an organization's change takes to reach a device
|
|
9
|
+
* that runs no more commands.
|
|
59
10
|
*/
|
|
60
11
|
export const FILE_OPS_DECISION_TTL_MS = 60_000;
|
|
61
|
-
/** How often the switching executor rereads the policy while
|
|
12
|
+
/** How often the switching executor rereads the policy while auto mode is on. */
|
|
62
13
|
const WATCH_INTERVAL_MS = 1000;
|
|
63
|
-
/** The refusal of an
|
|
14
|
+
/** The refusal of an auto-mode command whose sandbox was turned back on before it started. */
|
|
64
15
|
export const SANDBOX_BACK_ON = "The sandbox was turned back on on this machine while the command was starting, so it did " +
|
|
65
16
|
"not run. Run it again: it runs in the sandbox now.";
|
|
66
17
|
/**
|
|
67
|
-
* The OS-mode executor the program serves: each command runs in the
|
|
68
|
-
* {@link
|
|
69
|
-
* applies at
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
*
|
|
18
|
+
* The OS-mode executor the program serves: each command runs in the sandbox or, in auto mode,
|
|
19
|
+
* directly on the machine ({@link effectiveMode}). The policy is read per operation, so a change
|
|
20
|
+
* applies at the next command. File operations use the host executor's direct IO (any absolute
|
|
21
|
+
* path in auto mode, the roots in the sandbox) only while the latest command's decision is fresh;
|
|
22
|
+
* before any command, or once its limits are stale, the sandboxed helper runs (file operations
|
|
23
|
+
* never pass command review, so the org's ceiling may not be exceeded). When the policy turns the
|
|
24
|
+
* sandbox back on, every auto-mode command still running (and what it left in the background) is
|
|
25
|
+
* killed, and one still starting is refused right before its spawn; while auto mode is on, the
|
|
74
26
|
* policy is also checked every second so that happens without waiting for the next operation.
|
|
75
27
|
* Kills reach both executors' commands.
|
|
76
28
|
*/
|
|
@@ -81,8 +33,13 @@ export class SwitchingExecutor {
|
|
|
81
33
|
onSwitch;
|
|
82
34
|
mode = "os";
|
|
83
35
|
roots;
|
|
84
|
-
/** The policy
|
|
85
|
-
|
|
36
|
+
/** The policy said auto mode when last checked. */
|
|
37
|
+
auto = false;
|
|
38
|
+
/**
|
|
39
|
+
* Whether the OS sandbox can run here. When it cannot, sandbox-mode operations go to the
|
|
40
|
+
* sandboxed executor and are refused there with the reason; auto mode keeps working.
|
|
41
|
+
*/
|
|
42
|
+
sandboxReady;
|
|
86
43
|
watch;
|
|
87
44
|
/** The latest command's limits and when they came (file operations follow their decision). */
|
|
88
45
|
lastLimits;
|
|
@@ -95,91 +52,88 @@ export class SwitchingExecutor {
|
|
|
95
52
|
this.policy = policy;
|
|
96
53
|
this.onSwitch = onSwitch;
|
|
97
54
|
this.roots = sandboxed.roots;
|
|
55
|
+
this.sandboxReady = sandboxed.available !== false;
|
|
98
56
|
}
|
|
99
57
|
/**
|
|
100
|
-
* Rereads the policy; on a change fires `onSwitch` and, when the sandbox came back on, kills
|
|
101
|
-
*
|
|
58
|
+
* Rereads the policy; on a change fires `onSwitch` and, when the sandbox came back on, kills the
|
|
59
|
+
* auto-mode commands. Returns whether the policy says auto mode now.
|
|
102
60
|
*/
|
|
103
61
|
async sync(policy = this.policy()) {
|
|
104
|
-
const
|
|
105
|
-
if (
|
|
106
|
-
return
|
|
107
|
-
if (
|
|
108
|
-
this.
|
|
62
|
+
const auto = policy.mode === "auto";
|
|
63
|
+
if (auto === this.auto)
|
|
64
|
+
return auto;
|
|
65
|
+
if (auto) {
|
|
66
|
+
this.auto = true;
|
|
109
67
|
this.watch ??= setInterval(() => void this.sync().catch(() => { }), WATCH_INTERVAL_MS);
|
|
110
68
|
this.watch.unref();
|
|
111
|
-
this.onSwitch?.("
|
|
112
|
-
return
|
|
69
|
+
this.onSwitch?.("auto");
|
|
70
|
+
return auto;
|
|
113
71
|
}
|
|
114
|
-
//
|
|
72
|
+
// The sandbox is back on only once the auto-mode commands are gone: a failed kill keeps the
|
|
115
73
|
// state (and the watch), so the next sync tries again. Routing follows the policy meanwhile.
|
|
116
74
|
try {
|
|
117
75
|
await this.host.killAll();
|
|
118
76
|
}
|
|
119
77
|
catch {
|
|
120
|
-
return
|
|
78
|
+
return auto;
|
|
121
79
|
}
|
|
122
80
|
// The policy may have changed again while the kill ran: the next sync judges that.
|
|
123
|
-
if (!this.
|
|
124
|
-
return
|
|
125
|
-
this.
|
|
81
|
+
if (!this.auto || this.policy().mode === "auto")
|
|
82
|
+
return auto;
|
|
83
|
+
this.auto = false;
|
|
126
84
|
if (this.watch !== undefined)
|
|
127
85
|
clearInterval(this.watch);
|
|
128
86
|
this.watch = undefined;
|
|
129
87
|
this.onSwitch?.("sandbox");
|
|
130
|
-
return
|
|
88
|
+
return auto;
|
|
131
89
|
}
|
|
132
90
|
/**
|
|
133
91
|
* Records an exec's limits, refused ones included, so file operations follow the latest
|
|
134
92
|
* decision (see {@link FILE_OPS_DECISION_TTL_MS}).
|
|
135
93
|
*/
|
|
136
94
|
noteLimits(limits) {
|
|
137
|
-
|
|
138
|
-
this.lastLimits = {
|
|
139
|
-
tier,
|
|
140
|
-
...(defaultTier !== undefined && { defaultTier }),
|
|
141
|
-
...(allowUnsandboxed !== undefined && { allowUnsandboxed }),
|
|
142
|
-
};
|
|
95
|
+
this.lastLimits = { mode: limits.mode };
|
|
143
96
|
this.lastLimitsAt = Date.now();
|
|
144
97
|
}
|
|
145
98
|
/**
|
|
146
|
-
* The executor for file operations now
|
|
147
|
-
*
|
|
99
|
+
* The executor for file operations now. File operations carry no limits and never pass command
|
|
100
|
+
* review, so they only follow an exec's decision when it is fresh: the sandboxed helper before
|
|
101
|
+
* any exec and once those limits are stale (the safe default for an organization whose ceiling
|
|
102
|
+
* is sandbox), else `effectiveMode(policy, limits)`. On a machine without the OS sandbox, auto
|
|
103
|
+
* mode still works: without a fresh sandbox decision the host runs them (a sandbox decision is
|
|
104
|
+
* refused by the sandboxed executor with the reason).
|
|
148
105
|
*/
|
|
149
106
|
async files() {
|
|
150
107
|
const policy = this.policy();
|
|
151
108
|
await this.sync(policy);
|
|
152
|
-
if (
|
|
153
|
-
return this.sandboxed;
|
|
154
|
-
if (Date.now() - this.lastLimitsAt > FILE_OPS_DECISION_TTL_MS)
|
|
109
|
+
if (policy.mode === "sandbox")
|
|
155
110
|
return this.sandboxed;
|
|
156
|
-
|
|
157
|
-
|
|
111
|
+
if (this.lastLimits === undefined ||
|
|
112
|
+
Date.now() - this.lastLimitsAt > FILE_OPS_DECISION_TTL_MS) {
|
|
113
|
+
return this.sandboxReady ? this.sandboxed : this.host;
|
|
114
|
+
}
|
|
115
|
+
return this.lastLimits.mode === "sandbox" ? this.sandboxed : this.host;
|
|
158
116
|
}
|
|
159
117
|
async exec(args, signal) {
|
|
160
118
|
signal.throwIfAborted();
|
|
161
119
|
const policy = this.policy();
|
|
162
|
-
return this.execDecided(args, signal,
|
|
120
|
+
return this.execDecided(args, signal, { mode: effectiveMode(policy, args.limits) }, policy);
|
|
163
121
|
}
|
|
164
122
|
/**
|
|
165
123
|
* Runs one command as `how` decided (the caller logs the same decision). The policy it was
|
|
166
|
-
* decided on is synced first, so a sandbox turned back on stops the
|
|
167
|
-
*
|
|
124
|
+
* decided on is synced first, so a sandbox turned back on stops the auto-mode commands. An
|
|
125
|
+
* auto-mode command is decided again on the policy then, right before its spawn: one whose
|
|
168
126
|
* sandbox came back on while it was starting is refused ({@link SANDBOX_BACK_ON}).
|
|
169
127
|
*/
|
|
170
128
|
async execDecided(args, signal, how, policy = this.policy()) {
|
|
171
129
|
signal.throwIfAborted();
|
|
172
130
|
this.noteLimits(args.limits);
|
|
173
131
|
await this.sync(policy);
|
|
174
|
-
if (how.
|
|
175
|
-
throw new DeviceOpError("policy", how.refusal);
|
|
176
|
-
if (how.isolation !== "none")
|
|
132
|
+
if (how.mode !== "auto")
|
|
177
133
|
return this.sandboxed.exec(args, signal);
|
|
178
134
|
const beforeSpawn = () => {
|
|
179
|
-
|
|
180
|
-
if (now.isolation !== "none" || now.refusal !== undefined) {
|
|
135
|
+
if (this.policy().mode === "sandbox")
|
|
181
136
|
throw new DeviceOpError("policy", SANDBOX_BACK_ON);
|
|
182
|
-
}
|
|
183
137
|
};
|
|
184
138
|
return this.host.exec(args, signal, { beforeSpawn });
|
|
185
139
|
}
|
|
@@ -201,10 +155,6 @@ export class SwitchingExecutor {
|
|
|
201
155
|
ensureRoot() {
|
|
202
156
|
return this.sandboxed.ensureRoot();
|
|
203
157
|
}
|
|
204
|
-
/** The sandboxed executor's latest localhost proxy guard result (see `Executor`). */
|
|
205
|
-
proxyGuard() {
|
|
206
|
-
return this.sandboxed.proxyGuard?.() ?? { detected: false, reasons: [] };
|
|
207
|
-
}
|
|
208
158
|
/** Stops watching the policy. */
|
|
209
159
|
dispose() {
|
|
210
160
|
if (this.watch !== undefined)
|
package/dist/exec/types.d.ts
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
import type { DeviceErrorKind, DeviceExecArgs, ExecResult, SandboxRoots
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
tier: SandboxTier;
|
|
7
|
-
/** `none`: without a sandbox. */
|
|
8
|
-
isolation: "sandbox" | "none";
|
|
9
|
-
/** Set when the policy asks for no sandbox but the tiers do not allow it: the command is refused. */
|
|
10
|
-
refusal?: string;
|
|
1
|
+
import type { DeviceErrorKind, DeviceExecArgs, ExecResult, SandboxRoots } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import type { Policy, SandboxMode } from "../state.js";
|
|
3
|
+
/** How one command runs: in auto mode (no OS sandbox) or in the sandbox. */
|
|
4
|
+
export interface ExecMode {
|
|
5
|
+
mode: SandboxMode;
|
|
11
6
|
}
|
|
12
7
|
/** Hooks of one command. */
|
|
13
8
|
export interface ExecHooks {
|
|
@@ -17,7 +12,7 @@ export interface ExecHooks {
|
|
|
17
12
|
*/
|
|
18
13
|
beforeSpawn?: () => void;
|
|
19
14
|
}
|
|
20
|
-
/** What runs the operations on this machine: the OS sandbox (os) or a container (docker). */
|
|
15
|
+
/** What runs the operations on this machine: the OS sandbox / host (os) or a container (docker). */
|
|
21
16
|
export interface Executor {
|
|
22
17
|
readonly mode: "os" | "docker";
|
|
23
18
|
readonly roots: SandboxRoots;
|
|
@@ -45,21 +40,16 @@ export interface Executor {
|
|
|
45
40
|
* before and has vanished; it is never recreated silently.
|
|
46
41
|
*/
|
|
47
42
|
ensureRoot(): Promise<"ok" | "gone">;
|
|
48
|
-
/**
|
|
49
|
-
* OS mode: the latest localhost proxy guard result (a local forwarding proxy turns localhost
|
|
50
|
-
* off for `dev` / `open`, spec §5.2.3). Absent: none found.
|
|
51
|
-
*/
|
|
52
|
-
proxyGuard?(): ProxyGuardResult;
|
|
53
43
|
/**
|
|
54
44
|
* OS mode, an executor that can run without a sandbox: records an exec's limits, also of one
|
|
55
|
-
*
|
|
45
|
+
* the policy sent to the sandbox, so file operations follow the latest decision.
|
|
56
46
|
*/
|
|
57
47
|
noteLimits?(limits: DeviceExecArgs["limits"]): void;
|
|
58
48
|
/**
|
|
59
49
|
* OS mode, an executor that can run without a sandbox: runs one command as `how` (decided by
|
|
60
50
|
* the caller on `policy`, and logged by it) says, instead of deciding again in {@link exec}.
|
|
61
51
|
*/
|
|
62
|
-
execDecided?(args: DeviceExecArgs, signal: AbortSignal, how:
|
|
52
|
+
execDecided?(args: DeviceExecArgs, signal: AbortSignal, how: ExecMode, policy: Policy): Promise<ExecResult>;
|
|
63
53
|
}
|
|
64
54
|
/** An executor failure with a protocol error kind (default `error`). */
|
|
65
55
|
export declare class DeviceOpError extends Error {
|
package/dist/index.d.ts
CHANGED
package/dist/network.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { type
|
|
1
|
+
import { type SandboxLimits } from "@capybara-hq/plugin-sdk";
|
|
2
2
|
import type { Policy } from "./state.js";
|
|
3
|
-
/** What refused a host (spec §
|
|
3
|
+
/** What refused a host (spec §9.2). */
|
|
4
4
|
export type BlockedLayer = "device" | "org" | "address" | "unknown";
|
|
5
5
|
export interface BlockedHost {
|
|
6
6
|
host: string;
|
|
@@ -9,9 +9,9 @@ export interface BlockedHost {
|
|
|
9
9
|
}
|
|
10
10
|
/** At most this many blocked hosts are reported per command. */
|
|
11
11
|
export declare const MAX_BLOCKED_HOSTS = 20;
|
|
12
|
-
type Limits =
|
|
13
|
-
/** The local policy fields that decide network access. */
|
|
14
|
-
type NetworkPolicy = Pick<Policy, "allowedDomains"
|
|
12
|
+
type Limits = Pick<SandboxLimits, "mode" | "network">;
|
|
13
|
+
/** The local policy fields that decide sandbox-mode network access. */
|
|
14
|
+
type NetworkPolicy = Pick<Policy, "allowedDomains">;
|
|
15
15
|
/**
|
|
16
16
|
* Parses one sandbox-runtime 0.0.81 violation line `deny network-outbound <host>:<port> (<reason>)`;
|
|
17
17
|
* undefined for other lines. IPv6 hosts arrive bracketed and are returned without the brackets.
|
|
@@ -28,8 +28,8 @@ export declare function parseNetworkViolation(line: string): {
|
|
|
28
28
|
export declare function isPublicName(host: string): boolean;
|
|
29
29
|
/**
|
|
30
30
|
* The layer that refused `host`: `address` (an IP literal, a name `validateAllowHost` refuses, or
|
|
31
|
-
* a resolved-address reason), `org` (not in the
|
|
32
|
-
*
|
|
31
|
+
* a resolved-address reason), `org` (not in the kernel's allowlist; also when the device lacks it
|
|
32
|
+
* too), `device` (the kernel allows it, the local policy does not), else `unknown`.
|
|
33
33
|
*/
|
|
34
34
|
export declare function classifyHost(host: string, reason: string, limits: Limits, policy: NetworkPolicy): BlockedLayer;
|
|
35
35
|
/** The refused hosts in `lines`, deduped by host (first port kept), at most {@link MAX_BLOCKED_HOSTS}. */
|
package/dist/network.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isIP } from "node:net";
|
|
2
2
|
import { validateAllowHost } from "@capybara-hq/plugin-sdk";
|
|
3
|
-
import { covers
|
|
3
|
+
import { covers } from "./policy.js";
|
|
4
4
|
/** At most this many blocked hosts are reported per command. */
|
|
5
5
|
export const MAX_BLOCKED_HOSTS = 20;
|
|
6
6
|
// The proxy's line in @anthropic-ai/sandbox-runtime 0.0.81 (`recordOutboundDeny`):
|
|
@@ -61,26 +61,19 @@ function isAddress(host, reason) {
|
|
|
61
61
|
return reason.startsWith("resolved to ") || validateAllowHost(host).length > 0;
|
|
62
62
|
}
|
|
63
63
|
function orgAllows(host, limits) {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (limits.egress === "open")
|
|
64
|
+
// `open-public` is for cloud instances; a local sandbox-mode device gets an allowlist.
|
|
65
|
+
if (limits.network.kind === "open-public")
|
|
67
66
|
return true;
|
|
68
|
-
return admittedBy(limits.
|
|
67
|
+
return admittedBy(limits.network.hosts, host);
|
|
69
68
|
}
|
|
70
|
-
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
* yet) the owner's choice, else `dev`, stands with no dev hosts known.
|
|
74
|
-
*/
|
|
75
|
-
function deviceAllows(host, policy, limits) {
|
|
76
|
-
const tier = limits === undefined ? provisionalTier(policy) : deviceTier(policy, limits);
|
|
77
|
-
const list = deviceHosts(policy, tier, limits?.devHosts ?? []);
|
|
78
|
-
return list === undefined || admittedBy(list, host);
|
|
69
|
+
/** Whether the device's own allowlist admits `host` (sandbox mode). */
|
|
70
|
+
function deviceAllows(host, policy) {
|
|
71
|
+
return admittedBy(policy.allowedDomains, host);
|
|
79
72
|
}
|
|
80
73
|
/**
|
|
81
74
|
* The layer that refused `host`: `address` (an IP literal, a name `validateAllowHost` refuses, or
|
|
82
|
-
* a resolved-address reason), `org` (not in the
|
|
83
|
-
*
|
|
75
|
+
* a resolved-address reason), `org` (not in the kernel's allowlist; also when the device lacks it
|
|
76
|
+
* too), `device` (the kernel allows it, the local policy does not), else `unknown`.
|
|
84
77
|
*/
|
|
85
78
|
export function classifyHost(host, reason, limits, policy) {
|
|
86
79
|
const h = normalizeHost(host);
|
|
@@ -88,7 +81,7 @@ export function classifyHost(host, reason, limits, policy) {
|
|
|
88
81
|
return "address";
|
|
89
82
|
if (!orgAllows(h, limits))
|
|
90
83
|
return "org";
|
|
91
|
-
if (!deviceAllows(h, policy
|
|
84
|
+
if (!deviceAllows(h, policy))
|
|
92
85
|
return "device";
|
|
93
86
|
return "unknown";
|
|
94
87
|
}
|
|
@@ -147,7 +140,7 @@ export function denyReason(host, limits, policy) {
|
|
|
147
140
|
else if (isAddress(h, ""))
|
|
148
141
|
layer = "address";
|
|
149
142
|
else
|
|
150
|
-
layer = deviceAllows(h, policy
|
|
143
|
+
layer = deviceAllows(h, policy) ? "unknown" : "device";
|
|
151
144
|
switch (layer) {
|
|
152
145
|
case "device":
|
|
153
146
|
return `capybara: ${host} is not in this device's allowed domains`;
|
package/dist/policy-store.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ import { type Policy } from "./state.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* The device's local policy as the running program sees it. Commands read {@link current} at
|
|
5
5
|
* each exec, so a change applies at the next command without a restart, whether this program
|
|
6
|
-
* wrote it ({@link update}) or someone ran `capybara-device
|
|
7
|
-
* is a new object, so a reader can tell a reload by identity (the
|
|
8
|
-
*
|
|
6
|
+
* wrote it ({@link update}) or someone ran `capybara-device mode` / `capybara-device policy`
|
|
7
|
+
* meanwhile. Each new version is a new object, so a reader can tell a reload by identity (the
|
|
8
|
+
* switching executor kills auto-mode commands when the policy turns the sandbox back on).
|
|
9
9
|
*/
|
|
10
10
|
export declare class PolicyStore {
|
|
11
11
|
private readonly dir;
|
|
@@ -16,10 +16,10 @@ export declare class PolicyStore {
|
|
|
16
16
|
private readonly file;
|
|
17
17
|
private readonly err;
|
|
18
18
|
/**
|
|
19
|
-
* policy.json
|
|
20
|
-
* the key is dropped at the next write
|
|
19
|
+
* policy.json was written before auto mode (no `mode`) when the store was made: `start` prints
|
|
20
|
+
* one notice, and the key is dropped at the next write.
|
|
21
21
|
*/
|
|
22
|
-
readonly
|
|
22
|
+
readonly legacy: boolean;
|
|
23
23
|
/** The outcomes of the `policy` ops this machine served, by op id (next to policy.json). */
|
|
24
24
|
readonly ops: PolicyOpLog;
|
|
25
25
|
constructor(dir: string, opts?: {
|
|
@@ -28,8 +28,7 @@ export declare class PolicyStore {
|
|
|
28
28
|
});
|
|
29
29
|
/**
|
|
30
30
|
* The policy now: re-reads policy.json when it changed (or appeared, or vanished: defaults).
|
|
31
|
-
* An invalid file keeps the last good policy
|
|
32
|
-
* on (a broken file never keeps commands running unsandboxed), and is reported once per version.
|
|
31
|
+
* An invalid file keeps the last good policy and is reported once per version.
|
|
33
32
|
*/
|
|
34
33
|
current(): Policy;
|
|
35
34
|
/**
|
package/dist/policy-store.js
CHANGED
|
@@ -15,9 +15,9 @@ function versionOf(file) {
|
|
|
15
15
|
/**
|
|
16
16
|
* The device's local policy as the running program sees it. Commands read {@link current} at
|
|
17
17
|
* each exec, so a change applies at the next command without a restart, whether this program
|
|
18
|
-
* wrote it ({@link update}) or someone ran `capybara-device
|
|
19
|
-
* is a new object, so a reader can tell a reload by identity (the
|
|
20
|
-
*
|
|
18
|
+
* wrote it ({@link update}) or someone ran `capybara-device mode` / `capybara-device policy`
|
|
19
|
+
* meanwhile. Each new version is a new object, so a reader can tell a reload by identity (the
|
|
20
|
+
* switching executor kills auto-mode commands when the policy turns the sandbox back on).
|
|
21
21
|
*/
|
|
22
22
|
export class PolicyStore {
|
|
23
23
|
dir;
|
|
@@ -28,10 +28,10 @@ export class PolicyStore {
|
|
|
28
28
|
file;
|
|
29
29
|
err;
|
|
30
30
|
/**
|
|
31
|
-
* policy.json
|
|
32
|
-
* the key is dropped at the next write
|
|
31
|
+
* policy.json was written before auto mode (no `mode`) when the store was made: `start` prints
|
|
32
|
+
* one notice, and the key is dropped at the next write.
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
legacy;
|
|
35
35
|
/** The outcomes of the `policy` ops this machine served, by op id (next to policy.json). */
|
|
36
36
|
ops;
|
|
37
37
|
constructor(dir, opts = {}) {
|
|
@@ -43,18 +43,17 @@ export class PolicyStore {
|
|
|
43
43
|
// Without an initial policy, an unreadable file throws here: the program does not start.
|
|
44
44
|
if (opts.initial !== undefined) {
|
|
45
45
|
this.policy = opts.initial;
|
|
46
|
-
this.
|
|
46
|
+
this.legacy = false;
|
|
47
47
|
}
|
|
48
48
|
else {
|
|
49
49
|
const read = readPolicyFile(dir);
|
|
50
50
|
this.policy = read.policy;
|
|
51
|
-
this.
|
|
51
|
+
this.legacy = read.legacy;
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
/**
|
|
55
55
|
* The policy now: re-reads policy.json when it changed (or appeared, or vanished: defaults).
|
|
56
|
-
* An invalid file keeps the last good policy
|
|
57
|
-
* on (a broken file never keeps commands running unsandboxed), and is reported once per version.
|
|
56
|
+
* An invalid file keeps the last good policy and is reported once per version.
|
|
58
57
|
*/
|
|
59
58
|
current() {
|
|
60
59
|
const version = versionOf(this.file);
|
|
@@ -67,13 +66,10 @@ export class PolicyStore {
|
|
|
67
66
|
this.reportedBad = undefined;
|
|
68
67
|
}
|
|
69
68
|
catch (e) {
|
|
70
|
-
// A
|
|
71
|
-
if (this.policy.isolation === "none")
|
|
72
|
-
this.policy = { ...this.policy, isolation: "sandbox" };
|
|
69
|
+
// A broken file never changes the mode: the last good policy stands.
|
|
73
70
|
if (this.reportedBad !== version) {
|
|
74
71
|
this.reportedBad = version;
|
|
75
|
-
this.err(`${e.message}; keeping the last valid policy
|
|
76
|
-
"file is fixed");
|
|
72
|
+
this.err(`${e.message}; keeping the last valid policy until the file is fixed`);
|
|
77
73
|
}
|
|
78
74
|
}
|
|
79
75
|
return this.policy;
|