@capybara-hq/plugin-sdk 0.1.0-rc.4 → 0.1.0-rc.41
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/agent-docs/agent-templates.md +15 -3
- package/agent-docs/concepts.md +13 -3
- package/agent-docs/connector-templates.md +2 -1
- package/agent-docs/extension-points/channel.md +2 -2
- package/agent-docs/extension-points/config-source.md +1 -1
- package/agent-docs/extension-points/connector.md +1 -1
- package/agent-docs/extension-points/context.md +15 -0
- package/agent-docs/extension-points/counter.md +1 -1
- package/agent-docs/extension-points/directory.md +1 -1
- package/agent-docs/extension-points/file-preview.md +3 -1
- package/agent-docs/extension-points/identity-source.md +1 -1
- package/agent-docs/extension-points/model-provider.md +4 -2
- package/agent-docs/extension-points/object-store.md +3 -1
- package/agent-docs/extension-points/sandbox.md +3 -3
- package/agent-docs/extension-points/secret-store.md +1 -1
- package/agent-docs/extension-points/web-fetch.md +1 -1
- package/agent-docs/extension-points/web-search.md +1 -1
- package/agent-docs/extensions.md +29 -4
- package/agent-docs/plugin-format.md +43 -3
- package/agent-docs/setup.md +46 -0
- package/agent-docs/skills.md +1 -1
- package/agent-docs/tool-naming.md +1 -1
- package/dist/agent-template.d.ts +15 -6
- package/dist/agent-template.js +32 -13
- package/dist/bootstrap.d.ts +15 -1
- package/dist/bootstrap.js +17 -1
- package/dist/cards.d.ts +67 -13
- package/dist/cards.js +91 -21
- package/dist/channel.d.ts +148 -48
- package/dist/channel.js +10 -5
- package/dist/client-css.d.ts +36 -0
- package/dist/client-css.js +326 -0
- package/dist/client-section.d.ts +35 -0
- package/dist/client-section.js +133 -0
- package/dist/config-ui.d.ts +22 -0
- package/dist/config-ui.js +64 -4
- package/dist/connector-auth.d.ts +7 -2
- package/dist/connector-auth.js +29 -8
- package/dist/connector.d.ts +74 -32
- package/dist/context.d.ts +40 -0
- package/dist/context.js +2 -0
- package/dist/ctx.d.ts +120 -29
- package/dist/define-extension.d.ts +26 -12
- package/dist/define-extension.js +11 -4
- package/dist/device-denied-paths.d.ts +14 -0
- package/dist/device-denied-paths.js +41 -0
- package/dist/device-protocol.d.ts +481 -0
- package/dist/device-protocol.js +1113 -0
- package/dist/extension-point.d.ts +2 -0
- package/dist/extension-point.js +5 -2
- package/dist/file-preview.d.ts +23 -6
- package/dist/git-hosts.d.ts +44 -0
- package/dist/git-hosts.js +81 -0
- package/dist/hooks.d.ts +125 -20
- package/dist/hooks.js +5 -0
- package/dist/http-access.d.ts +62 -0
- package/dist/http-access.js +12 -0
- package/dist/identity.d.ts +22 -9
- package/dist/index.d.ts +26 -1
- package/dist/index.js +26 -1
- package/dist/marketplace.d.ts +26 -0
- package/dist/marketplace.js +43 -1
- package/dist/model-params.d.ts +41 -22
- package/dist/model-params.js +28 -15
- package/dist/model.d.ts +206 -71
- package/dist/model.js +152 -19
- package/dist/net.d.ts +52 -0
- package/dist/net.js +34 -0
- package/dist/network-classes.d.ts +98 -0
- package/dist/network-classes.js +291 -0
- package/dist/permissions.d.ts +41 -21
- package/dist/permissions.js +101 -42
- package/dist/plugin-manifest.d.ts +45 -5
- package/dist/plugin-manifest.js +169 -6
- package/dist/plugin-package.d.ts +82 -38
- package/dist/plugin-package.js +61 -29
- package/dist/sandbox-linux-runtime-source.d.ts +1 -0
- package/dist/sandbox-linux-runtime-source.js +2318 -0
- package/dist/sandbox-native-runtime-source.d.ts +8 -0
- package/dist/sandbox-native-runtime-source.js +465 -0
- package/dist/sandbox-operation.d.ts +284 -0
- package/dist/sandbox-operation.js +262 -0
- package/dist/sandbox-runtime-source.d.ts +19 -0
- package/dist/sandbox-runtime-source.js +28 -0
- package/dist/sandbox-write-denies.d.ts +139 -0
- package/dist/sandbox-write-denies.js +869 -0
- package/dist/sandbox.d.ts +190 -16
- package/dist/sandbox.js +89 -14
- package/dist/secret.d.ts +4 -1
- package/dist/secret.js +4 -1
- package/dist/setup-check.d.ts +115 -0
- package/dist/setup-check.js +142 -0
- package/dist/setup-guide-parse.d.ts +121 -0
- package/dist/setup-guide-parse.js +350 -0
- package/dist/setup-guide-render.d.ts +45 -0
- package/dist/setup-guide-render.js +351 -0
- package/dist/setup-guide.d.ts +94 -0
- package/dist/setup-guide.js +336 -0
- package/dist/shell-capabilities.d.ts +88 -0
- package/dist/shell-capabilities.js +13 -0
- package/dist/shell-environment.d.ts +57 -0
- package/dist/shell-environment.js +16 -0
- package/dist/skill-invocation.d.ts +4 -3
- package/dist/skill-invocation.js +5 -4
- package/dist/skill.d.ts +34 -21
- package/dist/skill.js +48 -31
- package/dist/storage.d.ts +52 -25
- package/dist/theme-assets.d.ts +39 -0
- package/dist/theme-assets.js +449 -0
- package/dist/theme-readability.d.ts +37 -0
- package/dist/theme-readability.js +145 -0
- package/dist/theme-tokens.generated.d.ts +287 -0
- package/dist/theme-tokens.generated.js +176 -0
- package/dist/theme.d.ts +152 -0
- package/dist/theme.js +392 -0
- package/dist/tool.d.ts +69 -26
- package/dist/tool.js +29 -9
- package/dist/ui-parts.d.ts +23 -0
- package/dist/ui-parts.js +348 -0
- package/dist/ui-resolve.d.ts +35 -0
- package/dist/ui-resolve.js +42 -0
- package/dist/ui-slots.d.ts +65 -0
- package/dist/ui-slots.js +261 -0
- package/dist/ui.d.ts +212 -76
- package/dist/ui.js +101 -27
- package/dist/url.d.ts +4 -1
- package/dist/url.js +4 -1
- package/package.json +8 -1
- package/schemas/bootstrap.v1.schema.json +6 -0
- package/schemas/connector-template.v1.schema.json +35 -0
- package/schemas/marketplace.v1.schema.json +14 -0
- package/schemas/plugin.v1.schema.json +115 -3
- package/schemas/theme.v1.schema.json +225 -0
|
@@ -0,0 +1,1113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The device protocol: the WebSocket frames between the kernel and a `capybara-device` program
|
|
3
|
+
* (a connected machine that serves as a device). Pure types and hand-written guards.
|
|
4
|
+
*
|
|
5
|
+
* Protocol 4 (the scope-bearing operations, `parseDeviceFrame`) is defined in the second half of
|
|
6
|
+
* this file. Protocol 2's frames keep a `Legacy` prefix where a newer name replaced them
|
|
7
|
+
* (`LegacyDeviceHello`, `parseLegacyDeviceFrame`, ...); protocol 4 reuses their value guards, and
|
|
8
|
+
* they go with the legacy device surface.
|
|
9
|
+
*/
|
|
10
|
+
import { isDeviceProjectName, MAX_DEVICE_PROJECT_NAME_LENGTH, MAX_DEVICE_SCAN_DEPTH, MAX_DEVICE_SCAN_RESULTS, validateAllowHost, validateSandboxPathShape, } from "./sandbox.js";
|
|
11
|
+
import { parseSandboxBrowseScope, parseSandboxExecutionRef, parseSandboxOperationRef, parseSandboxOperationScope, parseSandboxPreparationScope, } from "./sandbox-operation.js";
|
|
12
|
+
/** The protocol version carried in `X-Capybara-Device-Protocol` and the hello frame. */
|
|
13
|
+
export const DEVICE_PROTOCOL_VERSION = 4;
|
|
14
|
+
/**
|
|
15
|
+
* Protocol 3's version, which a deployment speaking {@link DEVICE_PROTOCOL_VERSION} refuses (426
|
|
16
|
+
* on the header, a protocol close on the hello): its owner report and runtime support describe the
|
|
17
|
+
* removed Full access ceiling, so a protocol 3 program must be updated.
|
|
18
|
+
*/
|
|
19
|
+
export const LEGACY_DEVICE_PROTOCOL_VERSION = 3;
|
|
20
|
+
const DEVICE_OPS = [
|
|
21
|
+
"create",
|
|
22
|
+
"exec",
|
|
23
|
+
"readFile",
|
|
24
|
+
"writeFile",
|
|
25
|
+
"stat",
|
|
26
|
+
"killTagged",
|
|
27
|
+
"killAll",
|
|
28
|
+
"stop",
|
|
29
|
+
"policy",
|
|
30
|
+
"policyStatus",
|
|
31
|
+
"list",
|
|
32
|
+
"scan",
|
|
33
|
+
];
|
|
34
|
+
const DEVICE_FEATURES = [
|
|
35
|
+
"networkReport",
|
|
36
|
+
"policyOp",
|
|
37
|
+
"policyOpId",
|
|
38
|
+
"projects",
|
|
39
|
+
];
|
|
40
|
+
/** The most entries a `list` op may ask for and its result may carry. */
|
|
41
|
+
export const MAX_DEVICE_LIST_ENTRIES = 1000;
|
|
42
|
+
/** The most blocked hosts an exec result reports; the parser cuts longer lists. */
|
|
43
|
+
export const MAX_NETWORK_BLOCKED_HOSTS = 20;
|
|
44
|
+
/** The most hosts a `policy` op's `remove` or an exec's `grants` may carry. */
|
|
45
|
+
const MAX_POLICY_HOSTS = 100;
|
|
46
|
+
/**
|
|
47
|
+
* Why a `policy` op naming hosts to `add` is refused: the deployment may only narrow a local
|
|
48
|
+
* device's sandbox; its owner widens it on the machine.
|
|
49
|
+
*/
|
|
50
|
+
export const POLICY_ADD_REFUSED = "The deployment may not add hosts to a device's sandbox allowlist: widening happens only on the machine (capybara-device policy add allowedDomains <host>)";
|
|
51
|
+
/** The longest `opId` a `policy` / `policyStatus` op may carry. */
|
|
52
|
+
export const MAX_POLICY_OP_ID_LENGTH = 128;
|
|
53
|
+
/** The most entries a hello's `recentOps` carries; the parser keeps the newest. */
|
|
54
|
+
export const MAX_RECENT_POLICY_OPS = 100;
|
|
55
|
+
/** A hello's `bootId`: 1 to 128 characters of letters, digits, `.`, `_`, `:` and `-`. */
|
|
56
|
+
export const DEVICE_BOOT_ID_RE = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
|
|
57
|
+
/** Whether `v` is a well-formed {@link LegacyDeviceHello.bootId}. */
|
|
58
|
+
export function isDeviceBootId(v) {
|
|
59
|
+
return typeof v === "string" && DEVICE_BOOT_ID_RE.test(v);
|
|
60
|
+
}
|
|
61
|
+
/** A frame that is not valid device protocol (or came from the wrong side). */
|
|
62
|
+
export class DeviceProtocolError extends Error {
|
|
63
|
+
constructor(message) {
|
|
64
|
+
super(message);
|
|
65
|
+
this.name = "DeviceProtocolError";
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function isRecord(v) {
|
|
69
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
70
|
+
}
|
|
71
|
+
function str(o, k) {
|
|
72
|
+
const v = o[k];
|
|
73
|
+
if (typeof v !== "string")
|
|
74
|
+
throw new DeviceProtocolError(`field "${k}" must be a string`);
|
|
75
|
+
return v;
|
|
76
|
+
}
|
|
77
|
+
function nonEmptyStr(o, k) {
|
|
78
|
+
const v = str(o, k);
|
|
79
|
+
if (v === "")
|
|
80
|
+
throw new DeviceProtocolError(`field "${k}" must not be empty`);
|
|
81
|
+
return v;
|
|
82
|
+
}
|
|
83
|
+
function posInt(o, k) {
|
|
84
|
+
const v = o[k];
|
|
85
|
+
if (typeof v !== "number" || !Number.isInteger(v) || v <= 0) {
|
|
86
|
+
throw new DeviceProtocolError(`field "${k}" must be a positive integer`);
|
|
87
|
+
}
|
|
88
|
+
return v;
|
|
89
|
+
}
|
|
90
|
+
function obj(v, what) {
|
|
91
|
+
if (!isRecord(v))
|
|
92
|
+
throw new DeviceProtocolError(`${what} must be an object`);
|
|
93
|
+
return v;
|
|
94
|
+
}
|
|
95
|
+
function bool(o, k) {
|
|
96
|
+
const v = o[k];
|
|
97
|
+
if (typeof v !== "boolean")
|
|
98
|
+
throw new DeviceProtocolError(`field "${k}" must be a boolean`);
|
|
99
|
+
return v;
|
|
100
|
+
}
|
|
101
|
+
function strArray(o, k, label = k) {
|
|
102
|
+
const v = o[k];
|
|
103
|
+
if (!Array.isArray(v) || v.some((x) => typeof x !== "string")) {
|
|
104
|
+
throw new DeviceProtocolError(`field "${label}" must be an array of strings`);
|
|
105
|
+
}
|
|
106
|
+
return v;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The exec `limits`: resources and the outer network (no mode: protocol 4 removed it). Unknown
|
|
110
|
+
* keys are refused, so a stale `mode` (or any field the device would ignore) is never accepted.
|
|
111
|
+
*/
|
|
112
|
+
function parseLimits(v) {
|
|
113
|
+
const o = strictRecord(v, ["cpu", "memoryMb", "diskMb", "network"], 'field "limits"');
|
|
114
|
+
const network = strictRecord(o.network, ["kind", "hosts"], 'field "network"');
|
|
115
|
+
const kind = oneOf(network, "kind", ["open-public", "allowlist"]);
|
|
116
|
+
const hosts = strArray(network, "hosts", "network.hosts");
|
|
117
|
+
for (const h of hosts) {
|
|
118
|
+
const problems = validateAllowHost(h);
|
|
119
|
+
if (problems.length > 0) {
|
|
120
|
+
throw new DeviceProtocolError(`invalid network host "${h}": ${problems.join("; ")}`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
cpu: num(o, "cpu"),
|
|
125
|
+
memoryMb: num(o, "memoryMb"),
|
|
126
|
+
diskMb: num(o, "diskMb"),
|
|
127
|
+
network: { kind, hosts },
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function oneOf(o, k, values) {
|
|
131
|
+
const v = str(o, k);
|
|
132
|
+
if (!values.includes(v)) {
|
|
133
|
+
throw new DeviceProtocolError(`unknown ${k} "${v}"`);
|
|
134
|
+
}
|
|
135
|
+
return v;
|
|
136
|
+
}
|
|
137
|
+
function num(o, k) {
|
|
138
|
+
const v = o[k];
|
|
139
|
+
if (typeof v !== "number" || !Number.isFinite(v)) {
|
|
140
|
+
throw new DeviceProtocolError(`field "${k}" must be a number`);
|
|
141
|
+
}
|
|
142
|
+
return v;
|
|
143
|
+
}
|
|
144
|
+
function parseRoots(v) {
|
|
145
|
+
if (!isRecord(v))
|
|
146
|
+
throw new DeviceProtocolError('field "roots" must be an object');
|
|
147
|
+
const roots = {};
|
|
148
|
+
for (const k of ["workspace", "skills", "tmp"]) {
|
|
149
|
+
const p = str(v, k);
|
|
150
|
+
if (!p.startsWith("/")) {
|
|
151
|
+
throw new DeviceProtocolError(`root "${k}" must be an absolute path`);
|
|
152
|
+
}
|
|
153
|
+
// the kernel builds paths as `${root}/…` and checks membership by prefix: a root must be a
|
|
154
|
+
// clean absolute directory path
|
|
155
|
+
const shape = validateSandboxPathShape(p);
|
|
156
|
+
if (shape.length > 0)
|
|
157
|
+
throw new DeviceProtocolError(`root "${k}": ${shape.join("; ")}`);
|
|
158
|
+
if (p.endsWith("/")) {
|
|
159
|
+
throw new DeviceProtocolError(`root "${k}" must not end with a slash`);
|
|
160
|
+
}
|
|
161
|
+
roots[k] = p;
|
|
162
|
+
}
|
|
163
|
+
return roots;
|
|
164
|
+
}
|
|
165
|
+
function hostArray(o, k) {
|
|
166
|
+
const v = strArray(o, k);
|
|
167
|
+
if (v.length > MAX_POLICY_HOSTS) {
|
|
168
|
+
throw new DeviceProtocolError(`field "${k}" must have at most ${MAX_POLICY_HOSTS} entries`);
|
|
169
|
+
}
|
|
170
|
+
return v;
|
|
171
|
+
}
|
|
172
|
+
/** A bounded array field of a result or summary, refused when it is longer than `max`. */
|
|
173
|
+
function boundedArray(v, label, max, what) {
|
|
174
|
+
if (!Array.isArray(v)) {
|
|
175
|
+
throw new DeviceProtocolError(`field "${label}" must be an array of ${what}`);
|
|
176
|
+
}
|
|
177
|
+
if (v.length > max) {
|
|
178
|
+
throw new DeviceProtocolError(`field "${label}" must have at most ${max} entries`);
|
|
179
|
+
}
|
|
180
|
+
return v;
|
|
181
|
+
}
|
|
182
|
+
/** A project name field (see {@link isDeviceProjectName}), named by `what` in the message. */
|
|
183
|
+
function projectName(v, what) {
|
|
184
|
+
if (!isDeviceProjectName(v)) {
|
|
185
|
+
throw new DeviceProtocolError(`${what} must be a non-empty name of at most ${MAX_DEVICE_PROJECT_NAME_LENGTH} UTF-16 code units, not "." or "..", without "/", control characters or leading or trailing whitespace`);
|
|
186
|
+
}
|
|
187
|
+
return v;
|
|
188
|
+
}
|
|
189
|
+
/** An absolute path field ({@link validateSandboxPathShape}), named by `what` in the message. */
|
|
190
|
+
function absolutePath(v, what) {
|
|
191
|
+
if (typeof v !== "string")
|
|
192
|
+
throw new DeviceProtocolError(`${what} must be a string`);
|
|
193
|
+
const problems = validateSandboxPathShape(v);
|
|
194
|
+
if (problems.length > 0)
|
|
195
|
+
throw new DeviceProtocolError(`${what}: ${problems.join("; ")}`);
|
|
196
|
+
return v;
|
|
197
|
+
}
|
|
198
|
+
/** A `list` result: null, or at most {@link MAX_DEVICE_LIST_ENTRIES} named entries. */
|
|
199
|
+
function parseListResult(result) {
|
|
200
|
+
if (result === null)
|
|
201
|
+
return null;
|
|
202
|
+
const r = obj(result, "list result");
|
|
203
|
+
const entries = boundedArray(r.entries, "entries", MAX_DEVICE_LIST_ENTRIES, "entries").map((e, i) => {
|
|
204
|
+
const what = `field "entries[${i}]"`;
|
|
205
|
+
if (!isRecord(e))
|
|
206
|
+
throw new DeviceProtocolError(`${what} must be an object`);
|
|
207
|
+
const name = e.name;
|
|
208
|
+
if (typeof name !== "string" ||
|
|
209
|
+
name === "" ||
|
|
210
|
+
name === "." ||
|
|
211
|
+
name === ".." ||
|
|
212
|
+
name.includes("/")) {
|
|
213
|
+
throw new DeviceProtocolError(`${what}.name must be a non-empty string without "/", other than "." and ".."`);
|
|
214
|
+
}
|
|
215
|
+
return { name, isDir: bool(e, "isDir") };
|
|
216
|
+
});
|
|
217
|
+
return { entries, truncated: bool(r, "truncated") };
|
|
218
|
+
}
|
|
219
|
+
/** A `scan` result: at most {@link MAX_DEVICE_SCAN_RESULTS} candidates. */
|
|
220
|
+
function parseScanResult(v) {
|
|
221
|
+
const label = "scan result";
|
|
222
|
+
return boundedArray(v, label, MAX_DEVICE_SCAN_RESULTS, "candidates").map((e, i) => {
|
|
223
|
+
const what = `field "${label}[${i}]"`;
|
|
224
|
+
if (!isRecord(e))
|
|
225
|
+
throw new DeviceProtocolError(`${what} must be an object`);
|
|
226
|
+
return {
|
|
227
|
+
path: absolutePath(e.path, `${what}.path`),
|
|
228
|
+
suggestedName: projectName(e.suggestedName, `${what}.suggestedName`),
|
|
229
|
+
};
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
function parsePolicySummary(v) {
|
|
233
|
+
const o = obj(v, 'field "policy"');
|
|
234
|
+
return { allowedDomains: strArray(o, "allowedDomains") };
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Whether `v` is a valid policy op id: non-empty, no `:`, at most
|
|
238
|
+
* {@link MAX_POLICY_OP_ID_LENGTH} characters.
|
|
239
|
+
*/
|
|
240
|
+
export function isPolicyOpId(v) {
|
|
241
|
+
return (typeof v === "string" && v !== "" && v.length <= MAX_POLICY_OP_ID_LENGTH && !v.includes(":"));
|
|
242
|
+
}
|
|
243
|
+
function opId(o) {
|
|
244
|
+
const v = o.opId;
|
|
245
|
+
if (!isPolicyOpId(v)) {
|
|
246
|
+
throw new DeviceProtocolError(`field "opId" must be a non-empty string without ":" of at most ${MAX_POLICY_OP_ID_LENGTH} characters`);
|
|
247
|
+
}
|
|
248
|
+
return v;
|
|
249
|
+
}
|
|
250
|
+
const OP_OUTCOMES = ["applied", "refused", "started"];
|
|
251
|
+
/**
|
|
252
|
+
* The valid entries of a hello's `recentOps`, one per op id (the last listed), the newest
|
|
253
|
+
* {@link MAX_RECENT_POLICY_OPS}.
|
|
254
|
+
*/
|
|
255
|
+
function parseRecentOps(v) {
|
|
256
|
+
// one entry per op id: the last one listed (the newest outcome), where it is listed
|
|
257
|
+
const byId = new Map();
|
|
258
|
+
for (const e of v) {
|
|
259
|
+
if (!isRecord(e) || !isPolicyOpId(e.opId))
|
|
260
|
+
continue;
|
|
261
|
+
if (!OP_OUTCOMES.includes(e.outcome))
|
|
262
|
+
continue;
|
|
263
|
+
byId.delete(e.opId);
|
|
264
|
+
byId.set(e.opId, { opId: e.opId, outcome: e.outcome });
|
|
265
|
+
}
|
|
266
|
+
return [...byId.values()].slice(-MAX_RECENT_POLICY_OPS);
|
|
267
|
+
}
|
|
268
|
+
/** Optional hello fields: malformed values are ignored so old and new peers interoperate. */
|
|
269
|
+
function optionalHelloFields(o) {
|
|
270
|
+
const out = {};
|
|
271
|
+
if (Array.isArray(o.features)) {
|
|
272
|
+
out.features = DEVICE_FEATURES.filter((f) => o.features.includes(f));
|
|
273
|
+
}
|
|
274
|
+
if (Array.isArray(o.recentOps))
|
|
275
|
+
out.recentOps = parseRecentOps(o.recentOps);
|
|
276
|
+
if (typeof o.home === "string" && validateSandboxPathShape(o.home).length === 0) {
|
|
277
|
+
out.home = o.home;
|
|
278
|
+
}
|
|
279
|
+
if (isDeviceBootId(o.bootId))
|
|
280
|
+
out.bootId = o.bootId;
|
|
281
|
+
return { ...out, ...optionalPolicy(o.policy) };
|
|
282
|
+
}
|
|
283
|
+
/** A summary field that is dropped when absent or unreadable. */
|
|
284
|
+
function optionalPolicy(v) {
|
|
285
|
+
if (v === undefined)
|
|
286
|
+
return {};
|
|
287
|
+
try {
|
|
288
|
+
return { policy: parsePolicySummary(v) };
|
|
289
|
+
}
|
|
290
|
+
catch {
|
|
291
|
+
// an unreadable summary is the same as none: the kernel learns the policy later
|
|
292
|
+
return {};
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
const BLOCK_LAYERS = ["device", "org", "address", "unknown"];
|
|
296
|
+
/**
|
|
297
|
+
* The valid entries of an exec result's `network.blocked`: a host that fails
|
|
298
|
+
* {@link validateAllowHost} is kept only with layer `address`; anything malformed is dropped;
|
|
299
|
+
* at most {@link MAX_NETWORK_BLOCKED_HOSTS}. Undefined when the field is not a report at all.
|
|
300
|
+
*/
|
|
301
|
+
function parseNetworkReport(v) {
|
|
302
|
+
if (!isRecord(v) || !Array.isArray(v.blocked))
|
|
303
|
+
return undefined;
|
|
304
|
+
const blocked = [];
|
|
305
|
+
for (const e of v.blocked) {
|
|
306
|
+
if (blocked.length >= MAX_NETWORK_BLOCKED_HOSTS)
|
|
307
|
+
break;
|
|
308
|
+
if (!isRecord(e))
|
|
309
|
+
continue;
|
|
310
|
+
const { host, port, layer } = e;
|
|
311
|
+
if (typeof host !== "string" || host === "" || host.length > 255)
|
|
312
|
+
continue;
|
|
313
|
+
if (typeof port !== "number" || !Number.isInteger(port) || port < 1 || port > 65535)
|
|
314
|
+
continue;
|
|
315
|
+
if (!BLOCK_LAYERS.includes(layer))
|
|
316
|
+
continue;
|
|
317
|
+
if (layer !== "address" && validateAllowHost(host).length > 0)
|
|
318
|
+
continue;
|
|
319
|
+
blocked.push({ host, port, layer: layer });
|
|
320
|
+
}
|
|
321
|
+
return { blocked };
|
|
322
|
+
}
|
|
323
|
+
function parseHello(o) {
|
|
324
|
+
const os = str(o, "os");
|
|
325
|
+
if (os !== "darwin" && os !== "linux")
|
|
326
|
+
throw new DeviceProtocolError(`unsupported os "${os}"`);
|
|
327
|
+
const mode = str(o, "mode");
|
|
328
|
+
if (mode !== "os" && mode !== "docker") {
|
|
329
|
+
throw new DeviceProtocolError(`unsupported mode "${mode}"`);
|
|
330
|
+
}
|
|
331
|
+
if (typeof o.sessionIsolation !== "boolean") {
|
|
332
|
+
throw new DeviceProtocolError('field "sessionIsolation" must be a boolean');
|
|
333
|
+
}
|
|
334
|
+
return {
|
|
335
|
+
type: "hello",
|
|
336
|
+
protocol: posInt(o, "protocol"),
|
|
337
|
+
programVersion: str(o, "programVersion"),
|
|
338
|
+
os,
|
|
339
|
+
arch: str(o, "arch"),
|
|
340
|
+
mode,
|
|
341
|
+
roots: parseRoots(o.roots),
|
|
342
|
+
sessionIsolation: o.sessionIsolation,
|
|
343
|
+
...optionalHelloFields(o),
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function parseRes(o) {
|
|
347
|
+
const id = nonEmptyStr(o, "id");
|
|
348
|
+
if (o.ok === true)
|
|
349
|
+
return { type: "res", id, ok: true, result: o.result };
|
|
350
|
+
if (o.ok !== false)
|
|
351
|
+
throw new DeviceProtocolError('field "ok" must be a boolean');
|
|
352
|
+
const e = o.error;
|
|
353
|
+
if (!isRecord(e))
|
|
354
|
+
throw new DeviceProtocolError('field "error" must be an object');
|
|
355
|
+
const kind = str(e, "kind");
|
|
356
|
+
if (kind !== "gone" && kind !== "fs_gone" && kind !== "policy" && kind !== "error") {
|
|
357
|
+
throw new DeviceProtocolError(`unknown error kind "${kind}"`);
|
|
358
|
+
}
|
|
359
|
+
return { type: "res", id, ok: false, error: { kind, message: str(e, "message") } };
|
|
360
|
+
}
|
|
361
|
+
function parseReq(o) {
|
|
362
|
+
const op = str(o, "op");
|
|
363
|
+
if (!DEVICE_OPS.includes(op))
|
|
364
|
+
throw new DeviceProtocolError(`unknown op "${op}"`);
|
|
365
|
+
const args = obj(o.args, 'field "args"');
|
|
366
|
+
validateArgs(op, args);
|
|
367
|
+
return {
|
|
368
|
+
type: "req",
|
|
369
|
+
id: nonEmptyStr(o, "id"),
|
|
370
|
+
op: op,
|
|
371
|
+
args,
|
|
372
|
+
deadlineMs: posInt(o, "deadlineMs"),
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function validateArgs(op, a) {
|
|
376
|
+
switch (op) {
|
|
377
|
+
case "exec": {
|
|
378
|
+
nonEmptyStr(a, "command");
|
|
379
|
+
posInt(a, "timeoutMs");
|
|
380
|
+
posInt(a, "maxOutputBytes");
|
|
381
|
+
str(a, "tag");
|
|
382
|
+
const actor = obj(a.actor, 'field "actor"');
|
|
383
|
+
str(actor, "accountId");
|
|
384
|
+
str(actor, "conversationId");
|
|
385
|
+
if (actor.runId !== undefined)
|
|
386
|
+
str(actor, "runId");
|
|
387
|
+
parseLimits(a.limits);
|
|
388
|
+
if (a.grants !== undefined) {
|
|
389
|
+
const g = obj(a.grants, 'field "grants"');
|
|
390
|
+
hostArray(g, "hosts");
|
|
391
|
+
const src = obj(g.source, 'field "grants.source"');
|
|
392
|
+
const kind = str(src, "kind");
|
|
393
|
+
if (kind !== "run" && kind !== "task") {
|
|
394
|
+
throw new DeviceProtocolError(`unknown grant source kind "${kind}"`);
|
|
395
|
+
}
|
|
396
|
+
nonEmptyStr(src, "id");
|
|
397
|
+
nonEmptyStr(src, "accountId");
|
|
398
|
+
}
|
|
399
|
+
if (a.cwd !== undefined)
|
|
400
|
+
str(a, "cwd");
|
|
401
|
+
if (a.env !== undefined) {
|
|
402
|
+
const env = obj(a.env, 'field "env"');
|
|
403
|
+
for (const k of Object.keys(env))
|
|
404
|
+
str(env, k);
|
|
405
|
+
}
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
case "readFile": {
|
|
409
|
+
str(a, "path");
|
|
410
|
+
const m = a.maxBytes;
|
|
411
|
+
if (typeof m !== "number" || !Number.isInteger(m) || m < 0) {
|
|
412
|
+
throw new DeviceProtocolError('field "maxBytes" must be a non-negative integer');
|
|
413
|
+
}
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
case "writeFile":
|
|
417
|
+
str(a, "path");
|
|
418
|
+
str(a, "dataB64");
|
|
419
|
+
return;
|
|
420
|
+
case "stat":
|
|
421
|
+
str(a, "path");
|
|
422
|
+
return;
|
|
423
|
+
case "killTagged":
|
|
424
|
+
strArray(a, "tags");
|
|
425
|
+
return;
|
|
426
|
+
case "killAll":
|
|
427
|
+
return;
|
|
428
|
+
case "create":
|
|
429
|
+
parseLimits(a.limits);
|
|
430
|
+
return;
|
|
431
|
+
case "stop": {
|
|
432
|
+
const r = str(a, "reason");
|
|
433
|
+
if (r !== "revoked" && r !== "deleted") {
|
|
434
|
+
throw new DeviceProtocolError(`unknown stop reason "${r}"`);
|
|
435
|
+
}
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
case "policy": {
|
|
439
|
+
opId(a);
|
|
440
|
+
if (a.add !== undefined)
|
|
441
|
+
throw new DeviceProtocolError(POLICY_ADD_REFUSED);
|
|
442
|
+
if (a.mode !== undefined)
|
|
443
|
+
throw new DeviceProtocolError('field "mode" is not part of device protocol 4; narrow with "ownerCeiling" instead');
|
|
444
|
+
if (a.remove !== undefined)
|
|
445
|
+
hostArray(a, "remove");
|
|
446
|
+
const src = obj(a.source, 'field "source"');
|
|
447
|
+
nonEmptyStr(src, "accountId");
|
|
448
|
+
str(src, "accountName");
|
|
449
|
+
const via = str(src, "via");
|
|
450
|
+
if (via !== "web" && via !== "im")
|
|
451
|
+
throw new DeviceProtocolError(`unknown via "${via}"`);
|
|
452
|
+
if (src.approvalId !== undefined)
|
|
453
|
+
str(src, "approvalId");
|
|
454
|
+
return;
|
|
455
|
+
}
|
|
456
|
+
case "policyStatus":
|
|
457
|
+
opId(a);
|
|
458
|
+
return;
|
|
459
|
+
case "list": {
|
|
460
|
+
absolutePath(a.path, 'field "path"');
|
|
461
|
+
const limit = posInt(a, "limit");
|
|
462
|
+
if (limit > MAX_DEVICE_LIST_ENTRIES) {
|
|
463
|
+
throw new DeviceProtocolError(`field "limit" must be at most ${MAX_DEVICE_LIST_ENTRIES}`);
|
|
464
|
+
}
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
case "scan":
|
|
468
|
+
absolutePath(a.base, 'field "base"');
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
export function parseOpResult(op, result) {
|
|
473
|
+
switch (op) {
|
|
474
|
+
case "exec": {
|
|
475
|
+
const r = obj(result, "exec result");
|
|
476
|
+
const exitCode = r.exitCode;
|
|
477
|
+
if (exitCode !== null && (typeof exitCode !== "number" || !Number.isFinite(exitCode))) {
|
|
478
|
+
throw new DeviceProtocolError('field "exitCode" must be a number or null');
|
|
479
|
+
}
|
|
480
|
+
const out = {
|
|
481
|
+
exitCode: exitCode,
|
|
482
|
+
stdout: str(r, "stdout"),
|
|
483
|
+
stderr: str(r, "stderr"),
|
|
484
|
+
truncated: bool(r, "truncated"),
|
|
485
|
+
timedOut: bool(r, "timedOut"),
|
|
486
|
+
};
|
|
487
|
+
// an optional report: a malformed one is dropped, never failing the command's result
|
|
488
|
+
const network = parseNetworkReport(r.network);
|
|
489
|
+
if (network)
|
|
490
|
+
out.network = network;
|
|
491
|
+
return out;
|
|
492
|
+
}
|
|
493
|
+
case "policy":
|
|
494
|
+
return { policy: parsePolicySummary(obj(result, "policy result").policy) };
|
|
495
|
+
case "policyStatus": {
|
|
496
|
+
const r = obj(result, "policyStatus result");
|
|
497
|
+
const out = {
|
|
498
|
+
outcome: oneOf(r, "outcome", [...OP_OUTCOMES, "unknown"]),
|
|
499
|
+
};
|
|
500
|
+
if (r.policy !== undefined)
|
|
501
|
+
out.policy = parsePolicySummary(r.policy);
|
|
502
|
+
if (r.message !== undefined)
|
|
503
|
+
out.message = str(r, "message");
|
|
504
|
+
return out;
|
|
505
|
+
}
|
|
506
|
+
case "readFile": {
|
|
507
|
+
if (result === null)
|
|
508
|
+
return null;
|
|
509
|
+
const r = obj(result, "readFile result");
|
|
510
|
+
return { dataB64: str(r, "dataB64"), truncated: bool(r, "truncated") };
|
|
511
|
+
}
|
|
512
|
+
case "stat": {
|
|
513
|
+
if (result === null)
|
|
514
|
+
return null;
|
|
515
|
+
const r = obj(result, "stat result");
|
|
516
|
+
return { size: num(r, "size"), isDir: bool(r, "isDir") };
|
|
517
|
+
}
|
|
518
|
+
case "create":
|
|
519
|
+
return { roots: parseRoots(obj(result, "create result").roots) };
|
|
520
|
+
case "list":
|
|
521
|
+
return parseListResult(result);
|
|
522
|
+
case "scan":
|
|
523
|
+
return parseScanResult(result);
|
|
524
|
+
default:
|
|
525
|
+
if (result !== null)
|
|
526
|
+
throw new DeviceProtocolError(`${op} result must be null`);
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Parses one WebSocket text frame. `from` is the sender: a device may send `hello`, `res` and
|
|
532
|
+
* `policy`, the
|
|
533
|
+
* kernel `req` and `cancel`. Throws {@link DeviceProtocolError} on bad JSON, an unknown `type`,
|
|
534
|
+
* missing fields or a frame of the wrong direction.
|
|
535
|
+
*/
|
|
536
|
+
export function parseLegacyDeviceFrame(raw, from) {
|
|
537
|
+
let o;
|
|
538
|
+
try {
|
|
539
|
+
o = JSON.parse(raw);
|
|
540
|
+
}
|
|
541
|
+
catch {
|
|
542
|
+
throw new DeviceProtocolError("frame is not valid JSON");
|
|
543
|
+
}
|
|
544
|
+
if (!isRecord(o))
|
|
545
|
+
throw new DeviceProtocolError("frame must be a JSON object");
|
|
546
|
+
const type = o.type;
|
|
547
|
+
switch (type) {
|
|
548
|
+
case "hello":
|
|
549
|
+
case "res":
|
|
550
|
+
case "policy":
|
|
551
|
+
if (from !== "device")
|
|
552
|
+
throw new DeviceProtocolError(`"${type}" must come from the device`);
|
|
553
|
+
if (type === "policy")
|
|
554
|
+
return { type, ...optionalPolicy(o.policy) };
|
|
555
|
+
return type === "hello" ? parseHello(o) : parseRes(o);
|
|
556
|
+
case "req":
|
|
557
|
+
case "cancel":
|
|
558
|
+
if (from !== "kernel")
|
|
559
|
+
throw new DeviceProtocolError(`"${type}" must come from the kernel`);
|
|
560
|
+
return type === "req" ? parseReq(o) : { type: "cancel", id: nonEmptyStr(o, "id") };
|
|
561
|
+
default:
|
|
562
|
+
throw new DeviceProtocolError(`unknown frame type "${String(type)}"`);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const object = (value) => {
|
|
566
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
567
|
+
throw new DeviceProtocolError("Expected an object");
|
|
568
|
+
return value;
|
|
569
|
+
};
|
|
570
|
+
function keys(value, allowed) {
|
|
571
|
+
for (const key of Object.keys(value))
|
|
572
|
+
if (!allowed.includes(key))
|
|
573
|
+
throw new DeviceProtocolError(`Unknown field ${key}`);
|
|
574
|
+
}
|
|
575
|
+
function string(value, max = 128) {
|
|
576
|
+
if (typeof value !== "string" || !value.length || value.length > max)
|
|
577
|
+
throw new DeviceProtocolError("Expected a bounded nonempty string");
|
|
578
|
+
return value;
|
|
579
|
+
}
|
|
580
|
+
function integer(value, min = 0, max = Number.MAX_SAFE_INTEGER) {
|
|
581
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < min || value > max)
|
|
582
|
+
throw new DeviceProtocolError("Expected a bounded integer");
|
|
583
|
+
return value;
|
|
584
|
+
}
|
|
585
|
+
function path(value) {
|
|
586
|
+
if (typeof value !== "string" || validateSandboxPathShape(value).length)
|
|
587
|
+
throw new DeviceProtocolError("Expected an absolute filesystem path");
|
|
588
|
+
}
|
|
589
|
+
function freeze(value) {
|
|
590
|
+
if (value !== null && typeof value === "object") {
|
|
591
|
+
for (const v of Object.values(value))
|
|
592
|
+
freeze(v);
|
|
593
|
+
Object.freeze(value);
|
|
594
|
+
}
|
|
595
|
+
return value;
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
* A strict copy of a wire object for the protocol 4 shapes: a plain object of own data properties
|
|
599
|
+
* only (no accessor is ever invoked, so a value cannot change between check and use), each key in
|
|
600
|
+
* `allowed`. Nested values are copied by the caller's own checks.
|
|
601
|
+
*/
|
|
602
|
+
function strictRecord(value, allowed, what) {
|
|
603
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
604
|
+
throw new DeviceProtocolError(`Invalid ${what}: expected an object`);
|
|
605
|
+
const proto = Object.getPrototypeOf(value);
|
|
606
|
+
if (proto !== Object.prototype && proto !== null)
|
|
607
|
+
throw new DeviceProtocolError(`Invalid ${what}: expected a plain object`);
|
|
608
|
+
const out = Object.create(null);
|
|
609
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
610
|
+
if (typeof key !== "string" || !allowed.includes(key))
|
|
611
|
+
throw new DeviceProtocolError(`Invalid ${what}: unknown field ${String(key)}`);
|
|
612
|
+
const d = Object.getOwnPropertyDescriptor(value, key);
|
|
613
|
+
if (!d || !("value" in d))
|
|
614
|
+
throw new DeviceProtocolError(`Invalid ${what}: accessor fields are not allowed`);
|
|
615
|
+
if (d.value !== undefined)
|
|
616
|
+
out[key] = d.value;
|
|
617
|
+
}
|
|
618
|
+
return out;
|
|
619
|
+
}
|
|
620
|
+
function strictHosts(value, what) {
|
|
621
|
+
if (!Array.isArray(value))
|
|
622
|
+
throw new DeviceProtocolError(`Invalid ${what}: expected at most 100 hosts`);
|
|
623
|
+
// one read of the length: a proxy or a getter cannot grow the list between check and copy
|
|
624
|
+
const length = value.length;
|
|
625
|
+
if (length > 100)
|
|
626
|
+
throw new DeviceProtocolError(`Invalid ${what}: expected at most 100 hosts`);
|
|
627
|
+
const hosts = [];
|
|
628
|
+
for (let i = 0; i < length; i++) {
|
|
629
|
+
const d = Object.getOwnPropertyDescriptor(value, i);
|
|
630
|
+
const h = d && "value" in d ? d.value : undefined;
|
|
631
|
+
if (typeof h !== "string" || validateAllowHost(h).length)
|
|
632
|
+
throw new DeviceProtocolError(`Invalid ${what}: expected hostnames or *.domain patterns`);
|
|
633
|
+
hosts.push(h);
|
|
634
|
+
}
|
|
635
|
+
return hosts;
|
|
636
|
+
}
|
|
637
|
+
function strictBoolean(o, key, what) {
|
|
638
|
+
const v = o[key];
|
|
639
|
+
if (typeof v !== "boolean")
|
|
640
|
+
throw new DeviceProtocolError(`Invalid ${what}: ${key} must be a boolean`);
|
|
641
|
+
return v;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Strictly parses a runtime permission report, version 2 (the protocol 4 `permissionSupport`, also
|
|
645
|
+
* the shape a cloud provider reports), and returns a frozen copy; throws {@link DeviceProtocolError}.
|
|
646
|
+
* Restricted support is all four claims or `null`; per-operation hosts need the allowlist; an
|
|
647
|
+
* operation boundary without restricted support claims no sandbox network at all. An
|
|
648
|
+
* instance boundary is the whole instance: it claims no restricted per-operation scope, no
|
|
649
|
+
* outside-sandbox escalation and no per-operation hosts (a host joins the device's list instead).
|
|
650
|
+
*/
|
|
651
|
+
export function parseSandboxPermissionSupport(value) {
|
|
652
|
+
const what = "runtime permission report";
|
|
653
|
+
const o = strictRecord(value, [
|
|
654
|
+
"version",
|
|
655
|
+
"boundary",
|
|
656
|
+
"restricted",
|
|
657
|
+
"network",
|
|
658
|
+
"outsideSandbox",
|
|
659
|
+
"background",
|
|
660
|
+
"unavailableReason",
|
|
661
|
+
], what);
|
|
662
|
+
if (o.version !== 2)
|
|
663
|
+
throw new DeviceProtocolError(`Invalid ${what}: expected version 2`);
|
|
664
|
+
if (o.boundary !== "operation" && o.boundary !== "instance")
|
|
665
|
+
throw new DeviceProtocolError(`Invalid ${what}: boundary must be operation or instance`);
|
|
666
|
+
let restricted = null;
|
|
667
|
+
if (o.restricted !== null) {
|
|
668
|
+
const claims = ["filesystem", "network", "native", "concurrentScopes"];
|
|
669
|
+
const r = strictRecord(o.restricted, claims, `${what} restricted claims`);
|
|
670
|
+
if (claims.some((k) => r[k] !== true))
|
|
671
|
+
throw new DeviceProtocolError(`Invalid ${what}: restricted support needs all four claims`);
|
|
672
|
+
restricted = { filesystem: true, network: true, native: true, concurrentScopes: true };
|
|
673
|
+
}
|
|
674
|
+
const n = strictRecord(o.network, ["openPublic", "allowlist", "perOperationHosts"], `${what} network`);
|
|
675
|
+
const network = {
|
|
676
|
+
openPublic: strictBoolean(n, "openPublic", what),
|
|
677
|
+
allowlist: strictBoolean(n, "allowlist", what),
|
|
678
|
+
perOperationHosts: strictBoolean(n, "perOperationHosts", what),
|
|
679
|
+
};
|
|
680
|
+
if (o.boundary === "operation" &&
|
|
681
|
+
restricted === null &&
|
|
682
|
+
(network.openPublic || network.allowlist || network.perOperationHosts))
|
|
683
|
+
throw new DeviceProtocolError(`Invalid ${what}: without restricted support an operation boundary has no sandbox network`);
|
|
684
|
+
if (network.perOperationHosts && !network.allowlist)
|
|
685
|
+
throw new DeviceProtocolError(`Invalid ${what}: per-operation hosts need the allowlist`);
|
|
686
|
+
const outsideSandbox = strictBoolean(o, "outsideSandbox", what);
|
|
687
|
+
const background = strictBoolean(o, "background", what);
|
|
688
|
+
if (o.boundary === "instance" &&
|
|
689
|
+
(restricted !== null || outsideSandbox || network.perOperationHosts))
|
|
690
|
+
throw new DeviceProtocolError(`Invalid ${what}: an instance boundary claims no restricted scope, outside escalation or per-operation hosts`);
|
|
691
|
+
if (o.unavailableReason !== undefined)
|
|
692
|
+
string(o.unavailableReason, 4096);
|
|
693
|
+
return freeze({
|
|
694
|
+
version: 2,
|
|
695
|
+
boundary: o.boundary,
|
|
696
|
+
restricted,
|
|
697
|
+
network,
|
|
698
|
+
outsideSandbox,
|
|
699
|
+
background,
|
|
700
|
+
...(o.unavailableReason !== undefined
|
|
701
|
+
? { unavailableReason: o.unavailableReason }
|
|
702
|
+
: {}),
|
|
703
|
+
});
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Strictly parses one authenticated owner-ceiling report of protocol 4 and returns a frozen copy;
|
|
707
|
+
* throws {@link DeviceProtocolError}. Unknown fields (the protocol 3 `fullAccess` and
|
|
708
|
+
* `restrictedAllowedDomains` among them) are refused.
|
|
709
|
+
*/
|
|
710
|
+
export function parseOwnerCeilingReport(value) {
|
|
711
|
+
const what = "owner ceiling report";
|
|
712
|
+
const o = strictRecord(value, [
|
|
713
|
+
"version",
|
|
714
|
+
"sequence",
|
|
715
|
+
"revision",
|
|
716
|
+
"outsideSandbox",
|
|
717
|
+
"network",
|
|
718
|
+
"allowedDomains",
|
|
719
|
+
"restrictedProjectReach",
|
|
720
|
+
], what);
|
|
721
|
+
if (o.version !== 3)
|
|
722
|
+
throw new DeviceProtocolError(`Invalid ${what}: expected version 3`);
|
|
723
|
+
if (o.network !== "open" && o.network !== "allowlist")
|
|
724
|
+
throw new DeviceProtocolError(`Invalid ${what}: network must be open or allowlist`);
|
|
725
|
+
if (o.restrictedProjectReach !== "workspace" && o.restrictedProjectReach !== "os-user")
|
|
726
|
+
throw new DeviceProtocolError(`Invalid ${what}: project reach must be workspace or os-user`);
|
|
727
|
+
return freeze({
|
|
728
|
+
version: 3,
|
|
729
|
+
sequence: integer(o.sequence),
|
|
730
|
+
revision: string(o.revision, 512),
|
|
731
|
+
outsideSandbox: strictBoolean(o, "outsideSandbox", what),
|
|
732
|
+
network: o.network,
|
|
733
|
+
allowedDomains: strictHosts(o.allowedDomains, `${what} allowedDomains`),
|
|
734
|
+
restrictedProjectReach: o.restrictedProjectReach,
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
/**
|
|
738
|
+
* Strictly parses an owner-ceiling narrowing of protocol 4 and returns a frozen copy; throws
|
|
739
|
+
* {@link DeviceProtocolError}. Every field can only narrow: `outsideSandbox` only to `false`,
|
|
740
|
+
* `network` only to `allowlist`, the project reach only to `workspace`, and hosts only removed.
|
|
741
|
+
* There is no way to name a host to add or a list to replace.
|
|
742
|
+
*/
|
|
743
|
+
export function parseOwnerCeilingNarrowing(value) {
|
|
744
|
+
const what = "owner ceiling narrowing";
|
|
745
|
+
const o = strictRecord(value, ["outsideSandbox", "network", "removeAllowedDomains", "restrictedProjectReach"], what);
|
|
746
|
+
if ((Object.hasOwn(o, "outsideSandbox") && o.outsideSandbox !== false) ||
|
|
747
|
+
(Object.hasOwn(o, "network") && o.network !== "allowlist") ||
|
|
748
|
+
(Object.hasOwn(o, "restrictedProjectReach") && o.restrictedProjectReach !== "workspace"))
|
|
749
|
+
throw new DeviceProtocolError("The deployment may only narrow the owner ceiling");
|
|
750
|
+
return freeze({
|
|
751
|
+
...(Object.hasOwn(o, "outsideSandbox") ? { outsideSandbox: false } : {}),
|
|
752
|
+
...(Object.hasOwn(o, "network") ? { network: "allowlist" } : {}),
|
|
753
|
+
...(Object.hasOwn(o, "removeAllowedDomains")
|
|
754
|
+
? {
|
|
755
|
+
removeAllowedDomains: strictHosts(o.removeAllowedDomains, `${what} removeAllowedDomains`),
|
|
756
|
+
}
|
|
757
|
+
: {}),
|
|
758
|
+
...(Object.hasOwn(o, "restrictedProjectReach")
|
|
759
|
+
? { restrictedProjectReach: "workspace" }
|
|
760
|
+
: {}),
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
const GENERATION = /^[0-9]{1,16}$/;
|
|
764
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
765
|
+
function generation(value) {
|
|
766
|
+
if (typeof value !== "string" || !GENERATION.test(value))
|
|
767
|
+
throw new DeviceProtocolError("Expected an instance generation");
|
|
768
|
+
return value;
|
|
769
|
+
}
|
|
770
|
+
function bindRequest(a) {
|
|
771
|
+
string(a.instanceId);
|
|
772
|
+
generation(a.instanceGeneration);
|
|
773
|
+
if (typeof a.bindingId !== "string" || !UUID.test(a.bindingId))
|
|
774
|
+
throw new DeviceProtocolError("Expected a binding id");
|
|
775
|
+
if (a.expectedPreviousGeneration !== null) {
|
|
776
|
+
generation(a.expectedPreviousGeneration);
|
|
777
|
+
if (BigInt(a.expectedPreviousGeneration) >= BigInt(a.instanceGeneration))
|
|
778
|
+
throw new DeviceProtocolError("Previous generation must precede the bound generation");
|
|
779
|
+
}
|
|
780
|
+
return a;
|
|
781
|
+
}
|
|
782
|
+
function identity(value) {
|
|
783
|
+
const o = object(value);
|
|
784
|
+
keys(o, ["runtimeHash", "helperHash", "imageFingerprint", "instanceGeneration"]);
|
|
785
|
+
for (const key of ["runtimeHash", "helperHash", "imageFingerprint"])
|
|
786
|
+
if (typeof o[key] !== "string" || !/^[a-f0-9]{64}$/.test(o[key]))
|
|
787
|
+
throw new DeviceProtocolError("Incomplete runtime identity");
|
|
788
|
+
string(o.instanceGeneration);
|
|
789
|
+
return o;
|
|
790
|
+
}
|
|
791
|
+
const natives = ["exec", "readFile", "writeFile", "stat", "list", "scan"];
|
|
792
|
+
const legacy = ["create", "stop", "policy", "policyStatus"];
|
|
793
|
+
function request(o) {
|
|
794
|
+
keys(o, ["type", "id", "op", "args", "deadlineMs"]);
|
|
795
|
+
const id = string(o.id), op = string(o.op), a = object(o.args), deadlineMs = integer(o.deadlineMs, 1);
|
|
796
|
+
if (legacy.includes(op)) {
|
|
797
|
+
if (op === "policy" && a.ownerCeiling !== undefined)
|
|
798
|
+
a.ownerCeiling = parseOwnerCeilingNarrowing(a.ownerCeiling);
|
|
799
|
+
const parsed = parseLegacyDeviceFrame(JSON.stringify(o), "kernel");
|
|
800
|
+
if (parsed.type !== "req")
|
|
801
|
+
throw new DeviceProtocolError("Expected request");
|
|
802
|
+
return freeze({ ...parsed, op: op, args: object(parsed.args) });
|
|
803
|
+
}
|
|
804
|
+
if (op === "bindInstance") {
|
|
805
|
+
keys(a, ["instanceId", "instanceGeneration", "bindingId", "expectedPreviousGeneration"]);
|
|
806
|
+
bindRequest(a);
|
|
807
|
+
}
|
|
808
|
+
else if (op === "revokeOperation") {
|
|
809
|
+
keys(a, ["ref"]);
|
|
810
|
+
a.ref = parseSandboxOperationRef(a.ref);
|
|
811
|
+
}
|
|
812
|
+
else if (["backgroundStatus", "backgroundTail", "backgroundReadOutput", "backgroundStop"].includes(op)) {
|
|
813
|
+
keys(a, [
|
|
814
|
+
"ref",
|
|
815
|
+
...(op === "backgroundReadOutput" ? ["offset", "maxBytes"] : []),
|
|
816
|
+
...(op === "backgroundTail" ? ["maxBytes"] : op === "backgroundStop" ? ["graceMs"] : []),
|
|
817
|
+
]);
|
|
818
|
+
a.ref = parseSandboxExecutionRef(a.ref);
|
|
819
|
+
if (op === "backgroundTail" || op === "backgroundReadOutput")
|
|
820
|
+
integer(a.maxBytes, 0, 65536);
|
|
821
|
+
if (op === "backgroundReadOutput")
|
|
822
|
+
integer(a.offset);
|
|
823
|
+
if (op === "backgroundStop")
|
|
824
|
+
integer(a.graceMs, 0, 60000);
|
|
825
|
+
}
|
|
826
|
+
else {
|
|
827
|
+
const preparation = op.startsWith("prepare"), browsing = op.startsWith("browse"), background = op === "backgroundStart";
|
|
828
|
+
const method = background ? "exec" : preparation ? op.slice(7) : browsing ? op.slice(6) : op;
|
|
829
|
+
const native = method[0]?.toLowerCase() + method.slice(1);
|
|
830
|
+
if (!natives.includes(native) ||
|
|
831
|
+
(preparation && !["readFile", "writeFile", "stat"].includes(native)) ||
|
|
832
|
+
(browsing && !["stat", "list", "scan"].includes(native)))
|
|
833
|
+
throw new DeviceProtocolError("Unknown operation");
|
|
834
|
+
a.scope = preparation
|
|
835
|
+
? parseSandboxPreparationScope(a.scope)
|
|
836
|
+
: browsing
|
|
837
|
+
? parseSandboxBrowseScope(a.scope)
|
|
838
|
+
: parseSandboxOperationScope(a.scope);
|
|
839
|
+
const fields = native === "exec"
|
|
840
|
+
? ["command", "cwd", "env", "timeoutMs", "maxOutputBytes", "limits", "grants"]
|
|
841
|
+
: native === "readFile"
|
|
842
|
+
? ["path", "maxBytes"]
|
|
843
|
+
: native === "writeFile"
|
|
844
|
+
? ["path", "dataB64"]
|
|
845
|
+
: native === "stat"
|
|
846
|
+
? ["path"]
|
|
847
|
+
: native === "list"
|
|
848
|
+
? ["path", "limit"]
|
|
849
|
+
: ["base"];
|
|
850
|
+
keys(a, ["scope", ...fields, ...(background ? ["taskId", "outputPath", "retainUntilMs"] : [])]);
|
|
851
|
+
if (native !== "exec")
|
|
852
|
+
path(a[native === "scan" ? "base" : "path"]);
|
|
853
|
+
const validated = { ...a };
|
|
854
|
+
delete validated.scope;
|
|
855
|
+
if (native === "exec") {
|
|
856
|
+
const scope = parseSandboxOperationScope(a.scope);
|
|
857
|
+
validated.tag = scope.tag;
|
|
858
|
+
validated.actor = scope.actor;
|
|
859
|
+
if (background) {
|
|
860
|
+
string(a.taskId);
|
|
861
|
+
path(a.outputPath);
|
|
862
|
+
integer(a.retainUntilMs, scope.admitBeforeMs);
|
|
863
|
+
delete validated.taskId;
|
|
864
|
+
delete validated.outputPath;
|
|
865
|
+
delete validated.retainUntilMs;
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
// Reuse the legacy pure value guards on the native fields.
|
|
869
|
+
parseLegacyDeviceFrame(JSON.stringify({ type: "req", id, op: native, args: validated, deadlineMs }), "kernel");
|
|
870
|
+
if (native === "writeFile" && (typeof a.dataB64 !== "string" || !isBase64(a.dataB64)))
|
|
871
|
+
throw new DeviceProtocolError("Invalid base64 payload");
|
|
872
|
+
}
|
|
873
|
+
return freeze({ type: "req", id, op: op, args: a, deadlineMs });
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Padded standard base64: whole quartets of `A-Z a-z 0-9 + /`, the last one possibly ending in
|
|
877
|
+
* `=` or `==`. One linear pass: a regex with a repeated group exhausts the engine's backtracking
|
|
878
|
+
* stack on payloads of a few megabytes.
|
|
879
|
+
*/
|
|
880
|
+
function isBase64(value) {
|
|
881
|
+
if (value.length % 4 !== 0)
|
|
882
|
+
return false;
|
|
883
|
+
const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0;
|
|
884
|
+
for (let i = 0; i < value.length - padding; i++) {
|
|
885
|
+
const c = value.charCodeAt(i);
|
|
886
|
+
if (!((c >= 65 && c <= 90) ||
|
|
887
|
+
(c >= 97 && c <= 122) ||
|
|
888
|
+
(c >= 48 && c <= 57) ||
|
|
889
|
+
c === 43 ||
|
|
890
|
+
c === 47))
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
893
|
+
return true;
|
|
894
|
+
}
|
|
895
|
+
/** Strictly parses one protocol 4 frame; throws {@link DeviceProtocolError}. */
|
|
896
|
+
export function parseDeviceFrame(raw, from, maxFrameBytes) {
|
|
897
|
+
if (maxFrameBytes !== undefined &&
|
|
898
|
+
new TextEncoder().encode(raw).byteLength > integer(maxFrameBytes, 1))
|
|
899
|
+
throw new DeviceProtocolError("Frame exceeds negotiated capacity; check device and deployment frame limits");
|
|
900
|
+
let parsed;
|
|
901
|
+
try {
|
|
902
|
+
parsed = JSON.parse(raw);
|
|
903
|
+
}
|
|
904
|
+
catch {
|
|
905
|
+
throw new DeviceProtocolError("Frame is not valid JSON");
|
|
906
|
+
}
|
|
907
|
+
const o = object(parsed);
|
|
908
|
+
if (o.type === "ready") {
|
|
909
|
+
if (from !== "kernel")
|
|
910
|
+
throw new DeviceProtocolError("Ready must come from kernel");
|
|
911
|
+
keys(o, ["type", "connectionId", "maxFrameBytes"]);
|
|
912
|
+
return freeze({
|
|
913
|
+
type: "ready",
|
|
914
|
+
connectionId: string(o.connectionId),
|
|
915
|
+
maxFrameBytes: integer(o.maxFrameBytes, 1024),
|
|
916
|
+
});
|
|
917
|
+
}
|
|
918
|
+
if (o.type === "req") {
|
|
919
|
+
if (from !== "kernel")
|
|
920
|
+
throw new DeviceProtocolError("Request must come from kernel");
|
|
921
|
+
return request(o);
|
|
922
|
+
}
|
|
923
|
+
if (o.type === "hello") {
|
|
924
|
+
if (from !== "device" || o.protocol !== DEVICE_PROTOCOL_VERSION)
|
|
925
|
+
throw new DeviceProtocolError(`Expected protocol ${DEVICE_PROTOCOL_VERSION} hello`);
|
|
926
|
+
const transport = object(o.transport);
|
|
927
|
+
keys(transport, ["maxFrameBytes"]);
|
|
928
|
+
const maxFrameBytes = integer(transport.maxFrameBytes, 1024);
|
|
929
|
+
const permissionSupport = parseSandboxPermissionSupport(o.permissionSupport);
|
|
930
|
+
if (o.instanceGeneration === null &&
|
|
931
|
+
(o.runtimeIdentity !== undefined ||
|
|
932
|
+
permissionSupport.restricted !== null ||
|
|
933
|
+
permissionSupport.outsideSandbox ||
|
|
934
|
+
permissionSupport.network.openPublic ||
|
|
935
|
+
permissionSupport.network.allowlist ||
|
|
936
|
+
permissionSupport.network.perOperationHosts ||
|
|
937
|
+
permissionSupport.background ||
|
|
938
|
+
!permissionSupport.unavailableReason))
|
|
939
|
+
throw new DeviceProtocolError("Unbound hello cannot advertise runtime capability");
|
|
940
|
+
const base = parseLegacyDeviceFrame(raw, from);
|
|
941
|
+
if (base.type !== "hello")
|
|
942
|
+
throw new DeviceProtocolError("Expected hello");
|
|
943
|
+
let runtimeIdentity;
|
|
944
|
+
if (o.runtimeIdentity !== undefined) {
|
|
945
|
+
runtimeIdentity = identity(o.runtimeIdentity);
|
|
946
|
+
if (runtimeIdentity.instanceGeneration !== string(o.instanceGeneration))
|
|
947
|
+
throw new DeviceProtocolError("Runtime identity generation mismatch");
|
|
948
|
+
}
|
|
949
|
+
return freeze({
|
|
950
|
+
...base,
|
|
951
|
+
protocol: DEVICE_PROTOCOL_VERSION,
|
|
952
|
+
permissionSupport,
|
|
953
|
+
transport: { maxFrameBytes },
|
|
954
|
+
ownerCeiling: parseOwnerCeilingReport(o.ownerCeiling),
|
|
955
|
+
instanceGeneration: o.instanceGeneration === null ? null : string(o.instanceGeneration),
|
|
956
|
+
...(runtimeIdentity && { runtimeIdentity }),
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
if (o.type === "policy") {
|
|
960
|
+
if (from !== "device")
|
|
961
|
+
throw new DeviceProtocolError("Policy must come from device");
|
|
962
|
+
keys(o, ["type", "policy", "ownerCeiling"]);
|
|
963
|
+
const base = parseLegacyDeviceFrame(JSON.stringify({ type: "policy", policy: o.policy }), from);
|
|
964
|
+
if (base.type !== "policy")
|
|
965
|
+
throw new DeviceProtocolError("Expected policy");
|
|
966
|
+
return freeze({ ...base, ownerCeiling: parseOwnerCeilingReport(o.ownerCeiling) });
|
|
967
|
+
}
|
|
968
|
+
if (o.type === "res" && o.ok === false) {
|
|
969
|
+
if (from !== "device")
|
|
970
|
+
throw new DeviceProtocolError("Response must come from device");
|
|
971
|
+
keys(o, ["type", "id", "ok", "error"]);
|
|
972
|
+
const e = object(o.error);
|
|
973
|
+
keys(e, ["kind", "message"]);
|
|
974
|
+
if (![
|
|
975
|
+
"gone",
|
|
976
|
+
"fs_gone",
|
|
977
|
+
"policy",
|
|
978
|
+
"error",
|
|
979
|
+
"denied",
|
|
980
|
+
"unsupported",
|
|
981
|
+
"unknown",
|
|
982
|
+
"invalid",
|
|
983
|
+
].includes(String(e.kind)))
|
|
984
|
+
throw new DeviceProtocolError("Unknown outcome kind");
|
|
985
|
+
return freeze({
|
|
986
|
+
type: "res",
|
|
987
|
+
id: string(o.id),
|
|
988
|
+
ok: false,
|
|
989
|
+
error: {
|
|
990
|
+
kind: e.kind,
|
|
991
|
+
message: string(e.message, 8192),
|
|
992
|
+
},
|
|
993
|
+
});
|
|
994
|
+
}
|
|
995
|
+
const base = parseLegacyDeviceFrame(raw, from);
|
|
996
|
+
if (base.type === "hello" || base.type === "req" || base.type === "policy")
|
|
997
|
+
throw new DeviceProtocolError("Expected staged frame");
|
|
998
|
+
return freeze(base);
|
|
999
|
+
}
|
|
1000
|
+
export function parseOperationResult(op, result, request) {
|
|
1001
|
+
if (op === "bindInstance") {
|
|
1002
|
+
if (!request || !("bindingId" in request))
|
|
1003
|
+
throw new DeviceProtocolError("Binding validation requires the exact request");
|
|
1004
|
+
const o = object(result);
|
|
1005
|
+
keys(o, [
|
|
1006
|
+
"instanceId",
|
|
1007
|
+
"instanceGeneration",
|
|
1008
|
+
"bindingId",
|
|
1009
|
+
"runtimeIdentity",
|
|
1010
|
+
"permissionSupport",
|
|
1011
|
+
"ownerCeiling",
|
|
1012
|
+
]);
|
|
1013
|
+
const runtimeIdentity = identity(o.runtimeIdentity);
|
|
1014
|
+
if (o.instanceId !== request.instanceId ||
|
|
1015
|
+
o.instanceGeneration !== request.instanceGeneration ||
|
|
1016
|
+
o.bindingId !== request.bindingId ||
|
|
1017
|
+
runtimeIdentity.instanceGeneration !== request.instanceGeneration)
|
|
1018
|
+
throw new DeviceProtocolError("Binding answer belongs to another instance generation");
|
|
1019
|
+
return freeze({
|
|
1020
|
+
instanceId: request.instanceId,
|
|
1021
|
+
instanceGeneration: request.instanceGeneration,
|
|
1022
|
+
bindingId: request.bindingId,
|
|
1023
|
+
runtimeIdentity,
|
|
1024
|
+
permissionSupport: parseSandboxPermissionSupport(o.permissionSupport),
|
|
1025
|
+
ownerCeiling: parseOwnerCeilingReport(o.ownerCeiling),
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
if (op === "backgroundReadOutput") {
|
|
1029
|
+
if (!request || !("offset" in request))
|
|
1030
|
+
throw new DeviceProtocolError("Output validation requires the exact request bounds");
|
|
1031
|
+
const offset = integer(request.offset), maxBytes = integer(request.maxBytes, 0, 65536);
|
|
1032
|
+
const o = object(result);
|
|
1033
|
+
if ("unavailable" in o) {
|
|
1034
|
+
keys(o, ["unavailable"]);
|
|
1035
|
+
if (o.unavailable !== "lost" && o.unavailable !== "expired")
|
|
1036
|
+
throw new DeviceProtocolError("Invalid output outcome");
|
|
1037
|
+
return freeze(o);
|
|
1038
|
+
}
|
|
1039
|
+
keys(o, ["dataB64", "offset", "nextOffset", "totalBytes"]);
|
|
1040
|
+
const total = integer(o.totalBytes), next = integer(o.nextOffset);
|
|
1041
|
+
if (integer(o.offset) !== offset ||
|
|
1042
|
+
typeof o.dataB64 !== "string" ||
|
|
1043
|
+
o.dataB64.length > 87384 ||
|
|
1044
|
+
!isBase64(o.dataB64))
|
|
1045
|
+
throw new DeviceProtocolError("Invalid output range or base64");
|
|
1046
|
+
const decoded = atob(o.dataB64);
|
|
1047
|
+
if (btoa(decoded) !== o.dataB64 ||
|
|
1048
|
+
decoded.length !== Math.min(maxBytes, Math.max(total - offset, 0)) ||
|
|
1049
|
+
!Number.isSafeInteger(offset + decoded.length) ||
|
|
1050
|
+
next !== offset + decoded.length)
|
|
1051
|
+
throw new DeviceProtocolError("Inconsistent output range; retry from the same offset");
|
|
1052
|
+
return freeze(o);
|
|
1053
|
+
}
|
|
1054
|
+
if (op === "backgroundStart")
|
|
1055
|
+
return parseSandboxExecutionRef(result);
|
|
1056
|
+
if (op === "backgroundStatus") {
|
|
1057
|
+
const o = object(result);
|
|
1058
|
+
keys(o, ["state", "exitCode", "timedOut"]);
|
|
1059
|
+
if (o.state === "running" || o.state === "lost")
|
|
1060
|
+
return freeze({ state: o.state });
|
|
1061
|
+
if (o.state !== "exited" ||
|
|
1062
|
+
(o.exitCode !== null && !Number.isInteger(o.exitCode)) ||
|
|
1063
|
+
typeof o.timedOut !== "boolean")
|
|
1064
|
+
throw new DeviceProtocolError("Invalid execution status");
|
|
1065
|
+
return freeze(o);
|
|
1066
|
+
}
|
|
1067
|
+
if (op === "backgroundTail") {
|
|
1068
|
+
if (result && typeof result === "object" && "unavailable" in result) {
|
|
1069
|
+
const o = object(result);
|
|
1070
|
+
keys(o, ["unavailable"]);
|
|
1071
|
+
if (o.unavailable !== "lost" && o.unavailable !== "expired")
|
|
1072
|
+
throw new DeviceProtocolError("Invalid output outcome");
|
|
1073
|
+
return freeze(o);
|
|
1074
|
+
}
|
|
1075
|
+
return parseOpResult("readFile", result);
|
|
1076
|
+
}
|
|
1077
|
+
if (op === "backgroundStop") {
|
|
1078
|
+
const o = object(result);
|
|
1079
|
+
keys(o, ["outcome", "exitCode", "timedOut"]);
|
|
1080
|
+
if (!["already-exited", "stopped", "cancel-pending", "lost"].includes(String(o.outcome)))
|
|
1081
|
+
throw new DeviceProtocolError("Invalid stop outcome");
|
|
1082
|
+
if (o.outcome === "already-exited" &&
|
|
1083
|
+
((o.exitCode !== null && !Number.isInteger(o.exitCode)) || typeof o.timedOut !== "boolean"))
|
|
1084
|
+
throw new DeviceProtocolError("Invalid stopped execution status");
|
|
1085
|
+
return freeze(o);
|
|
1086
|
+
}
|
|
1087
|
+
if (op === "policy") {
|
|
1088
|
+
const o = object(result);
|
|
1089
|
+
keys(o, ["policy", "ownerCeiling"]);
|
|
1090
|
+
return freeze({
|
|
1091
|
+
...parseOpResult("policy", { policy: o.policy }),
|
|
1092
|
+
ownerCeiling: parseOwnerCeilingReport(o.ownerCeiling),
|
|
1093
|
+
});
|
|
1094
|
+
}
|
|
1095
|
+
if (op === "policyStatus") {
|
|
1096
|
+
const o = object(result);
|
|
1097
|
+
keys(o, ["outcome", "policy", "message", "ownerCeiling"]);
|
|
1098
|
+
const { ownerCeiling, ...rest } = o;
|
|
1099
|
+
const status = parseOpResult("policyStatus", rest);
|
|
1100
|
+
if (status.outcome === "applied" && ownerCeiling === undefined)
|
|
1101
|
+
throw new DeviceProtocolError("An applied policy status carries the owner report");
|
|
1102
|
+
if (ownerCeiling !== undefined)
|
|
1103
|
+
status.ownerCeiling = parseOwnerCeilingReport(ownerCeiling);
|
|
1104
|
+
return freeze(status);
|
|
1105
|
+
}
|
|
1106
|
+
if (op === "revokeOperation") {
|
|
1107
|
+
if (result !== null)
|
|
1108
|
+
throw new DeviceProtocolError("Revoke result must be null");
|
|
1109
|
+
return null;
|
|
1110
|
+
}
|
|
1111
|
+
const raw = op.startsWith("prepare") ? op.slice(7) : op.startsWith("browse") ? op.slice(6) : op;
|
|
1112
|
+
return parseOpResult((raw.charAt(0).toLowerCase() + raw.slice(1)), result);
|
|
1113
|
+
}
|